@dvaji/cheerox 0.0.0-stage → 0.1.1
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/CONTRIBUTING.md +74 -0
- package/Cargo.lock +1094 -0
- package/Cargo.toml +46 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/RELEASING.md +114 -0
- package/THIRD_PARTY_NOTICES.md +7579 -0
- package/build.rs +3 -0
- package/cheerox.darwin-arm64.node +0 -0
- package/cheerox.darwin-x64.node +0 -0
- package/cheerox.linux-arm64-gnu.node +0 -0
- package/cheerox.linux-arm64-musl.node +0 -0
- package/cheerox.linux-x64-gnu.node +0 -0
- package/cheerox.linux-x64-musl.node +0 -0
- package/cheerox.win32-arm64-msvc.node +0 -0
- package/cheerox.win32-x64-msvc.node +0 -0
- package/dist/cheerio-compat-BS--qPzw.mjs +1648 -0
- package/dist/cheerio-compat-BS--qPzw.mjs.map +1 -0
- package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
- package/dist/cheerio-compat-DSwBAJ1s.cjs +1719 -0
- package/dist/cheerio-compat-DSwBAJ1s.cjs.map +1 -0
- package/dist/cheerio-compat.cjs +18 -0
- package/dist/cheerio-compat.d.cts +2 -0
- package/dist/cheerio-compat.d.mts +2 -0
- package/dist/cheerio-compat.d.ts +2 -0
- package/dist/cheerio-compat.mjs +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.mjs +53 -0
- package/dist/index.mjs.map +1 -0
- package/dist/legacy-parse.cjs +5 -0
- package/dist/legacy-parse.d.mts +8 -0
- package/dist/legacy-parse.d.ts +7 -0
- package/dist/legacy-parse.mjs +1 -0
- package/dist/modern-B7hyW4cI.d.cts +102 -0
- package/dist/modern-B7hyW4cI.d.ts +102 -0
- package/dist/modern-CHQGP0Yt.cjs +2858 -0
- package/dist/modern-CHQGP0Yt.cjs.map +1 -0
- package/dist/modern-CJvSD_y5.d.mts +102 -0
- package/dist/modern-CvXcTn82.mjs +2769 -0
- package/dist/modern-CvXcTn82.mjs.map +1 -0
- package/dist/modern.cjs +19 -0
- package/dist/modern.d.cts +3 -0
- package/dist/modern.d.mts +3 -0
- package/dist/modern.d.ts +3 -0
- package/dist/modern.mjs +3 -0
- package/dist/nodes-jbWWhssj.cjs +4210 -0
- package/dist/nodes-jbWWhssj.cjs.map +1 -0
- package/dist/nodes-sYxb5qyZ.mjs +3803 -0
- package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
- package/dist/slim.cjs +16 -0
- package/dist/slim.cjs.map +1 -0
- package/dist/slim.d.cts +6 -0
- package/dist/slim.d.mts +6 -0
- package/dist/slim.d.ts +6 -0
- package/dist/slim.mjs +12 -0
- package/dist/slim.mjs.map +1 -0
- package/dist/types-CA01Zg3r.d.cts +526 -0
- package/dist/types-CA01Zg3r.d.mts +526 -0
- package/dist/types-CA01Zg3r.d.ts +526 -0
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.mts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.mjs +25 -0
- package/dist/utils.mjs.map +1 -0
- package/native/src/arena.rs +2109 -0
- package/native/src/attributes.rs +946 -0
- package/native/src/codec.rs +298 -0
- package/native/src/diagnostics.rs +1249 -0
- package/native/src/document_order.rs +711 -0
- package/native/src/extraction.rs +321 -0
- package/native/src/gc.rs +175 -0
- package/native/src/hook_sink.rs +801 -0
- package/native/src/hook_stream.rs +513 -0
- package/native/src/id_hash.rs +42 -0
- package/native/src/immutable.rs +333 -0
- package/native/src/lib.rs +4119 -0
- package/native/src/location.rs +392 -0
- package/native/src/markup_tokenizer.rs +668 -0
- package/native/src/runtime.rs +368 -0
- package/native/src/selector.rs +1596 -0
- package/native/src/sink.rs +274 -0
- package/native/src/snapshot.rs +672 -0
- package/native/src/stream.rs +334 -0
- package/native/src/tokenizer.rs +581 -0
- package/native/src/tracker.rs +232 -0
- package/native/vendor/README.md +26 -0
- package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
- package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/html5ever-0.35.0/README.md +60 -0
- package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
- package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
- package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
- package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
- package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
- package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
- package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
- package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
- package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
- package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
- package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
- package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
- package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
- package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
- package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
- package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
- package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
- package/native/vendor/html5gum-0.8.4/README.md +110 -0
- package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
- package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
- package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
- package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
- package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
- package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
- package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
- package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
- package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
- package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
- package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
- package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
- package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
- package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
- package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
- package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
- package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
- package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
- package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
- package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
- package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
- package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
- package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
- package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
- package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
- package/package.json +167 -4
- package/scripts/check-native.mjs +15 -0
- package/scripts/check-release.mjs +234 -0
- package/scripts/fix-declarations.mjs +39 -0
- package/scripts/generate-notices.mjs +188 -0
- package/scripts/notices/README.md +13 -0
- package/scripts/notices/cheerio-MIT.txt +21 -0
- package/scripts/notices/html5ever-AUTHORS.txt +20 -0
- package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
- package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
- package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
- package/scripts/notices/htmlparser2-MIT.txt +18 -0
- package/scripts/notices/napi-rs-MIT.txt +43 -0
- package/scripts/notices/neon-MIT.txt +19 -0
- package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
- package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
- package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
- package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
- package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
- package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
- package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
- package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
- package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
- package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
- package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
- package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
- package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
- package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
- package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
- package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
- package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
- package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
- package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
- package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
- package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
- package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
- package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
- package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
- package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
- package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
- package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
- package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
- package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
- package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
- package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
- package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
- package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
- package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
- package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
- package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
- package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
- package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
- package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
- package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
- package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
- package/scripts/notices/runtime.json +1632 -0
- package/scripts/notices/sources.json +561 -0
- package/scripts/notices/xray-MIT.txt +21 -0
- package/src/attribute-object.ts +71 -0
- package/src/attributes.ts +308 -0
- package/src/cheerio-compat/css.ts +58 -0
- package/src/cheerio-compat/render.ts +106 -0
- package/src/cheerio-compat/selectors.ts +51 -0
- package/src/cheerio-compat/types.ts +52 -0
- package/src/cheerio-compat.ts +2055 -0
- package/src/codec.ts +96 -0
- package/src/dom.ts +87 -0
- package/src/extract-plan.ts +97 -0
- package/src/extract.ts +322 -0
- package/src/hooks.ts +369 -0
- package/src/index.ts +48 -0
- package/src/io.ts +491 -0
- package/src/match.ts +207 -0
- package/src/modern.ts +259 -0
- package/src/native.ts +149 -0
- package/src/nodes.ts +831 -0
- package/src/parse.ts +53 -0
- package/src/parser-types.ts +157 -0
- package/src/router.ts +1129 -0
- package/src/selection.ts +682 -0
- package/src/slim.ts +12 -0
- package/src/snapshot.ts +291 -0
- package/src/transport.ts +113 -0
- package/src/types.ts +186 -0
- package/src/utils.ts +29 -0
- package/src/workers.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.mjs +17 -0
package/src/codec.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node strings use UTF-16, including unpaired surrogates. Rust strings use
|
|
3
|
+
* Unicode scalar values. Reserve a small private-use alphabet on this private
|
|
4
|
+
* boundary so neither representation loses a code unit.
|
|
5
|
+
*/
|
|
6
|
+
const reserved = /[\ud800-\ue800]/;
|
|
7
|
+
const encoded = /[\ue000-\ue800]/;
|
|
8
|
+
const escapeUnit = 0xe800;
|
|
9
|
+
const isWellFormed = (String.prototype as String & { isWellFormed?(): boolean }).isWellFormed;
|
|
10
|
+
|
|
11
|
+
export function encodeString(value: string): string {
|
|
12
|
+
if (!encoded.test(value) && (isWellFormed ? isWellFormed.call(value) : !reserved.test(value))) return value;
|
|
13
|
+
const parts: string[] = [];
|
|
14
|
+
let start = 0;
|
|
15
|
+
for (let index = 0; index < value.length; index++) {
|
|
16
|
+
const unit = value.charCodeAt(index);
|
|
17
|
+
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
18
|
+
const next = value.charCodeAt(index + 1);
|
|
19
|
+
if (next >= 0xdc00 && next <= 0xdfff) { index++; continue; }
|
|
20
|
+
}
|
|
21
|
+
let replacement: string | undefined;
|
|
22
|
+
if (unit >= 0xd800 && unit <= 0xdfff) replacement = String.fromCharCode(0xe000 + unit - 0xd800);
|
|
23
|
+
else if (unit >= 0xe000 && unit <= escapeUnit) replacement = '\ue800' + value[index];
|
|
24
|
+
if (replacement !== undefined) {
|
|
25
|
+
parts.push(value.slice(start, index), replacement);
|
|
26
|
+
start = index + 1;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return start === 0 ? value : parts.join('') + value.slice(start);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function decodeString(value: string): string {
|
|
33
|
+
if (!encoded.test(value)) return value;
|
|
34
|
+
const parts: string[] = [];
|
|
35
|
+
let start = 0;
|
|
36
|
+
for (let index = 0; index < value.length; index++) {
|
|
37
|
+
const unit = value.charCodeAt(index);
|
|
38
|
+
if (unit < 0xe000 || unit > escapeUnit) continue;
|
|
39
|
+
const next = value.charCodeAt(index + 1);
|
|
40
|
+
if (unit === escapeUnit) {
|
|
41
|
+
if (next < 0xe000 || next > escapeUnit || Number.isNaN(next)) continue;
|
|
42
|
+
parts.push(value.slice(start, index), value[++index]);
|
|
43
|
+
} else parts.push(value.slice(start, index), String.fromCharCode(0xd800 + unit - 0xe000));
|
|
44
|
+
start = index + 1;
|
|
45
|
+
}
|
|
46
|
+
return start === 0 ? value : parts.join('') + value.slice(start);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Transform JSON string tokens rather than the whole JSON text: JSON.stringify
|
|
50
|
+
// escapes lone surrogates, while a literal backslash-u sequence is plain data.
|
|
51
|
+
const encodeJsonCandidate = /[\ud800-\ue800]|\\u(?:d[89a-f][\da-f]{2}|e[0-8][\da-f]{2})/i;
|
|
52
|
+
const escapedJsonCandidate = /\\u(?:d[89a-f][\da-f]{2}|e[0-8][\da-f]{2})/i;
|
|
53
|
+
const decodeJsonCandidate = /[\ue000-\ue800]|\\ue[0-8][\da-f]{2}/i;
|
|
54
|
+
|
|
55
|
+
export function encodeJson(value: string): string {
|
|
56
|
+
if (!encodeJsonCandidate.test(value)) return value;
|
|
57
|
+
if (isWellFormed?.call(value) && !encoded.test(value) && !escapedJsonCandidate.test(value)) return value;
|
|
58
|
+
return mapJsonStrings(value, encodeString, encodeJsonCandidate);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function decodeJson(value: string): string {
|
|
62
|
+
return decodeJsonCandidate.test(value) ? mapJsonStrings(value, decodeString, decodeJsonCandidate) : value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Replaces every string token of the JSON text `json` with `transform` of its
|
|
67
|
+
* value, in one left-to-right pass. A regular expression scanning for tokens
|
|
68
|
+
* retries from every quote of an unterminated token, which is quadratic.
|
|
69
|
+
*/
|
|
70
|
+
function mapJsonStrings(json: string, transform: (value: string) => string, candidate: RegExp): string {
|
|
71
|
+
const parts: string[] = [];
|
|
72
|
+
let copied = 0;
|
|
73
|
+
let quote = json.indexOf('"');
|
|
74
|
+
while (quote !== -1) {
|
|
75
|
+
let end = quote + 1;
|
|
76
|
+
while (end < json.length && json.charCodeAt(end) !== 0x22) end += json.charCodeAt(end) === 0x5c ? 2 : 1;
|
|
77
|
+
if (end >= json.length) break;
|
|
78
|
+
const token = json.slice(quote, end + 1);
|
|
79
|
+
if (candidate.test(token)) {
|
|
80
|
+
parts.push(json.slice(copied, quote), JSON.stringify(transform(JSON.parse(token))));
|
|
81
|
+
copied = end + 1;
|
|
82
|
+
}
|
|
83
|
+
quote = json.indexOf('"', end + 1);
|
|
84
|
+
}
|
|
85
|
+
return copied === 0 ? json : parts.join('') + json.slice(copied);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Private parser protocol flag, appended without deserializing large options. */
|
|
89
|
+
export function encodeOptions(value?: string): string {
|
|
90
|
+
if (value === undefined) return '{"__utf16Codec":true}';
|
|
91
|
+
const json = encodeJson(value);
|
|
92
|
+
if (!json.trimStart().startsWith('{')) return '{"__utf16Codec":true}';
|
|
93
|
+
const end = json.lastIndexOf('}');
|
|
94
|
+
const prefix = json.slice(0, end);
|
|
95
|
+
return prefix + (prefix.trimEnd().endsWith('{') ? '' : ',') + '"__utf16Codec":true' + json.slice(end);
|
|
96
|
+
}
|
package/src/dom.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { requireNative } from './native.js';
|
|
2
|
+
import { getNode, importNodes, nodeBinding, operation, cloneNode } from './nodes.js';
|
|
3
|
+
import { routeDocument } from './router.js';
|
|
4
|
+
import type * as Shape from './types.js';
|
|
5
|
+
|
|
6
|
+
export const ElementType = Object.freeze({ Root: 'root', Text: 'text', Directive: 'directive', Comment: 'comment', Script: 'script', Style: 'style', Tag: 'tag', CDATA: 'cdata', Doctype: 'doctype' } as typeof Shape.ElementType);
|
|
7
|
+
|
|
8
|
+
function initialize<T extends Node>(node: T, type: string, fields: Record<string, unknown>, children: readonly Shape.AnyNode[] = []): T {
|
|
9
|
+
const document = routeDocument(new (requireNative().NativeDocument)('', false));
|
|
10
|
+
const id = operation<number>(document, 'createNode', { type, ...fields });
|
|
11
|
+
if (children.length) operation(document, 'setNode', { id, field: 'children', value: importNodes(document, children) });
|
|
12
|
+
return getNode(document, id, node) as unknown as T;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** DOM constructors backed by native storage, also shared across ESM/CJS views. */
|
|
16
|
+
export abstract class Node {
|
|
17
|
+
static [Symbol.hasInstance](value: unknown): boolean {
|
|
18
|
+
const binding = nodeBinding(value);
|
|
19
|
+
if (!binding) return Function.prototype[Symbol.hasInstance].call(this, value);
|
|
20
|
+
const type = (value as Shape.AnyNode).type;
|
|
21
|
+
switch (this.name) {
|
|
22
|
+
case 'Node': return true;
|
|
23
|
+
case 'DataNode': return ['text', 'comment', 'directive'].includes(type);
|
|
24
|
+
case 'NodeWithChildren': return ['root', 'tag', 'script', 'style', 'cdata'].includes(type);
|
|
25
|
+
case 'Element': return ['tag', 'script', 'style'].includes(type);
|
|
26
|
+
case 'Document': return type === 'root';
|
|
27
|
+
case 'CDATA': return type === 'cdata';
|
|
28
|
+
case 'Text': return type === 'text';
|
|
29
|
+
case 'Comment': return type === 'comment';
|
|
30
|
+
case 'ProcessingInstruction': return type === 'directive';
|
|
31
|
+
default: return Function.prototype[Symbol.hasInstance].call(this, value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export interface Node extends Shape.Node {}
|
|
36
|
+
export abstract class DataNode extends Node {}
|
|
37
|
+
export interface DataNode extends Shape.DataNode {}
|
|
38
|
+
export abstract class NodeWithChildren extends Node {}
|
|
39
|
+
export interface NodeWithChildren extends Shape.NodeWithChildren {}
|
|
40
|
+
export class Text extends DataNode { declare readonly nodeType: 3; declare type: Shape.Text['type']; constructor(data: string) { super(); return initialize(this, 'text', { data }); } }
|
|
41
|
+
export interface Text extends Shape.Text {}
|
|
42
|
+
export class Comment extends DataNode { declare readonly nodeType: 8; declare type: Shape.Comment['type']; constructor(data: string) { super(); return initialize(this, 'comment', { data }); } }
|
|
43
|
+
export interface Comment extends Shape.Comment {}
|
|
44
|
+
export class ProcessingInstruction extends DataNode { declare readonly nodeType: 1; declare type: Shape.ProcessingInstruction['type']; constructor(name: string, data: string) { super(); return initialize(this, 'directive', { name, data }); } }
|
|
45
|
+
export interface ProcessingInstruction extends Shape.ProcessingInstruction {}
|
|
46
|
+
export class Document extends NodeWithChildren { declare readonly nodeType: 9; declare type: Shape.Document['type']; constructor(children: Shape.ChildNode[]) { super(); return initialize(this, 'root', {}, children); } }
|
|
47
|
+
export interface Document extends Shape.Document {}
|
|
48
|
+
export class CDATA extends NodeWithChildren { declare readonly nodeType: 4; declare type: Shape.CDATA['type']; constructor(children: Shape.ChildNode[]) { super(); return initialize(this, 'cdata', {}, children); } }
|
|
49
|
+
export interface CDATA extends Shape.CDATA {}
|
|
50
|
+
export class Element extends NodeWithChildren { declare readonly nodeType: 1; declare type: Shape.Element['type']; declare sourceCodeLocation?: Shape.TagSourceCodeLocation | null;
|
|
51
|
+
constructor(name: string, attribs: Record<string, string>, children: Shape.ChildNode[] = [], type?: Shape.Element['type']) {
|
|
52
|
+
super(); return initialize(this, type ?? (name === 'script' || name === 'style' ? name : 'tag'), { name, attribs }, children);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export interface Element extends Shape.Element {}
|
|
56
|
+
|
|
57
|
+
/** @internal */
|
|
58
|
+
export function nodePrototype(type: string): object {
|
|
59
|
+
return ({ root: Document, text: Text, comment: Comment, directive: ProcessingInstruction, cdata: CDATA, tag: Element, script: Element, style: Element }[type] ?? Node).prototype;
|
|
60
|
+
}
|
|
61
|
+
export const isTag = (node: Shape.Node): node is Shape.Element => ['tag', 'script', 'style'].includes(node.type);
|
|
62
|
+
export const isText = (node: Shape.Node): node is Shape.Text => node.type === 'text';
|
|
63
|
+
export const isComment = (node: Shape.Node): node is Shape.Comment => node.type === 'comment';
|
|
64
|
+
export const isDirective = (node: Shape.Node): node is Shape.ProcessingInstruction => node.type === 'directive';
|
|
65
|
+
export const isDocument = (node: Shape.Node): node is Shape.Document => node.type === 'root';
|
|
66
|
+
export const isCDATA = (node: Shape.Node): node is Shape.CDATA => node.type === 'cdata';
|
|
67
|
+
export const hasChildren = (node: Shape.Node): node is Shape.NodeWithChildren => 'children' in node;
|
|
68
|
+
|
|
69
|
+
// DOM aliases live on their class prototypes, as they do in domhandler. Native
|
|
70
|
+
// views intercept the underlying fields; external supplied nodes keep theirs.
|
|
71
|
+
Object.defineProperties(Node.prototype, {
|
|
72
|
+
cloneNode: { configurable: true, writable: true, value: cloneNode },
|
|
73
|
+
parentNode: { configurable: true, get(this: Shape.Node) { return this.parent; }, set(this: Shape.Node, value: Shape.ParentNode | null) { this.parent = value; } },
|
|
74
|
+
previousSibling: { configurable: true, get(this: Shape.Node) { return this.prev; }, set(this: Shape.Node, value: Shape.ChildNode | null) { this.prev = value; } },
|
|
75
|
+
nextSibling: { configurable: true, get(this: Shape.Node) { return this.next; }, set(this: Shape.Node, value: Shape.ChildNode | null) { this.next = value; } },
|
|
76
|
+
nodeType: { configurable: true, get(this: Shape.Node) { return ({ root: 9, text: 3, comment: 8, cdata: 4 } as Record<string, number>)[this.type] ?? 1; } },
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(DataNode.prototype, 'nodeValue', { configurable: true, get(this: Shape.DataNode) { return this.data; }, set(this: Shape.DataNode, value: string) { this.data = value; } });
|
|
79
|
+
Object.defineProperties(NodeWithChildren.prototype, {
|
|
80
|
+
childNodes: { configurable: true, get(this: Shape.NodeWithChildren) { return this.children; }, set(this: Shape.NodeWithChildren, value: Shape.ChildNode[]) { this.children = value; } },
|
|
81
|
+
firstChild: { configurable: true, get(this: Shape.NodeWithChildren) { return this.children[0] ?? null; } },
|
|
82
|
+
lastChild: { configurable: true, get(this: Shape.NodeWithChildren) { return this.children[this.children.length - 1] ?? null; } },
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperties(Element.prototype, {
|
|
85
|
+
tagName: { configurable: true, get(this: Shape.Element) { return this.name; }, set(this: Shape.Element, value: string) { this.name = value; } },
|
|
86
|
+
attributes: { configurable: true, get(this: Shape.Element) { return Object.keys(this.attribs).map((name) => ({ name, value: this.attribs[name], namespace: this['x-attribsNamespace']?.[name], prefix: this['x-attribsPrefix']?.[name] })); } },
|
|
87
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { CheerioOptions, ExtractMap } from './types.js';
|
|
2
|
+
import { types } from 'node:util';
|
|
3
|
+
|
|
4
|
+
export interface ExtractPlanEntry {
|
|
5
|
+
key: string;
|
|
6
|
+
selector: string;
|
|
7
|
+
all: boolean;
|
|
8
|
+
value: { kind: 'property'; name: string } | { kind: 'map'; entries: ExtractPlanEntry[] };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ExtractPlan { version: 1; entries: ExtractPlanEntry[] }
|
|
12
|
+
|
|
13
|
+
// Properties that can be returned as ordinary values. Properties returning DOM
|
|
14
|
+
// views stay on the regular API so their identities and document leases survive.
|
|
15
|
+
const scalarProperties = new Set([
|
|
16
|
+
'textContent', 'innerText', 'innerHTML', 'outerHTML', 'tagName', 'nodeName',
|
|
17
|
+
'nodeType', 'id', 'class', 'className', 'title', 'href', 'src', 'alt', 'name',
|
|
18
|
+
'value', 'type', 'checked', 'selected', 'disabled', 'multiple', 'readOnly',
|
|
19
|
+
'required', 'hidden', 'open', 'autofocus', 'controls', 'loop', 'muted',
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
function dataValue(object: object, key: string): { value: unknown } | undefined {
|
|
23
|
+
for (let current: object | null = object; current; current = Object.getPrototypeOf(current)) {
|
|
24
|
+
if (types.isProxy(current)) return undefined;
|
|
25
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
26
|
+
if (descriptor) return 'value' in descriptor ? { value: descriptor.value } : undefined;
|
|
27
|
+
}
|
|
28
|
+
return { value: undefined };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Compile pure extraction maps. User callbacks and accessors keep JS semantics. */
|
|
32
|
+
export function compileExtractPlan(map: ExtractMap, options: CheerioOptions): ExtractPlan | undefined {
|
|
33
|
+
if (options.pseudos || options.treeAdapter || options.Tokenizer || options.xml && typeof options.xml === 'object' && options.xml.Tokenizer) return undefined;
|
|
34
|
+
const ancestors = new Set<object>();
|
|
35
|
+
const compile = (input: unknown): ExtractPlanEntry[] | undefined => {
|
|
36
|
+
if (!input || typeof input !== 'object' || types.isProxy(input) || ancestors.has(input) || ancestors.size >= 64) return undefined;
|
|
37
|
+
for (let current = Object.getPrototypeOf(input); current; current = Object.getPrototypeOf(current)) {
|
|
38
|
+
if (types.isProxy(current)) return undefined;
|
|
39
|
+
}
|
|
40
|
+
ancestors.add(input);
|
|
41
|
+
try {
|
|
42
|
+
const entries: ExtractPlanEntry[] = [];
|
|
43
|
+
for (const key in input) {
|
|
44
|
+
// Cheerio uses an ordinary result object. Its legacy __proto__ setter
|
|
45
|
+
// semantics must execute through the JavaScript path.
|
|
46
|
+
if (key === '__proto__') return undefined;
|
|
47
|
+
const inherited = Object.getOwnPropertyDescriptor(Object.prototype, key);
|
|
48
|
+
if (inherited && (!('value' in inherited) || !inherited.writable)) return undefined;
|
|
49
|
+
const slot = dataValue(input, key);
|
|
50
|
+
if (!slot) return undefined;
|
|
51
|
+
const all = Array.isArray(slot.value);
|
|
52
|
+
if (slot.value && typeof slot.value === 'object' && types.isProxy(slot.value)) return undefined;
|
|
53
|
+
const first = all ? dataValue(slot.value as object, '0') : slot;
|
|
54
|
+
if (!first) return undefined;
|
|
55
|
+
const descriptor = first.value;
|
|
56
|
+
let selector: unknown;
|
|
57
|
+
let value: unknown;
|
|
58
|
+
if (typeof descriptor === 'string') { selector = descriptor; value = 'textContent'; }
|
|
59
|
+
else if (descriptor && typeof descriptor === 'object') {
|
|
60
|
+
if (types.isProxy(descriptor)) return undefined;
|
|
61
|
+
const selectorSlot = dataValue(descriptor, 'selector');
|
|
62
|
+
const valueSlot = dataValue(descriptor, 'value');
|
|
63
|
+
if (!selectorSlot || !valueSlot) return undefined;
|
|
64
|
+
selector = selectorSlot.value;
|
|
65
|
+
value = valueSlot.value ?? 'textContent';
|
|
66
|
+
} else return undefined;
|
|
67
|
+
if (typeof selector !== 'string') return undefined;
|
|
68
|
+
if (typeof value === 'string') {
|
|
69
|
+
if (!scalarProperties.has(value) && !/^(?:data-|aria-).+/.test(value)) return undefined;
|
|
70
|
+
entries.push({ key, selector, all, value: { kind: 'property', name: value } });
|
|
71
|
+
} else {
|
|
72
|
+
const nested = compile(value);
|
|
73
|
+
if (!nested) return undefined;
|
|
74
|
+
entries.push({ key, selector, all, value: { kind: 'map', entries: nested } });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return entries;
|
|
78
|
+
} finally { ancestors.delete(input); }
|
|
79
|
+
};
|
|
80
|
+
const entries = compile(map);
|
|
81
|
+
return entries && { version: 1, entries };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** JSON omits undefined fields; restore them recursively without changing order. */
|
|
85
|
+
export function hydrateExtractResult(result: Record<string, unknown>, entries: ExtractPlanEntry[]): void {
|
|
86
|
+
const values = entries.map((entry) => Object.hasOwn(result, entry.key) ? result[entry.key] : entry.all ? [] : undefined);
|
|
87
|
+
for (const entry of entries) delete result[entry.key];
|
|
88
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
89
|
+
const entry = entries[index];
|
|
90
|
+
Object.defineProperty(result, entry.key, { value: values[index], enumerable: true, configurable: true, writable: true });
|
|
91
|
+
if (entry.value.kind !== 'map') continue;
|
|
92
|
+
const nested = result[entry.key];
|
|
93
|
+
for (const value of entry.all && Array.isArray(nested) ? nested : [nested]) {
|
|
94
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) hydrateExtractResult(value as Record<string, unknown>, entry.value.entries);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
package/src/extract.ts
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { Cheerio, nativeExtractionMethods } from './selection.js';
|
|
2
|
+
import { types } from 'node:util';
|
|
3
|
+
import { compileExtractPlan, hydrateExtractResult } from './extract-plan.js';
|
|
4
|
+
import { decodeJson } from './codec.js';
|
|
5
|
+
import { DOCUMENT, writeCount } from './router.js';
|
|
6
|
+
import type { AnyNode, CheerioOptions, Element, ExtractMap, ExtractedMap, ExtractDescriptor } from './types.js';
|
|
7
|
+
|
|
8
|
+
type ExtractPlan = NonNullable<ReturnType<typeof compileExtractPlan>>;
|
|
9
|
+
|
|
10
|
+
export interface ExtractMethods<T = AnyNode> {
|
|
11
|
+
extract<M extends ExtractMap>(map: M): ExtractedMap<M>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function plainKeys(map: ExtractMap): string[] | undefined {
|
|
15
|
+
return !types.isProxy(map) && (Object.getPrototypeOf(map) === Object.prototype || Object.getPrototypeOf(map) === null) ? Object.keys(map) : undefined;
|
|
16
|
+
}
|
|
17
|
+
function nativePrefix(scope: Cheerio<AnyNode>, map: ExtractMap, keys: string[] | undefined, key: string): { keys: string[]; values: Record<string, unknown> } | undefined {
|
|
18
|
+
if (!keys || !hasNativeExtraction(scope)) return undefined;
|
|
19
|
+
const prefix = prefixPlan(map, keys, key, scope.options);
|
|
20
|
+
// A single scalar entry costs one selector call and one property call; the
|
|
21
|
+
// native plan also avoids constructing an intermediate selection for each.
|
|
22
|
+
if (!prefix) return undefined;
|
|
23
|
+
return { keys: prefix.names, values: runPlan(scope, prefix.plan) };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The native plan for the longest run of entries from `key` on that compile to one. */
|
|
27
|
+
function prefixPlan(map: ExtractMap, keys: string[], key: string, options: CheerioOptions): { names: string[]; plan: ExtractPlan } | undefined {
|
|
28
|
+
// Pure maps are the common case. Compile the complete plan once instead of
|
|
29
|
+
// recompiling each growing prefix; callbacks and inherited entries keep the
|
|
30
|
+
// incremental path so they retain their observable order.
|
|
31
|
+
if (keys[0] === key && keys.every((name) => {
|
|
32
|
+
const slot = Object.getOwnPropertyDescriptor(map, name);
|
|
33
|
+
return !!slot && slot.enumerable && 'value' in slot;
|
|
34
|
+
})) {
|
|
35
|
+
let count = 0;
|
|
36
|
+
let sameKeys = true;
|
|
37
|
+
for (const name in map) { if (name !== keys[count++]) { sameKeys = false; break; } }
|
|
38
|
+
if (sameKeys && count === keys.length) {
|
|
39
|
+
const complete = compileExtractPlan(map, options);
|
|
40
|
+
if (complete) return { names: keys, plan: complete };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const prefix: ExtractMap = Object.create(null);
|
|
44
|
+
let plan: ExtractPlan | undefined;
|
|
45
|
+
for (let position = keys.indexOf(key); position < keys.length; position++) {
|
|
46
|
+
const name = keys[position];
|
|
47
|
+
const slot = Object.getOwnPropertyDescriptor(map, name);
|
|
48
|
+
if (!slot || !slot.enumerable || !('value' in slot)) break;
|
|
49
|
+
Object.defineProperty(prefix, name, { value: slot.value, enumerable: true, configurable: true });
|
|
50
|
+
const compiled = compileExtractPlan(prefix, options);
|
|
51
|
+
if (!compiled) { delete prefix[name]; break; }
|
|
52
|
+
plan = compiled;
|
|
53
|
+
}
|
|
54
|
+
return plan ? { names: Object.keys(prefix), plan } : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function hasNativeExtraction(scope: Cheerio<AnyNode>): boolean {
|
|
58
|
+
return scope._make === nativeExtractionMethods.make && scope._findBySelector === nativeExtractionMethods.find && scope.prop === nativeExtractionMethods.prop && scope.extract === nativeExtractionMethods.extract;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Descriptor callbacks stay in JavaScript; matching and properties use Rust. */
|
|
62
|
+
export function extract<M extends ExtractMap>(this: Cheerio<AnyNode>, map: M): ExtractedMap<M> {
|
|
63
|
+
const result: Record<string, unknown> = {};
|
|
64
|
+
const keys = plainKeys(map);
|
|
65
|
+
const processed = new Set<string>();
|
|
66
|
+
for (const key in map) {
|
|
67
|
+
if (processed.has(key)) continue;
|
|
68
|
+
const prefix = Object.getPrototypeOf(result) === Object.prototype ? nativePrefix(this, map, keys, key) : undefined;
|
|
69
|
+
if (prefix) {
|
|
70
|
+
for (const name of prefix.keys) { Object.defineProperty(result, name, { value: prefix.values[name], enumerable: true, configurable: true, writable: true }); processed.add(name); }
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const entry = map[key];
|
|
74
|
+
const all = Array.isArray(entry);
|
|
75
|
+
const descriptor = (all ? entry[0] : entry) as string | ExtractDescriptor;
|
|
76
|
+
const selector = typeof descriptor === 'string' ? descriptor : descriptor.selector;
|
|
77
|
+
const value = typeof descriptor === 'string' ? 'textContent' : descriptor.value ?? 'textContent';
|
|
78
|
+
const selection = this._findBySelector(selector, all ? Infinity : 1);
|
|
79
|
+
const rows = typeof value === 'object' ? nativeRows(this, key, all, selector, value) : undefined;
|
|
80
|
+
const read = (index: number) => {
|
|
81
|
+
if (typeof value === 'function') return value(selection[index] as Element, key, result);
|
|
82
|
+
const scope = this._make === nativeExtractionMethods.make
|
|
83
|
+
? this._fromIds([selection.ids[index]])
|
|
84
|
+
: this._make(selection[index]);
|
|
85
|
+
if (typeof value === 'string') return scope.prop(value);
|
|
86
|
+
return rows ? completeRow(scope, value, rows, index) : scope.extract(value);
|
|
87
|
+
};
|
|
88
|
+
let extracted: unknown;
|
|
89
|
+
if (all) {
|
|
90
|
+
const values: unknown[] = [];
|
|
91
|
+
for (let index = 0; index < selection.length; index += 1) {
|
|
92
|
+
const value = read(index);
|
|
93
|
+
if (value != null) values.push(...(Array.isArray(value) ? value : [value]));
|
|
94
|
+
}
|
|
95
|
+
extracted = values;
|
|
96
|
+
} else {
|
|
97
|
+
extracted = selection.length ? read(0) : undefined;
|
|
98
|
+
}
|
|
99
|
+
result[key] = extracted;
|
|
100
|
+
}
|
|
101
|
+
return result as ExtractedMap<M>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Fields of each row that the native engine extracts, for every row in one call. */
|
|
105
|
+
interface NativeRows { keys: Set<string>; rows: Record<string, unknown>[]; writes: number }
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* For a nested map with callbacks, extracts the fields the native engine can
|
|
109
|
+
* for every matched element in one call, instead of one call per element;
|
|
110
|
+
* the remaining fields are then filled in map order, so each callback sees
|
|
111
|
+
* exactly the fields before it, as in Cheerio.
|
|
112
|
+
*/
|
|
113
|
+
function nativeRows(scope: Cheerio<AnyNode>, key: string, all: boolean, selector: string, map: ExtractMap): NativeRows | undefined {
|
|
114
|
+
if (!hasNativeExtraction(scope)) return undefined;
|
|
115
|
+
const planned = rowsPlan(key, all, selector, map, scope.options);
|
|
116
|
+
if (!planned) return undefined;
|
|
117
|
+
const writes = writeCount();
|
|
118
|
+
const extracted = runPlan(scope, planned.plan)[key];
|
|
119
|
+
const rows = (all ? extracted : extracted === undefined ? [] : [extracted]) as Record<string, unknown>[];
|
|
120
|
+
return Array.isArray(rows) ? { keys: planned.nativeKeys, rows, writes } : undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The native plan for the fields of `map` that compile to one, when only some do. */
|
|
124
|
+
function rowsPlan(key: string, all: boolean, selector: string, map: ExtractMap, options: CheerioOptions): { nativeKeys: Set<string>; plan: ExtractPlan } | undefined {
|
|
125
|
+
const keys = plainKeys(map);
|
|
126
|
+
if (!keys) return undefined;
|
|
127
|
+
const subset: ExtractMap = Object.create(null);
|
|
128
|
+
const nativeKeys = new Set<string>();
|
|
129
|
+
for (const name of keys) {
|
|
130
|
+
const slot = Object.getOwnPropertyDescriptor(map, name);
|
|
131
|
+
if (!slot || !slot.enumerable || !('value' in slot)) continue;
|
|
132
|
+
const single: ExtractMap = Object.create(null);
|
|
133
|
+
Object.defineProperty(single, name, { value: slot.value, enumerable: true, configurable: true });
|
|
134
|
+
if (!compileExtractPlan(single, options)) continue;
|
|
135
|
+
Object.defineProperty(subset, name, { value: slot.value, enumerable: true, configurable: true });
|
|
136
|
+
nativeKeys.add(name);
|
|
137
|
+
}
|
|
138
|
+
if (!nativeKeys.size || nativeKeys.size === keys.length) return undefined;
|
|
139
|
+
const outer: ExtractMap = Object.create(null);
|
|
140
|
+
Object.defineProperty(outer, key, { value: all ? [{ selector, value: subset }] : { selector, value: subset }, enumerable: true, configurable: true });
|
|
141
|
+
const plan = compileExtractPlan(outer, options);
|
|
142
|
+
return plan ? { nativeKeys, plan } : undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The plans a walk of `map` from a document's root runs first, in order, so
|
|
147
|
+
* a batch can run them on its parsing threads. Callbacks may change the
|
|
148
|
+
* document, so later plans of the walk stay on the calling thread.
|
|
149
|
+
*/
|
|
150
|
+
export function rootExtractPlans(map: ExtractMap, options: CheerioOptions): ExtractPlan[] {
|
|
151
|
+
// Reading a map ahead of the walk must not run code the walk would run in
|
|
152
|
+
// a different order, and a malformed map fails later with the walk's own error.
|
|
153
|
+
if (!isPlainData(map, 0) || hasInheritedEntryFields()) return [];
|
|
154
|
+
try { return collectRootExtractPlans(map, options); } catch { return []; }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Whether reading `value` and everything it holds runs no code: no proxies, accessors or class instances. */
|
|
158
|
+
function isPlainData(value: unknown, depth: number): boolean {
|
|
159
|
+
if (typeof value !== 'object' || value === null) return true;
|
|
160
|
+
if (depth > 32 || types.isProxy(value)) return false;
|
|
161
|
+
const prototype = Object.getPrototypeOf(value);
|
|
162
|
+
if (prototype !== Object.prototype && prototype !== Array.prototype && prototype !== null) return false;
|
|
163
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
164
|
+
const slot = Object.getOwnPropertyDescriptor(value, key)!;
|
|
165
|
+
if (!('value' in slot) || !isPlainData(slot.value, depth + 1)) return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function hasInheritedEntryFields(): boolean {
|
|
171
|
+
return 'selector' in Object.prototype || 'value' in Object.prototype || '0' in Array.prototype;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function collectRootExtractPlans(map: ExtractMap, options: CheerioOptions): ExtractPlan[] {
|
|
175
|
+
const plans: ExtractPlan[] = [];
|
|
176
|
+
const keys = plainKeys(map);
|
|
177
|
+
const processed = new Set<string>();
|
|
178
|
+
for (const key in map) {
|
|
179
|
+
if (processed.has(key)) continue;
|
|
180
|
+
const prefix = keys ? prefixPlan(map, keys, key, options) : undefined;
|
|
181
|
+
if (prefix) {
|
|
182
|
+
for (const name of prefix.names) processed.add(name);
|
|
183
|
+
plans.push(prefix.plan);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const entry = map[key];
|
|
187
|
+
const all = Array.isArray(entry);
|
|
188
|
+
const descriptor = (all ? entry[0] : entry) as string | ExtractDescriptor;
|
|
189
|
+
const value = typeof descriptor === 'string' ? 'textContent' : descriptor.value ?? 'textContent';
|
|
190
|
+
if (typeof value !== 'object' || value === null) continue;
|
|
191
|
+
const planned = rowsPlan(key, all, typeof descriptor === 'string' ? descriptor : descriptor.selector, value, options);
|
|
192
|
+
if (planned) plans.push(planned.plan);
|
|
193
|
+
}
|
|
194
|
+
return plans.filter((plan) => !JSON.stringify(plan).includes('innerHTML'));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Results of root plans a batch ran while parsing, valid while no document changes. */
|
|
198
|
+
const preparedPlans = new WeakMap<object, { writes: number; results: Map<string, string> }>();
|
|
199
|
+
|
|
200
|
+
export function prepareExtractPlans(document: object, plans: ExtractPlan[], results: (string | null)[] | null | undefined): void {
|
|
201
|
+
if (!results) return;
|
|
202
|
+
const prepared = new Map<string, string>();
|
|
203
|
+
for (let index = 0; index < plans.length; index++) {
|
|
204
|
+
const result = results[index];
|
|
205
|
+
if (typeof result === 'string') prepared.set(JSON.stringify(plans[index]), decodeJson(result));
|
|
206
|
+
}
|
|
207
|
+
preparedPlans.set(document, { writes: writeCount(), results: prepared });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function releaseExtractPlans(document: object): void {
|
|
211
|
+
preparedPlans.delete(document);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function runPlan(scope: Cheerio<AnyNode>, plan: ExtractPlan): Record<string, unknown> {
|
|
215
|
+
const document = scope[DOCUMENT] as { rootId(): number };
|
|
216
|
+
const prepared = preparedPlans.get(document);
|
|
217
|
+
const isRoot = prepared !== undefined && prepared.writes === writeCount() && scope.ids.length === 1 && scope.ids[0] === document.rootId();
|
|
218
|
+
const result = isRoot ? prepared.results.get(JSON.stringify(plan)) : undefined;
|
|
219
|
+
const values = result !== undefined ? JSON.parse(result) as Record<string, unknown> : scope.op<Record<string, unknown>>('extractPlan', { ids: scope.ids, plan });
|
|
220
|
+
hydrateExtractResult(values, plan.entries);
|
|
221
|
+
return values;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Builds row `index` of a nested map from its native fields and the scope's
|
|
226
|
+
* JavaScript fields. Native fields extracted before a callback changed any
|
|
227
|
+
* document are extracted again, as extracting row by row would see them.
|
|
228
|
+
*/
|
|
229
|
+
function completeRow(scope: Cheerio<AnyNode>, map: ExtractMap, rows: NativeRows, index: number): Record<string, unknown> {
|
|
230
|
+
const row: Record<string, unknown> = {};
|
|
231
|
+
const native = rows.rows[index] ?? {};
|
|
232
|
+
for (const name in map) {
|
|
233
|
+
if (rows.keys.has(name) && writeCount() === rows.writes) { row[name] = native[name]; continue; }
|
|
234
|
+
const entry: ExtractMap = Object.create(null);
|
|
235
|
+
Object.defineProperty(entry, name, { value: map[name], enumerable: true, configurable: true });
|
|
236
|
+
fillEntries(scope, entry, row);
|
|
237
|
+
}
|
|
238
|
+
return row;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Extracts `map`'s entries into `result`, which callbacks receive as built so far. */
|
|
242
|
+
function fillEntries(scope: Cheerio<AnyNode>, map: ExtractMap, result: Record<string, unknown>): void {
|
|
243
|
+
for (const key in map) {
|
|
244
|
+
const entry = map[key];
|
|
245
|
+
const all = Array.isArray(entry);
|
|
246
|
+
const descriptor = (all ? entry[0] : entry) as string | ExtractDescriptor;
|
|
247
|
+
const selector = typeof descriptor === 'string' ? descriptor : descriptor.selector;
|
|
248
|
+
const value = typeof descriptor === 'string' ? 'textContent' : descriptor.value ?? 'textContent';
|
|
249
|
+
const selection = scope._findBySelector(selector, all ? Infinity : 1);
|
|
250
|
+
const read = (index: number) => {
|
|
251
|
+
if (typeof value === 'function') return value(selection[index] as Element, key, result);
|
|
252
|
+
const child = scope._make === nativeExtractionMethods.make ? scope._fromIds([selection.ids[index]]) : scope._make(selection[index]);
|
|
253
|
+
return typeof value === 'string' ? child.prop(value) : child.extract(value);
|
|
254
|
+
};
|
|
255
|
+
if (all) {
|
|
256
|
+
const values: unknown[] = [];
|
|
257
|
+
for (let index = 0; index < selection.length; index += 1) {
|
|
258
|
+
const extracted = read(index);
|
|
259
|
+
if (extracted != null) values.push(...(Array.isArray(extracted) ? extracted : [extracted]));
|
|
260
|
+
}
|
|
261
|
+
result[key] = values;
|
|
262
|
+
} else result[key] = selection.length ? read(0) : undefined;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function installExtract(CheerioClass: { prototype: object }): void {
|
|
267
|
+
Object.assign(CheerioClass.prototype, { extract });
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Async batch fallback preserves callback values/order while yielding between visits. */
|
|
271
|
+
export async function extractCooperatively<M extends ExtractMap>(selection: Cheerio<AnyNode>, map: M): Promise<{ value: ExtractedMap<M> }> {
|
|
272
|
+
let deadline = performance.now() + 4;
|
|
273
|
+
let visits = 0;
|
|
274
|
+
const checkpoint = async () => {
|
|
275
|
+
if (++visits % 32 || performance.now() < deadline) return;
|
|
276
|
+
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
277
|
+
deadline = performance.now() + 4;
|
|
278
|
+
};
|
|
279
|
+
const walk = async (scope: Cheerio<AnyNode>, descriptors: ExtractMap): Promise<{ value: Record<string, unknown> }> => {
|
|
280
|
+
const result: Record<string, unknown> = {};
|
|
281
|
+
const keys = plainKeys(descriptors);
|
|
282
|
+
const processed = new Set<string>();
|
|
283
|
+
for (const key in descriptors) {
|
|
284
|
+
if (processed.has(key)) continue;
|
|
285
|
+
const prefix = Object.getPrototypeOf(result) === Object.prototype ? nativePrefix(scope, descriptors, keys, key) : undefined;
|
|
286
|
+
if (prefix) {
|
|
287
|
+
for (const name of prefix.keys) { Object.defineProperty(result, name, { value: prefix.values[name], enumerable: true, configurable: true, writable: true }); processed.add(name); }
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const entry = descriptors[key];
|
|
291
|
+
const all = Array.isArray(entry);
|
|
292
|
+
const descriptor = (all ? entry[0] : entry) as string | ExtractDescriptor;
|
|
293
|
+
const selector = typeof descriptor === 'string' ? descriptor : descriptor.selector;
|
|
294
|
+
const value = typeof descriptor === 'string' ? 'textContent' : descriptor.value ?? 'textContent';
|
|
295
|
+
const found = scope._findBySelector(selector, all ? Infinity : 1);
|
|
296
|
+
const rows = typeof value === 'object' ? nativeRows(scope, key, all, selector, value) : undefined;
|
|
297
|
+
const output: unknown[] = [];
|
|
298
|
+
let single: unknown;
|
|
299
|
+
const count = all ? found.length : Math.min(found.length, 1);
|
|
300
|
+
for (let index = 0; index < count; index++) {
|
|
301
|
+
await checkpoint();
|
|
302
|
+
// Callback return values, including Promise objects, remain ordinary
|
|
303
|
+
// values just as in synchronous Cheerio.extract().
|
|
304
|
+
let extracted: unknown;
|
|
305
|
+
if (typeof value === 'function') extracted = value(found[index] as Element, key, result);
|
|
306
|
+
else {
|
|
307
|
+
const child = scope._make === nativeExtractionMethods.make
|
|
308
|
+
? scope._fromIds([found.ids[index]])
|
|
309
|
+
: scope._make(found[index]);
|
|
310
|
+
extracted = typeof value === 'string' ? child.prop(value)
|
|
311
|
+
: rows ? completeRow(child, value, rows, index)
|
|
312
|
+
: child.extract === nativeExtractionMethods.extract ? (await walk(child, value)).value : child.extract(value);
|
|
313
|
+
}
|
|
314
|
+
if (all) { if (extracted != null) output.push(...(Array.isArray(extracted) ? extracted : [extracted])); }
|
|
315
|
+
else single = extracted;
|
|
316
|
+
}
|
|
317
|
+
result[key] = all ? output : single;
|
|
318
|
+
}
|
|
319
|
+
return { value: result };
|
|
320
|
+
};
|
|
321
|
+
return await walk(selection, map) as { value: ExtractedMap<M> };
|
|
322
|
+
}
|