xbintsc 0.3.25 → 0.3.46
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/README.md +39 -3
- package/dist/src/cli/main.js +124 -30
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/calls/access.js +8 -1
- package/dist/src/codegen/generator/calls/access.js.map +1 -1
- package/dist/src/codegen/generator/module.js +10 -0
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/state.js +24 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.d.ts +6 -0
- package/dist/src/codegen/generator/tables.js +38 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/commonjs.d.ts +52 -0
- package/dist/src/driver/bundler/commonjs.js +493 -0
- package/dist/src/driver/bundler/commonjs.js.map +1 -0
- package/dist/src/driver/bundler/graph.js +14 -0
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/bundler/merge.js +14 -0
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/types.d.ts +8 -0
- package/dist/src/driver/compiler.d.ts +8 -1
- package/dist/src/driver/compiler.js +73 -5
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.d.ts +53 -0
- package/dist/src/driver/config.js +141 -0
- package/dist/src/driver/config.js.map +1 -0
- package/dist/src/driver/icon.d.ts +62 -0
- package/dist/src/driver/icon.js +151 -0
- package/dist/src/driver/icon.js.map +1 -0
- package/dist/src/driver/index.d.ts +4 -0
- package/dist/src/driver/index.js +4 -0
- package/dist/src/driver/index.js.map +1 -1
- package/dist/src/driver/mac-bundle.d.ts +41 -0
- package/dist/src/driver/mac-bundle.js +123 -0
- package/dist/src/driver/mac-bundle.js.map +1 -0
- package/dist/src/driver/win-icon.d.ts +27 -0
- package/dist/src/driver/win-icon.js +80 -0
- package/dist/src/driver/win-icon.js.map +1 -0
- package/dist/src/parser/statements.js +17 -4
- package/dist/src/parser/statements.js.map +1 -1
- package/dist/tests/cli/main.test.js +46 -1
- package/dist/tests/cli/main.test.js.map +1 -1
- package/dist/tests/driver/config.test.d.ts +1 -0
- package/dist/tests/driver/config.test.js +124 -0
- package/dist/tests/driver/config.test.js.map +1 -0
- package/dist/tests/driver/icon.test.d.ts +1 -0
- package/dist/tests/driver/icon.test.js +421 -0
- package/dist/tests/driver/icon.test.js.map +1 -0
- package/dist/tests/driver/modules.test.js +109 -0
- package/dist/tests/driver/modules.test.js.map +1 -1
- package/dist/tests/e2e/fetch.test.d.ts +12 -0
- package/dist/tests/e2e/fetch.test.js +272 -0
- package/dist/tests/e2e/fetch.test.js.map +1 -0
- package/dist/tests/e2e/gc.test.d.ts +1 -0
- package/dist/tests/e2e/gc.test.js +168 -0
- package/dist/tests/e2e/gc.test.js.map +1 -0
- package/dist/tests/e2e/gui-dom.test.d.ts +5 -0
- package/dist/tests/e2e/gui-dom.test.js +102 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -0
- package/dist/tests/e2e/gui-events.test.d.ts +5 -0
- package/dist/tests/e2e/gui-events.test.js +60 -0
- package/dist/tests/e2e/gui-events.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +31 -0
- package/dist/tests/e2e/gui-helpers.js +74 -0
- package/dist/tests/e2e/gui-helpers.js.map +1 -0
- package/dist/tests/e2e/gui-layout.test.d.ts +4 -0
- package/dist/tests/e2e/gui-layout.test.js +88 -0
- package/dist/tests/e2e/gui-layout.test.js.map +1 -0
- package/dist/tests/e2e/gui-lifecycle.test.d.ts +5 -0
- package/dist/tests/e2e/gui-lifecycle.test.js +58 -0
- package/dist/tests/e2e/gui-lifecycle.test.js.map +1 -0
- package/dist/tests/e2e/gui-paint.test.d.ts +5 -0
- package/dist/tests/e2e/gui-paint.test.js +111 -0
- package/dist/tests/e2e/gui-paint.test.js.map +1 -0
- package/dist/tests/e2e/gui-script.test.d.ts +5 -0
- package/dist/tests/e2e/gui-script.test.js +78 -0
- package/dist/tests/e2e/gui-script.test.js.map +1 -0
- package/dist/tests/e2e/gui-style.test.d.ts +5 -0
- package/dist/tests/e2e/gui-style.test.js +175 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -0
- package/dist/tests/e2e/harness.d.ts +2 -0
- package/dist/tests/e2e/harness.js +1 -0
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/icon.test.d.ts +7 -0
- package/dist/tests/e2e/icon.test.js +80 -0
- package/dist/tests/e2e/icon.test.js.map +1 -0
- package/dist/tests/e2e/npm-modules.test.d.ts +4 -1
- package/dist/tests/e2e/npm-modules.test.js +47 -1
- package/dist/tests/e2e/npm-modules.test.js.map +1 -1
- package/dist/tests/e2e/runtime-coverage-typed-array.test.d.ts +9 -0
- package/dist/tests/e2e/runtime-coverage-typed-array.test.js +140 -0
- package/dist/tests/e2e/runtime-coverage-typed-array.test.js.map +1 -0
- package/dist/tests/parser/statements.test.js +11 -0
- package/dist/tests/parser/statements.test.js.map +1 -1
- package/package.json +3 -2
- package/runtime/ext_gui/css.cpp +4 -517
- package/runtime/ext_gui/css_internal.h +27 -0
- package/runtime/ext_gui/css_match.cpp +195 -0
- package/runtime/ext_gui/css_selector.cpp +207 -0
- package/runtime/ext_gui/css_sheet.cpp +129 -0
- package/runtime/ext_gui/dom.cpp +7 -403
- package/runtime/ext_gui/dom_api.cpp +63 -1146
- package/runtime/ext_gui/dom_api_document.cpp +96 -0
- package/runtime/ext_gui/dom_api_events.cpp +123 -0
- package/runtime/ext_gui/dom_api_internal.h +135 -0
- package/runtime/ext_gui/dom_api_node.cpp +329 -0
- package/runtime/ext_gui/dom_api_node_read.cpp +294 -0
- package/runtime/ext_gui/dom_api_proto.cpp +135 -0
- package/runtime/ext_gui/dom_api_style.cpp +192 -0
- package/runtime/ext_gui/dom_internal.h +39 -0
- package/runtime/ext_gui/dom_parse.cpp +348 -0
- package/runtime/ext_gui/dom_serialize.cpp +54 -0
- package/runtime/ext_gui/gui.cpp +56 -286
- package/runtime/ext_gui/gui_events.cpp +294 -0
- package/runtime/ext_gui/gui_internal.h +29 -0
- package/runtime/ext_gui/layout.cpp +6 -597
- package/runtime/ext_gui/layout_flex.cpp +200 -0
- package/runtime/ext_gui/layout_flow.cpp +191 -0
- package/runtime/ext_gui/layout_flow_internal.h +149 -0
- package/runtime/ext_gui/layout_inline.cpp +181 -0
- package/runtime/ext_gui/layout_internal.h +20 -0
- package/runtime/ext_gui/renderer.cpp +15 -679
- package/runtime/ext_gui/renderer_image.cpp +233 -0
- package/runtime/ext_gui/renderer_internal.h +112 -0
- package/runtime/ext_gui/renderer_shaders.h +108 -0
- package/runtime/ext_gui/renderer_text.cpp +342 -0
- package/runtime/ext_gui/style.cpp +12 -660
- package/runtime/ext_gui/style_internal.h +33 -0
- package/runtime/ext_gui/style_print.cpp +113 -0
- package/runtime/ext_gui/style_ua.cpp +98 -0
- package/runtime/ext_gui/style_values.cpp +468 -0
- package/runtime/ext_gui/window.cpp +1 -0
- package/runtime/ext_node/buffer/parts/prototype.inc +1 -0
- package/runtime/ext_node/dgram/dgram.c +1 -0
- package/runtime/ext_node/events/events.c +1 -0
- package/runtime/ext_node/fs/fs_ops.c +10 -25
- package/runtime/ext_node/fs/glob.c +13 -30
- package/runtime/ext_node/fs/promises.c +1 -0
- package/runtime/ext_node/http/parts/prototypes.inc +5 -0
- package/runtime/ext_node/net/parts/prototypes.inc +2 -0
- package/runtime/ext_node/process/process.c +9 -7
- package/runtime/ext_node/stream/stream.c +1 -0
- package/runtime/ext_node/util/util.c +6 -6
- package/runtime/rt.h +16 -0
- package/runtime/rt_internal.h +67 -2
- package/runtime/xt_alloc.c +442 -5
- package/runtime/xt_builtins.c +1 -0
- package/runtime/xt_containers/access.inc +32 -1
- package/runtime/xt_fetch/buffer.inc +64 -0
- package/runtime/xt_fetch/headers.inc +312 -0
- package/runtime/xt_fetch/request.inc +386 -0
- package/runtime/xt_fetch/response.inc +107 -0
- package/runtime/xt_fetch/socket.inc +72 -0
- package/runtime/xt_fetch/url.inc +108 -0
- package/runtime/xt_fetch.c +39 -0
- package/runtime/xt_generator.c +95 -1
- package/runtime/xt_loop.c +35 -1
- package/runtime/xt_promise.c +46 -0
- package/runtime/xt_stdlib2/error.inc +1 -0
- package/runtime/xt_stdlib2/regexp-match.inc +8 -7
- package/runtime/xt_stdlib2/set.inc +1 -1
- package/runtime/xt_symbol.c +2 -0
- package/runtime/xt_typed_array/construction.inc +142 -0
- package/runtime/xt_typed_array/elements.inc +92 -0
- package/runtime/xt_typed_array/methods.inc +329 -0
- package/runtime/xt_typed_array.c +72 -0
- package/runtime/xt_values/number-format.inc +26 -0
- package/scripts/check-file-length.ts +85 -0
- package/src/cli/main.ts +137 -29
- package/src/codegen/generator/calls/access.ts +9 -1
- package/src/codegen/generator/module.ts +10 -0
- package/src/codegen/generator/state.ts +24 -0
- package/src/codegen/generator/tables.ts +40 -0
- package/src/driver/bundler/commonjs.ts +553 -0
- package/src/driver/bundler/graph.ts +14 -0
- package/src/driver/bundler/merge.ts +15 -0
- package/src/driver/bundler/types.ts +8 -0
- package/src/driver/compiler.ts +83 -5
- package/src/driver/config.ts +173 -0
- package/src/driver/icon.ts +184 -0
- package/src/driver/index.ts +30 -0
- package/src/driver/mac-bundle.ts +156 -0
- package/src/driver/win-icon.ts +96 -0
- package/src/parser/statements.ts +17 -4
- package/dist/tests/e2e/gui.test.d.ts +0 -12
- package/dist/tests/e2e/gui.test.js +0 -702
- package/dist/tests/e2e/gui.test.js.map +0 -1
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — HTML tokenizer + tree builder.
|
|
3
|
+
*
|
|
4
|
+
* The tokenizer is a single pass that also swallows the contents of raw-text
|
|
5
|
+
* elements (<style>, <script>, ...). The tree builder keeps a stack of open
|
|
6
|
+
* elements and applies a small set of HTML "implied end tag" rules, which is
|
|
7
|
+
* all a UI document needs.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#include "dom_internal.h"
|
|
11
|
+
|
|
12
|
+
#include <cstdlib>
|
|
13
|
+
#include <cstring>
|
|
14
|
+
#include <string>
|
|
15
|
+
#include <unordered_map>
|
|
16
|
+
#include <utility>
|
|
17
|
+
#include <vector>
|
|
18
|
+
|
|
19
|
+
namespace xtgui {
|
|
20
|
+
|
|
21
|
+
/* -- helpers -------------------------------------------------------------- */
|
|
22
|
+
|
|
23
|
+
/** Decode the handful of entities that show up in UI text. */
|
|
24
|
+
static std::string decodeEntities(const std::string &text) {
|
|
25
|
+
if (text.find('&') == std::string::npos) return text;
|
|
26
|
+
std::string out;
|
|
27
|
+
out.reserve(text.size());
|
|
28
|
+
for (size_t i = 0; i < text.size(); i++) {
|
|
29
|
+
if (text[i] != '&') {
|
|
30
|
+
out.push_back(text[i]);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
size_t semi = text.find(';', i + 1);
|
|
34
|
+
if (semi == std::string::npos || semi - i > 12) {
|
|
35
|
+
out.push_back('&');
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
std::string name = text.substr(i + 1, semi - i - 1);
|
|
39
|
+
if (name == "amp") out.push_back('&');
|
|
40
|
+
else if (name == "lt") out.push_back('<');
|
|
41
|
+
else if (name == "gt") out.push_back('>');
|
|
42
|
+
else if (name == "quot") out.push_back('"');
|
|
43
|
+
else if (name == "apos") out.push_back('\'');
|
|
44
|
+
else if (name == "nbsp") out.push_back(' ');
|
|
45
|
+
else if (!name.empty() && name[0] == '#') {
|
|
46
|
+
long code = 0;
|
|
47
|
+
if (name.size() > 1 && (name[1] == 'x' || name[1] == 'X')) {
|
|
48
|
+
for (size_t k = 2; k < name.size(); k++) {
|
|
49
|
+
char digit = (char)std::tolower((unsigned char)name[k]);
|
|
50
|
+
int value = (digit >= '0' && digit <= '9') ? digit - '0'
|
|
51
|
+
: (digit >= 'a' && digit <= 'f') ? digit - 'a' + 10
|
|
52
|
+
: -1;
|
|
53
|
+
if (value < 0) break;
|
|
54
|
+
code = code * 16 + value;
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
code = strtol(name.c_str() + 1, nullptr, 10);
|
|
58
|
+
}
|
|
59
|
+
if (code > 0 && code < 128) out.push_back((char)code);
|
|
60
|
+
else out.push_back('?');
|
|
61
|
+
} else {
|
|
62
|
+
out.append(text, i, semi - i + 1);
|
|
63
|
+
i = semi;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
i = semi;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static const std::unordered_map<std::string, bool> &voidElements() {
|
|
72
|
+
static const std::unordered_map<std::string, bool> set = {
|
|
73
|
+
{"area", true}, {"base", true}, {"br", true}, {"col", true},
|
|
74
|
+
{"embed", true}, {"hr", true}, {"img", true}, {"input", true},
|
|
75
|
+
{"link", true}, {"meta", true}, {"param", true}, {"source", true},
|
|
76
|
+
{"track", true}, {"wbr", true},
|
|
77
|
+
};
|
|
78
|
+
return set;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Elements whose content is treated as raw text. `script` is discarded. */
|
|
82
|
+
static bool isRawText(const std::string &tag, bool *keepText) {
|
|
83
|
+
if (tag == "style" || tag == "title" || tag == "textarea") {
|
|
84
|
+
*keepText = true;
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if (tag == "script" || tag == "xmp" || tag == "iframe" || tag == "noembed" ||
|
|
88
|
+
tag == "noframes" || tag == "plaintext") {
|
|
89
|
+
*keepText = false;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* -- tokenizer ------------------------------------------------------------ */
|
|
96
|
+
|
|
97
|
+
enum class TokenKind { Start, End, Text, EndOfFile };
|
|
98
|
+
|
|
99
|
+
struct Token {
|
|
100
|
+
TokenKind kind = TokenKind::EndOfFile;
|
|
101
|
+
std::string name;
|
|
102
|
+
std::vector<Attr> attrs;
|
|
103
|
+
bool selfClosing = false;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
class Tokenizer {
|
|
107
|
+
public:
|
|
108
|
+
explicit Tokenizer(const std::string &html) : html_(html) {}
|
|
109
|
+
|
|
110
|
+
std::vector<Token> run() {
|
|
111
|
+
std::vector<Token> tokens;
|
|
112
|
+
while (position_ < html_.size()) {
|
|
113
|
+
if (html_[position_] == '<' && position_ + 1 < html_.size()) {
|
|
114
|
+
char next = html_[position_ + 1];
|
|
115
|
+
if (next == '!') {
|
|
116
|
+
skipBogusComment();
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (next == '/') {
|
|
120
|
+
Token token = readEndTag();
|
|
121
|
+
if (!token.name.empty()) tokens.push_back(std::move(token));
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (xt_dom_is_alpha(next)) {
|
|
125
|
+
Token token = readStartTag();
|
|
126
|
+
tokens.push_back(token);
|
|
127
|
+
bool keepText = false;
|
|
128
|
+
if (isRawText(token.name, &keepText) && !token.selfClosing) {
|
|
129
|
+
readRawText(token.name, keepText, tokens);
|
|
130
|
+
}
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
std::string text = readText();
|
|
135
|
+
if (!text.empty()) {
|
|
136
|
+
Token token;
|
|
137
|
+
token.kind = TokenKind::Text;
|
|
138
|
+
token.name = decodeEntities(text);
|
|
139
|
+
tokens.push_back(std::move(token));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return tokens;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private:
|
|
146
|
+
const std::string &html_;
|
|
147
|
+
size_t position_ = 0;
|
|
148
|
+
|
|
149
|
+
void skipBogusComment() {
|
|
150
|
+
if (html_.compare(position_, 4, "<!--") == 0) {
|
|
151
|
+
size_t end = html_.find("-->", position_ + 4);
|
|
152
|
+
position_ = (end == std::string::npos) ? html_.size() : end + 3;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
size_t end = html_.find('>', position_);
|
|
156
|
+
position_ = (end == std::string::npos) ? html_.size() : end + 1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
std::string readText() {
|
|
160
|
+
size_t start = position_;
|
|
161
|
+
while (position_ < html_.size() && html_[position_] != '<') position_++;
|
|
162
|
+
return html_.substr(start, position_ - start);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
std::string readName() {
|
|
166
|
+
size_t start = position_;
|
|
167
|
+
while (position_ < html_.size()) {
|
|
168
|
+
char c = html_[position_];
|
|
169
|
+
if (xt_dom_is_space(c) || c == '/' || c == '>') break;
|
|
170
|
+
position_++;
|
|
171
|
+
}
|
|
172
|
+
return xt_dom_lower(html_.substr(start, position_ - start));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
void skipWhitespace() {
|
|
176
|
+
while (position_ < html_.size() && xt_dom_is_space(html_[position_])) position_++;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
Token readStartTag() {
|
|
180
|
+
Token token;
|
|
181
|
+
token.kind = TokenKind::Start;
|
|
182
|
+
position_++; // consume '<'
|
|
183
|
+
token.name = readName();
|
|
184
|
+
while (position_ < html_.size()) {
|
|
185
|
+
skipWhitespace();
|
|
186
|
+
if (position_ >= html_.size()) break;
|
|
187
|
+
char c = html_[position_];
|
|
188
|
+
if (c == '>') {
|
|
189
|
+
position_++;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
if (c == '/') {
|
|
193
|
+
token.selfClosing = true;
|
|
194
|
+
position_++;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
readAttribute(token);
|
|
198
|
+
}
|
|
199
|
+
return token;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void readAttribute(Token &token) {
|
|
203
|
+
size_t start = position_;
|
|
204
|
+
while (position_ < html_.size()) {
|
|
205
|
+
char c = html_[position_];
|
|
206
|
+
if (xt_dom_is_space(c) || c == '=' || c == '>' || c == '/') break;
|
|
207
|
+
position_++;
|
|
208
|
+
}
|
|
209
|
+
std::string name = xt_dom_lower(html_.substr(start, position_ - start));
|
|
210
|
+
if (name.empty()) {
|
|
211
|
+
position_++;
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
skipWhitespace();
|
|
215
|
+
std::string value;
|
|
216
|
+
if (position_ < html_.size() && html_[position_] == '=') {
|
|
217
|
+
position_++;
|
|
218
|
+
skipWhitespace();
|
|
219
|
+
if (position_ < html_.size() && (html_[position_] == '"' || html_[position_] == '\'')) {
|
|
220
|
+
char quote = html_[position_++];
|
|
221
|
+
size_t begin = position_;
|
|
222
|
+
while (position_ < html_.size() && html_[position_] != quote) position_++;
|
|
223
|
+
value = html_.substr(begin, position_ - begin);
|
|
224
|
+
if (position_ < html_.size()) position_++;
|
|
225
|
+
} else {
|
|
226
|
+
size_t begin = position_;
|
|
227
|
+
while (position_ < html_.size() && !xt_dom_is_space(html_[position_]) && html_[position_] != '>') position_++;
|
|
228
|
+
value = html_.substr(begin, position_ - begin);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
token.attrs.push_back({name, decodeEntities(value)});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
Token readEndTag() {
|
|
235
|
+
Token token;
|
|
236
|
+
token.kind = TokenKind::End;
|
|
237
|
+
position_ += 2; // consume '</'
|
|
238
|
+
token.name = readName();
|
|
239
|
+
size_t end = html_.find('>', position_);
|
|
240
|
+
position_ = (end == std::string::npos) ? html_.size() : end + 1;
|
|
241
|
+
return token;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
void readRawText(const std::string &tag, bool keepText, std::vector<Token> &tokens) {
|
|
245
|
+
size_t start = position_;
|
|
246
|
+
std::string close = "</" + tag;
|
|
247
|
+
while (position_ < html_.size()) {
|
|
248
|
+
if (html_[position_] == '<' && xt_dom_lower(html_.substr(position_, close.size())) == close) {
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
position_++;
|
|
252
|
+
}
|
|
253
|
+
std::string raw = html_.substr(start, position_ - start);
|
|
254
|
+
if (keepText && !raw.empty()) {
|
|
255
|
+
Token text;
|
|
256
|
+
text.kind = TokenKind::Text;
|
|
257
|
+
text.name = raw;
|
|
258
|
+
tokens.push_back(std::move(text));
|
|
259
|
+
}
|
|
260
|
+
if (position_ < html_.size()) {
|
|
261
|
+
Token end = readEndTag();
|
|
262
|
+
tokens.push_back(std::move(end));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/* -- tree builder --------------------------------------------------------- */
|
|
268
|
+
|
|
269
|
+
namespace {
|
|
270
|
+
|
|
271
|
+
bool isBlockTag(const std::string &tag) {
|
|
272
|
+
static const std::unordered_map<std::string, bool> set = {
|
|
273
|
+
{"address", true}, {"article", true}, {"aside", true}, {"blockquote", true},
|
|
274
|
+
{"div", true}, {"dl", true}, {"fieldset", true}, {"figure", true},
|
|
275
|
+
{"footer", true}, {"form", true}, {"h1", true}, {"h2", true},
|
|
276
|
+
{"h3", true}, {"h4", true}, {"h5", true}, {"h6", true},
|
|
277
|
+
{"header", true}, {"hr", true}, {"main", true}, {"nav", true},
|
|
278
|
+
{"ol", true}, {"p", true}, {"pre", true}, {"section", true},
|
|
279
|
+
{"table", true}, {"ul", true},
|
|
280
|
+
};
|
|
281
|
+
return set.count(tag) != 0;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
bool isHeading(const std::string &tag) {
|
|
285
|
+
return tag.size() == 2 && tag[0] == 'h' && tag[1] >= '1' && tag[1] <= '6';
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Whether a start tag implicitly closes the currently open element. */
|
|
289
|
+
bool closesOpenElement(const std::string &open, const std::string &next) {
|
|
290
|
+
if (open.empty()) return false;
|
|
291
|
+
if (next == "li" && open == "li") return true;
|
|
292
|
+
if ((next == "dt" || next == "dd") && (open == "dt" || open == "dd")) return true;
|
|
293
|
+
if (next == "option" && open == "option") return true;
|
|
294
|
+
if (next == "p" && open == "p") return true;
|
|
295
|
+
if (isHeading(next) && (isHeading(open) || open == "p")) return true;
|
|
296
|
+
if (isBlockTag(next) && open == "p") return true;
|
|
297
|
+
if ((next == "td" || next == "th") && (open == "td" || open == "th")) return true;
|
|
298
|
+
if (next == "tr" && (open == "td" || open == "th" || open == "tr")) return true;
|
|
299
|
+
if ((next == "thead" || next == "tbody" || next == "tfoot") &&
|
|
300
|
+
(open == "td" || open == "th" || open == "tr" || open == "thead" ||
|
|
301
|
+
open == "tbody" || open == "tfoot")) {
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
} // namespace
|
|
308
|
+
|
|
309
|
+
std::unique_ptr<Node> xt_html_parse(const std::string &html) {
|
|
310
|
+
Tokenizer tokenizer(html);
|
|
311
|
+
std::vector<Token> tokens = tokenizer.run();
|
|
312
|
+
|
|
313
|
+
auto document = std::make_unique<Node>(NodeType::Document);
|
|
314
|
+
std::vector<Node *> stack;
|
|
315
|
+
stack.push_back(document.get());
|
|
316
|
+
|
|
317
|
+
for (Token &token : tokens) {
|
|
318
|
+
if (token.kind == TokenKind::Text) {
|
|
319
|
+
Node *current = stack.back();
|
|
320
|
+
/* Text inside <style>/<title> is kept verbatim; elsewhere keep it too so
|
|
321
|
+
* layout can collapse whitespace later. */
|
|
322
|
+
current->addText(token.name);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (token.kind == TokenKind::Start) {
|
|
326
|
+
while (stack.size() > 1 && closesOpenElement(stack.back()->tag, token.name)) {
|
|
327
|
+
stack.pop_back();
|
|
328
|
+
}
|
|
329
|
+
Node *element = stack.back()->addElement(token.name);
|
|
330
|
+
element->attrs = std::move(token.attrs);
|
|
331
|
+
bool isVoid = voidElements().count(token.name) != 0;
|
|
332
|
+
if (!isVoid && !token.selfClosing) stack.push_back(element);
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (token.kind == TokenKind::End) {
|
|
336
|
+
for (size_t i = stack.size(); i-- > 1;) {
|
|
337
|
+
if (stack[i]->tag == token.name) {
|
|
338
|
+
stack.resize(i);
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return document;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — DOM serialization and diagnostics.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include "dom_internal.h"
|
|
6
|
+
|
|
7
|
+
#include <cctype>
|
|
8
|
+
#include <string>
|
|
9
|
+
|
|
10
|
+
namespace xtgui {
|
|
11
|
+
|
|
12
|
+
static void serialize(const Node *node, std::string &out) {
|
|
13
|
+
if (node->type == NodeType::Document) {
|
|
14
|
+
for (const std::unique_ptr<Node> &child : node->children) serialize(child.get(), out);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (node->type == NodeType::Text) {
|
|
18
|
+
out += xt_dom_trim(node->text);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
out += "<" + node->tag;
|
|
22
|
+
for (const Attr &attr : node->attrs) out += " " + attr.name + "=\"" + attr.value + "\"";
|
|
23
|
+
out += ">";
|
|
24
|
+
for (const std::unique_ptr<Node> &child : node->children) serialize(child.get(), out);
|
|
25
|
+
out += "</" + node->tag + ">";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
std::string xt_dom_to_string(const Node *node) {
|
|
29
|
+
std::string out;
|
|
30
|
+
if (node != nullptr) serialize(node, out);
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
std::string xt_dom_describe(const Node *node) {
|
|
35
|
+
if (node == nullptr) return "";
|
|
36
|
+
if (node->isText()) return "#text";
|
|
37
|
+
if (!node->isElement()) return "#document";
|
|
38
|
+
std::string out = node->tag;
|
|
39
|
+
const std::string *id = node->attr("id");
|
|
40
|
+
if (id != nullptr && !id->empty()) out += "#" + *id;
|
|
41
|
+
const std::string *classes = node->attr("class");
|
|
42
|
+
if (classes != nullptr) {
|
|
43
|
+
size_t index = 0;
|
|
44
|
+
while (index < classes->size()) {
|
|
45
|
+
while (index < classes->size() && std::isspace((unsigned char)(*classes)[index])) index++;
|
|
46
|
+
size_t start = index;
|
|
47
|
+
while (index < classes->size() && !std::isspace((unsigned char)(*classes)[index])) index++;
|
|
48
|
+
if (index > start) out += "." + classes->substr(start, index - start);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
} // namespace xtgui
|