@geastack/engine 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/canvas.cpp +40 -5
- package/package.json +1 -1
- package/rasterized_font.cpp +118 -7
- package/touch_runtime.cpp +118 -67
- package/ui/canvas_element.h +1 -1
- package/ui/child_node_list.h +94 -0
- package/ui/control_commit.h +23 -0
- package/ui/css_features.h +5 -0
- package/ui/document.cpp +43 -96
- package/ui/document.h +61 -6
- package/ui/input.cpp +72 -20
- package/ui/input_retirement.h +22 -0
- package/ui/internal.h +3 -1
- package/ui/layout.cpp +31 -25
- package/ui/node.cpp +123 -31
- package/ui/node.h +55 -16
- package/ui/node_lifecycle.cpp +3 -0
- package/ui/node_model.h +20 -1
- package/ui/render.cpp +16 -3
- package/ui/selector.cpp +527 -0
- package/ui/selector.h +32 -0
- package/ui/style.cpp +94 -23
- package/ui/style.h +8 -3
- package/ui/text.cpp +73 -49
- package/ui/tree_events.cpp +376 -85
- package/ui/tree_events.h +34 -2
- package/ui/tree_internal.h +17 -5
- package/ui/tree_nodes.cpp +218 -13
- package/ui/tree_state.h +4 -0
- package/ui/tree_style.cpp +4 -0
- package/ui/virtual_keyboard.cpp +8 -1
package/ui/selector.cpp
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "selector.h"
|
|
3
|
+
#include "node.h"
|
|
4
|
+
#include "tree_internal.h"
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <cctype>
|
|
7
|
+
#include <cstdlib>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <stdexcept>
|
|
10
|
+
#include <vector>
|
|
11
|
+
namespace gea::embedded::ui::selector {
|
|
12
|
+
namespace {
|
|
13
|
+
constexpr size_t maxInput = 4096, maxTerms = 256, maxDepth = 8;
|
|
14
|
+
bool space(char c) {
|
|
15
|
+
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f';
|
|
16
|
+
}
|
|
17
|
+
std::string lower(std::string s) {
|
|
18
|
+
for (char &c : s)
|
|
19
|
+
if (c >= 'A' && c <= 'Z')
|
|
20
|
+
c += 32;
|
|
21
|
+
return s;
|
|
22
|
+
}
|
|
23
|
+
bool start(unsigned char c) {
|
|
24
|
+
return c == '_' || c == '-' || std::isalpha(c) || c >= 128 || c == '\\';
|
|
25
|
+
}
|
|
26
|
+
bool next(unsigned char c) { return start(c) || std::isdigit(c); }
|
|
27
|
+
void utf8(std::string &out, unsigned c) {
|
|
28
|
+
if (!c || c > 0x10ffff || (c >= 0xd800 && c <= 0xdfff))
|
|
29
|
+
c = 0xfffd;
|
|
30
|
+
if (c < 128)
|
|
31
|
+
out += char(c);
|
|
32
|
+
else if (c < 2048) {
|
|
33
|
+
out += char(0xc0 | (c >> 6));
|
|
34
|
+
out += char(0x80 | (c & 63));
|
|
35
|
+
} else if (c < 65536) {
|
|
36
|
+
out += char(0xe0 | (c >> 12));
|
|
37
|
+
out += char(0x80 | ((c >> 6) & 63));
|
|
38
|
+
out += char(0x80 | (c & 63));
|
|
39
|
+
} else {
|
|
40
|
+
out += char(0xf0 | (c >> 18));
|
|
41
|
+
out += char(0x80 | ((c >> 12) & 63));
|
|
42
|
+
out += char(0x80 | ((c >> 6) & 63));
|
|
43
|
+
out += char(0x80 | (c & 63));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
enum class Kind {
|
|
47
|
+
tag,
|
|
48
|
+
id,
|
|
49
|
+
klass,
|
|
50
|
+
attribute,
|
|
51
|
+
disabled,
|
|
52
|
+
enabled,
|
|
53
|
+
negation,
|
|
54
|
+
scope
|
|
55
|
+
};
|
|
56
|
+
struct Atom {
|
|
57
|
+
Kind kind;
|
|
58
|
+
std::string name, value, op;
|
|
59
|
+
bool insensitive = false, explicitCase = false;
|
|
60
|
+
std::shared_ptr<Program> nested;
|
|
61
|
+
};
|
|
62
|
+
struct Part {
|
|
63
|
+
std::vector<Atom> atoms;
|
|
64
|
+
char relation = 0;
|
|
65
|
+
};
|
|
66
|
+
using Chain = std::vector<Part>;
|
|
67
|
+
} // namespace
|
|
68
|
+
struct Program {
|
|
69
|
+
std::vector<Chain> alternatives;
|
|
70
|
+
};
|
|
71
|
+
namespace {
|
|
72
|
+
class Parser {
|
|
73
|
+
const std::string input;
|
|
74
|
+
size_t pos = 0, terms = 0;
|
|
75
|
+
ParseStatus error = ParseStatus::valid;
|
|
76
|
+
char peek() const { return pos < input.size() ? input[pos] : 0; }
|
|
77
|
+
bool ws() {
|
|
78
|
+
size_t old = pos;
|
|
79
|
+
while (space(peek()))
|
|
80
|
+
++pos;
|
|
81
|
+
return pos != old;
|
|
82
|
+
}
|
|
83
|
+
void fail(ParseStatus s = ParseStatus::invalidSyntax) {
|
|
84
|
+
if (error == ParseStatus::valid)
|
|
85
|
+
error = s;
|
|
86
|
+
}
|
|
87
|
+
bool term() {
|
|
88
|
+
if (++terms > maxTerms) {
|
|
89
|
+
fail(ParseStatus::limitExceeded);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
bool escape(std::string &out) {
|
|
95
|
+
if (peek() != '\\')
|
|
96
|
+
return false;
|
|
97
|
+
++pos;
|
|
98
|
+
char c = peek();
|
|
99
|
+
if (!c || c == '\n' || c == '\r' || c == '\f') {
|
|
100
|
+
fail();
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
if (std::isxdigit(static_cast<unsigned char>(c))) {
|
|
104
|
+
unsigned value = 0;
|
|
105
|
+
int n = 0;
|
|
106
|
+
while (n < 6 && std::isxdigit(static_cast<unsigned char>(peek()))) {
|
|
107
|
+
char h = input[pos++];
|
|
108
|
+
value = value * 16 +
|
|
109
|
+
(h >= '0' && h <= '9'
|
|
110
|
+
? h - '0'
|
|
111
|
+
: std::tolower(static_cast<unsigned char>(h)) - 'a' + 10);
|
|
112
|
+
++n;
|
|
113
|
+
}
|
|
114
|
+
if (space(peek())) {
|
|
115
|
+
if (peek() == '\r' && pos + 1 < input.size() && input[pos + 1] == '\n')
|
|
116
|
+
++pos;
|
|
117
|
+
++pos;
|
|
118
|
+
}
|
|
119
|
+
utf8(out, value);
|
|
120
|
+
} else
|
|
121
|
+
out += input[pos++];
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
std::string ident() {
|
|
125
|
+
std::string out;
|
|
126
|
+
if (!start(static_cast<unsigned char>(peek()))) {
|
|
127
|
+
fail();
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
if (peek() == '-' && pos + 1 < input.size() &&
|
|
131
|
+
std::isdigit(static_cast<unsigned char>(input[pos + 1]))) {
|
|
132
|
+
fail();
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
while (next(static_cast<unsigned char>(peek()))) {
|
|
136
|
+
if (peek() == '\\') {
|
|
137
|
+
if (!escape(out))
|
|
138
|
+
break;
|
|
139
|
+
} else
|
|
140
|
+
out += input[pos++];
|
|
141
|
+
}
|
|
142
|
+
if (out.empty() || out == "-")
|
|
143
|
+
fail();
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
std::string quoted() {
|
|
147
|
+
char quote = input[pos++];
|
|
148
|
+
std::string out;
|
|
149
|
+
while (peek() && peek() != quote) {
|
|
150
|
+
if (peek() == '\\') {
|
|
151
|
+
if (!escape(out))
|
|
152
|
+
return out;
|
|
153
|
+
} else if (peek() == '\n' || peek() == '\r' || peek() == '\f') {
|
|
154
|
+
fail();
|
|
155
|
+
return out;
|
|
156
|
+
} else
|
|
157
|
+
out += input[pos++];
|
|
158
|
+
}
|
|
159
|
+
if (peek() != quote)
|
|
160
|
+
fail();
|
|
161
|
+
else
|
|
162
|
+
++pos;
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
Atom attr() {
|
|
166
|
+
Atom a{Kind::attribute};
|
|
167
|
+
++pos;
|
|
168
|
+
ws();
|
|
169
|
+
a.name = lower(ident());
|
|
170
|
+
ws();
|
|
171
|
+
if (peek() == ']') {
|
|
172
|
+
++pos;
|
|
173
|
+
return a;
|
|
174
|
+
}
|
|
175
|
+
if (peek() == '=') {
|
|
176
|
+
a.op = "=";
|
|
177
|
+
++pos;
|
|
178
|
+
} else if (std::strchr("~|^$*", peek()) && peek()) {
|
|
179
|
+
a.op += input[pos++];
|
|
180
|
+
if (peek() != '=') {
|
|
181
|
+
fail();
|
|
182
|
+
return a;
|
|
183
|
+
}
|
|
184
|
+
a.op += '=';
|
|
185
|
+
++pos;
|
|
186
|
+
} else {
|
|
187
|
+
fail();
|
|
188
|
+
return a;
|
|
189
|
+
}
|
|
190
|
+
ws();
|
|
191
|
+
a.value = peek() == '\'' || peek() == '"' ? quoted() : ident();
|
|
192
|
+
ws();
|
|
193
|
+
if (peek() != ']') {
|
|
194
|
+
std::string flag = lower(ident());
|
|
195
|
+
if (flag != "i" && flag != "s")
|
|
196
|
+
fail();
|
|
197
|
+
a.insensitive = flag == "i";
|
|
198
|
+
a.explicitCase = true;
|
|
199
|
+
ws();
|
|
200
|
+
}
|
|
201
|
+
if (peek() != ']')
|
|
202
|
+
fail();
|
|
203
|
+
else
|
|
204
|
+
++pos;
|
|
205
|
+
return a;
|
|
206
|
+
}
|
|
207
|
+
Part compound(size_t depth) {
|
|
208
|
+
Part p;
|
|
209
|
+
if (peek() == '*') {
|
|
210
|
+
++pos;
|
|
211
|
+
p.atoms.push_back({Kind::tag, "*"});
|
|
212
|
+
term();
|
|
213
|
+
} else if (start(static_cast<unsigned char>(peek()))) {
|
|
214
|
+
p.atoms.push_back({Kind::tag, lower(ident())});
|
|
215
|
+
term();
|
|
216
|
+
}
|
|
217
|
+
while (error == ParseStatus::valid) {
|
|
218
|
+
char c = peek();
|
|
219
|
+
if (c == '#' || c == '.') {
|
|
220
|
+
++pos;
|
|
221
|
+
Atom a{c == '#' ? Kind::id : Kind::klass};
|
|
222
|
+
a.name = ident();
|
|
223
|
+
p.atoms.push_back(std::move(a));
|
|
224
|
+
term();
|
|
225
|
+
} else if (c == '[') {
|
|
226
|
+
p.atoms.push_back(attr());
|
|
227
|
+
term();
|
|
228
|
+
} else if (c == ':') {
|
|
229
|
+
++pos;
|
|
230
|
+
std::string name = lower(ident());
|
|
231
|
+
Atom a{Kind::scope};
|
|
232
|
+
if (name == "disabled")
|
|
233
|
+
a.kind = Kind::disabled;
|
|
234
|
+
else if (name == "enabled")
|
|
235
|
+
a.kind = Kind::enabled;
|
|
236
|
+
else if (name == "scope")
|
|
237
|
+
a.kind = Kind::scope;
|
|
238
|
+
else if (name == "not") {
|
|
239
|
+
a.kind = Kind::negation;
|
|
240
|
+
if (peek() != '(') {
|
|
241
|
+
fail();
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
++pos;
|
|
245
|
+
a.nested = list(depth + 1, ')');
|
|
246
|
+
if (peek() != ')') {
|
|
247
|
+
fail();
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
++pos;
|
|
251
|
+
} else {
|
|
252
|
+
fail(ParseStatus::unsupportedSyntax);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
p.atoms.push_back(std::move(a));
|
|
256
|
+
term();
|
|
257
|
+
} else
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
if (p.atoms.empty())
|
|
261
|
+
fail();
|
|
262
|
+
return p;
|
|
263
|
+
}
|
|
264
|
+
std::shared_ptr<Program> list(size_t depth, char end) {
|
|
265
|
+
if (depth > maxDepth) {
|
|
266
|
+
fail(ParseStatus::limitExceeded);
|
|
267
|
+
return {};
|
|
268
|
+
}
|
|
269
|
+
auto program = std::make_shared<Program>();
|
|
270
|
+
ws();
|
|
271
|
+
if (!peek() || peek() == end) {
|
|
272
|
+
fail();
|
|
273
|
+
return program;
|
|
274
|
+
}
|
|
275
|
+
while (error == ParseStatus::valid) {
|
|
276
|
+
Chain chain;
|
|
277
|
+
chain.push_back(compound(depth));
|
|
278
|
+
while (error == ParseStatus::valid) {
|
|
279
|
+
bool spaced = ws();
|
|
280
|
+
char c = peek();
|
|
281
|
+
if (!c || c == ',' || c == end)
|
|
282
|
+
break;
|
|
283
|
+
char relation = ' ';
|
|
284
|
+
if (c == '>') {
|
|
285
|
+
relation = '>';
|
|
286
|
+
++pos;
|
|
287
|
+
ws();
|
|
288
|
+
} else if (c == '+' || c == '~' || c == '|') {
|
|
289
|
+
fail(ParseStatus::unsupportedSyntax);
|
|
290
|
+
break;
|
|
291
|
+
} else if (!spaced) {
|
|
292
|
+
fail();
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
Part part = compound(depth);
|
|
296
|
+
part.relation = relation;
|
|
297
|
+
chain.push_back(std::move(part));
|
|
298
|
+
}
|
|
299
|
+
program->alternatives.push_back(std::move(chain));
|
|
300
|
+
if (peek() != ',')
|
|
301
|
+
break;
|
|
302
|
+
++pos;
|
|
303
|
+
ws();
|
|
304
|
+
if (!peek() || peek() == end) {
|
|
305
|
+
fail();
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return program;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
public:
|
|
313
|
+
explicit Parser(const char *s)
|
|
314
|
+
: input(s ? std::string(s, strnlen(s, maxInput + 1)) : "") {}
|
|
315
|
+
Query run() {
|
|
316
|
+
if (input.size() > maxInput)
|
|
317
|
+
return {{}, ParseStatus::limitExceeded};
|
|
318
|
+
auto p = list(0, 0);
|
|
319
|
+
ws();
|
|
320
|
+
if (peek())
|
|
321
|
+
fail();
|
|
322
|
+
return {error == ParseStatus::valid ? p : nullptr, error};
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
bool live(const Tree &t, int n) {
|
|
326
|
+
return n >= 0 && n < t.nodeCount() && NodeHandle(n).valid();
|
|
327
|
+
}
|
|
328
|
+
bool element(const Tree &t, int n) {
|
|
329
|
+
return live(t, n) && static_cast<int>(NodeHandle(n).nodeType) == 1;
|
|
330
|
+
}
|
|
331
|
+
std::string tag(const Tree &t, int n) {
|
|
332
|
+
const char *s = t.tagName(n);
|
|
333
|
+
return lower(s ? s : "");
|
|
334
|
+
}
|
|
335
|
+
bool within(const Tree &t, int n, int parent) {
|
|
336
|
+
for (int budget = t.nodeCount(); budget > 0 && live(t, n); --budget) {
|
|
337
|
+
if (n == parent)
|
|
338
|
+
return true;
|
|
339
|
+
n = t.node(n).parent;
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
// `stepper` is the existing native SDK control (NativeStepperProps.disabled),
|
|
344
|
+
// not an arbitrary HTML custom element. Its enabled state must not regress.
|
|
345
|
+
bool disableable(const std::string &t) {
|
|
346
|
+
return t == "button" || t == "input" || t == "select" || t == "textarea" ||
|
|
347
|
+
t == "fieldset" || t == "option" || t == "optgroup" || t == "stepper";
|
|
348
|
+
}
|
|
349
|
+
struct Work {
|
|
350
|
+
size_t left = 100000;
|
|
351
|
+
void take() {
|
|
352
|
+
if (left--)
|
|
353
|
+
return;
|
|
354
|
+
#if defined(__cpp_exceptions)
|
|
355
|
+
throw std::length_error("Native selector matching work limit exceeded");
|
|
356
|
+
#else
|
|
357
|
+
std::abort();
|
|
358
|
+
#endif
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
bool matchProgram(const Program &, const Tree &, int, int, Work &);
|
|
362
|
+
bool atom(const Atom &a, const Tree &t, int n, int scope, Work &work) {
|
|
363
|
+
work.take();
|
|
364
|
+
switch (a.kind) {
|
|
365
|
+
case Kind::tag:
|
|
366
|
+
return a.name == "*" || tag(t, n) == a.name ||
|
|
367
|
+
(a.name == "body" && n == t.mountedRoot());
|
|
368
|
+
case Kind::id:
|
|
369
|
+
return t.hasAttribute(n, "id") && a.name == t.getAttribute(n, "id");
|
|
370
|
+
case Kind::klass:
|
|
371
|
+
return t.hasClass(n, a.name);
|
|
372
|
+
case Kind::scope:
|
|
373
|
+
return n == scope;
|
|
374
|
+
case Kind::disabled:
|
|
375
|
+
return disabled(t, n);
|
|
376
|
+
case Kind::enabled:
|
|
377
|
+
return disableable(tag(t, n)) && !disabled(t, n);
|
|
378
|
+
case Kind::negation:
|
|
379
|
+
return a.nested && !matchProgram(*a.nested, t, n, scope, work);
|
|
380
|
+
case Kind::attribute: {
|
|
381
|
+
if (!t.hasAttribute(n, a.name.c_str()))
|
|
382
|
+
return false;
|
|
383
|
+
if (a.op.empty())
|
|
384
|
+
return true;
|
|
385
|
+
std::string value = t.getAttribute(n, a.name.c_str()), expected = a.value;
|
|
386
|
+
// HTML's legacy enumerated attribute values are ASCII insensitive even
|
|
387
|
+
// when the attribute is placed on a different HTML element. Explicit `s`
|
|
388
|
+
// overrides that default; data-* and arbitrary attributes remain sensitive.
|
|
389
|
+
const std::string insensitiveNames =
|
|
390
|
+
" accept accept-charset align alink axis bgcolor charset checked clear "
|
|
391
|
+
"codetype color compact declare defer dir direction disabled enctype "
|
|
392
|
+
"face "
|
|
393
|
+
"frame hreflang http-equiv lang language link media method multiple "
|
|
394
|
+
"nohref "
|
|
395
|
+
"noresize noshade nowrap readonly rel rev rules scope scrolling "
|
|
396
|
+
"selected "
|
|
397
|
+
"shape target text type valign valuetype vlink ";
|
|
398
|
+
const bool htmlDefault =
|
|
399
|
+
!a.explicitCase &&
|
|
400
|
+
insensitiveNames.find(" " + a.name + " ") != std::string::npos;
|
|
401
|
+
if (a.insensitive || htmlDefault) {
|
|
402
|
+
value = lower(value);
|
|
403
|
+
expected = lower(expected);
|
|
404
|
+
}
|
|
405
|
+
if (a.op == "=")
|
|
406
|
+
return value == expected;
|
|
407
|
+
if (a.op == "|=")
|
|
408
|
+
return value == expected || value.rfind(expected + "-", 0) == 0;
|
|
409
|
+
if (expected.empty())
|
|
410
|
+
return false;
|
|
411
|
+
if (a.op == "^=")
|
|
412
|
+
return value.rfind(expected, 0) == 0;
|
|
413
|
+
if (a.op == "$=")
|
|
414
|
+
return value.size() >= expected.size() &&
|
|
415
|
+
value.compare(value.size() - expected.size(), expected.size(),
|
|
416
|
+
expected) == 0;
|
|
417
|
+
if (a.op == "*=")
|
|
418
|
+
return value.find(expected) != std::string::npos;
|
|
419
|
+
if (a.op == "~=") {
|
|
420
|
+
size_t p = 0;
|
|
421
|
+
while (p < value.size()) {
|
|
422
|
+
while (p < value.size() && space(value[p]))
|
|
423
|
+
++p;
|
|
424
|
+
size_t e = p;
|
|
425
|
+
while (e < value.size() && !space(value[e]))
|
|
426
|
+
++e;
|
|
427
|
+
if (value.substr(p, e - p) == expected)
|
|
428
|
+
return true;
|
|
429
|
+
p = e;
|
|
430
|
+
}
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
bool part(const Part &p, const Tree &t, int n, int scope, Work &work) {
|
|
439
|
+
if (!element(t, n))
|
|
440
|
+
return false;
|
|
441
|
+
for (const auto &a : p.atoms)
|
|
442
|
+
if (!atom(a, t, n, scope, work))
|
|
443
|
+
return false;
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
446
|
+
bool chain(const Chain &c, size_t i, const Tree &t, int n, int scope,
|
|
447
|
+
int budget, Work &work) {
|
|
448
|
+
work.take();
|
|
449
|
+
if (budget <= 0 || !part(c[i], t, n, scope, work))
|
|
450
|
+
return false;
|
|
451
|
+
if (i == 0)
|
|
452
|
+
return true;
|
|
453
|
+
int parent = t.node(n).parent;
|
|
454
|
+
if (c[i].relation == '>')
|
|
455
|
+
return live(t, parent) &&
|
|
456
|
+
chain(c, i - 1, t, parent, scope, budget - 1, work);
|
|
457
|
+
for (int left = budget - 1; left > 0 && live(t, parent); --left) {
|
|
458
|
+
if (chain(c, i - 1, t, parent, scope, left, work))
|
|
459
|
+
return true;
|
|
460
|
+
parent = t.node(parent).parent;
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
bool matchProgram(const Program &p, const Tree &t, int n, int scope,
|
|
465
|
+
Work &work) {
|
|
466
|
+
for (const auto &c : p.alternatives)
|
|
467
|
+
if (!c.empty() &&
|
|
468
|
+
chain(c, c.size() - 1, t, n, scope, t.nodeCount() + 1, work))
|
|
469
|
+
return true;
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
} // namespace
|
|
473
|
+
bool disabled(const Tree &t, int n) {
|
|
474
|
+
if (!element(t, n))
|
|
475
|
+
return false;
|
|
476
|
+
const auto name = tag(t, n);
|
|
477
|
+
if (!disableable(name))
|
|
478
|
+
return false;
|
|
479
|
+
if (t.hasAttribute(n, "disabled"))
|
|
480
|
+
return true;
|
|
481
|
+
if (name == "optgroup")
|
|
482
|
+
return false;
|
|
483
|
+
if (name == "option") {
|
|
484
|
+
int p = t.node(n).parent;
|
|
485
|
+
return live(t, p) && tag(t, p) == "optgroup" &&
|
|
486
|
+
t.hasAttribute(p, "disabled");
|
|
487
|
+
}
|
|
488
|
+
for (int p = t.node(n).parent, budget = t.nodeCount();
|
|
489
|
+
budget > 0 && live(t, p); --budget, p = t.node(p).parent) {
|
|
490
|
+
if (tag(t, p) != "fieldset" || !t.hasAttribute(p, "disabled"))
|
|
491
|
+
continue;
|
|
492
|
+
int legend = -1;
|
|
493
|
+
for (int c = t.node(p).first_child, left = t.nodeCount();
|
|
494
|
+
left > 0 && live(t, c); --left, c = t.node(c).next_sibling)
|
|
495
|
+
if (tag(t, c) == "legend") {
|
|
496
|
+
legend = c;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
if (legend >= 0 && within(t, n, legend))
|
|
500
|
+
continue;
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
Query parse(const char *input) { return Parser(input).run(); }
|
|
506
|
+
void Query::requireValid() const {
|
|
507
|
+
if (status_ == ParseStatus::valid)
|
|
508
|
+
return;
|
|
509
|
+
#if defined(__cpp_exceptions)
|
|
510
|
+
throw std::invalid_argument(status_ == ParseStatus::unsupportedSyntax
|
|
511
|
+
? "Unsupported native selector grammar"
|
|
512
|
+
: status_ == ParseStatus::limitExceeded
|
|
513
|
+
? "Native selector limit exceeded"
|
|
514
|
+
: "Invalid native selector syntax");
|
|
515
|
+
#else
|
|
516
|
+
std::abort();
|
|
517
|
+
#endif
|
|
518
|
+
}
|
|
519
|
+
bool Query::matches(const Tree &t, int node, int scope) const {
|
|
520
|
+
requireValid();
|
|
521
|
+
Work work;
|
|
522
|
+
work.left = remainingWork_;
|
|
523
|
+
bool result = program_ && matchProgram(*program_, t, node, scope, work);
|
|
524
|
+
remainingWork_ = work.left;
|
|
525
|
+
return result;
|
|
526
|
+
}
|
|
527
|
+
} // namespace gea::embedded::ui::selector
|
package/ui/selector.h
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include <memory>
|
|
4
|
+
#include <string>
|
|
5
|
+
namespace gea::embedded::ui {
|
|
6
|
+
class Tree;
|
|
7
|
+
namespace selector {
|
|
8
|
+
enum class ParseStatus {
|
|
9
|
+
valid,
|
|
10
|
+
invalidSyntax,
|
|
11
|
+
unsupportedSyntax,
|
|
12
|
+
limitExceeded
|
|
13
|
+
};
|
|
14
|
+
struct Program;
|
|
15
|
+
// A bounded selector program. Invalid/unsupported input never produces a
|
|
16
|
+
// partially usable program. Public DOM entry points call requireValid().
|
|
17
|
+
class Query {
|
|
18
|
+
std::shared_ptr<const Program> program_;
|
|
19
|
+
ParseStatus status_;
|
|
20
|
+
mutable size_t remainingWork_ = 1000000;
|
|
21
|
+
|
|
22
|
+
public:
|
|
23
|
+
Query(std::shared_ptr<const Program> program, ParseStatus status)
|
|
24
|
+
: program_(std::move(program)), status_(status) {}
|
|
25
|
+
ParseStatus status() const { return status_; }
|
|
26
|
+
void requireValid() const;
|
|
27
|
+
bool matches(const Tree &tree, int node, int scope = -1) const;
|
|
28
|
+
};
|
|
29
|
+
Query parse(const char *input);
|
|
30
|
+
bool disabled(const Tree &tree, int node);
|
|
31
|
+
} // namespace selector
|
|
32
|
+
} // namespace gea::embedded::ui
|