1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*-
* Copyright 2021 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "css_rule.hxx"
#include "css.hxx"
#include <limits>
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest/doctest.h"
namespace rspamd::css {
/* Class methods */
void css_rule::override_values(const css_rule &other)
{
int bits = 0;
/* Ensure that our bitset is large enough */
static_assert(static_cast<std::size_t>(css_value::css_value_type::CSS_VALUE_NYI) << 1 <
std::numeric_limits<int>::max());
for (const auto &v : values) {
bits |= static_cast<int>(v.type);
}
for (const auto &ov : other.values) {
if (isset(&bits, static_cast<int>(ov.type))) {
/* We need to override the existing value */
/*
* The algorithm is not very efficient,
* so we need to sort the values first and have a O(N) algorithm
* On the other hand, values vectors are usually limited to the
* number of elements about less then 10, so this O(N^2) algorithm
* is probably ok here
*/
for (auto &v : values) {
if (v.type == ov.type) {
v = ov;
}
}
}
}
/* Copy only not set values */
std::copy_if(other.values.begin(), other.values.end(), std::back_inserter(values),
[&bits](const auto &elt) -> bool {
return !isset(&bits, static_cast<int>(elt.type));
});
}
void css_rule::merge_values(const css_rule &other)
{
unsigned int bits = 0;
/* Ensure that our bitset is large enough */
static_assert(static_cast<std::size_t>(css_value::css_value_type::CSS_VALUE_NYI) << 1 <
std::numeric_limits<int>::max());
for (const auto &v : values) {
bits |= static_cast<int>(v.type);
}
/* Copy only not set values */
std::copy_if(other.values.begin(), other.values.end(), std::back_inserter(values),
[&bits](const auto &elt) -> bool {
return !isset(&bits, static_cast<int>(elt.type));
});
}
auto css_declarations_block::add_rule(rule_shared_ptr rule) -> bool
{
auto it = rules.find(rule);
auto &&remote_prop = rule->get_prop();
auto ret = true;
if (rule->get_values().size() == 0) {
/* Ignore rules with no values */
return false;
}
if (it != rules.end()) {
auto &&local_rule = *it;
auto &&local_prop = local_rule->get_prop();
if (local_prop.flag == css_property_flag::FLAG_IMPORTANT) {
if (remote_prop.flag == css_property_flag::FLAG_IMPORTANT) {
local_rule->override_values(*rule);
}
else {
/* Override remote not important over local important */
local_rule->merge_values(*rule);
}
}
else if (local_prop.flag == css_property_flag::FLAG_NOT_IMPORTANT) {
if (remote_prop.flag == css_property_flag::FLAG_NOT_IMPORTANT) {
local_rule->override_values(*rule);
}
else {
/* Override local not important over important */
local_rule->merge_values(*rule);
}
}
else {
if (remote_prop.flag == css_property_flag::FLAG_IMPORTANT) {
/* Override with remote */
local_rule->override_values(*rule);
}
else if (remote_prop.flag == css_property_flag::FLAG_NOT_IMPORTANT) {
/* Ignore remote not important over local normal */
ret = false;
}
else {
/* Merge both */
local_rule->merge_values(*rule);
}
}
}
else {
rules.insert(std::move(rule));
}
return ret;
}
}
namespace rspamd::css {
/* Static functions */
static auto
allowed_property_value(const css_property &prop, const css_consumed_block &parser_block)
-> std::optional<css_value> {
if (prop.is_color()) {
if (parser_block.is_token()) {
/* A single token */
const auto &tok = parser_block.get_token_or_empty();
if (tok.type == css_parser_token::token_type::hash_token) {
return css_value::maybe_color_from_hex(tok.get_string_or_default(""));
}
else if (tok.type == css_parser_token::token_type::ident_token) {
auto &&ret = css_value::maybe_color_from_string(tok.get_string_or_default(""));
return ret;
}
}
else if (parser_block.is_function()) {
const auto &func = parser_block.get_function_or_invalid();
auto &&ret = css_value::maybe_color_from_function(func);
return ret;
}
}
if (prop.is_dimension()) {
if (parser_block.is_token()) {
/* A single token */
const auto &tok = parser_block.get_token_or_empty();
if (tok.type == css_parser_token::token_type::number_token) {
return css_value::maybe_dimension_from_number(tok);
}
}
}
if (prop.is_display()) {
if (parser_block.is_token()) {
/* A single token */
const auto &tok = parser_block.get_token_or_empty();
if (tok.type == css_parser_token::token_type::ident_token) {
return css_value::maybe_display_from_string(tok.get_string_or_default(""));
}
}
}
if (prop.is_normal_number()) {
if (parser_block.is_token()) {
/* A single token */
const auto &tok = parser_block.get_token_or_empty();
if (tok.type == css_parser_token::token_type::number_token) {
return css_value{tok.get_normal_number_or_default(0)};
}
}
}
return std::nullopt;
}
auto process_declaration_tokens(rspamd_mempool_t *pool,
blocks_gen_functor &&next_block_functor)
-> css_declarations_block_ptr {
css_declarations_block_ptr ret;
bool can_continue = true;
css_property cur_property{css_property_type::PROPERTY_NYI,
css_property_flag::FLAG_NORMAL};
static const css_property bad_property{css_property_type::PROPERTY_NYI,
css_property_flag::FLAG_NORMAL};
std::shared_ptr<css_rule> cur_rule;
enum {
parse_property,
parse_value,
ignore_value, /* For unknown properties */
} state = parse_property;
auto seen_not = false;
ret = std::make_shared<css_declarations_block>();
while (can_continue) {
const auto &next_tok = next_block_functor();
switch (next_tok.tag) {
case css_consumed_block::parser_tag_type::css_component:
/* Component can be a property or a compound list of values */
if (state == parse_property) {
cur_property = css_property::from_token(next_tok.get_token_or_empty())
.value_or(bad_property);
if (cur_property.type == css_property_type::PROPERTY_NYI) {
state = ignore_value;
/* Ignore everything till ; */
continue;
}
msg_debug_css("got css property: %s", cur_property.to_string());
/* We now expect colon block */
const auto &expect_colon_block = next_block_functor();
if (expect_colon_block.tag != css_consumed_block::parser_tag_type::css_component) {
state = ignore_value; /* Ignore up to the next rule */
}
else {
const auto &expect_colon_tok = expect_colon_block.get_token_or_empty();
if (expect_colon_tok.type != css_parser_token::token_type::colon_token) {
msg_debug_css("invalid rule, no colon after property");
state = ignore_value; /* Ignore up to the next rule */
}
else {
state = parse_value;
cur_rule = std::make_shared<css_rule>(cur_property);
}
}
}
else if (state == parse_value) {
/* Check semicolon */
if (next_tok.is_token()) {
const auto &parser_tok = next_tok.get_token_or_empty();
if (parser_tok.type == css_parser_token::token_type::semicolon_token && cur_rule) {
ret->add_rule(std::move(cur_rule));
state = parse_property;
seen_not = false;
continue;
}
else if (parser_tok.type == css_parser_token::token_type::delim_token) {
if (parser_tok.get_string_or_default("") == "!") {
/* Probably something like !important */
seen_not = true;
}
}
else if (parser_tok.type == css_parser_token::token_type::ident_token) {
if (parser_tok.get_string_or_default("") == "important") {
if (seen_not) {
msg_debug_css("add !important flag to property %s",
cur_property.to_string());
cur_property.flag = css_property_flag::FLAG_NOT_IMPORTANT;
}
else {
msg_debug_css("add important flag to property %s",
cur_property.to_string());
cur_property.flag = css_property_flag::FLAG_IMPORTANT;
}
seen_not = false;
continue;
}
else {
seen_not = false;
}
}
}
auto maybe_value = allowed_property_value(cur_property, next_tok);
if (maybe_value) {
msg_debug_css("added value %s to the property %s",
maybe_value.value().debug_str().c_str(),
cur_property.to_string());
cur_rule->add_value(maybe_value.value());
}
}
else {
/* Ignore all till ; */
if (next_tok.is_token()) {
const auto &parser_tok = next_tok.get_token_or_empty();
if (parser_tok.type == css_parser_token::token_type::semicolon_token) {
state = parse_property;
}
}
}
break;
case css_consumed_block::parser_tag_type::css_function:
if (state == parse_value) {
auto maybe_value = allowed_property_value(cur_property, next_tok);
if (maybe_value && cur_rule) {
msg_debug_css("added value %s to the property %s",
maybe_value.value().debug_str().c_str(),
cur_property.to_string());
cur_rule->add_value(maybe_value.value());
}
}
break;
case css_consumed_block::parser_tag_type::css_eof_block:
if (state == parse_value) {
ret->add_rule(std::move(cur_rule));
}
can_continue = false;
break;
default:
can_continue = false;
break;
}
}
return ret; /* copy elision */
}
auto
css_declarations_block::merge_block(const css_declarations_block &other, merge_type how)
-> void {
const auto &other_rules = other.get_rules();
for (auto &rule : other_rules) {
auto &&found_it = rules.find(rule);
if (found_it != rules.end()) {
/* Duplicate, need to merge */
switch (how) {
case merge_type::merge_override:
/* Override */
(*found_it)->override_values(*rule);
break;
case merge_type::merge_duplicate:
/* Merge values */
add_rule(rule);
break;
case merge_type::merge_parent:
/* Do not merge parent rule if more specific local one is presented */
break;
}
}
else {
/* New property, just insert */
rules.insert(rule);
}
}
}
void css_rule::add_value(const css_value &value) {
values.push_back(value);
}
TEST_SUITE("css rules") {
TEST_CASE("simple css rules") {
const std::vector<std::pair<const char *, std::vector<css_property>>> cases{
{
"font-size:12.0pt;line-height:115%",
{css_property(css_property_type::PROPERTY_FONT_SIZE)}
},
{
"font-size:12.0pt;display:none",
{css_property(css_property_type::PROPERTY_FONT_SIZE),
css_property(css_property_type::PROPERTY_DISPLAY)}
}
};
auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(),
"css", 0);
for (const auto &c : cases) {
auto res = process_declaration_tokens(pool,
get_rules_parser_functor(pool, c.first));
CHECK(res.get() != nullptr);
for (auto i = 0; i < c.second.size(); i ++) {
CHECK(res->has_property(c.second[i]));
}
}
}
}
} // namespace rspamd::css
|