@salve-software/react-native-nitro-jsdom 1.1.0 → 2.0.0
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/android/CMakeLists.txt +4 -0
- package/cpp/HybridHtmlSandbox.cpp +7 -3
- package/cpp/lexbor/LexborDocument.cpp +62 -0
- package/cpp/lexbor/LexborDocument.hpp +14 -0
- package/cpp/quickjs/DOMBindings.cpp +9 -1
- package/cpp/quickjs/DOMBindingsInternal.cpp +67 -0
- package/cpp/quickjs/DOMBindingsInternal.hpp +39 -0
- package/cpp/quickjs/QuickJSRuntime.cpp +13 -0
- package/cpp/quickjs/QuickJSRuntime.hpp +28 -0
- package/cpp/quickjs/bindings/AbortBindings.cpp +14 -0
- package/cpp/quickjs/bindings/BlobBindings.cpp +28 -1
- package/cpp/quickjs/bindings/BlobBindings.hpp +3 -3
- package/cpp/quickjs/bindings/CSSOMBindings.cpp +46 -0
- package/cpp/quickjs/bindings/CSSOMBindings.hpp +3 -1
- package/cpp/quickjs/bindings/DOMParserBindings.cpp +299 -0
- package/cpp/quickjs/bindings/DOMParserBindings.hpp +45 -0
- package/cpp/quickjs/bindings/DocumentBindings.cpp +166 -11
- package/cpp/quickjs/bindings/ElementBindings.cpp +296 -20
- package/cpp/quickjs/bindings/EventBindings.cpp +277 -5
- package/cpp/quickjs/bindings/EventBindings.hpp +3 -0
- package/cpp/quickjs/bindings/EventTargetBindings.cpp +75 -0
- package/cpp/quickjs/bindings/EventTargetBindings.hpp +24 -0
- package/cpp/quickjs/bindings/FetchBindings.cpp +43 -8
- package/cpp/quickjs/bindings/FormBindings.cpp +223 -0
- package/cpp/quickjs/bindings/FormBindings.hpp +9 -5
- package/cpp/quickjs/bindings/LayoutStubBindings.cpp +104 -0
- package/cpp/quickjs/bindings/LayoutStubBindings.hpp +28 -0
- package/cpp/quickjs/bindings/LiveCollectionBindings.cpp +45 -0
- package/cpp/quickjs/bindings/TimerBindings.cpp +79 -0
- package/cpp/quickjs/bindings/TreeWalkerBindings.cpp +306 -0
- package/cpp/quickjs/bindings/TreeWalkerBindings.hpp +28 -0
- package/cpp/quickjs/bindings/UrlBindings.cpp +9 -0
- package/cpp/quickjs/bindings/WindowBindings.cpp +286 -4
- package/cpp/quickjs/bindings/WindowBindings.hpp +3 -2
- package/package.json +17 -2
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
#include "DOMParserBindings.hpp"
|
|
2
|
+
#include "../DOMBindingsInternal.hpp"
|
|
3
|
+
#include "../QuickJSRuntime.hpp"
|
|
4
|
+
#include "../../lexbor/LexborDocument.hpp"
|
|
5
|
+
#include <cstring>
|
|
6
|
+
#include <string>
|
|
7
|
+
|
|
8
|
+
namespace margelo::nitro::nitrojsdom {
|
|
9
|
+
|
|
10
|
+
namespace {
|
|
11
|
+
|
|
12
|
+
JSClassID js_parsed_document_class_id = 0;
|
|
13
|
+
JSClassDef js_parsed_document_class = { "Document", .finalizer = nullptr };
|
|
14
|
+
|
|
15
|
+
LexborDocument* unwrap_parsed_doc(JSContext* ctx, JSValue val) {
|
|
16
|
+
return static_cast<LexborDocument*>(JS_GetOpaque(val, js_parsed_document_class_id));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
JSValue make_parsed_document(JSContext* ctx, LexborDocument* doc) {
|
|
20
|
+
register_document(ctx, doc);
|
|
21
|
+
JSValue obj = JS_NewObjectClass(ctx, js_parsed_document_class_id);
|
|
22
|
+
JS_SetOpaque(obj, doc);
|
|
23
|
+
register_document_wrapper(ctx, doc, obj);
|
|
24
|
+
return obj;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ── Document-scoped query/creation methods (mirror DocumentBindings.cpp's
|
|
28
|
+
// js_doc_* functions, but resolve `this`'s own LexborDocument instead of
|
|
29
|
+
// always reading the sandbox's primary get_doc(ctx)) ────────────────────────
|
|
30
|
+
|
|
31
|
+
JSValue js_pdoc_getElementById(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
32
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
33
|
+
if (!doc || argc < 1) return JS_NULL;
|
|
34
|
+
const char* id = JS_ToCString(ctx, argv[0]);
|
|
35
|
+
if (!id) return JS_NULL;
|
|
36
|
+
void* el = doc->getElementById(id);
|
|
37
|
+
JS_FreeCString(ctx, id);
|
|
38
|
+
return make_element(ctx, el);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
JSValue js_pdoc_querySelector(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
42
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
43
|
+
if (!doc || argc < 1) return JS_NULL;
|
|
44
|
+
const char* sel = JS_ToCString(ctx, argv[0]);
|
|
45
|
+
if (!sel) return JS_NULL;
|
|
46
|
+
void* el = doc->querySelector_el(sel);
|
|
47
|
+
JS_FreeCString(ctx, sel);
|
|
48
|
+
return make_element(ctx, el);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
JSValue js_pdoc_querySelectorAll(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
52
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
53
|
+
if (!doc || argc < 1) return JS_NewArray(ctx);
|
|
54
|
+
const char* sel = JS_ToCString(ctx, argv[0]);
|
|
55
|
+
if (!sel) return JS_NewArray(ctx);
|
|
56
|
+
auto results = doc->querySelectorAll_el(sel);
|
|
57
|
+
JS_FreeCString(ctx, sel);
|
|
58
|
+
return make_element_array(ctx, results);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Static snapshot, not a live HTMLCollection — see header comment.
|
|
62
|
+
JSValue js_pdoc_getElementsByClassName(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
63
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
64
|
+
if (!doc || argc < 1) return JS_NewArray(ctx);
|
|
65
|
+
const char* names = JS_ToCString(ctx, argv[0]);
|
|
66
|
+
if (!names) return JS_NewArray(ctx);
|
|
67
|
+
auto results = doc->querySelectorAll_el(classNames_to_selector(names));
|
|
68
|
+
JS_FreeCString(ctx, names);
|
|
69
|
+
return make_element_array(ctx, results);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Static snapshot, not a live HTMLCollection — see header comment.
|
|
73
|
+
JSValue js_pdoc_getElementsByTagName(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
74
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
75
|
+
if (!doc || argc < 1) return JS_NewArray(ctx);
|
|
76
|
+
const char* tag = JS_ToCString(ctx, argv[0]);
|
|
77
|
+
if (!tag) return JS_NewArray(ctx);
|
|
78
|
+
auto results = doc->querySelectorAll_el(tag);
|
|
79
|
+
JS_FreeCString(ctx, tag);
|
|
80
|
+
return make_element_array(ctx, results);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
JSValue js_pdoc_createElement(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
84
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
85
|
+
if (!doc || argc < 1) return JS_NULL;
|
|
86
|
+
const char* tag = JS_ToCString(ctx, argv[0]);
|
|
87
|
+
if (!tag) return JS_NULL;
|
|
88
|
+
void* el = doc->createElement(tag);
|
|
89
|
+
JS_FreeCString(ctx, tag);
|
|
90
|
+
return make_element(ctx, el);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
JSValue js_pdoc_createTextNode(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
94
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
95
|
+
if (!doc || argc < 1) return JS_NULL;
|
|
96
|
+
const char* text = JS_ToCString(ctx, argv[0]);
|
|
97
|
+
if (!text) return JS_NULL;
|
|
98
|
+
void* node = doc->createTextNode(text);
|
|
99
|
+
JS_FreeCString(ctx, text);
|
|
100
|
+
return make_element(ctx, node);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
JSValue js_pdoc_createComment(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
104
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
105
|
+
if (!doc || argc < 1) return JS_NULL;
|
|
106
|
+
const char* text = JS_ToCString(ctx, argv[0]);
|
|
107
|
+
if (!text) return JS_NULL;
|
|
108
|
+
void* node = doc->createComment(text);
|
|
109
|
+
JS_FreeCString(ctx, text);
|
|
110
|
+
return make_element(ctx, node);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
JSValue js_pdoc_createDocumentFragment(JSContext* ctx, JSValue this_val, int, JSValue*) {
|
|
114
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
115
|
+
if (!doc) return JS_NULL;
|
|
116
|
+
return make_element(ctx, doc->createDocumentFragment());
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
JSValue js_pdoc_get_body(JSContext* ctx, JSValue this_val) {
|
|
120
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
121
|
+
return doc ? make_element(ctx, doc->body()) : JS_NULL;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
JSValue js_pdoc_get_head(JSContext* ctx, JSValue this_val) {
|
|
125
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
126
|
+
return doc ? make_element(ctx, doc->head()) : JS_NULL;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
JSValue js_pdoc_get_documentElement(JSContext* ctx, JSValue this_val) {
|
|
130
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
131
|
+
return doc ? make_element(ctx, doc->documentElement()) : JS_NULL;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Unlike document.doctype (DocumentBindings.cpp), this isn't cached: a
|
|
135
|
+
// secondary document is far less likely to have `.doctype` read repeatedly,
|
|
136
|
+
// so a fresh plain object per access keeps this file simpler.
|
|
137
|
+
JSValue js_pdoc_get_doctype(JSContext* ctx, JSValue this_val) {
|
|
138
|
+
auto* doc = unwrap_parsed_doc(ctx, this_val);
|
|
139
|
+
if (!doc) return JS_NULL;
|
|
140
|
+
void* dt = doc->doctype();
|
|
141
|
+
if (!dt) return JS_NULL;
|
|
142
|
+
return build_doctype_object(ctx, doc, dt);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── DOMParser.parseFromString() / document.implementation.createHTMLDocument() ─
|
|
146
|
+
|
|
147
|
+
JSValue js_native_parse_from_string(JSContext* ctx, JSValue, int argc, JSValue* argv) {
|
|
148
|
+
if (argc < 2) return JS_ThrowTypeError(ctx, "parseFromString requires 2 arguments");
|
|
149
|
+
const char* html = JS_ToCString(ctx, argv[0]);
|
|
150
|
+
if (!html) return JS_EXCEPTION;
|
|
151
|
+
const char* type = JS_ToCString(ctx, argv[1]);
|
|
152
|
+
if (!type) { JS_FreeCString(ctx, html); return JS_EXCEPTION; }
|
|
153
|
+
|
|
154
|
+
static const char* kSupportedTypes[] = {
|
|
155
|
+
"text/html", "text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml",
|
|
156
|
+
};
|
|
157
|
+
bool supported = false;
|
|
158
|
+
for (const char* t : kSupportedTypes) {
|
|
159
|
+
if (std::strcmp(type, t) == 0) { supported = true; break; }
|
|
160
|
+
}
|
|
161
|
+
if (!supported) {
|
|
162
|
+
std::string msg = "Invalid DOMParser type: " + std::string(type);
|
|
163
|
+
JS_FreeCString(ctx, html);
|
|
164
|
+
JS_FreeCString(ctx, type);
|
|
165
|
+
return JS_ThrowTypeError(ctx, "%s", msg.c_str());
|
|
166
|
+
}
|
|
167
|
+
// Every supported type is parsed with the HTML parser regardless of MIME
|
|
168
|
+
// type — this sandbox has no separate XML parser wired up. Good enough for
|
|
169
|
+
// the common case (parsing HTML/XHTML/SVG markup); genuine XML-specific
|
|
170
|
+
// syntax (e.g. CDATA sections, processing instructions) is not modeled.
|
|
171
|
+
JS_FreeCString(ctx, type);
|
|
172
|
+
|
|
173
|
+
auto* doc = new LexborDocument();
|
|
174
|
+
doc->parse(html);
|
|
175
|
+
JS_FreeCString(ctx, html);
|
|
176
|
+
|
|
177
|
+
auto* rctx = get_ctx(ctx);
|
|
178
|
+
JSValue result = make_parsed_document(ctx, doc);
|
|
179
|
+
if (rctx) rctx->extra_documents.emplace_back(doc);
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
JSValue js_native_create_html_document(JSContext* ctx, JSValue, int argc, JSValue* argv) {
|
|
184
|
+
std::string title;
|
|
185
|
+
if (argc >= 1 && !JS_IsUndefined(argv[0])) {
|
|
186
|
+
const char* t = JS_ToCString(ctx, argv[0]);
|
|
187
|
+
if (t) { title = t; JS_FreeCString(ctx, t); }
|
|
188
|
+
}
|
|
189
|
+
std::string escaped;
|
|
190
|
+
escaped.reserve(title.size());
|
|
191
|
+
for (char c : title) {
|
|
192
|
+
if (c == '<') escaped += "<";
|
|
193
|
+
else if (c == '>') escaped += ">";
|
|
194
|
+
else if (c == '&') escaped += "&";
|
|
195
|
+
else escaped += c;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
auto* doc = new LexborDocument();
|
|
199
|
+
doc->parse("<!doctype html><html><head><title>" + escaped + "</title></head><body></body></html>");
|
|
200
|
+
|
|
201
|
+
auto* rctx = get_ctx(ctx);
|
|
202
|
+
JSValue result = make_parsed_document(ctx, doc);
|
|
203
|
+
if (rctx) rctx->extra_documents.emplace_back(doc);
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const char* kDOMParserBootstrapScript = R"JS(
|
|
208
|
+
(function() {
|
|
209
|
+
function DOMParser() {}
|
|
210
|
+
DOMParser.prototype.parseFromString = function(str, type) {
|
|
211
|
+
return __nativeParseFromString(String(str), String(type));
|
|
212
|
+
};
|
|
213
|
+
globalThis.DOMParser = DOMParser;
|
|
214
|
+
|
|
215
|
+
document.implementation = {
|
|
216
|
+
createHTMLDocument: function(title) {
|
|
217
|
+
return __nativeCreateHTMLDocument(title === undefined ? undefined : String(title));
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
function definePDocTitle(proto) {
|
|
222
|
+
Object.defineProperty(proto, 'title', {
|
|
223
|
+
configurable: true,
|
|
224
|
+
get: function() {
|
|
225
|
+
var head = this.head;
|
|
226
|
+
if (!head) return '';
|
|
227
|
+
var titleEl = head.querySelector('title');
|
|
228
|
+
return titleEl ? titleEl.textContent : '';
|
|
229
|
+
},
|
|
230
|
+
set: function(value) {
|
|
231
|
+
var head = this.head;
|
|
232
|
+
if (!head) return;
|
|
233
|
+
var titleEl = head.querySelector('title');
|
|
234
|
+
if (!titleEl) {
|
|
235
|
+
titleEl = this.createElement('title');
|
|
236
|
+
head.appendChild(titleEl);
|
|
237
|
+
}
|
|
238
|
+
titleEl.textContent = String(value);
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
definePDocTitle(globalThis.__parsedDocumentProto);
|
|
243
|
+
delete globalThis.__parsedDocumentProto;
|
|
244
|
+
})();
|
|
245
|
+
)JS";
|
|
246
|
+
|
|
247
|
+
} // namespace
|
|
248
|
+
|
|
249
|
+
void DOMParserBindings::install(JSContext* ctx) {
|
|
250
|
+
JS_NewClassID(&js_parsed_document_class_id);
|
|
251
|
+
JS_NewClass(JS_GetRuntime(ctx), js_parsed_document_class_id, &js_parsed_document_class);
|
|
252
|
+
|
|
253
|
+
JSValue proto = JS_NewObject(ctx);
|
|
254
|
+
JS_SetPropertyStr(ctx, proto, "getElementById", JS_NewCFunction(ctx, js_pdoc_getElementById, "getElementById", 1));
|
|
255
|
+
JS_SetPropertyStr(ctx, proto, "querySelector", JS_NewCFunction(ctx, js_pdoc_querySelector, "querySelector", 1));
|
|
256
|
+
JS_SetPropertyStr(ctx, proto, "querySelectorAll", JS_NewCFunction(ctx, js_pdoc_querySelectorAll, "querySelectorAll", 1));
|
|
257
|
+
JS_SetPropertyStr(ctx, proto, "getElementsByClassName", JS_NewCFunction(ctx, js_pdoc_getElementsByClassName, "getElementsByClassName", 1));
|
|
258
|
+
JS_SetPropertyStr(ctx, proto, "getElementsByTagName", JS_NewCFunction(ctx, js_pdoc_getElementsByTagName, "getElementsByTagName", 1));
|
|
259
|
+
JS_SetPropertyStr(ctx, proto, "createElement", JS_NewCFunction(ctx, js_pdoc_createElement, "createElement", 1));
|
|
260
|
+
JS_SetPropertyStr(ctx, proto, "createTextNode", JS_NewCFunction(ctx, js_pdoc_createTextNode, "createTextNode", 1));
|
|
261
|
+
JS_SetPropertyStr(ctx, proto, "createComment", JS_NewCFunction(ctx, js_pdoc_createComment, "createComment", 1));
|
|
262
|
+
JS_SetPropertyStr(ctx, proto, "createDocumentFragment", JS_NewCFunction(ctx, js_pdoc_createDocumentFragment, "createDocumentFragment", 0));
|
|
263
|
+
|
|
264
|
+
define_prop(ctx, proto, "body", js_pdoc_get_body, nullptr);
|
|
265
|
+
define_prop(ctx, proto, "head", js_pdoc_get_head, nullptr);
|
|
266
|
+
define_prop(ctx, proto, "documentElement", js_pdoc_get_documentElement, nullptr);
|
|
267
|
+
define_prop(ctx, proto, "doctype", js_pdoc_get_doctype, nullptr);
|
|
268
|
+
|
|
269
|
+
JS_SetPropertyStr(ctx, proto, "nodeType", JS_NewInt32(ctx, 9 /* DOCUMENT_NODE */));
|
|
270
|
+
JS_SetPropertyStr(ctx, proto, "nodeName", JS_NewString(ctx, "#document"));
|
|
271
|
+
JS_SetPropertyStr(ctx, proto, "ownerDocument", JS_NULL);
|
|
272
|
+
define_node_type_constants(ctx, proto);
|
|
273
|
+
|
|
274
|
+
JSValue global = JS_GetGlobalObject(ctx);
|
|
275
|
+
|
|
276
|
+
// Chain onto Document.prototype (set up by DocumentBindings.cpp) purely so
|
|
277
|
+
// `parsedDoc instanceof Document` holds, matching real jsdom/browsers.
|
|
278
|
+
JSValue document_ctor = JS_GetPropertyStr(ctx, global, "Document");
|
|
279
|
+
JSValue document_proto_val = JS_GetPropertyStr(ctx, document_ctor, "prototype");
|
|
280
|
+
JS_SetPrototype(ctx, proto, document_proto_val);
|
|
281
|
+
JS_FreeValue(ctx, document_proto_val);
|
|
282
|
+
JS_FreeValue(ctx, document_ctor);
|
|
283
|
+
|
|
284
|
+
JS_SetClassProto(ctx, js_parsed_document_class_id, JS_DupValue(ctx, proto));
|
|
285
|
+
|
|
286
|
+
JS_SetPropertyStr(ctx, global, "__parsedDocumentProto", proto);
|
|
287
|
+
JS_SetPropertyStr(ctx, global, "__nativeParseFromString", JS_NewCFunction(ctx, js_native_parse_from_string, "__nativeParseFromString", 2));
|
|
288
|
+
JS_SetPropertyStr(ctx, global, "__nativeCreateHTMLDocument", JS_NewCFunction(ctx, js_native_create_html_document, "__nativeCreateHTMLDocument", 1));
|
|
289
|
+
JS_FreeValue(ctx, global);
|
|
290
|
+
|
|
291
|
+
JSValue result = JS_Eval(ctx, kDOMParserBootstrapScript, strlen(kDOMParserBootstrapScript),
|
|
292
|
+
"<domparser-bootstrap>", JS_EVAL_TYPE_GLOBAL);
|
|
293
|
+
if (JS_IsException(result)) {
|
|
294
|
+
JS_FreeValue(ctx, JS_GetException(ctx));
|
|
295
|
+
}
|
|
296
|
+
JS_FreeValue(ctx, result);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
} // namespace margelo::nitro::nitrojsdom
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// Registers globalThis.DOMParser (`.parseFromString()`) and
|
|
4
|
+
// document.implementation.createHTMLDocument(), each of which produces a
|
|
5
|
+
// genuinely separate, inert Document backed by its own LexborDocument
|
|
6
|
+
// instance (see doc_for_node()/register_document() in DOMBindingsInternal —
|
|
7
|
+
// that's what makes mutating a second document safe rather than corrupting
|
|
8
|
+
// the primary document's memory arena).
|
|
9
|
+
//
|
|
10
|
+
// Scope, matching real-world DOMParser usage (parsing a fetched/embedded
|
|
11
|
+
// HTML string and reading it back, occasionally assembling a few new nodes):
|
|
12
|
+
// - Full read/query support: querySelector(All), getElementById,
|
|
13
|
+
// getElementsBy{ClassName,TagName} (as static snapshots, not live
|
|
14
|
+
// HTMLCollections — see below), all generic Node traversal/attribute
|
|
15
|
+
// methods (those are shared with the primary document's Element/Node
|
|
16
|
+
// prototypes and don't need document-specific wiring).
|
|
17
|
+
// - createElement/createTextNode/createComment/createDocumentFragment,
|
|
18
|
+
// appendChild/removeChild/insertBefore/replaceChild, and the
|
|
19
|
+
// textContent/innerHTML setters/insertAdjacentHTML/matches()/closest()
|
|
20
|
+
// inherited from Element.prototype — all resolve the correct owning
|
|
21
|
+
// document dynamically, so building up a secondary document is fully
|
|
22
|
+
// safe, not just read-only.
|
|
23
|
+
// - title get/set (mirrors document.title's head/<title> shim).
|
|
24
|
+
// Not supported on secondary documents (their bindings are hardwired to the
|
|
25
|
+
// primary document — see ShadowRootBindings/CustomElementsBindings/
|
|
26
|
+
// TemplateBindings/LiveCollectionBindings): Shadow DOM (attachShadow),
|
|
27
|
+
// Custom Elements, <template>.content, MutationObserver, live
|
|
28
|
+
// HTMLCollections (.forms/.images/.scripts/.links, and
|
|
29
|
+
// getElementsBy{ClassName,TagName} return static arrays here instead).
|
|
30
|
+
// Scripts inside parsed HTML never execute (per spec, these documents are
|
|
31
|
+
// inert) — no script pipeline is wired to them.
|
|
32
|
+
//
|
|
33
|
+
// Must run after ElementBindings (shares its Element/Node classes/protos)
|
|
34
|
+
// and DocumentBindings (adds .implementation onto globalThis.document).
|
|
35
|
+
|
|
36
|
+
#include "quickjs.h"
|
|
37
|
+
|
|
38
|
+
namespace margelo::nitro::nitrojsdom {
|
|
39
|
+
|
|
40
|
+
class DOMParserBindings {
|
|
41
|
+
public:
|
|
42
|
+
static void install(JSContext* ctx);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
} // namespace margelo::nitro::nitrojsdom
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
#include "DocumentBindings.hpp"
|
|
2
2
|
#include "LiveCollectionBindings.hpp"
|
|
3
|
+
#include "DOMExceptionBindings.hpp"
|
|
3
4
|
#include "../DOMBindingsInternal.hpp"
|
|
4
5
|
#include "../QuickJSRuntime.hpp"
|
|
5
6
|
#include "../../lexbor/LexborDocument.hpp"
|
|
7
|
+
#include <lexbor/html/html.h>
|
|
8
|
+
#include <lexbor/dom/dom.h>
|
|
6
9
|
#include <cstring>
|
|
7
10
|
#include <string>
|
|
8
11
|
|
|
@@ -103,6 +106,92 @@ JSValue js_doc_createTextNode(JSContext* ctx, JSValue, int argc, JSValue* argv)
|
|
|
103
106
|
return make_element(ctx, node);
|
|
104
107
|
}
|
|
105
108
|
|
|
109
|
+
// Implements the WHATWG "validate and extract a namespace and qualified name"
|
|
110
|
+
// algorithm (https://dom.spec.whatwg.org/#validate-and-extract): splits
|
|
111
|
+
// qualifiedName into prefix/localName and checks the namespace/prefix
|
|
112
|
+
// consistency rules, throwing the spec-mandated DOMException on failure.
|
|
113
|
+
// Full XML Name-production character validation (which characters are legal
|
|
114
|
+
// in a Name) is not implemented — only structural checks (empty name, colon
|
|
115
|
+
// placement, xml/xmlns reservations) — deliberately narrower than real
|
|
116
|
+
// jsdom, since that character-class check is a much larger, separate lift.
|
|
117
|
+
JSValue validate_and_extract_qname(JSContext* ctx, const std::string& ns,
|
|
118
|
+
const std::string& qualifiedName, bool ns_is_null,
|
|
119
|
+
std::string& out_prefix, std::string& out_local) {
|
|
120
|
+
if (qualifiedName.empty()) {
|
|
121
|
+
return throw_dom_exception(ctx, "InvalidCharacterError",
|
|
122
|
+
"The qualified name provided is empty.");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
auto colon = qualifiedName.find(':');
|
|
126
|
+
auto last_colon = qualifiedName.rfind(':');
|
|
127
|
+
if (colon != last_colon) {
|
|
128
|
+
return throw_dom_exception(ctx, "InvalidCharacterError",
|
|
129
|
+
"The qualified name provided has more than one ':'.");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
std::string prefix;
|
|
133
|
+
std::string local = qualifiedName;
|
|
134
|
+
if (colon != std::string::npos) {
|
|
135
|
+
prefix = qualifiedName.substr(0, colon);
|
|
136
|
+
local = qualifiedName.substr(colon + 1);
|
|
137
|
+
if (prefix.empty() || local.empty()) {
|
|
138
|
+
return throw_dom_exception(ctx, "InvalidCharacterError",
|
|
139
|
+
"The qualified name provided is malformed.");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!prefix.empty() && ns_is_null) {
|
|
144
|
+
return throw_dom_exception(ctx, "NamespaceError",
|
|
145
|
+
"A namespace is required for a prefixed qualified name.");
|
|
146
|
+
}
|
|
147
|
+
if (prefix == "xml" && ns != "http://www.w3.org/XML/1998/namespace") {
|
|
148
|
+
return throw_dom_exception(ctx, "NamespaceError",
|
|
149
|
+
"The 'xml' prefix requires the XML namespace.");
|
|
150
|
+
}
|
|
151
|
+
bool is_xmlns = (qualifiedName == "xmlns" || prefix == "xmlns");
|
|
152
|
+
if (is_xmlns && ns != "http://www.w3.org/2000/xmlns/") {
|
|
153
|
+
return throw_dom_exception(ctx, "NamespaceError",
|
|
154
|
+
"The 'xmlns' prefix/qualified name requires the XMLNS namespace.");
|
|
155
|
+
}
|
|
156
|
+
if (!is_xmlns && ns == "http://www.w3.org/2000/xmlns/") {
|
|
157
|
+
return throw_dom_exception(ctx, "NamespaceError",
|
|
158
|
+
"The XMLNS namespace requires the 'xmlns' prefix or qualified name.");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
out_prefix = prefix;
|
|
162
|
+
out_local = local;
|
|
163
|
+
return JS_UNDEFINED;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
JSValue js_doc_createElementNS(JSContext* ctx, JSValue, int argc, JSValue* argv) {
|
|
167
|
+
if (argc < 2) {
|
|
168
|
+
return JS_ThrowTypeError(ctx,
|
|
169
|
+
"Failed to execute 'createElementNS' on 'Document': 2 arguments required.");
|
|
170
|
+
}
|
|
171
|
+
// Per WebIDL, a nullable DOMString? argument only maps to the null
|
|
172
|
+
// namespace when explicitly `null` — `undefined` stringifies to "undefined"
|
|
173
|
+
// like any other DOMString argument, it is not treated as null.
|
|
174
|
+
bool ns_is_null = JS_IsNull(argv[0]);
|
|
175
|
+
std::string ns;
|
|
176
|
+
if (!ns_is_null) {
|
|
177
|
+
const char* ns_c = JS_ToCString(ctx, argv[0]);
|
|
178
|
+
if (!ns_c) return JS_EXCEPTION;
|
|
179
|
+
ns = ns_c;
|
|
180
|
+
JS_FreeCString(ctx, ns_c);
|
|
181
|
+
}
|
|
182
|
+
const char* qname = JS_ToCString(ctx, argv[1]);
|
|
183
|
+
if (!qname) return JS_EXCEPTION;
|
|
184
|
+
std::string qualifiedName = qname;
|
|
185
|
+
JS_FreeCString(ctx, qname);
|
|
186
|
+
|
|
187
|
+
std::string prefix, local;
|
|
188
|
+
JSValue validation_error = validate_and_extract_qname(ctx, ns, qualifiedName, ns_is_null, prefix, local);
|
|
189
|
+
if (JS_IsException(validation_error)) return validation_error;
|
|
190
|
+
|
|
191
|
+
void* el = get_doc(ctx)->createElementNS(ns, qualifiedName);
|
|
192
|
+
return make_element(ctx, el);
|
|
193
|
+
}
|
|
194
|
+
|
|
106
195
|
JSValue js_doc_createComment(JSContext* ctx, JSValue, int argc, JSValue* argv) {
|
|
107
196
|
if (argc < 1) return JS_NULL;
|
|
108
197
|
const char* text = JS_ToCString(ctx, argv[0]);
|
|
@@ -116,6 +205,28 @@ JSValue js_doc_createDocumentFragment(JSContext* ctx, JSValue, int, JSValue*) {
|
|
|
116
205
|
return make_element(ctx, get_doc(ctx)->createDocumentFragment());
|
|
117
206
|
}
|
|
118
207
|
|
|
208
|
+
JSValue js_doc_importNode(JSContext* ctx, JSValue, int argc, JSValue* argv) {
|
|
209
|
+
if (argc < 1) {
|
|
210
|
+
return JS_ThrowTypeError(ctx,
|
|
211
|
+
"Failed to execute 'importNode' on 'Document': 1 argument required, but only 0 present.");
|
|
212
|
+
}
|
|
213
|
+
auto* node = unwrap_node(ctx, argv[0]);
|
|
214
|
+
if (!node) {
|
|
215
|
+
return JS_ThrowTypeError(ctx,
|
|
216
|
+
"Failed to execute 'importNode' on 'Document': parameter 1 is not of type 'Node'.");
|
|
217
|
+
}
|
|
218
|
+
if (node->type == LXB_DOM_NODE_TYPE_DOCUMENT) {
|
|
219
|
+
return throw_dom_exception(ctx, "NotSupportedError",
|
|
220
|
+
"Failed to execute 'importNode' on 'Document': The node provided is a document, which may not be imported.");
|
|
221
|
+
}
|
|
222
|
+
bool deep = argc >= 2 && JS_ToBool(ctx, argv[1]) > 0;
|
|
223
|
+
|
|
224
|
+
lxb_dom_document_t* target_doc = lxb_dom_interface_document(
|
|
225
|
+
static_cast<lxb_html_document_t*>(get_doc(ctx)->documentHtmlPtr()));
|
|
226
|
+
lxb_dom_node_t* imported = lxb_dom_document_import_node(target_doc, node, deep);
|
|
227
|
+
return make_element(ctx, imported);
|
|
228
|
+
}
|
|
229
|
+
|
|
119
230
|
JSValue js_doc_get_body(JSContext* ctx, JSValue) {
|
|
120
231
|
return make_element(ctx, get_doc(ctx)->body());
|
|
121
232
|
}
|
|
@@ -147,20 +258,45 @@ JSValue js_doc_get_doctype(JSContext* ctx, JSValue) {
|
|
|
147
258
|
return JS_DupValue(ctx, *static_cast<JSValue*>(rctx->doctype_wrapper));
|
|
148
259
|
}
|
|
149
260
|
|
|
150
|
-
JSValue obj =
|
|
151
|
-
std::string name = get_doc(ctx)->doctypeName(dt);
|
|
152
|
-
JS_SetPropertyStr(ctx, obj, "name", JS_NewStringLen(ctx, name.data(), name.size()));
|
|
153
|
-
std::string publicId = get_doc(ctx)->doctypePublicId(dt);
|
|
154
|
-
JS_SetPropertyStr(ctx, obj, "publicId", JS_NewStringLen(ctx, publicId.data(), publicId.size()));
|
|
155
|
-
std::string systemId = get_doc(ctx)->doctypeSystemId(dt);
|
|
156
|
-
JS_SetPropertyStr(ctx, obj, "systemId", JS_NewStringLen(ctx, systemId.data(), systemId.size()));
|
|
157
|
-
JS_SetPropertyStr(ctx, obj, "nodeType", JS_NewInt32(ctx, 10));
|
|
158
|
-
JS_SetPropertyStr(ctx, obj, "nodeName", JS_NewStringLen(ctx, name.data(), name.size()));
|
|
159
|
-
|
|
261
|
+
JSValue obj = build_doctype_object(ctx, get_doc(ctx), dt);
|
|
160
262
|
if (rctx) rctx->doctype_wrapper = new JSValue(JS_DupValue(ctx, obj));
|
|
161
263
|
return obj;
|
|
162
264
|
}
|
|
163
265
|
|
|
266
|
+
JSValue js_doc_get_activeElement(JSContext* ctx, JSValue) {
|
|
267
|
+
auto* rctx = get_ctx(ctx);
|
|
268
|
+
if (rctx && rctx->active_element) return make_element(ctx, rctx->active_element);
|
|
269
|
+
return make_element(ctx, get_doc(ctx)->body());
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
JSValue js_doc_get_readyState(JSContext* ctx, JSValue) {
|
|
273
|
+
auto* rctx = get_ctx(ctx);
|
|
274
|
+
const std::string& state = rctx ? rctx->ready_state : "loading";
|
|
275
|
+
return JS_NewStringLen(ctx, state.data(), state.size());
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// A doctype-less document renders in quirks mode in every real browser (and
|
|
279
|
+
// jsdom mirrors that), so this is a real, if approximate, signal rather than
|
|
280
|
+
// a hardcoded stub — no actual quirks-mode CSS behavior differs here since
|
|
281
|
+
// there's no layout engine to diverge.
|
|
282
|
+
JSValue js_doc_get_compatMode(JSContext* ctx, JSValue) {
|
|
283
|
+
bool has_doctype = get_doc(ctx)->doctype() != nullptr;
|
|
284
|
+
const char* mode = has_doctype ? "CSS1Compat" : "BackCompat";
|
|
285
|
+
return JS_NewString(ctx, mode);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// document uses its own object/prototype (not node_proto), so it doesn't
|
|
289
|
+
// inherit the Node-level baseURI getter — mirrors js_el_get_baseURI in
|
|
290
|
+
// ElementBindings.cpp (always location.href; no <base> element support).
|
|
291
|
+
JSValue js_doc_get_baseURI(JSContext* ctx, JSValue) {
|
|
292
|
+
JSValue global = JS_GetGlobalObject(ctx);
|
|
293
|
+
JSValue location = JS_GetPropertyStr(ctx, global, "location");
|
|
294
|
+
JS_FreeValue(ctx, global);
|
|
295
|
+
JSValue href = JS_GetPropertyStr(ctx, location, "href");
|
|
296
|
+
JS_FreeValue(ctx, location);
|
|
297
|
+
return href;
|
|
298
|
+
}
|
|
299
|
+
|
|
164
300
|
const char* kDocumentTitleBootstrapScript = R"JS(
|
|
165
301
|
(function() {
|
|
166
302
|
Object.defineProperty(document, 'title', {
|
|
@@ -199,9 +335,11 @@ void DocumentBindings::install(JSContext* ctx) {
|
|
|
199
335
|
JS_SetPropertyStr(ctx, doc, "getElementsByTagName", JS_NewCFunction(ctx, js_doc_getElementsByTagName, "getElementsByTagName", 1));
|
|
200
336
|
JS_SetPropertyStr(ctx, doc, "getElementsByName", JS_NewCFunction(ctx, js_doc_getElementsByName, "getElementsByName", 1));
|
|
201
337
|
JS_SetPropertyStr(ctx, doc, "createElement", JS_NewCFunction(ctx, js_doc_createElement, "createElement", 1));
|
|
338
|
+
JS_SetPropertyStr(ctx, doc, "createElementNS", JS_NewCFunction(ctx, js_doc_createElementNS, "createElementNS", 2));
|
|
202
339
|
JS_SetPropertyStr(ctx, doc, "createTextNode", JS_NewCFunction(ctx, js_doc_createTextNode, "createTextNode", 1));
|
|
203
340
|
JS_SetPropertyStr(ctx, doc, "createComment", JS_NewCFunction(ctx, js_doc_createComment, "createComment", 1));
|
|
204
341
|
JS_SetPropertyStr(ctx, doc, "createDocumentFragment", JS_NewCFunction(ctx, js_doc_createDocumentFragment, "createDocumentFragment", 0));
|
|
342
|
+
JS_SetPropertyStr(ctx, doc, "importNode", JS_NewCFunction(ctx, js_doc_importNode, "importNode", 2));
|
|
205
343
|
|
|
206
344
|
define_prop(ctx, doc, "body", js_doc_get_body, nullptr);
|
|
207
345
|
define_prop(ctx, doc, "head", js_doc_get_head, nullptr);
|
|
@@ -211,14 +349,31 @@ void DocumentBindings::install(JSContext* ctx) {
|
|
|
211
349
|
define_prop(ctx, doc, "images", js_doc_get_images, nullptr);
|
|
212
350
|
define_prop(ctx, doc, "scripts", js_doc_get_scripts, nullptr);
|
|
213
351
|
define_prop(ctx, doc, "links", js_doc_get_links, nullptr);
|
|
352
|
+
define_prop(ctx, doc, "activeElement", js_doc_get_activeElement, nullptr);
|
|
353
|
+
define_prop(ctx, doc, "readyState", js_doc_get_readyState, nullptr);
|
|
354
|
+
define_prop(ctx, doc, "compatMode", js_doc_get_compatMode, nullptr);
|
|
355
|
+
define_prop(ctx, doc, "baseURI", js_doc_get_baseURI, nullptr);
|
|
356
|
+
|
|
357
|
+
JS_SetPropertyStr(ctx, doc, "characterSet", JS_NewString(ctx, "UTF-8"));
|
|
358
|
+
JS_SetPropertyStr(ctx, doc, "contentType", JS_NewString(ctx, "text/html"));
|
|
214
359
|
|
|
215
360
|
RuntimeContext* rctx = get_ctx(ctx);
|
|
216
361
|
bool hidden = !(rctx && rctx->pretend_to_be_visual);
|
|
217
362
|
JS_SetPropertyStr(ctx, doc, "hidden", JS_NewBool(ctx, hidden));
|
|
363
|
+
JS_SetPropertyStr(ctx, doc, "nodeType", JS_NewInt32(ctx, 9 /* DOCUMENT_NODE */));
|
|
364
|
+
JS_SetPropertyStr(ctx, doc, "nodeName", JS_NewString(ctx, "#document"));
|
|
365
|
+
JS_SetPropertyStr(ctx, doc, "ownerDocument", JS_NULL);
|
|
366
|
+
// document uses its own object/prototype (not node_proto), so it doesn't
|
|
367
|
+
// inherit js_el_get_namespaceURI — set explicitly rather than leaving this
|
|
368
|
+
// undefined, matching real jsdom (a Document node has no namespace).
|
|
369
|
+
JS_SetPropertyStr(ctx, doc, "namespaceURI", JS_NULL);
|
|
218
370
|
|
|
219
371
|
JSValue document_proto = JS_NewObject(ctx);
|
|
220
372
|
JS_SetPrototype(ctx, doc, document_proto);
|
|
221
|
-
|
|
373
|
+
define_node_type_constants(ctx, document_proto);
|
|
374
|
+
JSValue document_ctor = define_global_constructor(ctx, "Document", document_proto);
|
|
375
|
+
define_node_type_constants(ctx, document_ctor);
|
|
376
|
+
JS_FreeValue(ctx, document_ctor);
|
|
222
377
|
JS_FreeValue(ctx, document_proto);
|
|
223
378
|
|
|
224
379
|
JS_SetPropertyStr(ctx, global, "document", doc);
|