Skip to content

Commit 682b130

Browse files
committed
Parse grid related css properties
Properties list: grid-area grid-column grid-column-start grid-column-end grid-row grid-row-start grid-row-end grid-template-areas grid-template-columns grid-template-rows
1 parent ddc987b commit 682b130

22 files changed

+1140
-83
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ set(SOURCE_LITEHTML
8383
src/flex_line.cpp
8484
src/background.cpp
8585
src/gradient.cpp
86+
include/litehtml/render_grid.h
87+
src/render_grid.cpp
88+
include/litehtml/grid.h
89+
src/grid.cpp
8690
)
8791

8892
set(HEADER_LITEHTML

include/litehtml/css_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ template<class Input>
4343
css_token_vector normalize(Input input, int options = 0, keep_whitespace_fn keep_whitespace = 0);
4444

4545
vector<css_token_vector> parse_comma_separated_list(const css_token_vector& tokens);
46+
vector<css_token_vector> parse_slash_separated_list(const css_token_vector& tokens);
4647
bool is_declaration_value(const css_token_vector& tokens, int index = 0);
4748
bool is_any_value(const css_token_vector& tokens);
4849
bool skip_whitespace(const css_token_vector& tokens, int& index);

include/litehtml/element.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace litehtml
208208
css().get_display() == display_flex ||
209209
css().get_display() == display_table ||
210210
css().get_display() == display_list_item ||
211-
css().get_display() == display_flex)
211+
css().get_display() == display_grid)
212212
{
213213
return true;
214214
}

include/litehtml/grid.h

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
#ifndef LITEHTML_GRID_H
2+
#define LITEHTML_GRID_H
3+
4+
#include <optional>
5+
#include <vector>
6+
#include "string_id.h"
7+
8+
namespace litehtml
9+
{
10+
// <grid-line> =
11+
// auto |
12+
// <custom-ident> |
13+
// [ [ <integer [-∞,-1]> | <integer [1,∞]> ] && <custom-ident>? ] |
14+
// [ span && [ <integer [1,∞]> || <custom-ident> ] ]
15+
struct css_grid_line
16+
{
17+
typedef std::vector<css_grid_line> vector;
18+
19+
struct span_def
20+
{
21+
string_id custom_ident;
22+
int value;
23+
24+
span_def() : custom_ident(empty_id),value(1) {}
25+
span_def(string_id _custom_ident, int _val) : custom_ident(_custom_ident), value(_val) {}
26+
};
27+
28+
struct integer_custom_ident_def
29+
{
30+
string_id custom_ident;
31+
int value;
32+
integer_custom_ident_def() : custom_ident(empty_id),value(0) {}
33+
integer_custom_ident_def(string_id _custom_ident, int _val) : custom_ident(_custom_ident), value(_val) {}
34+
};
35+
36+
struct custom_ident_def
37+
{
38+
string_id custom_ident;
39+
40+
custom_ident_def() : custom_ident(empty_id) {}
41+
explicit custom_ident_def(string_id _custom_ident) : custom_ident(_custom_ident) {}
42+
};
43+
44+
struct auto_def { };
45+
46+
std::variant<auto_def, span_def, integer_custom_ident_def, custom_ident_def> line;
47+
48+
css_grid_line() : line(auto_def()) {}
49+
50+
bool from_tokens(const css_token_vector& tokens);
51+
template<class T> bool is() const { return std::holds_alternative<T>(line); }
52+
template<class T> const T& get() const { return std::get<T>(*this); }
53+
};
54+
55+
struct css_grid_template_areas
56+
{
57+
int columns;
58+
int rows;
59+
std::vector<string_vector> areas;
60+
css_grid_template_areas() : columns(0), rows(0), areas() {}
61+
62+
bool is_none() const { return areas.empty(); }
63+
bool from_tokens(const css_token_vector& tokens);
64+
void clear()
65+
{
66+
columns = rows = 0;
67+
areas.clear();
68+
}
69+
};
70+
71+
// grid-template-columns =
72+
// none |
73+
// <track-list> |
74+
// <auto-track-list> |
75+
// subgrid <line-name-list>?
76+
struct css_grid_template
77+
{
78+
struct none {};
79+
80+
struct line_names;
81+
struct track_size;
82+
struct track_repeat;
83+
struct fixed_size;
84+
struct auto_repeat;
85+
struct fixed_repeat;
86+
struct name_repeat;
87+
88+
// <track-list> =
89+
// [ <line-names>? [ <track-size> | <track-repeat> ] ]+ <line-names>?
90+
struct track_list
91+
{
92+
std::vector<std::variant<line_names, track_size, track_repeat>> value;
93+
94+
bool parse(const css_token_vector& tokens);
95+
};
96+
97+
// <auto-track-list> =
98+
// [ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>? <auto-repeat> [ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>?
99+
struct auto_track_list
100+
{
101+
std::vector<std::variant<line_names, fixed_size, auto_repeat, fixed_repeat>> value;
102+
103+
bool parse(const css_token_vector& tokens);
104+
};
105+
106+
// <line-names> =
107+
// '[' <custom-ident>* ']'
108+
struct line_names
109+
{
110+
std::vector<std::string> names;
111+
112+
bool parse(const css_token& token);
113+
};
114+
115+
// <name-repeat> =
116+
// repeat( [ <integer [1,∞]> | auto-fill ] , <line-names>+ )
117+
struct name_repeat
118+
{
119+
struct auto_fill {};
120+
121+
std::variant<int, auto_fill> arg1;
122+
std::vector<line_names> arg2;
123+
124+
bool parse(const css_token& token);
125+
};
126+
127+
// subgrid
128+
// <line-name-list> =
129+
// [ <line-names> | <name-repeat> ]+
130+
struct subgrid
131+
{
132+
std::vector<std::variant<line_names, name_repeat>> line_name_list;
133+
134+
bool parse(const css_token_vector& tokens);
135+
};
136+
137+
// <track-breadth> =
138+
// <length-percentage [0,∞]> |
139+
// <flex [0,∞]> |
140+
// min-content |
141+
// max-content |
142+
// auto
143+
struct track_breadth
144+
{
145+
struct flex
146+
{
147+
int n;
148+
flex() : n(0) {}
149+
};
150+
151+
std::variant<css_length, flex> value;
152+
153+
bool parse(css_token_vector::const_iterator& iter, const css_token_vector::const_iterator& end);
154+
};
155+
156+
// <inflexible-breadth> =
157+
// <length-percentage [0,∞]> |
158+
// min-content |
159+
// max-content |
160+
// auto
161+
struct inflexible_breadth
162+
{
163+
css_length value;
164+
165+
bool parse(const css_token& token)
166+
{
167+
return value.from_token(token, f_length_percentage, minmax_predef_str);
168+
}
169+
};
170+
171+
// <length-percentage> =
172+
// <length> |
173+
// <percentage>
174+
//
175+
// <fixed-breadth> =
176+
// <length-percentage [0,∞]>
177+
struct fixed_breadth
178+
{
179+
css_length value;
180+
181+
bool parse(const css_token& token)
182+
{
183+
return value.from_token(token, f_length_percentage);
184+
}
185+
};
186+
187+
struct minmax
188+
{
189+
std::variant<fixed_breadth, inflexible_breadth> arg1;
190+
std::variant<track_breadth, fixed_breadth> arg2;
191+
192+
bool parse(const css_token& token);
193+
};
194+
195+
struct fit_content
196+
{
197+
fixed_breadth value;
198+
199+
bool parse(const css_token& token);
200+
};
201+
202+
// <track-size> =
203+
// <track-breadth> |
204+
// minmax( <inflexible-breadth> , <track-breadth> ) |
205+
// fit-content( <length-percentage [0,∞]> )
206+
struct track_size
207+
{
208+
std::variant<track_breadth, minmax, fit_content> value;
209+
210+
bool parse(css_token_vector::const_iterator& iter, const css_token_vector::const_iterator& end);
211+
};
212+
213+
// <track-repeat> =
214+
// repeat( [ <integer [1,∞]> ] , [ <line-names>? <track-size> ]+ <line-names>? )
215+
struct track_repeat
216+
{
217+
int arg1;
218+
std::vector<std::variant<line_names, track_size>> arg2;
219+
220+
bool parse(const css_token& token);
221+
};
222+
223+
// <fixed-size> =
224+
// <fixed-breadth> |
225+
// minmax( <fixed-breadth> , <track-breadth> ) |
226+
// minmax( <inflexible-breadth> , <fixed-breadth> )
227+
struct fixed_size
228+
{
229+
std::variant<fixed_breadth, minmax> value;
230+
231+
bool parse(const css_token& token);
232+
};
233+
234+
// <fixed-repeat> =
235+
// repeat( [ <integer [1,∞]> ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
236+
struct fixed_repeat
237+
{
238+
int arg1;
239+
std::vector<std::variant<line_names, fixed_size>> arg2;
240+
241+
fixed_repeat() : arg1(0) {}
242+
bool parse(const css_token& token);
243+
};
244+
245+
enum auto_repeat_type
246+
{
247+
auto_fill,
248+
auto_fit,
249+
};
250+
// <auto-repeat> =
251+
// repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
252+
struct auto_repeat
253+
{
254+
auto_repeat_type arg1;
255+
std::vector<std::variant<line_names, fixed_size>> arg2;
256+
257+
bool parse(const css_token& token);
258+
};
259+
260+
static constexpr const char* minmax_predef_str = "auto;min-content;max-content";
261+
enum minmax_predef
262+
{
263+
minmax_auto,
264+
minmax_min_content,
265+
minmax_max_content,
266+
};
267+
268+
std::variant<none, track_list, auto_track_list, subgrid> value;
269+
270+
bool from_tokens(const css_token_vector& tokens);
271+
};
272+
}
273+
274+
#endif //LITEHTML_GRID_H

include/litehtml/html.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace litehtml
3535
void join_string(string& str, const string_vector& tokens, const string& delims);
3636
double t_strtod(const char* string, char** endPtr = nullptr);
3737
string get_escaped_string(const string& in_str);
38+
bool starts_width(const std::string &str, const std::string_view &substr);
3839

3940
template<typename X, typename A>
4041
bool is_one_of(X x, A a)

include/litehtml/render_block.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace litehtml
2424
virtual void fix_line_width(element_float /*flt*/,
2525
const containing_block_context &/*containing_block_size*/, formatting_context* /*fmt_ctx*/)
2626
{}
27+
void children_to_blocks();
2728

2829
public:
2930
explicit render_item_block(std::shared_ptr<element> src_el) : render_item(std::move(src_el))

include/litehtml/render_grid.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef LITEHTML_RENDER_GRID_H
2+
#define LITEHTML_RENDER_GRID_H
3+
4+
#include "render_block.h"
5+
6+
namespace litehtml
7+
{
8+
class render_item_grid : public render_item_block
9+
{
10+
int _render_content(int x, int y, bool second_pass, const containing_block_context &self_size, formatting_context* fmt_ctx) override;
11+
12+
public:
13+
explicit render_item_grid(std::shared_ptr<element> src_el) : render_item_block(std::move(src_el))
14+
{}
15+
16+
std::shared_ptr<render_item> clone() override
17+
{
18+
return std::make_shared<render_item_grid>(src_el());
19+
}
20+
std::shared_ptr<render_item> init() override;
21+
22+
int get_first_baseline() override;
23+
int get_last_baseline() override;
24+
};
25+
}
26+
27+
#endif //LITEHTML_RENDER_GRID_H

include/litehtml/render_item.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ namespace litehtml
376376
return false;
377377
}
378378

379+
bool is_grid_item() const
380+
{
381+
auto par = parent();
382+
if(par && (par->css().get_display() == display_inline_grid || par->css().get_display() == display_grid))
383+
{
384+
return true;
385+
}
386+
return false;
387+
}
388+
379389
int render(int x, int y, const containing_block_context& containing_block_size, formatting_context* fmt_ctx, bool second_pass = false);
380390
void apply_relative_shift(const containing_block_context &containing_block_size);
381391
void calc_outlines( int parent_width );

include/litehtml/string_id.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ STRING_ID(
341341
_device_aspect_ratio_,
342342
_color_index_,
343343
_monochrome_,
344+
345+
_grid_column_,
346+
_grid_row_,
347+
_grid_column_start_,
348+
_grid_column_end_,
349+
_grid_row_start_,
350+
_grid_row_end_,
351+
_grid_area_,
352+
_grid_template_areas_,
353+
_grid_template_rows_,
354+
_grid_template_columns_,
344355
)
345356
#undef STRING_ID
346357
extern const string_id empty_id; // _id("")

0 commit comments

Comments
 (0)