@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
|
@@ -0,0 +1,2858 @@
|
|
|
1
|
+
const require_nodes = require("./nodes-jbWWhssj.cjs");
|
|
2
|
+
const require_utils = require("./utils.cjs");
|
|
3
|
+
let node_util = require("node:util");
|
|
4
|
+
let node_timers_promises = require("node:timers/promises");
|
|
5
|
+
let node_os = require("node:os");
|
|
6
|
+
let node_stream = require("node:stream");
|
|
7
|
+
let node_http = require("node:http");
|
|
8
|
+
let node_https = require("node:https");
|
|
9
|
+
let node_querystring = require("node:querystring");
|
|
10
|
+
// src/extract-plan.ts
|
|
11
|
+
const scalarProperties = /* @__PURE__ */ new Set([
|
|
12
|
+
"textContent",
|
|
13
|
+
"innerText",
|
|
14
|
+
"innerHTML",
|
|
15
|
+
"outerHTML",
|
|
16
|
+
"tagName",
|
|
17
|
+
"nodeName",
|
|
18
|
+
"nodeType",
|
|
19
|
+
"id",
|
|
20
|
+
"class",
|
|
21
|
+
"className",
|
|
22
|
+
"title",
|
|
23
|
+
"href",
|
|
24
|
+
"src",
|
|
25
|
+
"alt",
|
|
26
|
+
"name",
|
|
27
|
+
"value",
|
|
28
|
+
"type",
|
|
29
|
+
"checked",
|
|
30
|
+
"selected",
|
|
31
|
+
"disabled",
|
|
32
|
+
"multiple",
|
|
33
|
+
"readOnly",
|
|
34
|
+
"required",
|
|
35
|
+
"hidden",
|
|
36
|
+
"open",
|
|
37
|
+
"autofocus",
|
|
38
|
+
"controls",
|
|
39
|
+
"loop",
|
|
40
|
+
"muted"
|
|
41
|
+
]);
|
|
42
|
+
function dataValue(object, key) {
|
|
43
|
+
for (let current = object; current; current = Object.getPrototypeOf(current)) {
|
|
44
|
+
if (node_util.types.isProxy(current)) return void 0;
|
|
45
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
46
|
+
if (descriptor) return "value" in descriptor ? { value: descriptor.value } : void 0;
|
|
47
|
+
}
|
|
48
|
+
return { value: void 0 };
|
|
49
|
+
}
|
|
50
|
+
/** Compile pure extraction maps. User callbacks and accessors keep JS semantics. */
|
|
51
|
+
function compileExtractPlan(map, options) {
|
|
52
|
+
if (options.pseudos || options.treeAdapter || options.Tokenizer || options.xml && typeof options.xml === "object" && options.xml.Tokenizer) return void 0;
|
|
53
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
54
|
+
const compile = (input) => {
|
|
55
|
+
if (!input || typeof input !== "object" || node_util.types.isProxy(input) || ancestors.has(input) || ancestors.size >= 64) return void 0;
|
|
56
|
+
for (let current = Object.getPrototypeOf(input); current; current = Object.getPrototypeOf(current)) if (node_util.types.isProxy(current)) return void 0;
|
|
57
|
+
ancestors.add(input);
|
|
58
|
+
try {
|
|
59
|
+
const entries = [];
|
|
60
|
+
for (const key in input) {
|
|
61
|
+
if (key === "__proto__") return void 0;
|
|
62
|
+
const inherited = Object.getOwnPropertyDescriptor(Object.prototype, key);
|
|
63
|
+
if (inherited && (!("value" in inherited) || !inherited.writable)) return void 0;
|
|
64
|
+
const slot = dataValue(input, key);
|
|
65
|
+
if (!slot) return void 0;
|
|
66
|
+
const all = Array.isArray(slot.value);
|
|
67
|
+
if (slot.value && typeof slot.value === "object" && node_util.types.isProxy(slot.value)) return void 0;
|
|
68
|
+
const first = all ? dataValue(slot.value, "0") : slot;
|
|
69
|
+
if (!first) return void 0;
|
|
70
|
+
const descriptor = first.value;
|
|
71
|
+
let selector;
|
|
72
|
+
let value;
|
|
73
|
+
if (typeof descriptor === "string") {
|
|
74
|
+
selector = descriptor;
|
|
75
|
+
value = "textContent";
|
|
76
|
+
} else if (descriptor && typeof descriptor === "object") {
|
|
77
|
+
if (node_util.types.isProxy(descriptor)) return void 0;
|
|
78
|
+
const selectorSlot = dataValue(descriptor, "selector");
|
|
79
|
+
const valueSlot = dataValue(descriptor, "value");
|
|
80
|
+
if (!selectorSlot || !valueSlot) return void 0;
|
|
81
|
+
selector = selectorSlot.value;
|
|
82
|
+
value = valueSlot.value ?? "textContent";
|
|
83
|
+
} else return void 0;
|
|
84
|
+
if (typeof selector !== "string") return void 0;
|
|
85
|
+
if (typeof value === "string") {
|
|
86
|
+
if (!scalarProperties.has(value) && !/^(?:data-|aria-).+/.test(value)) return void 0;
|
|
87
|
+
entries.push({
|
|
88
|
+
key,
|
|
89
|
+
selector,
|
|
90
|
+
all,
|
|
91
|
+
value: {
|
|
92
|
+
kind: "property",
|
|
93
|
+
name: value
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
const nested = compile(value);
|
|
98
|
+
if (!nested) return void 0;
|
|
99
|
+
entries.push({
|
|
100
|
+
key,
|
|
101
|
+
selector,
|
|
102
|
+
all,
|
|
103
|
+
value: {
|
|
104
|
+
kind: "map",
|
|
105
|
+
entries: nested
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return entries;
|
|
111
|
+
} finally {
|
|
112
|
+
ancestors.delete(input);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const entries = compile(map);
|
|
116
|
+
return entries && {
|
|
117
|
+
version: 1,
|
|
118
|
+
entries
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** JSON omits undefined fields; restore them recursively without changing order. */
|
|
122
|
+
function hydrateExtractResult(result, entries) {
|
|
123
|
+
const values = entries.map((entry) => Object.hasOwn(result, entry.key) ? result[entry.key] : entry.all ? [] : void 0);
|
|
124
|
+
for (const entry of entries) delete result[entry.key];
|
|
125
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
126
|
+
const entry = entries[index];
|
|
127
|
+
Object.defineProperty(result, entry.key, {
|
|
128
|
+
value: values[index],
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true
|
|
132
|
+
});
|
|
133
|
+
if (entry.value.kind !== "map") continue;
|
|
134
|
+
const nested = result[entry.key];
|
|
135
|
+
for (const value of entry.all && Array.isArray(nested) ? nested : [nested]) if (value && typeof value === "object" && !Array.isArray(value)) hydrateExtractResult(value, entry.value.entries);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// src/attributes.ts
|
|
139
|
+
const elementTypes = /* @__PURE__ */ new Set([
|
|
140
|
+
"tag",
|
|
141
|
+
"script",
|
|
142
|
+
"style"
|
|
143
|
+
]);
|
|
144
|
+
/** Boolean attributes read as their name in HTML, matched case-insensitively as Cheerio does. */
|
|
145
|
+
const booleanAttribute = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
|
|
146
|
+
const absent = Symbol("absent");
|
|
147
|
+
const deferred = Symbol("deferred");
|
|
148
|
+
/**
|
|
149
|
+
* Reads an attribute of the selection's first node, as Cheerio reads it from
|
|
150
|
+
* `this[0]`, from cached records; `deferred` leaves the read to the native
|
|
151
|
+
* `attr` operation and `absent` means there is no such attribute.
|
|
152
|
+
*/
|
|
153
|
+
function readAttribute(selection, name) {
|
|
154
|
+
const node = selection[0];
|
|
155
|
+
if (typeof node !== "object" || node === null) return absent;
|
|
156
|
+
const state = node[require_nodes.VIEW_STATE];
|
|
157
|
+
if (state === void 0 || node[require_nodes.VIEW_SELF] !== node) return typeof node.type === "string" ? deferred : absent;
|
|
158
|
+
const local = node[require_nodes.VIEW_LOCAL];
|
|
159
|
+
const snapshot = require_nodes.plainSnapshot(state, local);
|
|
160
|
+
const record = snapshot ? void 0 : require_nodes.recordAt(state, local);
|
|
161
|
+
const type = snapshot ? snapshot.typeOf(local) : record?.type;
|
|
162
|
+
if (type === void 0 || !elementTypes.has(type)) return absent;
|
|
163
|
+
const value = require_nodes.attributeAt(state, local, name);
|
|
164
|
+
if (value === void 0) {
|
|
165
|
+
const attributes = record?.attribs;
|
|
166
|
+
if (attributes && Object.hasOwn(attributes, name)) return deferred;
|
|
167
|
+
return name === "value" ? deferred : absent;
|
|
168
|
+
}
|
|
169
|
+
const options = selection.options ?? {};
|
|
170
|
+
return !(options.xmlMode ?? (options.xml === true || typeof options.xml === "object" && options.xml !== null)) && booleanAttribute.test(name) ? name : value;
|
|
171
|
+
}
|
|
172
|
+
/** Writes one attribute through the routed document's typed path when it has one. */
|
|
173
|
+
function setAttribute(selection, ids, name, value) {
|
|
174
|
+
const document = selection[require_nodes.DOCUMENT];
|
|
175
|
+
if (document.setAttribute) document.setAttribute(ids, name, value);
|
|
176
|
+
else selection.op("attr", {
|
|
177
|
+
ids,
|
|
178
|
+
name,
|
|
179
|
+
value
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
function isElement(selection, id) {
|
|
183
|
+
return [
|
|
184
|
+
"tag",
|
|
185
|
+
"script",
|
|
186
|
+
"style"
|
|
187
|
+
].includes(require_nodes.nodeRecord(selection[require_nodes.DOCUMENT], id)?.type);
|
|
188
|
+
}
|
|
189
|
+
function nativeValue(value) {
|
|
190
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
191
|
+
if (typeof value === "number") return Number.isFinite(value) ? value === 0 ? 0 : value : { __nativeNumber: String(value) };
|
|
192
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
193
|
+
const json = JSON.stringify(value, (_key, value) => {
|
|
194
|
+
if (typeof value === "function" || typeof value === "symbol" || typeof value === "bigint" || value === void 0) return { __opaqueJavaScriptValue: true };
|
|
195
|
+
if (typeof value === "number" && !Number.isFinite(value)) return { __nativeNumber: String(value) };
|
|
196
|
+
if (value && typeof value === "object") {
|
|
197
|
+
if (seen.has(value)) return { __opaqueJavaScriptValue: true };
|
|
198
|
+
seen.add(value);
|
|
199
|
+
}
|
|
200
|
+
return value;
|
|
201
|
+
});
|
|
202
|
+
return json === void 0 ? { __opaqueJavaScriptValue: true } : JSON.parse(json);
|
|
203
|
+
}
|
|
204
|
+
function hydrateValue(value) {
|
|
205
|
+
if (value && typeof value === "object") {
|
|
206
|
+
if (Object.keys(value).length === 1 && typeof value.__nativeNumber === "string") return Number(value.__nativeNumber);
|
|
207
|
+
for (const key of Object.keys(value)) value[key] = hydrateValue(value[key]);
|
|
208
|
+
}
|
|
209
|
+
return value;
|
|
210
|
+
}
|
|
211
|
+
function dataObject(selection, id) {
|
|
212
|
+
const document = selection[require_nodes.DOCUMENT];
|
|
213
|
+
let data = document.nodeData?.(id);
|
|
214
|
+
if (!data) {
|
|
215
|
+
const dispatch = document.nodeDataOperation?.(id) ?? ((command, args) => JSON.parse(document.op(command, JSON.stringify(args))));
|
|
216
|
+
data = new Proxy({}, {
|
|
217
|
+
set(object, name, value) {
|
|
218
|
+
Reflect.set(object, name, value);
|
|
219
|
+
if (typeof name === "string") dispatch("data", {
|
|
220
|
+
ids: [id],
|
|
221
|
+
name,
|
|
222
|
+
value: nativeValue(value)
|
|
223
|
+
});
|
|
224
|
+
return true;
|
|
225
|
+
},
|
|
226
|
+
defineProperty(object, name, descriptor) {
|
|
227
|
+
Reflect.defineProperty(object, name, descriptor);
|
|
228
|
+
if (typeof name === "string" && "value" in descriptor) dispatch("data", {
|
|
229
|
+
ids: [id],
|
|
230
|
+
name,
|
|
231
|
+
value: nativeValue(descriptor.value)
|
|
232
|
+
});
|
|
233
|
+
return true;
|
|
234
|
+
},
|
|
235
|
+
deleteProperty(object, name) {
|
|
236
|
+
Reflect.deleteProperty(object, name);
|
|
237
|
+
if (typeof name === "string") dispatch("removeData", {
|
|
238
|
+
ids: [id],
|
|
239
|
+
name
|
|
240
|
+
});
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
document.retainNodeData?.(id, data);
|
|
245
|
+
}
|
|
246
|
+
const node = require_nodes.getNode(selection[require_nodes.DOCUMENT], id);
|
|
247
|
+
if (!Object.hasOwn(node, "data")) {
|
|
248
|
+
const values = data;
|
|
249
|
+
Object.defineProperty(node, "data", {
|
|
250
|
+
configurable: true,
|
|
251
|
+
enumerable: true,
|
|
252
|
+
get: () => values,
|
|
253
|
+
set: (value) => {
|
|
254
|
+
for (const key of Object.keys(values)) delete values[key];
|
|
255
|
+
for (const key of Object.keys(value ?? {})) values[key] = value[key];
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return data;
|
|
260
|
+
}
|
|
261
|
+
function property(selection, id, name, raw = false) {
|
|
262
|
+
const result = selection.op("prop", {
|
|
263
|
+
ids: [id],
|
|
264
|
+
name,
|
|
265
|
+
...raw ? { raw: true } : {}
|
|
266
|
+
});
|
|
267
|
+
if (name === "sourceCodeLocation") return require_nodes.hydrateLocation(result);
|
|
268
|
+
return result?.__nativeNodeProperty ? require_nodes.getNode(selection[require_nodes.DOCUMENT], id)[result.__nativeNodeProperty] : result === null ? void 0 : result;
|
|
269
|
+
}
|
|
270
|
+
function propertyValue(selection, name, value) {
|
|
271
|
+
if (name === "children" || name === "childNodes") return require_nodes.importNodes(selection[require_nodes.DOCUMENT], value);
|
|
272
|
+
if ([
|
|
273
|
+
"parent",
|
|
274
|
+
"parentNode",
|
|
275
|
+
"prev",
|
|
276
|
+
"previousSibling",
|
|
277
|
+
"next",
|
|
278
|
+
"nextSibling"
|
|
279
|
+
].includes(name)) return value == null ? null : require_nodes.importNodes(selection[require_nodes.DOCUMENT], [value])[0] ?? null;
|
|
280
|
+
return value;
|
|
281
|
+
}
|
|
282
|
+
function attr(name, value) {
|
|
283
|
+
if (typeof name === "object" || value !== void 0) {
|
|
284
|
+
if (typeof value === "function") {
|
|
285
|
+
if (typeof name !== "string") throw new Error("Bad combination of arguments.");
|
|
286
|
+
this.ids.forEach((id, index) => {
|
|
287
|
+
if (!isElement(this, id)) return;
|
|
288
|
+
const node = require_nodes.getNode(this[require_nodes.DOCUMENT], id);
|
|
289
|
+
const current = node.attribs[name];
|
|
290
|
+
const next = value.call(node, index, current);
|
|
291
|
+
setAttribute(this, [id], name, next === null ? null : String(next));
|
|
292
|
+
});
|
|
293
|
+
} else if (typeof name === "object") for (const key of Object.keys(name)) setAttribute(this, this.ids, key, name[key] === null ? null : String(name[key]));
|
|
294
|
+
else setAttribute(this, this.ids, name, value === null ? null : String(value));
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
if (arguments.length > 1) return this;
|
|
298
|
+
if (typeof name === "string") {
|
|
299
|
+
const cached = readAttribute(this, name);
|
|
300
|
+
if (cached === absent) return void 0;
|
|
301
|
+
if (cached !== deferred) return cached;
|
|
302
|
+
}
|
|
303
|
+
const result = this.op("attr", {
|
|
304
|
+
ids: this.ids,
|
|
305
|
+
...typeof name === "string" ? { name } : {}
|
|
306
|
+
});
|
|
307
|
+
if (result === null) return void 0;
|
|
308
|
+
return !name && this.ids.length ? require_nodes.getNode(this[require_nodes.DOCUMENT], this.ids[0]).attribs : result;
|
|
309
|
+
}
|
|
310
|
+
function prop(name, value) {
|
|
311
|
+
if (typeof name === "string" && value === void 0) return this.ids.length ? property(this, this.ids[0], name) : void 0;
|
|
312
|
+
if (typeof name === "object" || value !== void 0) {
|
|
313
|
+
if (typeof value === "function") {
|
|
314
|
+
if (typeof name === "object") throw new TypeError("Bad combination of arguments.");
|
|
315
|
+
this.ids.forEach((id, index) => {
|
|
316
|
+
if (!isElement(this, id)) return;
|
|
317
|
+
const next = value.call(require_nodes.getNode(this[require_nodes.DOCUMENT], id), index, property(this, id, name, true));
|
|
318
|
+
this.op("prop", {
|
|
319
|
+
ids: [id],
|
|
320
|
+
name,
|
|
321
|
+
value: propertyValue(this, name, next)
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
} else if (typeof name === "object") for (const key of Object.keys(name)) this.op("prop", {
|
|
325
|
+
ids: this.ids,
|
|
326
|
+
name: key,
|
|
327
|
+
value: propertyValue(this, key, name[key])
|
|
328
|
+
});
|
|
329
|
+
else this.op("prop", {
|
|
330
|
+
ids: this.ids,
|
|
331
|
+
name,
|
|
332
|
+
value: propertyValue(this, name, value)
|
|
333
|
+
});
|
|
334
|
+
return this;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function data(name, value) {
|
|
338
|
+
const first = this.ids[0];
|
|
339
|
+
if (first === void 0 || !isElement(this, first)) return void 0;
|
|
340
|
+
if (name != null && (typeof name === "object" || value !== void 0)) {
|
|
341
|
+
for (const id of this.ids) {
|
|
342
|
+
if (!isElement(this, id)) continue;
|
|
343
|
+
const record = dataObject(this, id);
|
|
344
|
+
if (typeof name === "object") for (const key of Object.keys(name)) record[key] = name[key];
|
|
345
|
+
else if (typeof name === "string") record[name] = value;
|
|
346
|
+
}
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
const record = dataObject(this, first);
|
|
350
|
+
if (typeof name === "string" && Object.hasOwn(record, name)) return record[name];
|
|
351
|
+
const found = this.op("data", {
|
|
352
|
+
ids: [first],
|
|
353
|
+
...name == null ? {} : { name }
|
|
354
|
+
});
|
|
355
|
+
if (!found.found) return void 0;
|
|
356
|
+
if (name == null) {
|
|
357
|
+
for (const key of Object.keys(found.value)) if (!Object.hasOwn(record, key)) record[key] = hydrateValue(found.value[key]);
|
|
358
|
+
return record;
|
|
359
|
+
}
|
|
360
|
+
record[name] = hydrateValue(found.value);
|
|
361
|
+
return record[name];
|
|
362
|
+
}
|
|
363
|
+
function css(name, value) {
|
|
364
|
+
if (name != null && value != null || typeof name === "object" && !Array.isArray(name)) {
|
|
365
|
+
if (typeof name === "object") for (const [propertyIndex, key] of Object.keys(name).entries()) if (typeof name[key] === "function") for (const id of this.ids) {
|
|
366
|
+
if (!isElement(this, id)) continue;
|
|
367
|
+
const current = this.op("css", {
|
|
368
|
+
ids: [id],
|
|
369
|
+
name: key
|
|
370
|
+
}) ?? void 0;
|
|
371
|
+
const next = name[key].call(require_nodes.getNode(this[require_nodes.DOCUMENT], id), propertyIndex, current);
|
|
372
|
+
this.op("css", {
|
|
373
|
+
ids: [id],
|
|
374
|
+
name: key,
|
|
375
|
+
value: next == null ? null : String(next)
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
else this.op("css", {
|
|
379
|
+
ids: this.ids,
|
|
380
|
+
name: key,
|
|
381
|
+
value: name[key] == null ? null : String(name[key])
|
|
382
|
+
});
|
|
383
|
+
else if (typeof name === "string" && typeof value === "function") this.ids.forEach((id, index) => {
|
|
384
|
+
if (!isElement(this, id)) return;
|
|
385
|
+
const current = this.op("css", {
|
|
386
|
+
ids: [id],
|
|
387
|
+
name
|
|
388
|
+
}) ?? void 0;
|
|
389
|
+
const next = value.call(require_nodes.getNode(this[require_nodes.DOCUMENT], id), index, current);
|
|
390
|
+
this.op("css", {
|
|
391
|
+
ids: [id],
|
|
392
|
+
name,
|
|
393
|
+
value: next == null ? null : String(next)
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
else if (typeof name === "string") this.op("css", {
|
|
397
|
+
ids: this.ids,
|
|
398
|
+
name,
|
|
399
|
+
value: value == null ? null : String(value)
|
|
400
|
+
});
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
return this.op("css", {
|
|
404
|
+
ids: this.ids,
|
|
405
|
+
...name === void 0 ? {} : { name }
|
|
406
|
+
}) ?? void 0;
|
|
407
|
+
}
|
|
408
|
+
function classMethod(command) {
|
|
409
|
+
return function(name, state) {
|
|
410
|
+
if (typeof name === "function") this.ids.forEach((id, index) => {
|
|
411
|
+
if (!isElement(this, id)) return;
|
|
412
|
+
const node = require_nodes.getNode(this[require_nodes.DOCUMENT], id);
|
|
413
|
+
const next = command === "toggleClass" ? name.call(node, index, node.attribs.class ?? "", state) : name.call(node, index, node.attribs.class ?? "");
|
|
414
|
+
if (typeof next === "string") this.op(command, {
|
|
415
|
+
ids: [id],
|
|
416
|
+
name: next,
|
|
417
|
+
...typeof state === "boolean" ? { state } : {}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
else if (typeof name === "string" || command === "removeClass" && arguments.length === 0) this.op(command, {
|
|
421
|
+
ids: this.ids,
|
|
422
|
+
...typeof name === "string" ? { name } : { all: true },
|
|
423
|
+
...typeof state === "boolean" ? { state } : {}
|
|
424
|
+
});
|
|
425
|
+
return this;
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
function installAttributes(CheerioClass) {
|
|
429
|
+
Object.assign(CheerioClass.prototype, {
|
|
430
|
+
attr,
|
|
431
|
+
prop,
|
|
432
|
+
data,
|
|
433
|
+
css,
|
|
434
|
+
removeAttr(name) {
|
|
435
|
+
const ids = this.ids;
|
|
436
|
+
for (const single of String(name).split(/\s+/)) if (single) setAttribute(this, ids, single, null);
|
|
437
|
+
return this;
|
|
438
|
+
},
|
|
439
|
+
hasClass(name) {
|
|
440
|
+
return this.op("hasClass", {
|
|
441
|
+
ids: this.ids,
|
|
442
|
+
name
|
|
443
|
+
});
|
|
444
|
+
},
|
|
445
|
+
addClass: classMethod("addClass"),
|
|
446
|
+
removeClass: classMethod("removeClass"),
|
|
447
|
+
toggleClass: classMethod("toggleClass"),
|
|
448
|
+
val(value) {
|
|
449
|
+
if (arguments.length === 0) return this.op("val", { ids: this.ids }) ?? void 0;
|
|
450
|
+
const result = this.op("val", {
|
|
451
|
+
ids: this.ids,
|
|
452
|
+
value: value === void 0 ? { __undefinedJavaScriptValue: true } : value
|
|
453
|
+
});
|
|
454
|
+
return result?.chain === false ? result.returnValue : result === false ? void 0 : this;
|
|
455
|
+
},
|
|
456
|
+
serializeArray() {
|
|
457
|
+
return this.op("serializeArray", { ids: this.ids });
|
|
458
|
+
},
|
|
459
|
+
serialize() {
|
|
460
|
+
return this.op("serialize", { ids: this.ids });
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
// src/parse.ts
|
|
465
|
+
/** Parse with the Rust HTML5 or HTML/XML engine, preserving fragment context. */
|
|
466
|
+
function parseWithNative(content, options, isDocument, context) {
|
|
467
|
+
if (!isDocument && context) {
|
|
468
|
+
const document = require_nodes.nodeBinding(context)?.document ?? require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument("", false, JSON.stringify(require_nodes.cleanParserOptions(options))), options);
|
|
469
|
+
const contextId = require_nodes.importNodes(document, [context])[0];
|
|
470
|
+
const children = require_nodes.operation(document, "create", {
|
|
471
|
+
html: content,
|
|
472
|
+
contextId,
|
|
473
|
+
options
|
|
474
|
+
});
|
|
475
|
+
const root = require_nodes.operation(document, "createNode", {
|
|
476
|
+
type: "root",
|
|
477
|
+
options
|
|
478
|
+
});
|
|
479
|
+
require_nodes.operation(document, "setNode", {
|
|
480
|
+
id: root,
|
|
481
|
+
field: "children",
|
|
482
|
+
value: children,
|
|
483
|
+
options
|
|
484
|
+
});
|
|
485
|
+
return require_nodes.getNode(document, root);
|
|
486
|
+
}
|
|
487
|
+
const document = require_nodes.parseNativeDocument(content, options, isDocument);
|
|
488
|
+
return require_nodes.getNode(document, document.rootId());
|
|
489
|
+
}
|
|
490
|
+
/** The HTML5 adapter uses Rust's HTML5 tree builder. */
|
|
491
|
+
function parseWithParse5(content, options, isDocument, context) {
|
|
492
|
+
return parseWithNative(content, {
|
|
493
|
+
...options,
|
|
494
|
+
_useHtmlParser2: false,
|
|
495
|
+
xml: false,
|
|
496
|
+
xmlMode: false
|
|
497
|
+
}, isDocument, context);
|
|
498
|
+
}
|
|
499
|
+
/** The htmlparser2-compatible adapter uses the Rust HTML/XML tokenizer. */
|
|
500
|
+
function parseWithHtmlparser2(content, options) {
|
|
501
|
+
return parseWithNative(content, {
|
|
502
|
+
...options,
|
|
503
|
+
_useHtmlParser2: true
|
|
504
|
+
}, false, null);
|
|
505
|
+
}
|
|
506
|
+
/** Normalize parser inputs while keeping every DOM operation in the native arena. */
|
|
507
|
+
function getParse(parser) {
|
|
508
|
+
return function parse(content, options, isDocument, context) {
|
|
509
|
+
if (Buffer.isBuffer(content)) content = content.toString();
|
|
510
|
+
if (typeof content === "string") return parser(content, options, isDocument, context);
|
|
511
|
+
if (!Array.isArray(content) && content.type === "root" && require_nodes.nodeBinding(content)) return content;
|
|
512
|
+
const document = require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument("", false, JSON.stringify(require_nodes.cleanParserOptions(options))), options);
|
|
513
|
+
const ids = require_nodes.importNodes(document, content);
|
|
514
|
+
if (!Array.isArray(content) && content.type === "root") return require_nodes.getNode(document, ids[0]);
|
|
515
|
+
require_nodes.operation(document, "setNode", {
|
|
516
|
+
id: document.rootId(),
|
|
517
|
+
field: "children",
|
|
518
|
+
value: ids,
|
|
519
|
+
options
|
|
520
|
+
});
|
|
521
|
+
return require_nodes.getNode(document, document.rootId());
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
const parse$1 = getParse((content, options, isDocument, context) => options._useHtmlParser2 ? parseWithHtmlparser2(content, options) : parseWithParse5(content, options, isDocument, context));
|
|
525
|
+
// src/match.ts
|
|
526
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
527
|
+
const compiledLimit = 512;
|
|
528
|
+
/**
|
|
529
|
+
* Matches `ids` against selectors simple enough to evaluate on cached records,
|
|
530
|
+
* with the native engine's semantics; `undefined` means the caller must ask
|
|
531
|
+
* the native engine (any other syntax, foreign documents or parser options).
|
|
532
|
+
*/
|
|
533
|
+
function matchSimpleSelector(document, ids, selector, options) {
|
|
534
|
+
if (!require_nodes.isRouted(document)) return void 0;
|
|
535
|
+
const compounds = compile(selector);
|
|
536
|
+
if (!compounds) return void 0;
|
|
537
|
+
const isXml = isXmlMode(options);
|
|
538
|
+
const isLowerCaseTags = options.lowerCaseTags ?? !isXml;
|
|
539
|
+
const isLowerCaseAttributes = options.lowerCaseAttributeNames ?? !isXml;
|
|
540
|
+
const result = [];
|
|
541
|
+
for (const id of ids) {
|
|
542
|
+
const state = require_nodes.stateIn(document, id);
|
|
543
|
+
if (!sameMatchOptions(state.options, isXml, isLowerCaseTags, isLowerCaseAttributes)) return void 0;
|
|
544
|
+
const local = id - state.base;
|
|
545
|
+
for (const compound of compounds) if (matchesCompound(state, local, compound, isLowerCaseTags, isLowerCaseAttributes)) {
|
|
546
|
+
result.push(id);
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return result;
|
|
551
|
+
}
|
|
552
|
+
/** Nodes a single `find()` walks in JavaScript before leaving larger subtrees to the native engine. */
|
|
553
|
+
const walkBudget = 4096;
|
|
554
|
+
/** Documents small enough that walking all of them in JavaScript beats a native call. */
|
|
555
|
+
const smallDocumentNodes = 512;
|
|
556
|
+
/** Documents small enough that taking a snapshot to walk them still beats a native call. */
|
|
557
|
+
const tinyDocumentNodes = 64;
|
|
558
|
+
/**
|
|
559
|
+
* Selects the descendants of one element, or of the root of a small
|
|
560
|
+
* document, that match a simple selector, in document order, as the native
|
|
561
|
+
* `select` does; `undefined` defers to it.
|
|
562
|
+
*/
|
|
563
|
+
function selectSimpleWithin(document, contexts, selector, options) {
|
|
564
|
+
if (contexts.length !== 1 || !require_nodes.isRouted(document)) return void 0;
|
|
565
|
+
const compounds = compile(selector);
|
|
566
|
+
if (!compounds) return void 0;
|
|
567
|
+
const isXml = isXmlMode(options);
|
|
568
|
+
const isLowerCaseTags = options.lowerCaseTags ?? !isXml;
|
|
569
|
+
const isLowerCaseAttributes = options.lowerCaseAttributeNames ?? !isXml;
|
|
570
|
+
const state = require_nodes.stateIn(document, contexts[0]);
|
|
571
|
+
if (!sameMatchOptions(state.options, isXml, isLowerCaseTags, isLowerCaseAttributes)) return void 0;
|
|
572
|
+
const context = contexts[0] - state.base;
|
|
573
|
+
const size = Math.max(state.baseline, state.snapshotSize);
|
|
574
|
+
const contextType = context === (state.rootLocal ??= state.raw.rootId()) ? "root" : typeAt(state, context);
|
|
575
|
+
const isReadable = state.bulk instanceof require_nodes.NodeSnapshot ? size <= smallDocumentNodes : state.generation === 0 && size <= tinyDocumentNodes;
|
|
576
|
+
const isSmallRoot = contextType === "root" && isReadable;
|
|
577
|
+
if (!isElementType(contextType) && !isSmallRoot) return void 0;
|
|
578
|
+
const result = [];
|
|
579
|
+
const stack = [];
|
|
580
|
+
pushChildren(state, context, stack);
|
|
581
|
+
for (let visited = 0; stack.length; visited++) {
|
|
582
|
+
if (visited === walkBudget) return void 0;
|
|
583
|
+
const local = stack.pop();
|
|
584
|
+
const type = typeAt(state, local);
|
|
585
|
+
if (type === "root") {
|
|
586
|
+
if (isSmallRoot) return void 0;
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
if (isElementType(type)) {
|
|
590
|
+
for (const compound of compounds) if (matchesCompound(state, local, compound, isLowerCaseTags, isLowerCaseAttributes)) {
|
|
591
|
+
result.push(state.base + local);
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
pushChildren(state, local, stack);
|
|
596
|
+
}
|
|
597
|
+
return result;
|
|
598
|
+
}
|
|
599
|
+
const isElementType = (type) => type === "tag" || type === "script" || type === "style";
|
|
600
|
+
function typeAt(state, local) {
|
|
601
|
+
const snapshot = require_nodes.structureSnapshot(state, local);
|
|
602
|
+
return snapshot ? snapshot.typeOf(local) : require_nodes.recordAt(state, local)?.type;
|
|
603
|
+
}
|
|
604
|
+
/** Pushes children last to first, so they pop in document order. */
|
|
605
|
+
function pushChildren(state, local, stack) {
|
|
606
|
+
const snapshot = require_nodes.structureSnapshot(state, local);
|
|
607
|
+
if (snapshot) {
|
|
608
|
+
for (let index = snapshot.childCount(local) - 1; index >= 0; index--) stack.push(snapshot.childAt(local, index));
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const children = require_nodes.recordAt(state, local)?.children ?? [];
|
|
612
|
+
for (let index = children.length - 1; index >= 0; index--) stack.push(children[index]);
|
|
613
|
+
}
|
|
614
|
+
function isXmlMode(options) {
|
|
615
|
+
return options.xmlMode ?? (options.xml === true || typeof options.xml === "object" && options.xml !== null);
|
|
616
|
+
}
|
|
617
|
+
/** Records were parsed with the document's options; matching must use the same name casing. */
|
|
618
|
+
function sameMatchOptions(documentOptions, isXml, isLowerCaseTags, isLowerCaseAttributes) {
|
|
619
|
+
const isDocumentXml = isXmlMode(documentOptions);
|
|
620
|
+
return isDocumentXml === isXml && (documentOptions.lowerCaseTags ?? !isDocumentXml) === isLowerCaseTags && (documentOptions.lowerCaseAttributeNames ?? !isDocumentXml) === isLowerCaseAttributes;
|
|
621
|
+
}
|
|
622
|
+
function matchesCompound(state, local, compound, isLowerCaseTags, isLowerCaseAttributes) {
|
|
623
|
+
const snapshot = require_nodes.structureSnapshot(state, local);
|
|
624
|
+
const record = snapshot ? void 0 : require_nodes.recordAt(state, local);
|
|
625
|
+
if (!snapshot && !record) return false;
|
|
626
|
+
if (!isElementType(snapshot ? snapshot.typeOf(local) : record.type)) return false;
|
|
627
|
+
if (compound.tag !== void 0 && (snapshot ? snapshot.nameOf(local) : record.name) !== (isLowerCaseTags ? compound.lowerTag : compound.tag)) return false;
|
|
628
|
+
for (const id of compound.ids) if (require_nodes.attributeAt(state, local, "id") !== id) return false;
|
|
629
|
+
if (compound.classes.length) {
|
|
630
|
+
const classes = require_nodes.attributeAt(state, local, "class");
|
|
631
|
+
if (classes === void 0) return false;
|
|
632
|
+
for (const name of compound.classes) if (!hasClass(classes, name)) return false;
|
|
633
|
+
}
|
|
634
|
+
for (const name of isLowerCaseAttributes ? compound.lowerAttributes : compound.attributes) if (require_nodes.attributeAt(state, local, name) === void 0) return false;
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
const isAsciiWhitespace = (code) => code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
|
|
638
|
+
/** Whether `name` is one of the ASCII-whitespace-separated tokens of `classes`. */
|
|
639
|
+
function hasClass(classes, name) {
|
|
640
|
+
for (let start = classes.indexOf(name); start !== -1; start = classes.indexOf(name, start + 1)) {
|
|
641
|
+
const end = start + name.length;
|
|
642
|
+
if ((start === 0 || isAsciiWhitespace(classes.charCodeAt(start - 1))) && (end === classes.length || isAsciiWhitespace(classes.charCodeAt(end)))) return true;
|
|
643
|
+
}
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
function asciiLowerCase(value) {
|
|
647
|
+
return /[A-Z]/.test(value) ? value.replace(/[A-Z]/g, (letter) => letter.toLowerCase()) : value;
|
|
648
|
+
}
|
|
649
|
+
function compile(selector) {
|
|
650
|
+
const cached = compiled.get(selector);
|
|
651
|
+
if (cached !== void 0) return cached;
|
|
652
|
+
const result = parse(selector);
|
|
653
|
+
if (compiled.size >= compiledLimit) compiled.clear();
|
|
654
|
+
compiled.set(selector, result);
|
|
655
|
+
return result;
|
|
656
|
+
}
|
|
657
|
+
const identifierStart = /[A-Za-z_]/;
|
|
658
|
+
const identifierPart = /[A-Za-z0-9_-]/;
|
|
659
|
+
/** Parses the supported subset; anything else (escapes, combinators, pseudos, values) yields `null`. */
|
|
660
|
+
function parse(selector) {
|
|
661
|
+
const compounds = [];
|
|
662
|
+
let index = 0;
|
|
663
|
+
const skipSpace = () => {
|
|
664
|
+
while (index < selector.length && isAsciiWhitespace(selector.charCodeAt(index))) index++;
|
|
665
|
+
};
|
|
666
|
+
const identifier = () => {
|
|
667
|
+
const start = index;
|
|
668
|
+
if (selector[index] === "-") index++;
|
|
669
|
+
if (!identifierStart.test(selector[index] ?? "")) return null;
|
|
670
|
+
while (index < selector.length && identifierPart.test(selector[index])) index++;
|
|
671
|
+
return selector.slice(start, index);
|
|
672
|
+
};
|
|
673
|
+
for (;;) {
|
|
674
|
+
skipSpace();
|
|
675
|
+
const compound = {
|
|
676
|
+
tag: void 0,
|
|
677
|
+
lowerTag: void 0,
|
|
678
|
+
ids: [],
|
|
679
|
+
classes: [],
|
|
680
|
+
attributes: [],
|
|
681
|
+
lowerAttributes: []
|
|
682
|
+
};
|
|
683
|
+
let hasPart = false;
|
|
684
|
+
if (selector[index] === "*") {
|
|
685
|
+
index++;
|
|
686
|
+
hasPart = true;
|
|
687
|
+
} else if (/[A-Za-z]/.test(selector[index] ?? "")) {
|
|
688
|
+
compound.tag = identifier();
|
|
689
|
+
hasPart = true;
|
|
690
|
+
}
|
|
691
|
+
for (;;) {
|
|
692
|
+
const character = selector[index];
|
|
693
|
+
if (character === "#" || character === ".") {
|
|
694
|
+
index++;
|
|
695
|
+
const name = identifier();
|
|
696
|
+
if (name === null) return null;
|
|
697
|
+
(character === "#" ? compound.ids : compound.classes).push(name);
|
|
698
|
+
} else if (character === "[") {
|
|
699
|
+
index++;
|
|
700
|
+
skipSpace();
|
|
701
|
+
const name = identifier();
|
|
702
|
+
if (name === null) return null;
|
|
703
|
+
skipSpace();
|
|
704
|
+
if (selector[index] !== "]") return null;
|
|
705
|
+
index++;
|
|
706
|
+
compound.attributes.push(name);
|
|
707
|
+
} else break;
|
|
708
|
+
hasPart = true;
|
|
709
|
+
}
|
|
710
|
+
if (!hasPart) return null;
|
|
711
|
+
compound.lowerTag = compound.tag === void 0 ? void 0 : asciiLowerCase(compound.tag);
|
|
712
|
+
compound.lowerAttributes = compound.attributes.map(asciiLowerCase);
|
|
713
|
+
compounds.push(compound);
|
|
714
|
+
skipSpace();
|
|
715
|
+
if (index === selector.length) return compounds;
|
|
716
|
+
if (selector[index] !== ",") return null;
|
|
717
|
+
index++;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
// src/selection.ts
|
|
721
|
+
const numeric = /^(?:0|[1-9]\d*)$/;
|
|
722
|
+
const isMarkup = (value) => /<[a-zA-Z!/?]/.test(value);
|
|
723
|
+
function* customPseudoArguments(selector, name) {
|
|
724
|
+
let attributeDepth = 0;
|
|
725
|
+
let quote = "";
|
|
726
|
+
for (let index = 0; index < selector.length; index++) {
|
|
727
|
+
const character = selector[index];
|
|
728
|
+
if (character === "\\") {
|
|
729
|
+
index++;
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
if (quote) {
|
|
733
|
+
if (character === quote) quote = "";
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
if (character === "\"" || character === "'") {
|
|
737
|
+
quote = character;
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
if (character === "[") {
|
|
741
|
+
attributeDepth++;
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
if (character === "]") {
|
|
745
|
+
attributeDepth--;
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
if (attributeDepth || character !== ":" || selector[index - 1] === ":") continue;
|
|
749
|
+
const start = index + 1;
|
|
750
|
+
let end = start;
|
|
751
|
+
while (/[\w-]/.test(selector[end] ?? "") && end < selector.length) end++;
|
|
752
|
+
if (selector.slice(start, end).toLowerCase() !== name.toLowerCase()) continue;
|
|
753
|
+
index = end - 1;
|
|
754
|
+
if (selector[end] !== "(") {
|
|
755
|
+
yield void 0;
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
const argumentStart = end + 1;
|
|
759
|
+
let depth = 1;
|
|
760
|
+
let argumentQuote = "";
|
|
761
|
+
for (end++; end < selector.length; end++) {
|
|
762
|
+
const next = selector[end];
|
|
763
|
+
if (next === "\\") {
|
|
764
|
+
end++;
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
if (argumentQuote) {
|
|
768
|
+
if (next === argumentQuote) argumentQuote = "";
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
if (next === "\"" || next === "'") {
|
|
772
|
+
argumentQuote = next;
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
if (next === "(") depth++;
|
|
776
|
+
else if (next === ")" && --depth === 0) break;
|
|
777
|
+
}
|
|
778
|
+
if (depth) continue;
|
|
779
|
+
yield selector.slice(argumentStart, end);
|
|
780
|
+
index = end;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
const isIndex = (property) => {
|
|
784
|
+
if (typeof property !== "string") return false;
|
|
785
|
+
const first = property.charCodeAt(0);
|
|
786
|
+
return first >= 48 && first <= 57 && numeric.test(property);
|
|
787
|
+
};
|
|
788
|
+
/** Results up to this size are plain objects holding their nodes, like Cheerio's own selections. */
|
|
789
|
+
const plainSelectionLimit = 5;
|
|
790
|
+
const lengthOf = (target) => target[require_nodes.VALUES]?.length ?? target[require_nodes.IDS].length;
|
|
791
|
+
function nodeAt(target, index) {
|
|
792
|
+
if (target[require_nodes.VALUES]) return target[require_nodes.VALUES][index];
|
|
793
|
+
const id = target[require_nodes.IDS][index];
|
|
794
|
+
return id === void 0 ? void 0 : require_nodes.getNode(target[require_nodes.DOCUMENT], id);
|
|
795
|
+
}
|
|
796
|
+
function releaseIds(target) {
|
|
797
|
+
if (target[require_nodes.LEASE]) require_nodes.updateLease(target[require_nodes.DOCUMENT], target[require_nodes.LEASE], target[require_nodes.VALUES] ? [] : target[require_nodes.IDS]);
|
|
798
|
+
}
|
|
799
|
+
function writeIndex(target, index, value) {
|
|
800
|
+
if (target[require_nodes.VALUES]) target[require_nodes.VALUES][index] = value;
|
|
801
|
+
else {
|
|
802
|
+
const id = require_nodes.importNodes(target[require_nodes.DOCUMENT], [value])[0];
|
|
803
|
+
if (id === void 0) {
|
|
804
|
+
target[require_nodes.VALUES] = target[require_nodes.IDS].map((existing) => require_nodes.getNode(target[require_nodes.DOCUMENT], existing));
|
|
805
|
+
target[require_nodes.VALUES][index] = value;
|
|
806
|
+
} else target[require_nodes.IDS][index] = id;
|
|
807
|
+
}
|
|
808
|
+
releaseIds(target);
|
|
809
|
+
}
|
|
810
|
+
/** Large selections resolve nodes only when an index is read. */
|
|
811
|
+
const selectionHandler = {
|
|
812
|
+
get: (target, property) => isIndex(property) ? nodeAt(target, Number(property)) : property === "length" ? lengthOf(target) : Reflect.get(target, property),
|
|
813
|
+
set: (target, property, value) => {
|
|
814
|
+
if (isIndex(property)) {
|
|
815
|
+
writeIndex(target, Number(property), value);
|
|
816
|
+
return true;
|
|
817
|
+
}
|
|
818
|
+
if (property === "length") {
|
|
819
|
+
if (target[require_nodes.VALUES]) target[require_nodes.VALUES].length = value;
|
|
820
|
+
else target[require_nodes.IDS].length = value;
|
|
821
|
+
releaseIds(target);
|
|
822
|
+
return true;
|
|
823
|
+
}
|
|
824
|
+
return Reflect.set(target, property, value);
|
|
825
|
+
},
|
|
826
|
+
has: (target, property) => isIndex(property) ? Number(property) < lengthOf(target) : property === "length" || Reflect.has(target, property),
|
|
827
|
+
ownKeys: (target) => [
|
|
828
|
+
...Array.from({ length: lengthOf(target) }, (_, i) => String(i)),
|
|
829
|
+
"length",
|
|
830
|
+
...Reflect.ownKeys(target)
|
|
831
|
+
],
|
|
832
|
+
getOwnPropertyDescriptor: (target, property) => {
|
|
833
|
+
if (isIndex(property) && Number(property) < lengthOf(target)) return {
|
|
834
|
+
value: nodeAt(target, Number(property)),
|
|
835
|
+
writable: true,
|
|
836
|
+
configurable: true,
|
|
837
|
+
enumerable: true
|
|
838
|
+
};
|
|
839
|
+
if (property === "length") return {
|
|
840
|
+
value: lengthOf(target),
|
|
841
|
+
writable: true,
|
|
842
|
+
configurable: true,
|
|
843
|
+
enumerable: true
|
|
844
|
+
};
|
|
845
|
+
return Reflect.getOwnPropertyDescriptor(target, property);
|
|
846
|
+
},
|
|
847
|
+
deleteProperty: (target, property) => {
|
|
848
|
+
if (isIndex(property)) {
|
|
849
|
+
if (target[require_nodes.VALUES]) delete target[require_nodes.VALUES][Number(property)];
|
|
850
|
+
else delete target[require_nodes.IDS][Number(property)];
|
|
851
|
+
releaseIds(target);
|
|
852
|
+
return true;
|
|
853
|
+
}
|
|
854
|
+
return Reflect.deleteProperty(target, property);
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
/** An array-like selection of native nodes. */
|
|
858
|
+
var Cheerio = class {
|
|
859
|
+
/** @internal */ get _document() {
|
|
860
|
+
return this[require_nodes.DOCUMENT];
|
|
861
|
+
}
|
|
862
|
+
/** @internal */ get _api() {
|
|
863
|
+
return this[require_nodes.API];
|
|
864
|
+
}
|
|
865
|
+
/** @internal */ get _ids() {
|
|
866
|
+
return this[require_nodes.IDS];
|
|
867
|
+
}
|
|
868
|
+
constructor(api, document, ids = [], root = null, options = {}, values) {
|
|
869
|
+
this[require_nodes.DOCUMENT] = document;
|
|
870
|
+
this[require_nodes.API] = api;
|
|
871
|
+
this[require_nodes.IDS] = ids;
|
|
872
|
+
this[require_nodes.VALUES] = values;
|
|
873
|
+
this[require_nodes.LEASE] = void 0;
|
|
874
|
+
const count = values ? values.length : ids.length;
|
|
875
|
+
const isPlain = count <= plainSelectionLimit;
|
|
876
|
+
this[require_nodes.IS_PLAIN] = isPlain;
|
|
877
|
+
if (isPlain) this.length = count;
|
|
878
|
+
this.options = options;
|
|
879
|
+
this._root = root;
|
|
880
|
+
if (isPlain) {
|
|
881
|
+
if (values) for (let index = 0; index < count; index++) this[index] = values[index];
|
|
882
|
+
else for (let index = 0; index < count; index++) this[index] = require_nodes.getNode(document, ids[index]);
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
if (!values) this[require_nodes.LEASE] = require_nodes.leaseNodes(document, this, ids);
|
|
886
|
+
return new Proxy(this, selectionHandler);
|
|
887
|
+
}
|
|
888
|
+
/** @internal Re-reads a plain selection after direct index writes or after its nodes moved documents. */
|
|
889
|
+
_syncIndices() {
|
|
890
|
+
const length = this.length;
|
|
891
|
+
const values = this[require_nodes.VALUES];
|
|
892
|
+
const ids = this[require_nodes.IDS];
|
|
893
|
+
let isChanged = (values ?? ids).length !== length;
|
|
894
|
+
for (let index = 0; !isChanged && index < length; index++) isChanged = values ? this[index] !== values[index] : require_nodes.viewId(this[index]) !== ids[index];
|
|
895
|
+
if (!isChanged) return;
|
|
896
|
+
const items = Array.from({ length }, (_unused, index) => this[index]);
|
|
897
|
+
const isAllNodes = items.every((value) => value && typeof value === "object" && typeof value.type === "string");
|
|
898
|
+
this[require_nodes.VALUES] = isAllNodes ? void 0 : items;
|
|
899
|
+
this[require_nodes.IDS] = isAllNodes ? require_nodes.importNodes(this[require_nodes.DOCUMENT], items) : [];
|
|
900
|
+
}
|
|
901
|
+
/** @internal */ get ids() {
|
|
902
|
+
if (this[require_nodes.IS_PLAIN]) this._syncIndices();
|
|
903
|
+
return this[require_nodes.VALUES] ? require_nodes.importNodes(this[require_nodes.DOCUMENT], this[require_nodes.VALUES]) : this[require_nodes.IDS];
|
|
904
|
+
}
|
|
905
|
+
/** @internal */ op(command, args = {}) {
|
|
906
|
+
const values = args;
|
|
907
|
+
return require_nodes.operation(this[require_nodes.DOCUMENT], command, {
|
|
908
|
+
...values,
|
|
909
|
+
options: values.options ?? this.options
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
/** @internal */ _query(command, args) {
|
|
913
|
+
const pseudos = this.options.pseudos;
|
|
914
|
+
const callbacks = pseudos ? Object.entries(pseudos).filter((entry) => typeof entry[1] === "function") : [];
|
|
915
|
+
if (!callbacks.length) {
|
|
916
|
+
if (command === "matches" && args.ids) {
|
|
917
|
+
const matched = matchSimpleSelector(this[require_nodes.DOCUMENT], args.ids, args.selector, args.options ?? this.options);
|
|
918
|
+
if (matched) return matched;
|
|
919
|
+
}
|
|
920
|
+
if (command === "select" && args.contexts) {
|
|
921
|
+
const found = selectSimpleWithin(this[require_nodes.DOCUMENT], args.contexts, args.selector, args.options ?? this.options);
|
|
922
|
+
if (found) return found;
|
|
923
|
+
}
|
|
924
|
+
return command === "select" && !args.options ? this[require_nodes.DOCUMENT].select(args.selector, args.contexts) : this.op(command, args);
|
|
925
|
+
}
|
|
926
|
+
const pseudoMatches = {};
|
|
927
|
+
const candidates = [.../* @__PURE__ */ new Set([
|
|
928
|
+
...this[require_nodes.DOCUMENT].select("*"),
|
|
929
|
+
...args.ids ?? [],
|
|
930
|
+
...args.contexts ? this[require_nodes.DOCUMENT].select("*", args.contexts) : []
|
|
931
|
+
])];
|
|
932
|
+
for (const [name, callback] of callbacks) for (const argument of customPseudoArguments(args.selector, name)) {
|
|
933
|
+
if (argument !== void 0 && callback.length < 2) throw new Error(`Pseudo-class :${name} doesn't have any arguments`);
|
|
934
|
+
if (argument === void 0 && callback.length >= 2) throw new Error(`Pseudo-class :${name} requires an argument`);
|
|
935
|
+
pseudoMatches[`${name}(${argument ?? ""})`] = candidates.filter((id) => callback(require_nodes.getNode(this[require_nodes.DOCUMENT], id), argument ?? null));
|
|
936
|
+
}
|
|
937
|
+
return this.op(command, {
|
|
938
|
+
...args,
|
|
939
|
+
pseudoMatches
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
/** @internal */ _fromIds(ids) {
|
|
943
|
+
const result = new this[require_nodes.API].fn.constructor(this[require_nodes.API], this[require_nodes.DOCUMENT], ids, this._root, this.options);
|
|
944
|
+
result.prevObject = this;
|
|
945
|
+
return result;
|
|
946
|
+
}
|
|
947
|
+
/** @internal */ _fromValues(values) {
|
|
948
|
+
const allNodes = values.every((value) => value && typeof value === "object" && typeof value.type === "string");
|
|
949
|
+
const result = new this[require_nodes.API].fn.constructor(this[require_nodes.API], this[require_nodes.DOCUMENT], allNodes ? require_nodes.importNodes(this[require_nodes.DOCUMENT], values) : [], this._root, this.options, allNodes ? void 0 : values);
|
|
950
|
+
result.prevObject = this;
|
|
951
|
+
return result;
|
|
952
|
+
}
|
|
953
|
+
_make(value, context) {
|
|
954
|
+
const result = typeof value === "string" || value?.cheerio ? this[require_nodes.API](value, context) : this._fromValues(value == null ? [] : typeof value === "object" && "length" in value && !value.type ? Array.from(value) : [value]);
|
|
955
|
+
result.prevObject = this;
|
|
956
|
+
return result;
|
|
957
|
+
}
|
|
958
|
+
_parse(content, options, isDocument, context) {
|
|
959
|
+
return parse$1(content, options, isDocument, context);
|
|
960
|
+
}
|
|
961
|
+
_render(content) {
|
|
962
|
+
return this[require_nodes.DOCUMENT].outerHtml(require_nodes.importNodes(this[require_nodes.DOCUMENT], content));
|
|
963
|
+
}
|
|
964
|
+
/** @internal */ _domIds(content, contextId) {
|
|
965
|
+
if (content == null || content === false) return [];
|
|
966
|
+
if (typeof content === "string") return this.op("create", {
|
|
967
|
+
html: content,
|
|
968
|
+
contextId,
|
|
969
|
+
options: this.options
|
|
970
|
+
});
|
|
971
|
+
if (content?.cheerio) {
|
|
972
|
+
if (!content[require_nodes.DOCUMENT] || !require_nodes.sameNodeSpace(this[require_nodes.DOCUMENT], content[require_nodes.DOCUMENT])) return require_nodes.importNodes(this[require_nodes.DOCUMENT], content.toArray());
|
|
973
|
+
require_nodes.exposeDocumentTo(this[require_nodes.DOCUMENT], content[require_nodes.DOCUMENT]);
|
|
974
|
+
return content.ids;
|
|
975
|
+
}
|
|
976
|
+
if (typeof content === "object" && "length" in content && !content.type) return Array.from(content).flatMap((item) => this._domIds(item, contextId));
|
|
977
|
+
return require_nodes.importNodes(this[require_nodes.DOCUMENT], [content]);
|
|
978
|
+
}
|
|
979
|
+
_makeDomArray(content, clone = false) {
|
|
980
|
+
const ids = this._domIds(content);
|
|
981
|
+
return (clone ? this.op("clone", {
|
|
982
|
+
ids,
|
|
983
|
+
deep: true
|
|
984
|
+
}) : ids).map((id) => require_nodes.getNode(this[require_nodes.DOCUMENT], id));
|
|
985
|
+
}
|
|
986
|
+
find(selector) {
|
|
987
|
+
if (!selector) return this._fromIds([]);
|
|
988
|
+
if (typeof selector === "string") return this._findBySelector(selector, Infinity);
|
|
989
|
+
const ids = require_nodes.importNodes(this[require_nodes.DOCUMENT], selector);
|
|
990
|
+
return this._fromIds(ids.filter((id) => this.ids.some((container) => this.op("contains", {
|
|
991
|
+
container,
|
|
992
|
+
contained: id
|
|
993
|
+
}))));
|
|
994
|
+
}
|
|
995
|
+
_findBySelector(selector, limit) {
|
|
996
|
+
const ids = this._query("select", {
|
|
997
|
+
selector,
|
|
998
|
+
contexts: this.ids
|
|
999
|
+
});
|
|
1000
|
+
return this._fromIds(Number.isFinite(limit) ? ids.slice(0, limit) : ids);
|
|
1001
|
+
}
|
|
1002
|
+
/** @internal */ _filterIds(ids, selector) {
|
|
1003
|
+
if (!selector) return ids;
|
|
1004
|
+
if (typeof selector === "string") return this._query("matches", {
|
|
1005
|
+
ids,
|
|
1006
|
+
selector,
|
|
1007
|
+
options: this.options
|
|
1008
|
+
});
|
|
1009
|
+
return ids.filter((id, index) => this._predicate(selector, require_nodes.getNode(this[require_nodes.DOCUMENT], id), index));
|
|
1010
|
+
}
|
|
1011
|
+
/** @internal */ _predicate(selector, node, index) {
|
|
1012
|
+
if (typeof selector === "function") return !!selector.call(node, index, node);
|
|
1013
|
+
if (typeof selector === "string") return this._query("matches", {
|
|
1014
|
+
ids: require_nodes.importNodes(this[require_nodes.DOCUMENT], [node]),
|
|
1015
|
+
selector,
|
|
1016
|
+
options: this.options
|
|
1017
|
+
}).length > 0;
|
|
1018
|
+
if (selector?.cheerio) return selector.toArray().includes(node);
|
|
1019
|
+
return node === selector;
|
|
1020
|
+
}
|
|
1021
|
+
/** @internal */ _traverse(kind, selector) {
|
|
1022
|
+
let ids = require_nodes.traverseRecords(this[require_nodes.DOCUMENT], this.ids, kind) ?? this.op("traverse", {
|
|
1023
|
+
ids: this.ids,
|
|
1024
|
+
kind
|
|
1025
|
+
});
|
|
1026
|
+
ids = this._filterIds(ids, selector);
|
|
1027
|
+
if (this.length > 1 && ids.length > 1) {
|
|
1028
|
+
if (kind === "parents" || kind === "siblings") ids = this.op("uniqueSort", { ids });
|
|
1029
|
+
else if (![
|
|
1030
|
+
"next",
|
|
1031
|
+
"prev",
|
|
1032
|
+
"contents"
|
|
1033
|
+
].includes(kind)) ids = [...new Set(ids)];
|
|
1034
|
+
if (kind === "parents") ids.reverse();
|
|
1035
|
+
}
|
|
1036
|
+
return this._fromIds(ids);
|
|
1037
|
+
}
|
|
1038
|
+
parent(selector) {
|
|
1039
|
+
return this._traverse("parent", selector);
|
|
1040
|
+
}
|
|
1041
|
+
parents(selector) {
|
|
1042
|
+
return this._traverse("parents", selector);
|
|
1043
|
+
}
|
|
1044
|
+
next(selector) {
|
|
1045
|
+
return this._traverse("next", selector);
|
|
1046
|
+
}
|
|
1047
|
+
prev(selector) {
|
|
1048
|
+
return this._traverse("prev", selector);
|
|
1049
|
+
}
|
|
1050
|
+
nextAll(selector) {
|
|
1051
|
+
return this._traverse("nextAll", selector);
|
|
1052
|
+
}
|
|
1053
|
+
prevAll(selector) {
|
|
1054
|
+
return this._traverse("prevAll", selector);
|
|
1055
|
+
}
|
|
1056
|
+
siblings(selector) {
|
|
1057
|
+
return this._traverse("siblings", selector);
|
|
1058
|
+
}
|
|
1059
|
+
children(selector) {
|
|
1060
|
+
return this._traverse("children", selector);
|
|
1061
|
+
}
|
|
1062
|
+
contents() {
|
|
1063
|
+
return this._traverse("contents");
|
|
1064
|
+
}
|
|
1065
|
+
/** @internal */ _until(kind, until, filter) {
|
|
1066
|
+
let ids = [];
|
|
1067
|
+
for (const id of this.ids) for (const candidate of require_nodes.traverseRecords(this[require_nodes.DOCUMENT], [id], kind) ?? this.op("traverse", {
|
|
1068
|
+
ids: [id],
|
|
1069
|
+
kind
|
|
1070
|
+
})) {
|
|
1071
|
+
if (until && this._predicate(until, require_nodes.getNode(this[require_nodes.DOCUMENT], candidate), ids.length)) break;
|
|
1072
|
+
ids.push(candidate);
|
|
1073
|
+
}
|
|
1074
|
+
ids = this._filterIds(ids, filter);
|
|
1075
|
+
if (this.length > 1 && ids.length > 1) ids = kind === "parents" ? this.op("uniqueSort", { ids }).reverse() : [...new Set(ids)];
|
|
1076
|
+
return this._fromIds(ids);
|
|
1077
|
+
}
|
|
1078
|
+
parentsUntil(until, filter) {
|
|
1079
|
+
return this._until("parents", until, filter);
|
|
1080
|
+
}
|
|
1081
|
+
nextUntil(until, filter) {
|
|
1082
|
+
return this._until("nextAll", until, filter);
|
|
1083
|
+
}
|
|
1084
|
+
prevUntil(until, filter) {
|
|
1085
|
+
return this._until("prevAll", until, filter);
|
|
1086
|
+
}
|
|
1087
|
+
closest(selector) {
|
|
1088
|
+
if (!selector) return this._fromIds([]);
|
|
1089
|
+
const result = [];
|
|
1090
|
+
for (const id of this.ids) {
|
|
1091
|
+
const record = require_nodes.nodeRecord(this[require_nodes.DOCUMENT], id);
|
|
1092
|
+
const candidates = [...[
|
|
1093
|
+
"tag",
|
|
1094
|
+
"script",
|
|
1095
|
+
"style"
|
|
1096
|
+
].includes(record.type) ? [id] : [], ...require_nodes.traverseRecords(this[require_nodes.DOCUMENT], [id], "parents") ?? this.op("traverse", {
|
|
1097
|
+
ids: [id],
|
|
1098
|
+
kind: "parents"
|
|
1099
|
+
})];
|
|
1100
|
+
const matched = typeof selector === "string" ? new Set(this._filterIds(candidates, selector)) : void 0;
|
|
1101
|
+
const match = matched ? candidates.find((candidate) => matched.has(candidate)) : candidates.find((candidate) => this._predicate(selector, require_nodes.getNode(this[require_nodes.DOCUMENT], candidate), 0));
|
|
1102
|
+
if (match !== void 0 && !result.includes(match)) result.push(match);
|
|
1103
|
+
}
|
|
1104
|
+
return this._fromIds(result);
|
|
1105
|
+
}
|
|
1106
|
+
each(callback) {
|
|
1107
|
+
if (this[require_nodes.IS_PLAIN]) {
|
|
1108
|
+
const length = this.length;
|
|
1109
|
+
for (let i = 0; i < length; i++) if (callback.call(this[i], i, this[i]) === false) break;
|
|
1110
|
+
return this;
|
|
1111
|
+
}
|
|
1112
|
+
const values = this[require_nodes.VALUES], ids = this[require_nodes.IDS], document = this[require_nodes.DOCUMENT];
|
|
1113
|
+
const length = values ? values.length : ids.length;
|
|
1114
|
+
for (let i = 0; i < length; i++) {
|
|
1115
|
+
const node = values ? values[i] : require_nodes.getNode(document, ids[i]);
|
|
1116
|
+
if (callback.call(node, i, node) === false) break;
|
|
1117
|
+
}
|
|
1118
|
+
return this;
|
|
1119
|
+
}
|
|
1120
|
+
map(callback) {
|
|
1121
|
+
const result = [];
|
|
1122
|
+
const items = this._items();
|
|
1123
|
+
for (let i = 0; i < items.length; i++) {
|
|
1124
|
+
const value = callback.call(items[i], i, items[i]);
|
|
1125
|
+
if (value != null) result.push(...Array.isArray(value) ? value : [value]);
|
|
1126
|
+
}
|
|
1127
|
+
return this._fromValues(result);
|
|
1128
|
+
}
|
|
1129
|
+
filter(selector) {
|
|
1130
|
+
return typeof selector === "string" ? this._fromIds(this._query("matches", {
|
|
1131
|
+
ids: this.ids,
|
|
1132
|
+
selector,
|
|
1133
|
+
options: this.options
|
|
1134
|
+
})) : this._fromValues(this.toArray().filter((node, i) => this._predicate(selector, node, i)));
|
|
1135
|
+
}
|
|
1136
|
+
filterArray(nodes, match, xmlMode, root) {
|
|
1137
|
+
const matcher = match;
|
|
1138
|
+
if (typeof match !== "string") return nodes.filter((node, index) => typeof matcher === "function" ? matcher.call(node, index, node) : matcher?.cheerio ? matcher.toArray().includes(node) : matcher === node);
|
|
1139
|
+
const document = this?.[require_nodes.DOCUMENT] ?? require_nodes.nodeBinding(root)?.document ?? require_nodes.nodeBinding(nodes[0])?.document ?? require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument("", false));
|
|
1140
|
+
const ids = require_nodes.importNodes(document, nodes);
|
|
1141
|
+
const selected = new Set(require_nodes.operation(document, "matches", {
|
|
1142
|
+
ids,
|
|
1143
|
+
selector: match,
|
|
1144
|
+
options: { xmlMode }
|
|
1145
|
+
}));
|
|
1146
|
+
return nodes.filter((node) => {
|
|
1147
|
+
const id = require_nodes.nodeId(node, document);
|
|
1148
|
+
return id !== void 0 && selected.has(id);
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
not(selector) {
|
|
1152
|
+
if (typeof selector === "string") {
|
|
1153
|
+
const matched = new Set(this._query("matches", {
|
|
1154
|
+
ids: this.ids,
|
|
1155
|
+
selector,
|
|
1156
|
+
options: this.options
|
|
1157
|
+
}));
|
|
1158
|
+
return this._fromIds(this.ids.filter((id) => !matched.has(id)));
|
|
1159
|
+
}
|
|
1160
|
+
return this._fromValues(this.toArray().filter((node, i) => !this._predicate(selector, node, i)));
|
|
1161
|
+
}
|
|
1162
|
+
is(selector) {
|
|
1163
|
+
return typeof selector === "string" ? this._query("matches", {
|
|
1164
|
+
ids: this.ids,
|
|
1165
|
+
selector,
|
|
1166
|
+
options: this.options
|
|
1167
|
+
}).length > 0 : !!selector && this.toArray().some((node, i) => this._predicate(selector, node, i));
|
|
1168
|
+
}
|
|
1169
|
+
has(selector) {
|
|
1170
|
+
return this.filter(((_i, node) => this._make(node).find(selector).length > 0));
|
|
1171
|
+
}
|
|
1172
|
+
first() {
|
|
1173
|
+
return this.length > 1 ? this.slice(0, 1) : this;
|
|
1174
|
+
}
|
|
1175
|
+
last() {
|
|
1176
|
+
return this.length > 0 ? this.slice(-1) : this;
|
|
1177
|
+
}
|
|
1178
|
+
eq(index) {
|
|
1179
|
+
index = +index;
|
|
1180
|
+
if (index === 0 && this.length <= 1) return this;
|
|
1181
|
+
if (index < 0) index += this.length;
|
|
1182
|
+
const item = this._itemAt(index);
|
|
1183
|
+
return this._fromValues(item === void 0 ? [] : [item]);
|
|
1184
|
+
}
|
|
1185
|
+
get(index) {
|
|
1186
|
+
return index == null ? this.toArray() : this._itemAt(index < 0 ? this.length + index : index);
|
|
1187
|
+
}
|
|
1188
|
+
toArray() {
|
|
1189
|
+
return this._items();
|
|
1190
|
+
}
|
|
1191
|
+
/** @internal Every item, read without a proxy trap per index. */
|
|
1192
|
+
_items() {
|
|
1193
|
+
if (this[require_nodes.IS_PLAIN]) {
|
|
1194
|
+
const length = this.length;
|
|
1195
|
+
const items = new Array(length);
|
|
1196
|
+
for (let i = 0; i < length; i++) items[i] = this[i];
|
|
1197
|
+
return items;
|
|
1198
|
+
}
|
|
1199
|
+
const values = this[require_nodes.VALUES], ids = this[require_nodes.IDS], document = this[require_nodes.DOCUMENT];
|
|
1200
|
+
if (!values) require_nodes.prepareBulkViews(document, ids);
|
|
1201
|
+
return values ? values.slice() : ids.map((id) => require_nodes.getNode(document, id));
|
|
1202
|
+
}
|
|
1203
|
+
/** @internal */ _itemAt(index) {
|
|
1204
|
+
if (this[require_nodes.IS_PLAIN]) return this[index];
|
|
1205
|
+
const values = this[require_nodes.VALUES];
|
|
1206
|
+
if (values) return values[index];
|
|
1207
|
+
const id = this[require_nodes.IDS][index];
|
|
1208
|
+
return id === void 0 ? void 0 : require_nodes.getNode(this[require_nodes.DOCUMENT], id);
|
|
1209
|
+
}
|
|
1210
|
+
slice(start, end) {
|
|
1211
|
+
const ids = this.ids;
|
|
1212
|
+
return this[require_nodes.VALUES] ? this._fromValues(this[require_nodes.VALUES].slice(start, end)) : this._fromIds(ids.slice(start, end));
|
|
1213
|
+
}
|
|
1214
|
+
end() {
|
|
1215
|
+
return this.prevObject ?? this._make([]);
|
|
1216
|
+
}
|
|
1217
|
+
add(other, context) {
|
|
1218
|
+
const added = this[require_nodes.API](other, context);
|
|
1219
|
+
const addedIds = added[require_nodes.DOCUMENT] && require_nodes.sameNodeSpace(this[require_nodes.DOCUMENT], added[require_nodes.DOCUMENT]) ? added.ids : require_nodes.importNodes(this[require_nodes.DOCUMENT], added.toArray());
|
|
1220
|
+
return this._fromIds(this.op("uniqueSort", { ids: [...this.ids, ...addedIds] }));
|
|
1221
|
+
}
|
|
1222
|
+
addBack(selector) {
|
|
1223
|
+
return this.prevObject ? this.add(selector ? this.prevObject.filter(selector) : this.prevObject) : this;
|
|
1224
|
+
}
|
|
1225
|
+
index(selector) {
|
|
1226
|
+
const haystack = selector == null ? this.parent().children() : typeof selector === "string" ? this[require_nodes.API](selector) : this;
|
|
1227
|
+
const needle = selector == null || typeof selector === "string" ? this[0] : selector?.cheerio ? selector[0] : selector;
|
|
1228
|
+
return haystack.toArray().indexOf(needle);
|
|
1229
|
+
}
|
|
1230
|
+
splice(...args) {
|
|
1231
|
+
return Reflect.apply(Array.prototype.splice, this, args);
|
|
1232
|
+
}
|
|
1233
|
+
[Symbol.iterator]() {
|
|
1234
|
+
return this._items()[Symbol.iterator]();
|
|
1235
|
+
}
|
|
1236
|
+
/** @internal */ _insert(position, content) {
|
|
1237
|
+
if (typeof content[0] !== "function") {
|
|
1238
|
+
const ids = this.ids;
|
|
1239
|
+
if (ids.length) this.op("insert", {
|
|
1240
|
+
targetIds: ids,
|
|
1241
|
+
nodeIds: this._domIds(content),
|
|
1242
|
+
position
|
|
1243
|
+
});
|
|
1244
|
+
return this;
|
|
1245
|
+
}
|
|
1246
|
+
const targets = this.ids.filter((id) => [
|
|
1247
|
+
"root",
|
|
1248
|
+
"tag",
|
|
1249
|
+
"script",
|
|
1250
|
+
"style",
|
|
1251
|
+
"cdata"
|
|
1252
|
+
].includes(require_nodes.nodeRecord(this[require_nodes.DOCUMENT], id).type));
|
|
1253
|
+
if (!targets.length) return this;
|
|
1254
|
+
return this.each((index, node) => {
|
|
1255
|
+
if (!targets.includes(this.ids[index])) return;
|
|
1256
|
+
if ((position === "before" || position === "after") && require_nodes.nodeRecord(this[require_nodes.DOCUMENT], this.ids[index]).parent == null) return;
|
|
1257
|
+
const value = content[0].call(node, index, this[require_nodes.DOCUMENT].innerHtml(this.ids[index]));
|
|
1258
|
+
let nodes = this._domIds(value);
|
|
1259
|
+
if (index < this.length - 1) nodes = this.op("clone", {
|
|
1260
|
+
ids: nodes,
|
|
1261
|
+
deep: true
|
|
1262
|
+
});
|
|
1263
|
+
this.op("insert", {
|
|
1264
|
+
targetIds: [this.ids[index]],
|
|
1265
|
+
nodeIds: nodes,
|
|
1266
|
+
position
|
|
1267
|
+
});
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
append(...content) {
|
|
1271
|
+
return this._insert("append", content);
|
|
1272
|
+
}
|
|
1273
|
+
prepend(...content) {
|
|
1274
|
+
return this._insert("prepend", content);
|
|
1275
|
+
}
|
|
1276
|
+
before(...content) {
|
|
1277
|
+
return this._insert("before", content);
|
|
1278
|
+
}
|
|
1279
|
+
after(...content) {
|
|
1280
|
+
return this._insert("after", content);
|
|
1281
|
+
}
|
|
1282
|
+
appendTo(target) {
|
|
1283
|
+
this[require_nodes.API](target).append(this);
|
|
1284
|
+
return this;
|
|
1285
|
+
}
|
|
1286
|
+
prependTo(target) {
|
|
1287
|
+
this[require_nodes.API](target).prepend(this);
|
|
1288
|
+
return this;
|
|
1289
|
+
}
|
|
1290
|
+
/** @internal */ _insertRelative(target, position) {
|
|
1291
|
+
const targets = this[require_nodes.API](target).ids;
|
|
1292
|
+
this.remove();
|
|
1293
|
+
const inserted = [];
|
|
1294
|
+
for (const id of targets) {
|
|
1295
|
+
if (require_nodes.nodeRecord(this[require_nodes.DOCUMENT], id).parent == null) continue;
|
|
1296
|
+
const clones = this.op("clone", {
|
|
1297
|
+
ids: this.ids,
|
|
1298
|
+
deep: true
|
|
1299
|
+
});
|
|
1300
|
+
this.op("insert", {
|
|
1301
|
+
targetIds: [id],
|
|
1302
|
+
nodeIds: clones,
|
|
1303
|
+
position
|
|
1304
|
+
});
|
|
1305
|
+
inserted.push(...clones);
|
|
1306
|
+
}
|
|
1307
|
+
return this._fromIds(inserted);
|
|
1308
|
+
}
|
|
1309
|
+
insertBefore(target) {
|
|
1310
|
+
return this._insertRelative(target, "before");
|
|
1311
|
+
}
|
|
1312
|
+
insertAfter(target) {
|
|
1313
|
+
return this._insertRelative(target, "after");
|
|
1314
|
+
}
|
|
1315
|
+
remove(selector) {
|
|
1316
|
+
this.op("remove", { ids: selector ? this.filter(selector).ids : this.ids });
|
|
1317
|
+
return this;
|
|
1318
|
+
}
|
|
1319
|
+
replaceWith(content) {
|
|
1320
|
+
return this.each((index, node) => {
|
|
1321
|
+
if (require_nodes.nodeRecord(this[require_nodes.DOCUMENT], this.ids[index]).parent == null) return;
|
|
1322
|
+
const value = typeof content === "function" ? content.call(node, index, node) : content;
|
|
1323
|
+
this.op("insert", {
|
|
1324
|
+
targetIds: [this.ids[index]],
|
|
1325
|
+
nodeIds: this._domIds(value),
|
|
1326
|
+
position: "replace"
|
|
1327
|
+
});
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
empty() {
|
|
1331
|
+
for (const id of this.ids) this.op("setNode", {
|
|
1332
|
+
id,
|
|
1333
|
+
field: "children",
|
|
1334
|
+
value: []
|
|
1335
|
+
});
|
|
1336
|
+
return this;
|
|
1337
|
+
}
|
|
1338
|
+
html(content) {
|
|
1339
|
+
if (content === void 0) return this.length && [
|
|
1340
|
+
"root",
|
|
1341
|
+
"tag",
|
|
1342
|
+
"script",
|
|
1343
|
+
"style",
|
|
1344
|
+
"cdata"
|
|
1345
|
+
].includes(require_nodes.nodeRecord(this[require_nodes.DOCUMENT], this.ids[0]).type) ? this[require_nodes.DOCUMENT].innerHtml(this.ids[0]) : null;
|
|
1346
|
+
if (!content?.cheerio) {
|
|
1347
|
+
this.op("html", {
|
|
1348
|
+
ids: this.ids,
|
|
1349
|
+
value: String(content)
|
|
1350
|
+
});
|
|
1351
|
+
return this;
|
|
1352
|
+
}
|
|
1353
|
+
for (const id of this.ids) {
|
|
1354
|
+
const nodes = content?.cheerio ? require_nodes.importNodes(this[require_nodes.DOCUMENT], content.toArray()) : this._domIds(String(content), id);
|
|
1355
|
+
this.op("setNode", {
|
|
1356
|
+
id,
|
|
1357
|
+
field: "children",
|
|
1358
|
+
value: nodes
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
return this;
|
|
1362
|
+
}
|
|
1363
|
+
text(content) {
|
|
1364
|
+
if (content === void 0) return this[require_nodes.DOCUMENT].text(this.ids);
|
|
1365
|
+
if (typeof content !== "function") {
|
|
1366
|
+
this.op("text", {
|
|
1367
|
+
ids: this.ids,
|
|
1368
|
+
value: String(content)
|
|
1369
|
+
});
|
|
1370
|
+
return this;
|
|
1371
|
+
}
|
|
1372
|
+
for (let i = 0; i < this.ids.length; i++) {
|
|
1373
|
+
const id = this.ids[i];
|
|
1374
|
+
const value = typeof content === "function" ? content.call(require_nodes.getNode(this[require_nodes.DOCUMENT], id), i, this[require_nodes.DOCUMENT].text([id])) : content;
|
|
1375
|
+
this.op("text", {
|
|
1376
|
+
ids: [id],
|
|
1377
|
+
value: String(value)
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
return this;
|
|
1381
|
+
}
|
|
1382
|
+
toString() {
|
|
1383
|
+
return this[require_nodes.DOCUMENT].outerHtml(this.ids);
|
|
1384
|
+
}
|
|
1385
|
+
clone() {
|
|
1386
|
+
return this._fromIds(this.op("clone", {
|
|
1387
|
+
ids: this.ids,
|
|
1388
|
+
deep: true
|
|
1389
|
+
}));
|
|
1390
|
+
}
|
|
1391
|
+
/** @internal */ _deepest(id, tagsOnly = false) {
|
|
1392
|
+
for (;;) {
|
|
1393
|
+
const children = this.op("traverse", {
|
|
1394
|
+
ids: [id],
|
|
1395
|
+
kind: "children"
|
|
1396
|
+
});
|
|
1397
|
+
const child = tagsOnly ? children.find((candidate) => require_nodes.nodeRecord(this[require_nodes.DOCUMENT], candidate).type === "tag") : children[0];
|
|
1398
|
+
if (child === void 0) return id;
|
|
1399
|
+
id = child;
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
/** @internal */ _wrap(content, inner) {
|
|
1403
|
+
const lastParent = this.parents().last();
|
|
1404
|
+
for (let i = 0; i < this.length; i++) {
|
|
1405
|
+
const id = this.ids[i];
|
|
1406
|
+
const node = require_nodes.getNode(this[require_nodes.DOCUMENT], id);
|
|
1407
|
+
const value = typeof content === "function" ? content.call(node, i, node) : typeof content === "string" && !isMarkup(content) ? lastParent.find(content).clone() : content;
|
|
1408
|
+
let wrappers = this._domIds(value);
|
|
1409
|
+
if (i < this.length - 1) wrappers = this.op("clone", {
|
|
1410
|
+
ids: wrappers,
|
|
1411
|
+
deep: true
|
|
1412
|
+
});
|
|
1413
|
+
const wrapper = wrappers[0];
|
|
1414
|
+
if (wrapper === void 0 || ![
|
|
1415
|
+
"root",
|
|
1416
|
+
"tag",
|
|
1417
|
+
"script",
|
|
1418
|
+
"style"
|
|
1419
|
+
].includes(require_nodes.nodeRecord(this[require_nodes.DOCUMENT], wrapper).type)) continue;
|
|
1420
|
+
if (inner) {
|
|
1421
|
+
if (![
|
|
1422
|
+
"root",
|
|
1423
|
+
"tag",
|
|
1424
|
+
"script",
|
|
1425
|
+
"style",
|
|
1426
|
+
"cdata"
|
|
1427
|
+
].includes(require_nodes.nodeRecord(this[require_nodes.DOCUMENT], id).type)) continue;
|
|
1428
|
+
const contents = this.op("traverse", {
|
|
1429
|
+
ids: [id],
|
|
1430
|
+
kind: "contents"
|
|
1431
|
+
});
|
|
1432
|
+
this.op("setNode", {
|
|
1433
|
+
id: this._deepest(wrapper),
|
|
1434
|
+
field: "children",
|
|
1435
|
+
value: contents
|
|
1436
|
+
});
|
|
1437
|
+
this.op("setNode", {
|
|
1438
|
+
id,
|
|
1439
|
+
field: "children",
|
|
1440
|
+
value: [wrapper]
|
|
1441
|
+
});
|
|
1442
|
+
} else if (require_nodes.nodeRecord(this[require_nodes.DOCUMENT], id).parent != null) {
|
|
1443
|
+
const [inserted = wrapper] = this.op("insert", {
|
|
1444
|
+
targetIds: [id],
|
|
1445
|
+
nodeIds: [wrapper],
|
|
1446
|
+
position: "before"
|
|
1447
|
+
});
|
|
1448
|
+
this.op("setNode", {
|
|
1449
|
+
id: this._deepest(inserted),
|
|
1450
|
+
field: "children",
|
|
1451
|
+
value: [id]
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
return this;
|
|
1456
|
+
}
|
|
1457
|
+
wrap(content) {
|
|
1458
|
+
return this._wrap(content, false);
|
|
1459
|
+
}
|
|
1460
|
+
wrapInner(content) {
|
|
1461
|
+
return this._wrap(content, true);
|
|
1462
|
+
}
|
|
1463
|
+
unwrap(selector) {
|
|
1464
|
+
this.op("remove", {
|
|
1465
|
+
ids: this.parent(selector).not("body").ids,
|
|
1466
|
+
keepChildren: true
|
|
1467
|
+
});
|
|
1468
|
+
return this;
|
|
1469
|
+
}
|
|
1470
|
+
wrapAll(content) {
|
|
1471
|
+
if (!this.length) return this;
|
|
1472
|
+
const first = this[0];
|
|
1473
|
+
const root = [...this._make(typeof content === "function" ? content.call(first, 0, first) : content).insertBefore(first).ids].reverse().find((id) => require_nodes.nodeRecord(this[require_nodes.DOCUMENT], id).type === "tag");
|
|
1474
|
+
if (root !== void 0) this.op("insert", {
|
|
1475
|
+
targetIds: [this._deepest(root, true)],
|
|
1476
|
+
nodeIds: this.ids,
|
|
1477
|
+
position: "append"
|
|
1478
|
+
});
|
|
1479
|
+
return this;
|
|
1480
|
+
}
|
|
1481
|
+
extract(map) {
|
|
1482
|
+
return extract.call(this, map);
|
|
1483
|
+
}
|
|
1484
|
+
};
|
|
1485
|
+
Object.defineProperty(Cheerio.prototype, "cheerio", {
|
|
1486
|
+
value: "[cheerio object]",
|
|
1487
|
+
writable: true,
|
|
1488
|
+
configurable: true
|
|
1489
|
+
});
|
|
1490
|
+
const inspectCustom = Symbol.for("nodejs.util.inspect.custom");
|
|
1491
|
+
Object.defineProperty(Cheerio.prototype, inspectCustom, {
|
|
1492
|
+
configurable: true,
|
|
1493
|
+
writable: true,
|
|
1494
|
+
value(depth, options, inspect) {
|
|
1495
|
+
const prototype = Object.getPrototypeOf(this);
|
|
1496
|
+
if (depth < 0) return options.stylize(`[${prototype.constructor.name}]`, "special");
|
|
1497
|
+
const shown = Object.create(prototype);
|
|
1498
|
+
Object.defineProperty(shown, inspectCustom, { value: void 0 });
|
|
1499
|
+
for (const key of Object.keys(this)) shown[key] = this[key];
|
|
1500
|
+
return inspect(shown, {
|
|
1501
|
+
...options,
|
|
1502
|
+
depth
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
});
|
|
1506
|
+
installAttributes(Cheerio);
|
|
1507
|
+
/** @internal */
|
|
1508
|
+
const nativeExtractionMethods = Object.freeze({
|
|
1509
|
+
make: Cheerio.prototype._make,
|
|
1510
|
+
find: Cheerio.prototype._findBySelector,
|
|
1511
|
+
prop: Cheerio.prototype.prop,
|
|
1512
|
+
extract: Cheerio.prototype.extract
|
|
1513
|
+
});
|
|
1514
|
+
// src/extract.ts
|
|
1515
|
+
function plainKeys(map) {
|
|
1516
|
+
return !node_util.types.isProxy(map) && (Object.getPrototypeOf(map) === Object.prototype || Object.getPrototypeOf(map) === null) ? Object.keys(map) : void 0;
|
|
1517
|
+
}
|
|
1518
|
+
function nativePrefix(scope, map, keys, key) {
|
|
1519
|
+
if (!keys || !hasNativeExtraction(scope)) return void 0;
|
|
1520
|
+
const prefix = prefixPlan(map, keys, key, scope.options);
|
|
1521
|
+
if (!prefix) return void 0;
|
|
1522
|
+
return {
|
|
1523
|
+
keys: prefix.names,
|
|
1524
|
+
values: runPlan(scope, prefix.plan)
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
/** The native plan for the longest run of entries from `key` on that compile to one. */
|
|
1528
|
+
function prefixPlan(map, keys, key, options) {
|
|
1529
|
+
if (keys[0] === key && keys.every((name) => {
|
|
1530
|
+
const slot = Object.getOwnPropertyDescriptor(map, name);
|
|
1531
|
+
return !!slot && slot.enumerable && "value" in slot;
|
|
1532
|
+
})) {
|
|
1533
|
+
let count = 0;
|
|
1534
|
+
let sameKeys = true;
|
|
1535
|
+
for (const name in map) if (name !== keys[count++]) {
|
|
1536
|
+
sameKeys = false;
|
|
1537
|
+
break;
|
|
1538
|
+
}
|
|
1539
|
+
if (sameKeys && count === keys.length) {
|
|
1540
|
+
const complete = compileExtractPlan(map, options);
|
|
1541
|
+
if (complete) return {
|
|
1542
|
+
names: keys,
|
|
1543
|
+
plan: complete
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
const prefix = Object.create(null);
|
|
1548
|
+
let plan;
|
|
1549
|
+
for (let position = keys.indexOf(key); position < keys.length; position++) {
|
|
1550
|
+
const name = keys[position];
|
|
1551
|
+
const slot = Object.getOwnPropertyDescriptor(map, name);
|
|
1552
|
+
if (!slot || !slot.enumerable || !("value" in slot)) break;
|
|
1553
|
+
Object.defineProperty(prefix, name, {
|
|
1554
|
+
value: slot.value,
|
|
1555
|
+
enumerable: true,
|
|
1556
|
+
configurable: true
|
|
1557
|
+
});
|
|
1558
|
+
const compiled = compileExtractPlan(prefix, options);
|
|
1559
|
+
if (!compiled) {
|
|
1560
|
+
delete prefix[name];
|
|
1561
|
+
break;
|
|
1562
|
+
}
|
|
1563
|
+
plan = compiled;
|
|
1564
|
+
}
|
|
1565
|
+
return plan ? {
|
|
1566
|
+
names: Object.keys(prefix),
|
|
1567
|
+
plan
|
|
1568
|
+
} : void 0;
|
|
1569
|
+
}
|
|
1570
|
+
function hasNativeExtraction(scope) {
|
|
1571
|
+
return scope._make === nativeExtractionMethods.make && scope._findBySelector === nativeExtractionMethods.find && scope.prop === nativeExtractionMethods.prop && scope.extract === nativeExtractionMethods.extract;
|
|
1572
|
+
}
|
|
1573
|
+
/** Descriptor callbacks stay in JavaScript; matching and properties use Rust. */
|
|
1574
|
+
function extract(map) {
|
|
1575
|
+
const result = {};
|
|
1576
|
+
const keys = plainKeys(map);
|
|
1577
|
+
const processed = /* @__PURE__ */ new Set();
|
|
1578
|
+
for (const key in map) {
|
|
1579
|
+
if (processed.has(key)) continue;
|
|
1580
|
+
const prefix = Object.getPrototypeOf(result) === Object.prototype ? nativePrefix(this, map, keys, key) : void 0;
|
|
1581
|
+
if (prefix) {
|
|
1582
|
+
for (const name of prefix.keys) {
|
|
1583
|
+
Object.defineProperty(result, name, {
|
|
1584
|
+
value: prefix.values[name],
|
|
1585
|
+
enumerable: true,
|
|
1586
|
+
configurable: true,
|
|
1587
|
+
writable: true
|
|
1588
|
+
});
|
|
1589
|
+
processed.add(name);
|
|
1590
|
+
}
|
|
1591
|
+
continue;
|
|
1592
|
+
}
|
|
1593
|
+
const entry = map[key];
|
|
1594
|
+
const all = Array.isArray(entry);
|
|
1595
|
+
const descriptor = all ? entry[0] : entry;
|
|
1596
|
+
const selector = typeof descriptor === "string" ? descriptor : descriptor.selector;
|
|
1597
|
+
const value = typeof descriptor === "string" ? "textContent" : descriptor.value ?? "textContent";
|
|
1598
|
+
const selection = this._findBySelector(selector, all ? Infinity : 1);
|
|
1599
|
+
const rows = typeof value === "object" ? nativeRows(this, key, all, selector, value) : void 0;
|
|
1600
|
+
const read = (index) => {
|
|
1601
|
+
if (typeof value === "function") return value(selection[index], key, result);
|
|
1602
|
+
const scope = this._make === nativeExtractionMethods.make ? this._fromIds([selection.ids[index]]) : this._make(selection[index]);
|
|
1603
|
+
if (typeof value === "string") return scope.prop(value);
|
|
1604
|
+
return rows ? completeRow(scope, value, rows, index) : scope.extract(value);
|
|
1605
|
+
};
|
|
1606
|
+
let extracted;
|
|
1607
|
+
if (all) {
|
|
1608
|
+
const values = [];
|
|
1609
|
+
for (let index = 0; index < selection.length; index += 1) {
|
|
1610
|
+
const value = read(index);
|
|
1611
|
+
if (value != null) values.push(...Array.isArray(value) ? value : [value]);
|
|
1612
|
+
}
|
|
1613
|
+
extracted = values;
|
|
1614
|
+
} else extracted = selection.length ? read(0) : void 0;
|
|
1615
|
+
result[key] = extracted;
|
|
1616
|
+
}
|
|
1617
|
+
return result;
|
|
1618
|
+
}
|
|
1619
|
+
/**
|
|
1620
|
+
* For a nested map with callbacks, extracts the fields the native engine can
|
|
1621
|
+
* for every matched element in one call, instead of one call per element;
|
|
1622
|
+
* the remaining fields are then filled in map order, so each callback sees
|
|
1623
|
+
* exactly the fields before it, as in Cheerio.
|
|
1624
|
+
*/
|
|
1625
|
+
function nativeRows(scope, key, all, selector, map) {
|
|
1626
|
+
if (!hasNativeExtraction(scope)) return void 0;
|
|
1627
|
+
const planned = rowsPlan(key, all, selector, map, scope.options);
|
|
1628
|
+
if (!planned) return void 0;
|
|
1629
|
+
const writes = require_nodes.writeCount();
|
|
1630
|
+
const extracted = runPlan(scope, planned.plan)[key];
|
|
1631
|
+
const rows = all ? extracted : extracted === void 0 ? [] : [extracted];
|
|
1632
|
+
return Array.isArray(rows) ? {
|
|
1633
|
+
keys: planned.nativeKeys,
|
|
1634
|
+
rows,
|
|
1635
|
+
writes
|
|
1636
|
+
} : void 0;
|
|
1637
|
+
}
|
|
1638
|
+
/** The native plan for the fields of `map` that compile to one, when only some do. */
|
|
1639
|
+
function rowsPlan(key, all, selector, map, options) {
|
|
1640
|
+
const keys = plainKeys(map);
|
|
1641
|
+
if (!keys) return void 0;
|
|
1642
|
+
const subset = Object.create(null);
|
|
1643
|
+
const nativeKeys = /* @__PURE__ */ new Set();
|
|
1644
|
+
for (const name of keys) {
|
|
1645
|
+
const slot = Object.getOwnPropertyDescriptor(map, name);
|
|
1646
|
+
if (!slot || !slot.enumerable || !("value" in slot)) continue;
|
|
1647
|
+
const single = Object.create(null);
|
|
1648
|
+
Object.defineProperty(single, name, {
|
|
1649
|
+
value: slot.value,
|
|
1650
|
+
enumerable: true,
|
|
1651
|
+
configurable: true
|
|
1652
|
+
});
|
|
1653
|
+
if (!compileExtractPlan(single, options)) continue;
|
|
1654
|
+
Object.defineProperty(subset, name, {
|
|
1655
|
+
value: slot.value,
|
|
1656
|
+
enumerable: true,
|
|
1657
|
+
configurable: true
|
|
1658
|
+
});
|
|
1659
|
+
nativeKeys.add(name);
|
|
1660
|
+
}
|
|
1661
|
+
if (!nativeKeys.size || nativeKeys.size === keys.length) return void 0;
|
|
1662
|
+
const outer = Object.create(null);
|
|
1663
|
+
Object.defineProperty(outer, key, {
|
|
1664
|
+
value: all ? [{
|
|
1665
|
+
selector,
|
|
1666
|
+
value: subset
|
|
1667
|
+
}] : {
|
|
1668
|
+
selector,
|
|
1669
|
+
value: subset
|
|
1670
|
+
},
|
|
1671
|
+
enumerable: true,
|
|
1672
|
+
configurable: true
|
|
1673
|
+
});
|
|
1674
|
+
const plan = compileExtractPlan(outer, options);
|
|
1675
|
+
return plan ? {
|
|
1676
|
+
nativeKeys,
|
|
1677
|
+
plan
|
|
1678
|
+
} : void 0;
|
|
1679
|
+
}
|
|
1680
|
+
/**
|
|
1681
|
+
* The plans a walk of `map` from a document's root runs first, in order, so
|
|
1682
|
+
* a batch can run them on its parsing threads. Callbacks may change the
|
|
1683
|
+
* document, so later plans of the walk stay on the calling thread.
|
|
1684
|
+
*/
|
|
1685
|
+
function rootExtractPlans(map, options) {
|
|
1686
|
+
if (!isPlainData(map, 0) || hasInheritedEntryFields()) return [];
|
|
1687
|
+
try {
|
|
1688
|
+
return collectRootExtractPlans(map, options);
|
|
1689
|
+
} catch {
|
|
1690
|
+
return [];
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
/** Whether reading `value` and everything it holds runs no code: no proxies, accessors or class instances. */
|
|
1694
|
+
function isPlainData(value, depth) {
|
|
1695
|
+
if (typeof value !== "object" || value === null) return true;
|
|
1696
|
+
if (depth > 32 || node_util.types.isProxy(value)) return false;
|
|
1697
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1698
|
+
if (prototype !== Object.prototype && prototype !== Array.prototype && prototype !== null) return false;
|
|
1699
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
1700
|
+
const slot = Object.getOwnPropertyDescriptor(value, key);
|
|
1701
|
+
if (!("value" in slot) || !isPlainData(slot.value, depth + 1)) return false;
|
|
1702
|
+
}
|
|
1703
|
+
return true;
|
|
1704
|
+
}
|
|
1705
|
+
function hasInheritedEntryFields() {
|
|
1706
|
+
return "selector" in Object.prototype || "value" in Object.prototype || "0" in Array.prototype;
|
|
1707
|
+
}
|
|
1708
|
+
function collectRootExtractPlans(map, options) {
|
|
1709
|
+
const plans = [];
|
|
1710
|
+
const keys = plainKeys(map);
|
|
1711
|
+
const processed = /* @__PURE__ */ new Set();
|
|
1712
|
+
for (const key in map) {
|
|
1713
|
+
if (processed.has(key)) continue;
|
|
1714
|
+
const prefix = keys ? prefixPlan(map, keys, key, options) : void 0;
|
|
1715
|
+
if (prefix) {
|
|
1716
|
+
for (const name of prefix.names) processed.add(name);
|
|
1717
|
+
plans.push(prefix.plan);
|
|
1718
|
+
continue;
|
|
1719
|
+
}
|
|
1720
|
+
const entry = map[key];
|
|
1721
|
+
const all = Array.isArray(entry);
|
|
1722
|
+
const descriptor = all ? entry[0] : entry;
|
|
1723
|
+
const value = typeof descriptor === "string" ? "textContent" : descriptor.value ?? "textContent";
|
|
1724
|
+
if (typeof value !== "object" || value === null) continue;
|
|
1725
|
+
const planned = rowsPlan(key, all, typeof descriptor === "string" ? descriptor : descriptor.selector, value, options);
|
|
1726
|
+
if (planned) plans.push(planned.plan);
|
|
1727
|
+
}
|
|
1728
|
+
return plans.filter((plan) => !JSON.stringify(plan).includes("innerHTML"));
|
|
1729
|
+
}
|
|
1730
|
+
/** Results of root plans a batch ran while parsing, valid while no document changes. */
|
|
1731
|
+
const preparedPlans = /* @__PURE__ */ new WeakMap();
|
|
1732
|
+
function prepareExtractPlans(document, plans, results) {
|
|
1733
|
+
if (!results) return;
|
|
1734
|
+
const prepared = /* @__PURE__ */ new Map();
|
|
1735
|
+
for (let index = 0; index < plans.length; index++) {
|
|
1736
|
+
const result = results[index];
|
|
1737
|
+
if (typeof result === "string") prepared.set(JSON.stringify(plans[index]), require_nodes.decodeJson(result));
|
|
1738
|
+
}
|
|
1739
|
+
preparedPlans.set(document, {
|
|
1740
|
+
writes: require_nodes.writeCount(),
|
|
1741
|
+
results: prepared
|
|
1742
|
+
});
|
|
1743
|
+
}
|
|
1744
|
+
function releaseExtractPlans(document) {
|
|
1745
|
+
preparedPlans.delete(document);
|
|
1746
|
+
}
|
|
1747
|
+
function runPlan(scope, plan) {
|
|
1748
|
+
const document = scope[require_nodes.DOCUMENT];
|
|
1749
|
+
const prepared = preparedPlans.get(document);
|
|
1750
|
+
const result = prepared !== void 0 && prepared.writes === require_nodes.writeCount() && scope.ids.length === 1 && scope.ids[0] === document.rootId() ? prepared.results.get(JSON.stringify(plan)) : void 0;
|
|
1751
|
+
const values = result !== void 0 ? JSON.parse(result) : scope.op("extractPlan", {
|
|
1752
|
+
ids: scope.ids,
|
|
1753
|
+
plan
|
|
1754
|
+
});
|
|
1755
|
+
hydrateExtractResult(values, plan.entries);
|
|
1756
|
+
return values;
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Builds row `index` of a nested map from its native fields and the scope's
|
|
1760
|
+
* JavaScript fields. Native fields extracted before a callback changed any
|
|
1761
|
+
* document are extracted again, as extracting row by row would see them.
|
|
1762
|
+
*/
|
|
1763
|
+
function completeRow(scope, map, rows, index) {
|
|
1764
|
+
const row = {};
|
|
1765
|
+
const native = rows.rows[index] ?? {};
|
|
1766
|
+
for (const name in map) {
|
|
1767
|
+
if (rows.keys.has(name) && require_nodes.writeCount() === rows.writes) {
|
|
1768
|
+
row[name] = native[name];
|
|
1769
|
+
continue;
|
|
1770
|
+
}
|
|
1771
|
+
const entry = Object.create(null);
|
|
1772
|
+
Object.defineProperty(entry, name, {
|
|
1773
|
+
value: map[name],
|
|
1774
|
+
enumerable: true,
|
|
1775
|
+
configurable: true
|
|
1776
|
+
});
|
|
1777
|
+
fillEntries(scope, entry, row);
|
|
1778
|
+
}
|
|
1779
|
+
return row;
|
|
1780
|
+
}
|
|
1781
|
+
/** Extracts `map`'s entries into `result`, which callbacks receive as built so far. */
|
|
1782
|
+
function fillEntries(scope, map, result) {
|
|
1783
|
+
for (const key in map) {
|
|
1784
|
+
const entry = map[key];
|
|
1785
|
+
const all = Array.isArray(entry);
|
|
1786
|
+
const descriptor = all ? entry[0] : entry;
|
|
1787
|
+
const selector = typeof descriptor === "string" ? descriptor : descriptor.selector;
|
|
1788
|
+
const value = typeof descriptor === "string" ? "textContent" : descriptor.value ?? "textContent";
|
|
1789
|
+
const selection = scope._findBySelector(selector, all ? Infinity : 1);
|
|
1790
|
+
const read = (index) => {
|
|
1791
|
+
if (typeof value === "function") return value(selection[index], key, result);
|
|
1792
|
+
const child = scope._make === nativeExtractionMethods.make ? scope._fromIds([selection.ids[index]]) : scope._make(selection[index]);
|
|
1793
|
+
return typeof value === "string" ? child.prop(value) : child.extract(value);
|
|
1794
|
+
};
|
|
1795
|
+
if (all) {
|
|
1796
|
+
const values = [];
|
|
1797
|
+
for (let index = 0; index < selection.length; index += 1) {
|
|
1798
|
+
const extracted = read(index);
|
|
1799
|
+
if (extracted != null) values.push(...Array.isArray(extracted) ? extracted : [extracted]);
|
|
1800
|
+
}
|
|
1801
|
+
result[key] = values;
|
|
1802
|
+
} else result[key] = selection.length ? read(0) : void 0;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
/** Async batch fallback preserves callback values/order while yielding between visits. */
|
|
1806
|
+
async function extractCooperatively(selection, map) {
|
|
1807
|
+
let deadline = performance.now() + 4;
|
|
1808
|
+
let visits = 0;
|
|
1809
|
+
const checkpoint = async () => {
|
|
1810
|
+
if (++visits % 32 || performance.now() < deadline) return;
|
|
1811
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
1812
|
+
deadline = performance.now() + 4;
|
|
1813
|
+
};
|
|
1814
|
+
const walk = async (scope, descriptors) => {
|
|
1815
|
+
const result = {};
|
|
1816
|
+
const keys = plainKeys(descriptors);
|
|
1817
|
+
const processed = /* @__PURE__ */ new Set();
|
|
1818
|
+
for (const key in descriptors) {
|
|
1819
|
+
if (processed.has(key)) continue;
|
|
1820
|
+
const prefix = Object.getPrototypeOf(result) === Object.prototype ? nativePrefix(scope, descriptors, keys, key) : void 0;
|
|
1821
|
+
if (prefix) {
|
|
1822
|
+
for (const name of prefix.keys) {
|
|
1823
|
+
Object.defineProperty(result, name, {
|
|
1824
|
+
value: prefix.values[name],
|
|
1825
|
+
enumerable: true,
|
|
1826
|
+
configurable: true,
|
|
1827
|
+
writable: true
|
|
1828
|
+
});
|
|
1829
|
+
processed.add(name);
|
|
1830
|
+
}
|
|
1831
|
+
continue;
|
|
1832
|
+
}
|
|
1833
|
+
const entry = descriptors[key];
|
|
1834
|
+
const all = Array.isArray(entry);
|
|
1835
|
+
const descriptor = all ? entry[0] : entry;
|
|
1836
|
+
const selector = typeof descriptor === "string" ? descriptor : descriptor.selector;
|
|
1837
|
+
const value = typeof descriptor === "string" ? "textContent" : descriptor.value ?? "textContent";
|
|
1838
|
+
const found = scope._findBySelector(selector, all ? Infinity : 1);
|
|
1839
|
+
const rows = typeof value === "object" ? nativeRows(scope, key, all, selector, value) : void 0;
|
|
1840
|
+
const output = [];
|
|
1841
|
+
let single;
|
|
1842
|
+
const count = all ? found.length : Math.min(found.length, 1);
|
|
1843
|
+
for (let index = 0; index < count; index++) {
|
|
1844
|
+
await checkpoint();
|
|
1845
|
+
let extracted;
|
|
1846
|
+
if (typeof value === "function") extracted = value(found[index], key, result);
|
|
1847
|
+
else {
|
|
1848
|
+
const child = scope._make === nativeExtractionMethods.make ? scope._fromIds([found.ids[index]]) : scope._make(found[index]);
|
|
1849
|
+
extracted = typeof value === "string" ? child.prop(value) : rows ? completeRow(child, value, rows, index) : child.extract === nativeExtractionMethods.extract ? (await walk(child, value)).value : child.extract(value);
|
|
1850
|
+
}
|
|
1851
|
+
if (all) {
|
|
1852
|
+
if (extracted != null) output.push(...Array.isArray(extracted) ? extracted : [extracted]);
|
|
1853
|
+
} else single = extracted;
|
|
1854
|
+
}
|
|
1855
|
+
result[key] = all ? output : single;
|
|
1856
|
+
}
|
|
1857
|
+
return { value: result };
|
|
1858
|
+
};
|
|
1859
|
+
return await walk(selection, map);
|
|
1860
|
+
}
|
|
1861
|
+
// src/workers.ts
|
|
1862
|
+
/** Set the process-wide native CPU-worker budget before starting async work. */
|
|
1863
|
+
function configureWorkers(options) {
|
|
1864
|
+
const threads = typeof options === "number" ? options : options?.threads;
|
|
1865
|
+
if (!Number.isSafeInteger(threads) || threads < 1 || threads > 256) throw new RangeError("threads must be an integer from 1 to 256");
|
|
1866
|
+
const binding = require_nodes.requireNative();
|
|
1867
|
+
if (!binding.configureWorkerPool || !binding.workerPoolInfo) throw new Error("This native build does not support a shared worker pool");
|
|
1868
|
+
binding.configureWorkerPool(threads);
|
|
1869
|
+
return binding.workerPoolInfo();
|
|
1870
|
+
}
|
|
1871
|
+
/** Observe the configured budget and actual concurrent native CPU work. */
|
|
1872
|
+
function workerPoolInfo() {
|
|
1873
|
+
const binding = require_nodes.requireNative();
|
|
1874
|
+
if (!binding.workerPoolInfo) throw new Error("This native build does not support worker pool diagnostics");
|
|
1875
|
+
return binding.workerPoolInfo();
|
|
1876
|
+
}
|
|
1877
|
+
// src/io.ts
|
|
1878
|
+
function encodingLabel(label) {
|
|
1879
|
+
if (!label) return void 0;
|
|
1880
|
+
if (label.trim().toLowerCase() === "x-user-defined") return "x-user-defined";
|
|
1881
|
+
try {
|
|
1882
|
+
return new TextDecoder(label.trim()).encoding;
|
|
1883
|
+
} catch {
|
|
1884
|
+
return;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
/** WHATWG decoding with BOM, transport headers, and the HTML meta prescan. */
|
|
1888
|
+
function sniffEncoding(buffer, options = {}) {
|
|
1889
|
+
let label;
|
|
1890
|
+
if (buffer[0] === 239 && buffer[1] === 187 && buffer[2] === 191) label = "utf-8";
|
|
1891
|
+
else if (buffer[0] === 255 && buffer[1] === 254) label = "utf-16le";
|
|
1892
|
+
else if (buffer[0] === 254 && buffer[1] === 255) label = "utf-16be";
|
|
1893
|
+
label ??= encodingLabel(options.userEncoding);
|
|
1894
|
+
label ??= encodingLabel(options.transportLayerEncodingLabel);
|
|
1895
|
+
if (!label && buffer[0] === 0 && buffer[1] === 60 && buffer[2] === 0 && buffer[3] === 63 && buffer[4] === 0 && buffer[5] === 120) label = "utf-16be";
|
|
1896
|
+
if (!label && buffer[0] === 60 && buffer[1] === 0 && buffer[2] === 63 && buffer[3] === 0 && buffer[4] === 120 && buffer[5] === 0) label = "utf-16le";
|
|
1897
|
+
if (!label) {
|
|
1898
|
+
const prefix = Buffer.from(buffer.subarray(0, options.maxBytes ?? 1024)).toString("latin1").replace(/<!--[\s\S]*?(?:-->|$)/g, "");
|
|
1899
|
+
for (const token of prefix.matchAll(/<meta\b([^>]*)>/gi)) {
|
|
1900
|
+
const attributes = {};
|
|
1901
|
+
for (const attribute of token[1].matchAll(/([^\s=/'">]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g)) {
|
|
1902
|
+
const key = attribute[1].toLowerCase();
|
|
1903
|
+
if (!(key in attributes)) attributes[key] = attribute[2] ?? attribute[3] ?? attribute[4] ?? "";
|
|
1904
|
+
}
|
|
1905
|
+
label = encodingLabel(attributes.charset ?? (attributes["http-equiv"]?.toLowerCase() === "content-type" ? /\bcharset\s*=\s*["']?([^\s;"']+)/i.exec(attributes.content ?? "")?.[1] : void 0));
|
|
1906
|
+
if (label) {
|
|
1907
|
+
if (label === "utf-16le" || label === "utf-16be") label = "utf-8";
|
|
1908
|
+
if (label === "x-user-defined") label = "windows-1252";
|
|
1909
|
+
break;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
if (!label) {
|
|
1913
|
+
label = encodingLabel(/<\?xml\b[^>]*\bencoding\s*=\s*["']([^"']+)["']/i.exec(prefix)?.[1]);
|
|
1914
|
+
if (label === "utf-16le" || label === "utf-16be") label = "utf-8";
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
label ??= encodingLabel(options.defaultEncoding) ?? "windows-1252";
|
|
1918
|
+
return label;
|
|
1919
|
+
}
|
|
1920
|
+
function decodeBuffer(buffer, options = {}) {
|
|
1921
|
+
const label = sniffEncoding(buffer, options);
|
|
1922
|
+
if (label === "x-user-defined") {
|
|
1923
|
+
let decoded = "";
|
|
1924
|
+
for (const byte of buffer) decoded += String.fromCharCode(byte < 128 ? byte : 63232 + byte);
|
|
1925
|
+
return decoded;
|
|
1926
|
+
}
|
|
1927
|
+
return new TextDecoder(label).decode(buffer);
|
|
1928
|
+
}
|
|
1929
|
+
function decodedInput(buffer, options) {
|
|
1930
|
+
const { encoding, ...parserOptions } = options;
|
|
1931
|
+
return [decodeBuffer(buffer, {
|
|
1932
|
+
defaultEncoding: !!(options.xmlMode || options.xml) ? "utf-8" : "windows-1252",
|
|
1933
|
+
...encoding
|
|
1934
|
+
}), parserOptions];
|
|
1935
|
+
}
|
|
1936
|
+
function loadBuffer(buffer, options = {}) {
|
|
1937
|
+
return load(...decodedInput(buffer, options));
|
|
1938
|
+
}
|
|
1939
|
+
function completion(callback) {
|
|
1940
|
+
let completed = false;
|
|
1941
|
+
return (error, document) => {
|
|
1942
|
+
if (completed) return;
|
|
1943
|
+
completed = true;
|
|
1944
|
+
callback(error, document);
|
|
1945
|
+
};
|
|
1946
|
+
}
|
|
1947
|
+
function requestHeaders(input) {
|
|
1948
|
+
if (!input) return new Headers();
|
|
1949
|
+
if (input instanceof Headers) return new Headers(input);
|
|
1950
|
+
const headers = new Headers();
|
|
1951
|
+
if (Array.isArray(input)) {
|
|
1952
|
+
if (input.length % 2) throw new TypeError("HTTP header arrays must contain name/value pairs");
|
|
1953
|
+
for (let index = 0; index < input.length; index += 2) headers.append(input[index], input[index + 1]);
|
|
1954
|
+
} else {
|
|
1955
|
+
const entries = Symbol.iterator in input ? input : Object.entries(input);
|
|
1956
|
+
for (const [name, value] of entries) {
|
|
1957
|
+
if (value == null) continue;
|
|
1958
|
+
for (const item of Array.isArray(value) ? value : [value]) headers.append(name, item);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
return headers;
|
|
1962
|
+
}
|
|
1963
|
+
/** Node transport exposes informational responses and leaves wire headers alone. */
|
|
1964
|
+
async function requestDocument(url, method, headers, body, options, signal) {
|
|
1965
|
+
if (options.typeOfService != null && (!Number.isInteger(options.typeOfService) || options.typeOfService < 0 || options.typeOfService > 255)) throw Object.assign(/* @__PURE__ */ new Error("typeOfService must be an integer between 0 and 255"), {
|
|
1966
|
+
name: "InvalidArgumentError",
|
|
1967
|
+
code: "UND_ERR_INVALID_ARG"
|
|
1968
|
+
});
|
|
1969
|
+
let payload = body;
|
|
1970
|
+
if (body && typeof body === "object" && !(body instanceof Uint8Array) && !(body instanceof node_stream.Readable)) {
|
|
1971
|
+
let form;
|
|
1972
|
+
if (body instanceof FormData) form = body;
|
|
1973
|
+
else {
|
|
1974
|
+
form = new FormData();
|
|
1975
|
+
for (const [name, value] of body) if (typeof value === "string") form.append(name, value);
|
|
1976
|
+
else if ("name" in value && typeof value.name === "string") form.append(name, value, value.name);
|
|
1977
|
+
else form.append(name, value);
|
|
1978
|
+
}
|
|
1979
|
+
const encoded = new Request(url, {
|
|
1980
|
+
method: "POST",
|
|
1981
|
+
body: form
|
|
1982
|
+
});
|
|
1983
|
+
payload = node_stream.Readable.from((async function* () {
|
|
1984
|
+
let previous = -1;
|
|
1985
|
+
let last = -1;
|
|
1986
|
+
for await (const chunk of node_stream.Readable.fromWeb(encoded.body)) {
|
|
1987
|
+
const bytes = chunk;
|
|
1988
|
+
if (bytes.length >= 2) previous = bytes[bytes.length - 2];
|
|
1989
|
+
else if (bytes.length === 1) previous = last;
|
|
1990
|
+
if (bytes.length) last = bytes[bytes.length - 1];
|
|
1991
|
+
yield bytes;
|
|
1992
|
+
}
|
|
1993
|
+
if (previous !== 13 || last !== 10) yield Buffer.from("\r\n");
|
|
1994
|
+
})());
|
|
1995
|
+
if (!requestHeaders(options.headers).has("content-type")) headers.set("content-type", encoded.headers.get("content-type"));
|
|
1996
|
+
}
|
|
1997
|
+
if (payload != null && !(payload instanceof node_stream.Readable) && !(typeof payload === "object" && "getReader" in payload) && !headers.has("content-length") && !headers.has("transfer-encoding")) headers.set("content-length", String(typeof payload === "string" ? Buffer.byteLength(payload) : payload.byteLength));
|
|
1998
|
+
const declaredLength = headers.has("content-length") ? Number(headers.get("content-length")) : void 0;
|
|
1999
|
+
const lengthError = () => Object.assign(/* @__PURE__ */ new Error("Request body length does not match content-length header"), {
|
|
2000
|
+
name: "RequestContentLengthMismatchError",
|
|
2001
|
+
code: "UND_ERR_REQ_CONTENT_LENGTH_MISMATCH"
|
|
2002
|
+
});
|
|
2003
|
+
const knownLength = typeof payload === "string" ? Buffer.byteLength(payload) : payload instanceof Uint8Array ? payload.byteLength : void 0;
|
|
2004
|
+
if (declaredLength != null && knownLength != null && declaredLength !== knownLength) throw lengthError();
|
|
2005
|
+
if (options.reset === true && !headers.has("connection")) headers.set("connection", "close");
|
|
2006
|
+
return new Promise((resolve, reject) => {
|
|
2007
|
+
let informationalError;
|
|
2008
|
+
const request = (url.protocol === "https:" ? node_https.request : node_http.request)(url, {
|
|
2009
|
+
method,
|
|
2010
|
+
headers: Object.fromEntries(headers),
|
|
2011
|
+
signal,
|
|
2012
|
+
...typeof options.highWaterMark === "number" ? { highWaterMark: options.highWaterMark } : {}
|
|
2013
|
+
}, (response) => {
|
|
2014
|
+
if (informationalError) {
|
|
2015
|
+
response.destroy();
|
|
2016
|
+
return;
|
|
2017
|
+
}
|
|
2018
|
+
const rawHeaders = {};
|
|
2019
|
+
for (let index = 0; index < response.rawHeaders.length; index += 2) {
|
|
2020
|
+
const name = response.rawHeaders[index].toLowerCase();
|
|
2021
|
+
const value = response.rawHeaders[index + 1];
|
|
2022
|
+
if (!Object.hasOwn(rawHeaders, name)) Object.defineProperty(rawHeaders, name, {
|
|
2023
|
+
value,
|
|
2024
|
+
enumerable: true,
|
|
2025
|
+
writable: true,
|
|
2026
|
+
configurable: true
|
|
2027
|
+
});
|
|
2028
|
+
else rawHeaders[name] = [...Array.isArray(rawHeaders[name]) ? rawHeaders[name] : [rawHeaders[name]], value];
|
|
2029
|
+
}
|
|
2030
|
+
const responseHeaders = new Headers();
|
|
2031
|
+
for (const [name, value] of Object.entries(rawHeaders)) {
|
|
2032
|
+
if (value == null) continue;
|
|
2033
|
+
for (const item of Array.isArray(value) ? name === "content-type" ? value.slice(0, 1) : value : [value]) responseHeaders.append(name, item);
|
|
2034
|
+
}
|
|
2035
|
+
const status = response.statusCode ?? 0;
|
|
2036
|
+
resolve({
|
|
2037
|
+
status,
|
|
2038
|
+
ok: status >= 200 && status < 300,
|
|
2039
|
+
headers: responseHeaders,
|
|
2040
|
+
rawHeaders,
|
|
2041
|
+
body: node_stream.Readable.toWeb(response)
|
|
2042
|
+
});
|
|
2043
|
+
});
|
|
2044
|
+
request.once("error", (error) => reject(signal.aborted ? signal.reason : error));
|
|
2045
|
+
request.on("information", (information) => {
|
|
2046
|
+
try {
|
|
2047
|
+
options.onInfo?.({
|
|
2048
|
+
statusCode: information.statusCode,
|
|
2049
|
+
headers: information.headers
|
|
2050
|
+
});
|
|
2051
|
+
} catch (error) {
|
|
2052
|
+
informationalError = error;
|
|
2053
|
+
reject(error);
|
|
2054
|
+
request.destroy(error);
|
|
2055
|
+
}
|
|
2056
|
+
});
|
|
2057
|
+
request.once("socket", (socket) => {
|
|
2058
|
+
const transport = socket;
|
|
2059
|
+
try {
|
|
2060
|
+
if (transport.setTypeOfService && options.typeOfService != null) transport.setTypeOfService(options.typeOfService);
|
|
2061
|
+
} catch (error) {
|
|
2062
|
+
request.destroy(error);
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
if (socket.connecting) {
|
|
2066
|
+
const timer = setTimeout(() => request.destroy(Object.assign(/* @__PURE__ */ new Error("Connect Timeout Error"), {
|
|
2067
|
+
name: "ConnectTimeoutError",
|
|
2068
|
+
code: "UND_ERR_CONNECT_TIMEOUT"
|
|
2069
|
+
})), 1e4);
|
|
2070
|
+
timer.unref();
|
|
2071
|
+
socket.once(url.protocol === "https:" ? "secureConnect" : "connect", () => clearTimeout(timer));
|
|
2072
|
+
request.once("close", () => clearTimeout(timer));
|
|
2073
|
+
}
|
|
2074
|
+
});
|
|
2075
|
+
if (payload instanceof node_stream.Readable || payload && typeof payload === "object" && "getReader" in payload) {
|
|
2076
|
+
const source = payload instanceof node_stream.Readable ? payload : node_stream.Readable.fromWeb(payload);
|
|
2077
|
+
if (declaredLength != null) {
|
|
2078
|
+
let sent = 0;
|
|
2079
|
+
source.on("data", (chunk) => {
|
|
2080
|
+
sent += typeof chunk === "string" ? Buffer.byteLength(chunk) : chunk.byteLength;
|
|
2081
|
+
if (sent > declaredLength) {
|
|
2082
|
+
request.destroy(lengthError());
|
|
2083
|
+
source.destroy();
|
|
2084
|
+
}
|
|
2085
|
+
});
|
|
2086
|
+
source.once("end", () => {
|
|
2087
|
+
if (sent !== declaredLength) request.destroy(lengthError());
|
|
2088
|
+
});
|
|
2089
|
+
}
|
|
2090
|
+
source.once("error", (error) => request.destroy(error));
|
|
2091
|
+
request.once("close", () => {
|
|
2092
|
+
if (!source.destroyed) source.destroy();
|
|
2093
|
+
});
|
|
2094
|
+
source.pipe(request);
|
|
2095
|
+
} else request.end(payload);
|
|
2096
|
+
});
|
|
2097
|
+
}
|
|
2098
|
+
/** A Node writable that feeds the native parser with bounded backpressure. */
|
|
2099
|
+
function stringStream(options, callback) {
|
|
2100
|
+
const complete = completion(callback);
|
|
2101
|
+
const opts = flattenOptions(options);
|
|
2102
|
+
const xml = !!opts._useHtmlParser2;
|
|
2103
|
+
const binding = require_nodes.requireNative();
|
|
2104
|
+
let native;
|
|
2105
|
+
let writeNative;
|
|
2106
|
+
let finishNative;
|
|
2107
|
+
let abortNative;
|
|
2108
|
+
let carry = "";
|
|
2109
|
+
if (opts._useHtmlParser2 && opts.Tokenizer) {
|
|
2110
|
+
const session = require_nodes.createTokenizerSession(opts);
|
|
2111
|
+
const chunks = [];
|
|
2112
|
+
writeNative = async (chunk) => {
|
|
2113
|
+
chunks.push(chunk);
|
|
2114
|
+
session.write(chunk);
|
|
2115
|
+
};
|
|
2116
|
+
finishNative = async () => {
|
|
2117
|
+
const document = new binding.NativeDocument(chunks.join(""), true, JSON.stringify(session.end()));
|
|
2118
|
+
chunks.length = 0;
|
|
2119
|
+
return createAPI(require_nodes.routeDocument(document, opts), opts);
|
|
2120
|
+
};
|
|
2121
|
+
abortNative = () => {
|
|
2122
|
+
chunks.length = 0;
|
|
2123
|
+
};
|
|
2124
|
+
} else if (!opts._useHtmlParser2 && opts.treeAdapter) {
|
|
2125
|
+
const session = require_nodes.createTreeAdapterSession(opts, true);
|
|
2126
|
+
writeNative = (chunk) => session.write(chunk);
|
|
2127
|
+
finishNative = async () => createAPI(await session.end(), opts);
|
|
2128
|
+
abortNative = () => session.abort();
|
|
2129
|
+
} else if (!opts._useHtmlParser2 && opts.onParseError) {
|
|
2130
|
+
const session = require_nodes.createDiagnosticSession(opts, true);
|
|
2131
|
+
writeNative = (chunk) => session.write(chunk);
|
|
2132
|
+
finishNative = async () => createAPI(await session.end(), opts);
|
|
2133
|
+
abortNative = () => session.abort();
|
|
2134
|
+
} else {
|
|
2135
|
+
if (!binding.NativeStream) throw new Error("This native build does not support incremental parsing");
|
|
2136
|
+
native = new binding.NativeStream(JSON.stringify(require_nodes.parserOptions("", opts)), true);
|
|
2137
|
+
writeNative = (chunk) => native.write(chunk);
|
|
2138
|
+
finishNative = async () => {
|
|
2139
|
+
const document = await native.finish();
|
|
2140
|
+
require_nodes.deliverParseErrors(document, opts);
|
|
2141
|
+
return createAPI(require_nodes.routeDocument(document, opts), opts);
|
|
2142
|
+
};
|
|
2143
|
+
abortNative = () => native.abort();
|
|
2144
|
+
}
|
|
2145
|
+
const stream = new node_stream.Writable({
|
|
2146
|
+
decodeStrings: false,
|
|
2147
|
+
write(chunk, _encoding, done) {
|
|
2148
|
+
if (typeof chunk !== "string") throw new TypeError(xml ? "Expected a string" : "Parser can work only with string streams.");
|
|
2149
|
+
if (opts._useHtmlParser2 && opts.Tokenizer) {
|
|
2150
|
+
writeNative(chunk).then(() => done(), (error) => done(error));
|
|
2151
|
+
return;
|
|
2152
|
+
}
|
|
2153
|
+
let text = carry + chunk;
|
|
2154
|
+
carry = "";
|
|
2155
|
+
const last = text.charCodeAt(text.length - 1);
|
|
2156
|
+
if (last >= 55296 && last <= 56319) {
|
|
2157
|
+
carry = text.slice(-1);
|
|
2158
|
+
text = text.slice(0, -1);
|
|
2159
|
+
}
|
|
2160
|
+
if (!text) {
|
|
2161
|
+
done();
|
|
2162
|
+
return;
|
|
2163
|
+
}
|
|
2164
|
+
writeNative(text).then(() => done(), (error) => done(error));
|
|
2165
|
+
},
|
|
2166
|
+
final(done) {
|
|
2167
|
+
(async () => {
|
|
2168
|
+
if (carry) await writeNative(carry);
|
|
2169
|
+
return finishNative();
|
|
2170
|
+
})().then((document) => {
|
|
2171
|
+
complete(null, document);
|
|
2172
|
+
done();
|
|
2173
|
+
}, (error) => done(error));
|
|
2174
|
+
},
|
|
2175
|
+
destroy(error, done) {
|
|
2176
|
+
abortNative();
|
|
2177
|
+
done(error);
|
|
2178
|
+
}
|
|
2179
|
+
});
|
|
2180
|
+
stream.on("error", (error) => complete(error));
|
|
2181
|
+
return stream;
|
|
2182
|
+
}
|
|
2183
|
+
/** Sniff a bounded prefix, then decode and parse each chunk as it arrives. */
|
|
2184
|
+
function decodeStream(options, callback) {
|
|
2185
|
+
const complete = completion(callback);
|
|
2186
|
+
const { encoding = {}, ...parserOptions } = options ?? {};
|
|
2187
|
+
const opts = flattenOptions(parserOptions);
|
|
2188
|
+
const decoderOptions = {
|
|
2189
|
+
defaultEncoding: opts.xmlMode ? "utf-8" : "windows-1252",
|
|
2190
|
+
...encoding
|
|
2191
|
+
};
|
|
2192
|
+
const maxBytes = decoderOptions.maxBytes ?? 1024;
|
|
2193
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0) throw new RangeError("encoding.maxBytes must be a non-negative integer");
|
|
2194
|
+
const prefixLimit = Math.max(6, maxBytes);
|
|
2195
|
+
const prefix = [];
|
|
2196
|
+
let prefixLength = 0;
|
|
2197
|
+
let decoder;
|
|
2198
|
+
let userDefined = false;
|
|
2199
|
+
let started = false;
|
|
2200
|
+
const parser = stringStream(opts, (error, document) => complete(error, document));
|
|
2201
|
+
let downstreamError;
|
|
2202
|
+
parser.on("error", (error) => {
|
|
2203
|
+
downstreamError = error;
|
|
2204
|
+
stream.destroy(error);
|
|
2205
|
+
});
|
|
2206
|
+
const writeText = (text) => new Promise((resolve, reject) => {
|
|
2207
|
+
if (downstreamError) {
|
|
2208
|
+
reject(downstreamError);
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
if (!text) {
|
|
2212
|
+
resolve();
|
|
2213
|
+
return;
|
|
2214
|
+
}
|
|
2215
|
+
parser.write(text, (error) => error ? reject(error) : resolve());
|
|
2216
|
+
});
|
|
2217
|
+
const decode = (bytes, final = false) => {
|
|
2218
|
+
if (userDefined) {
|
|
2219
|
+
let text = "";
|
|
2220
|
+
for (const byte of bytes) text += String.fromCharCode(byte < 128 ? byte : 63232 + byte);
|
|
2221
|
+
return text;
|
|
2222
|
+
}
|
|
2223
|
+
return decoder.decode(bytes, { stream: !final });
|
|
2224
|
+
};
|
|
2225
|
+
const start = async () => {
|
|
2226
|
+
const bytes = Buffer.concat(prefix, prefixLength);
|
|
2227
|
+
const label = sniffEncoding(bytes, decoderOptions);
|
|
2228
|
+
userDefined = label === "x-user-defined";
|
|
2229
|
+
if (!userDefined) decoder = new TextDecoder(label);
|
|
2230
|
+
started = true;
|
|
2231
|
+
prefix.length = 0;
|
|
2232
|
+
await writeText(decode(bytes));
|
|
2233
|
+
};
|
|
2234
|
+
const accept = async (bytes) => {
|
|
2235
|
+
if (!started) {
|
|
2236
|
+
const count = Math.min(prefixLimit - prefixLength, bytes.length);
|
|
2237
|
+
if (count) {
|
|
2238
|
+
prefix.push(Buffer.from(bytes.subarray(0, count)));
|
|
2239
|
+
prefixLength += count;
|
|
2240
|
+
}
|
|
2241
|
+
bytes = bytes.subarray(count);
|
|
2242
|
+
const declaredEncoding = encodingLabel(decoderOptions.userEncoding) || encodingLabel(decoderOptions.transportLayerEncodingLabel);
|
|
2243
|
+
if (prefixLength >= prefixLimit || declaredEncoding && prefixLength >= 3) await start();
|
|
2244
|
+
}
|
|
2245
|
+
if (started && bytes.length) await writeText(decode(bytes));
|
|
2246
|
+
};
|
|
2247
|
+
const stream = new node_stream.Writable({
|
|
2248
|
+
write(chunk, encoding, done) {
|
|
2249
|
+
accept(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding)).then(() => done(), (error) => done(error));
|
|
2250
|
+
},
|
|
2251
|
+
final(done) {
|
|
2252
|
+
(async () => {
|
|
2253
|
+
if (!started) await start();
|
|
2254
|
+
await writeText(decode(/* @__PURE__ */ new Uint8Array(), true));
|
|
2255
|
+
await new Promise((resolve, reject) => {
|
|
2256
|
+
parser.once("error", reject);
|
|
2257
|
+
parser.end(() => {
|
|
2258
|
+
parser.removeListener("error", reject);
|
|
2259
|
+
resolve();
|
|
2260
|
+
});
|
|
2261
|
+
});
|
|
2262
|
+
})().then(() => done(), (error) => done(error));
|
|
2263
|
+
},
|
|
2264
|
+
destroy(error, done) {
|
|
2265
|
+
parser.destroy(error ?? void 0);
|
|
2266
|
+
done(error);
|
|
2267
|
+
}
|
|
2268
|
+
});
|
|
2269
|
+
stream.on("error", (error) => complete(error));
|
|
2270
|
+
return stream;
|
|
2271
|
+
}
|
|
2272
|
+
/** Load HTML or XML with bounded redirects through Node's built-in transport. */
|
|
2273
|
+
async function fromURL(url, options = {}) {
|
|
2274
|
+
const signal = options.requestOptions?.signal;
|
|
2275
|
+
if (!signal) return loadURL(url, options);
|
|
2276
|
+
if (typeof signal !== "object" && typeof signal !== "function") throw new TypeError("signal must be an AbortSignal or EventEmitter");
|
|
2277
|
+
if ("addEventListener" in signal) return loadURL(url, options);
|
|
2278
|
+
if (!("once" in signal) || !("removeListener" in signal)) throw new TypeError("signal must be an AbortSignal or EventEmitter");
|
|
2279
|
+
const emitter = signal;
|
|
2280
|
+
const controller = new AbortController();
|
|
2281
|
+
const abort = () => controller.abort();
|
|
2282
|
+
emitter.once("abort", abort);
|
|
2283
|
+
try {
|
|
2284
|
+
return await loadURL(url, {
|
|
2285
|
+
...options,
|
|
2286
|
+
requestOptions: {
|
|
2287
|
+
...options.requestOptions,
|
|
2288
|
+
signal: controller.signal
|
|
2289
|
+
}
|
|
2290
|
+
});
|
|
2291
|
+
} finally {
|
|
2292
|
+
emitter.removeListener("abort", abort);
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
async function loadURL(url, options) {
|
|
2296
|
+
const { requestOptions = {}, encoding, ...parserOptions } = options;
|
|
2297
|
+
const maxRedirects = requestOptions.maxRedirections ?? 5;
|
|
2298
|
+
if (!Number.isInteger(maxRedirects) || maxRedirects < 0) throw new RangeError("maxRedirections must be a non-negative integer");
|
|
2299
|
+
const headers = requestHeaders(requestOptions.headers);
|
|
2300
|
+
if (!Object.hasOwn(requestOptions, "headers")) headers.set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
|
2301
|
+
let current = new URL(url);
|
|
2302
|
+
if (requestOptions.query) {
|
|
2303
|
+
if (current.search || current.hash) throw new TypeError("URL must not contain a query or fragment when requestOptions.query is supplied");
|
|
2304
|
+
current.search = (0, node_querystring.stringify)(requestOptions.query);
|
|
2305
|
+
}
|
|
2306
|
+
let method = requestOptions.method ?? "GET";
|
|
2307
|
+
let body = requestOptions.body;
|
|
2308
|
+
let response;
|
|
2309
|
+
const controller = new AbortController();
|
|
2310
|
+
const signal = requestOptions.signal ? AbortSignal.any([requestOptions.signal, controller.signal]) : controller.signal;
|
|
2311
|
+
const timeoutError = (kind) => Object.assign(/* @__PURE__ */ new Error(`${kind} Timeout Error`), {
|
|
2312
|
+
name: `${kind}TimeoutError`,
|
|
2313
|
+
code: kind === "Headers" ? "UND_ERR_HEADERS_TIMEOUT" : "UND_ERR_BODY_TIMEOUT"
|
|
2314
|
+
});
|
|
2315
|
+
for (const timeout of [requestOptions.headersTimeout, requestOptions.bodyTimeout]) if (timeout != null && (!Number.isFinite(timeout) || timeout < 0)) throw new RangeError("HTTP timeouts must be non-negative finite numbers");
|
|
2316
|
+
const suppliedDispatcher = requestOptions.dispatcher;
|
|
2317
|
+
if (suppliedDispatcher && typeof suppliedDispatcher.dispatch !== "function") throw new TypeError("requestOptions.dispatcher must implement dispatch");
|
|
2318
|
+
const dispatcher = suppliedDispatcher && { dispatch(dispatchOptions, handler) {
|
|
2319
|
+
const { dispatcher: _dispatcher, throwOnError: _throwOnError, maxRedirections: _maxRedirections, query: _query, ...transport } = requestOptions;
|
|
2320
|
+
return suppliedDispatcher.dispatch({
|
|
2321
|
+
...transport,
|
|
2322
|
+
...dispatchOptions
|
|
2323
|
+
}, handler);
|
|
2324
|
+
} };
|
|
2325
|
+
for (let redirects = 0;; redirects += 1) {
|
|
2326
|
+
let timer;
|
|
2327
|
+
const headersTimeout = requestOptions.headersTimeout ?? 3e5;
|
|
2328
|
+
if (headersTimeout) {
|
|
2329
|
+
timer = setTimeout(() => controller.abort(timeoutError("Headers")), headersTimeout);
|
|
2330
|
+
timer.unref();
|
|
2331
|
+
}
|
|
2332
|
+
try {
|
|
2333
|
+
if (dispatcher) {
|
|
2334
|
+
const init = {
|
|
2335
|
+
method,
|
|
2336
|
+
headers,
|
|
2337
|
+
body,
|
|
2338
|
+
redirect: "manual",
|
|
2339
|
+
signal,
|
|
2340
|
+
dispatcher
|
|
2341
|
+
};
|
|
2342
|
+
if (body != null && typeof body !== "string") init.duplex = "half";
|
|
2343
|
+
response = await fetch(current, init);
|
|
2344
|
+
} else response = await requestDocument(current, method, headers, body, requestOptions, signal);
|
|
2345
|
+
} catch (error) {
|
|
2346
|
+
throw signal.aborted ? signal.reason : error;
|
|
2347
|
+
} finally {
|
|
2348
|
+
if (timer) clearTimeout(timer);
|
|
2349
|
+
}
|
|
2350
|
+
const location = response.headers.get("location");
|
|
2351
|
+
if (![
|
|
2352
|
+
300,
|
|
2353
|
+
301,
|
|
2354
|
+
302,
|
|
2355
|
+
303,
|
|
2356
|
+
307,
|
|
2357
|
+
308
|
|
2358
|
+
].includes(response.status) || !location) break;
|
|
2359
|
+
const rewriteToGet = response.status === 303 && method.toUpperCase() !== "HEAD" || (response.status === 301 || response.status === 302) && method.toUpperCase() === "POST";
|
|
2360
|
+
if (body instanceof node_stream.Readable && body.readableDidRead && !rewriteToGet) break;
|
|
2361
|
+
await response.body?.cancel();
|
|
2362
|
+
if (redirects >= maxRedirects) throw new Error("max redirects reached");
|
|
2363
|
+
const next = new URL(location, current);
|
|
2364
|
+
headers.delete("host");
|
|
2365
|
+
if (response.status === 303) {
|
|
2366
|
+
for (const name of [...headers.keys()]) if (name.startsWith("content-")) headers.delete(name);
|
|
2367
|
+
}
|
|
2368
|
+
if (next.origin !== current.origin) {
|
|
2369
|
+
headers.delete("authorization");
|
|
2370
|
+
headers.delete("cookie");
|
|
2371
|
+
headers.delete("proxy-authorization");
|
|
2372
|
+
}
|
|
2373
|
+
if (rewriteToGet) {
|
|
2374
|
+
method = "GET";
|
|
2375
|
+
body = void 0;
|
|
2376
|
+
headers.delete("content-length");
|
|
2377
|
+
headers.delete("content-type");
|
|
2378
|
+
}
|
|
2379
|
+
current = next;
|
|
2380
|
+
}
|
|
2381
|
+
if (!response.ok) {
|
|
2382
|
+
await response.body?.cancel();
|
|
2383
|
+
const error = /* @__PURE__ */ new Error("Response Error");
|
|
2384
|
+
error.name = "ResponseError";
|
|
2385
|
+
error.statusCode = response.status;
|
|
2386
|
+
error.code = "UND_ERR_RESPONSE";
|
|
2387
|
+
error.headers = response.rawHeaders ?? Object.fromEntries(response.headers);
|
|
2388
|
+
throw error;
|
|
2389
|
+
}
|
|
2390
|
+
const contentType = response.headers.get("content-type") ?? "text/html";
|
|
2391
|
+
let parsedType;
|
|
2392
|
+
try {
|
|
2393
|
+
parsedType = new node_util.MIMEType(contentType);
|
|
2394
|
+
} catch {
|
|
2395
|
+
await response.body?.cancel();
|
|
2396
|
+
throw new Error(`Could not parse MIME type string "${contentType}"`);
|
|
2397
|
+
}
|
|
2398
|
+
const mime = parsedType.essence;
|
|
2399
|
+
const xml = (parsedType.type === "application" || parsedType.type === "text") && parsedType.subtype === "xml" || parsedType.subtype.endsWith("+xml");
|
|
2400
|
+
if (mime !== "text/html" && !xml) {
|
|
2401
|
+
await response.body?.cancel();
|
|
2402
|
+
throw new RangeError(`The content-type "${mime}" is neither HTML nor XML.`);
|
|
2403
|
+
}
|
|
2404
|
+
const charset = parsedType.params.get("charset") ?? void 0;
|
|
2405
|
+
if (encoding) encoding.transportLayerEncodingLabel = charset;
|
|
2406
|
+
const streamOptions = {
|
|
2407
|
+
xmlMode: xml,
|
|
2408
|
+
baseURI: current,
|
|
2409
|
+
...parserOptions,
|
|
2410
|
+
encoding: {
|
|
2411
|
+
...encoding,
|
|
2412
|
+
transportLayerEncodingLabel: charset
|
|
2413
|
+
}
|
|
2414
|
+
};
|
|
2415
|
+
return new Promise((resolve, reject) => {
|
|
2416
|
+
const parser = decodeStream(streamOptions, (error, document) => error ? reject(error) : resolve(document));
|
|
2417
|
+
const reader = response.body?.getReader();
|
|
2418
|
+
parser.once("error", (error) => {
|
|
2419
|
+
controller.abort(error);
|
|
2420
|
+
reader?.cancel(error).catch(() => {});
|
|
2421
|
+
});
|
|
2422
|
+
(async () => {
|
|
2423
|
+
let received = 0;
|
|
2424
|
+
const maxResponseSize = requestOptions.maxResponseSize;
|
|
2425
|
+
if (maxResponseSize != null && (typeof maxResponseSize !== "number" || !Number.isSafeInteger(maxResponseSize) || maxResponseSize < -1)) throw new RangeError("maxResponseSize must be -1 or a non-negative integer");
|
|
2426
|
+
while (reader) {
|
|
2427
|
+
let timer;
|
|
2428
|
+
const bodyTimeout = requestOptions.bodyTimeout ?? 3e5;
|
|
2429
|
+
if (bodyTimeout) {
|
|
2430
|
+
timer = setTimeout(() => controller.abort(timeoutError("Body")), bodyTimeout);
|
|
2431
|
+
timer.unref();
|
|
2432
|
+
}
|
|
2433
|
+
let chunk;
|
|
2434
|
+
try {
|
|
2435
|
+
chunk = await reader.read();
|
|
2436
|
+
} catch (error) {
|
|
2437
|
+
throw controller.signal.aborted ? controller.signal.reason : error;
|
|
2438
|
+
} finally {
|
|
2439
|
+
if (timer) clearTimeout(timer);
|
|
2440
|
+
}
|
|
2441
|
+
if (chunk.done) break;
|
|
2442
|
+
received += chunk.value.byteLength;
|
|
2443
|
+
if (typeof maxResponseSize === "number" && maxResponseSize >= 0 && received > maxResponseSize) throw Object.assign(/* @__PURE__ */ new Error("Response content exceeded maxResponseSize"), {
|
|
2444
|
+
name: "ResponseExceededMaxSizeError",
|
|
2445
|
+
code: "UND_ERR_RES_EXCEEDED_MAX_SIZE"
|
|
2446
|
+
});
|
|
2447
|
+
await new Promise((resolve, reject) => parser.write(Buffer.from(chunk.value.buffer, chunk.value.byteOffset, chunk.value.byteLength), (error) => error ? reject(error) : resolve()));
|
|
2448
|
+
}
|
|
2449
|
+
parser.end();
|
|
2450
|
+
})().catch((error) => {
|
|
2451
|
+
parser.destroy(error);
|
|
2452
|
+
reject(error);
|
|
2453
|
+
});
|
|
2454
|
+
});
|
|
2455
|
+
}
|
|
2456
|
+
// src/modern.ts
|
|
2457
|
+
const dom = {
|
|
2458
|
+
Node: require_nodes.Node,
|
|
2459
|
+
DataNode: require_nodes.DataNode,
|
|
2460
|
+
NodeWithChildren: require_nodes.NodeWithChildren,
|
|
2461
|
+
Document: require_nodes.Document,
|
|
2462
|
+
Element: require_nodes.Element,
|
|
2463
|
+
Text: require_nodes.Text,
|
|
2464
|
+
Comment: require_nodes.Comment,
|
|
2465
|
+
ProcessingInstruction: require_nodes.ProcessingInstruction,
|
|
2466
|
+
CDATA: require_nodes.CDATA,
|
|
2467
|
+
ElementType: require_nodes.ElementType,
|
|
2468
|
+
isTag: require_nodes.isTag,
|
|
2469
|
+
isText: require_nodes.isText,
|
|
2470
|
+
isComment: require_nodes.isComment,
|
|
2471
|
+
isDirective: require_nodes.isDirective,
|
|
2472
|
+
isDocument: require_nodes.isDocument,
|
|
2473
|
+
isCDATA: require_nodes.isCDATA,
|
|
2474
|
+
hasChildren: require_nodes.hasChildren
|
|
2475
|
+
};
|
|
2476
|
+
const defaultOptions = { _useHtmlParser2: false };
|
|
2477
|
+
/** Cheerio's option merge: without overrides, calls share the base options object. */
|
|
2478
|
+
function flattenOptions(options, base) {
|
|
2479
|
+
if (!options) return base ?? defaultOptions;
|
|
2480
|
+
const result = {
|
|
2481
|
+
_useHtmlParser2: false,
|
|
2482
|
+
...base,
|
|
2483
|
+
...options
|
|
2484
|
+
};
|
|
2485
|
+
if (options?.xml) {
|
|
2486
|
+
result._useHtmlParser2 = true;
|
|
2487
|
+
result.xmlMode = true;
|
|
2488
|
+
if (options.xml !== true) Object.assign(result, options.xml);
|
|
2489
|
+
} else if (options?.xmlMode) result._useHtmlParser2 = true;
|
|
2490
|
+
return result;
|
|
2491
|
+
}
|
|
2492
|
+
/** A synchronous, constructable Cheerio factory backed by a mutable Rust DOM. */
|
|
2493
|
+
function createAPI(document, options, initialRoot = document.rootId()) {
|
|
2494
|
+
const internal = flattenOptions(options);
|
|
2495
|
+
class LoadedCheerio extends Cheerio {}
|
|
2496
|
+
let api;
|
|
2497
|
+
const createRoot = (rootIds, opts) => {
|
|
2498
|
+
const rootInstance = new LoadedCheerio(api, document, rootIds, null, opts);
|
|
2499
|
+
rootInstance._root = rootInstance;
|
|
2500
|
+
return rootInstance;
|
|
2501
|
+
};
|
|
2502
|
+
const initialize = function(selector, context, root, override) {
|
|
2503
|
+
require_nodes.resumeCollection(document);
|
|
2504
|
+
if (selector?.cheerio) {
|
|
2505
|
+
require_nodes.exposeDocumentTo(document, selector[require_nodes.DOCUMENT]);
|
|
2506
|
+
return selector;
|
|
2507
|
+
}
|
|
2508
|
+
const opts = flattenOptions(override, internal);
|
|
2509
|
+
const rootInstance = createRoot(root === void 0 ? [initialRoot] : typeof root === "string" ? [makeRoot(document, require_nodes.operation(document, "create", {
|
|
2510
|
+
html: root,
|
|
2511
|
+
options: opts
|
|
2512
|
+
}))] : root?.cheerio ? require_nodes.importNodes(document, root.toArray()) : require_nodes.importNodes(document, root), opts);
|
|
2513
|
+
if (!selector) return new LoadedCheerio(api, document, [], rootInstance, opts);
|
|
2514
|
+
const id = require_nodes.viewIdIn(document, selector);
|
|
2515
|
+
if (id !== void 0) return new LoadedCheerio(api, document, [id], rootInstance, opts);
|
|
2516
|
+
if (typeof selector === "string" && require_utils.isHtml(selector)) return new LoadedCheerio(api, document, require_nodes.operation(document, "create", {
|
|
2517
|
+
html: selector,
|
|
2518
|
+
options: opts
|
|
2519
|
+
}), rootInstance, opts);
|
|
2520
|
+
if (typeof selector === "object") {
|
|
2521
|
+
const values = "length" in selector && !selector.type ? Array.from(selector) : [selector];
|
|
2522
|
+
const allNodes = values.every((value) => value && typeof value === "object" && typeof value.type === "string");
|
|
2523
|
+
return new LoadedCheerio(api, document, allNodes ? require_nodes.importNodes(document, values) : [], rootInstance, opts, allNodes ? void 0 : values);
|
|
2524
|
+
}
|
|
2525
|
+
if (typeof selector !== "string") throw new TypeError("Unexpected type of selector");
|
|
2526
|
+
let search = selector;
|
|
2527
|
+
let searchContext = rootInstance;
|
|
2528
|
+
if (context) {
|
|
2529
|
+
if (typeof context === "string") {
|
|
2530
|
+
if (require_utils.isHtml(context)) searchContext = new LoadedCheerio(api, document, [makeRoot(document, require_nodes.operation(document, "create", {
|
|
2531
|
+
html: context,
|
|
2532
|
+
options: opts
|
|
2533
|
+
}))], rootInstance, opts);
|
|
2534
|
+
else search = `${context} ${search}`;
|
|
2535
|
+
} else if (context.cheerio) searchContext = context;
|
|
2536
|
+
else searchContext = new LoadedCheerio(api, document, require_nodes.importNodes(document, context), rootInstance, opts);
|
|
2537
|
+
}
|
|
2538
|
+
return searchContext.find(search);
|
|
2539
|
+
};
|
|
2540
|
+
api = Object.assign(initialize, staticMethods, {
|
|
2541
|
+
load,
|
|
2542
|
+
_root: require_nodes.rootNode(document, initialRoot),
|
|
2543
|
+
_options: internal,
|
|
2544
|
+
fn: LoadedCheerio.prototype,
|
|
2545
|
+
prototype: LoadedCheerio.prototype
|
|
2546
|
+
});
|
|
2547
|
+
return api;
|
|
2548
|
+
}
|
|
2549
|
+
const staticMethods = {
|
|
2550
|
+
root() {
|
|
2551
|
+
return this(this._root);
|
|
2552
|
+
},
|
|
2553
|
+
html(dom, renderOptions) {
|
|
2554
|
+
if (!this) return "";
|
|
2555
|
+
if (dom && typeof dom === "object" && !("length" in dom) && !("type" in dom)) {
|
|
2556
|
+
renderOptions = dom;
|
|
2557
|
+
dom = void 0;
|
|
2558
|
+
}
|
|
2559
|
+
const nodes = dom === void 0 ? [this._root] : this(dom).toArray();
|
|
2560
|
+
const binding = require_nodes.nodeBinding(this._root);
|
|
2561
|
+
const base = this._options;
|
|
2562
|
+
const options = !renderOptions && (!base._useHtmlParser2 && !base.xml && !base.xmlMode || base.xml === true && base.xmlMode === true && base._useHtmlParser2 === true) ? base : flattenOptions({
|
|
2563
|
+
...base,
|
|
2564
|
+
...flattenOptions(renderOptions)
|
|
2565
|
+
}, base);
|
|
2566
|
+
return require_nodes.operation(binding.document, "render", {
|
|
2567
|
+
ids: require_nodes.importNodes(binding.document, nodes),
|
|
2568
|
+
options
|
|
2569
|
+
});
|
|
2570
|
+
},
|
|
2571
|
+
xml(dom) {
|
|
2572
|
+
const binding = require_nodes.nodeBinding(this._root);
|
|
2573
|
+
return require_nodes.operation(binding.document, "render", {
|
|
2574
|
+
ids: require_nodes.importNodes(binding.document, dom === void 0 ? [this._root] : this(dom).toArray()),
|
|
2575
|
+
options: {
|
|
2576
|
+
...this._options,
|
|
2577
|
+
xmlMode: true,
|
|
2578
|
+
_useHtmlParser2: true
|
|
2579
|
+
}
|
|
2580
|
+
});
|
|
2581
|
+
},
|
|
2582
|
+
text(elements) {
|
|
2583
|
+
if (!elements && !this) return "";
|
|
2584
|
+
const nodes = elements ?? [this._root];
|
|
2585
|
+
const binding = this ? require_nodes.nodeBinding(this._root) : require_nodes.nodeBinding(nodes[0]);
|
|
2586
|
+
if (binding) return binding.document.text(require_nodes.importNodes(binding.document, nodes));
|
|
2587
|
+
const doc = require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument("", false));
|
|
2588
|
+
return doc.text(require_nodes.importNodes(doc, nodes));
|
|
2589
|
+
},
|
|
2590
|
+
parseHTML(data, context, keepScripts = typeof context === "boolean" ? context : false) {
|
|
2591
|
+
if (!data || typeof data !== "string") return null;
|
|
2592
|
+
if (typeof context === "boolean") keepScripts = context;
|
|
2593
|
+
const parsed = this.load(data, this._options, false);
|
|
2594
|
+
if (!keepScripts) parsed("script").remove();
|
|
2595
|
+
return parsed._root.children.slice();
|
|
2596
|
+
},
|
|
2597
|
+
extract(map) {
|
|
2598
|
+
return this.root().extract(map);
|
|
2599
|
+
},
|
|
2600
|
+
contains,
|
|
2601
|
+
merge
|
|
2602
|
+
};
|
|
2603
|
+
function makeRoot(document, children) {
|
|
2604
|
+
const id = require_nodes.operation(document, "createNode", { type: "root" });
|
|
2605
|
+
require_nodes.operation(document, "setNode", {
|
|
2606
|
+
id,
|
|
2607
|
+
field: "children",
|
|
2608
|
+
value: children
|
|
2609
|
+
});
|
|
2610
|
+
return id;
|
|
2611
|
+
}
|
|
2612
|
+
function load(content, options, isDocument = true) {
|
|
2613
|
+
if (content == null) throw new Error("cheerio.load() expects a string");
|
|
2614
|
+
const opts = flattenOptions(options);
|
|
2615
|
+
const binding = require_nodes.requireNative();
|
|
2616
|
+
const string = Buffer.isBuffer(content) ? content.toString() : typeof content === "string" ? content : void 0;
|
|
2617
|
+
const document = string === void 0 ? require_nodes.routeDocument(new binding.NativeDocument("", false, JSON.stringify(require_nodes.cleanParserOptions(opts))), opts) : require_nodes.parseNativeDocument(string, opts, isDocument);
|
|
2618
|
+
if (string !== void 0) return createAPI(document, opts);
|
|
2619
|
+
const nodes = require_nodes.importNodes(document, content);
|
|
2620
|
+
if (!Array.isArray(content) && content.type === "root") return createAPI(document, opts, nodes[0]);
|
|
2621
|
+
require_nodes.operation(document, "setNode", {
|
|
2622
|
+
id: document.rootId(),
|
|
2623
|
+
field: "children",
|
|
2624
|
+
value: nodes
|
|
2625
|
+
});
|
|
2626
|
+
return createAPI(require_nodes.routeDocument(document, opts), opts);
|
|
2627
|
+
}
|
|
2628
|
+
async function loadAsync(content, options, isDocument = true) {
|
|
2629
|
+
if (content == null) throw new Error("cheerio.load() expects a string");
|
|
2630
|
+
if (typeof content !== "string" && !Buffer.isBuffer(content)) return load(content, options, isDocument);
|
|
2631
|
+
const binding = require_nodes.requireNative();
|
|
2632
|
+
const opts = flattenOptions(options);
|
|
2633
|
+
const html = String(content);
|
|
2634
|
+
if (!opts._useHtmlParser2 && opts.treeAdapter) {
|
|
2635
|
+
const session = require_nodes.createTreeAdapterSession(opts, isDocument);
|
|
2636
|
+
try {
|
|
2637
|
+
await session.write(html);
|
|
2638
|
+
return createAPI(await session.end(), opts);
|
|
2639
|
+
} catch (error) {
|
|
2640
|
+
session.abort();
|
|
2641
|
+
throw error;
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
if (opts._useHtmlParser2 && opts.Tokenizer) return createAPI(require_nodes.parseNativeDocument(html, opts, isDocument), opts);
|
|
2645
|
+
const document = binding.parseAsync ? await binding.parseAsync(html, isDocument, JSON.stringify(require_nodes.parserOptions(html, opts))) : new binding.NativeDocument(html, isDocument, JSON.stringify(require_nodes.parserOptions(html, opts)));
|
|
2646
|
+
require_nodes.deliverParseErrors(document, opts);
|
|
2647
|
+
return createAPI(require_nodes.routeDocument(document, opts), opts);
|
|
2648
|
+
}
|
|
2649
|
+
function contains(container, contained) {
|
|
2650
|
+
if (!container || !contained || container === contained) return false;
|
|
2651
|
+
let parent = require_nodes.nodeBinding(container);
|
|
2652
|
+
if (!parent) {
|
|
2653
|
+
const document = require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument("", false));
|
|
2654
|
+
require_nodes.importNodes(document, [container]);
|
|
2655
|
+
parent = require_nodes.nodeBinding(container);
|
|
2656
|
+
}
|
|
2657
|
+
const child = require_nodes.nodeBinding(contained);
|
|
2658
|
+
return !!parent && !!child && require_nodes.sameNodeSpace(child.document, parent.document) && require_nodes.operation(parent.document, "contains", {
|
|
2659
|
+
container: parent.id,
|
|
2660
|
+
contained: child.id
|
|
2661
|
+
});
|
|
2662
|
+
}
|
|
2663
|
+
function merge(first, second) {
|
|
2664
|
+
const arrayLike = (value) => Array.isArray(value) || value && typeof value === "object" && typeof value.length === "number" && value.length >= 0 && Array.from({ length: value.length }, (_unused, i) => i in value).every(Boolean);
|
|
2665
|
+
if (!arrayLike(first) || !arrayLike(second)) return void 0;
|
|
2666
|
+
const writable = first;
|
|
2667
|
+
let length = first.length;
|
|
2668
|
+
for (let i = 0, count = +second.length; i < count; i++) writable[length++] = second[i];
|
|
2669
|
+
writable.length = length;
|
|
2670
|
+
return writable;
|
|
2671
|
+
}
|
|
2672
|
+
function parallelOptions(options) {
|
|
2673
|
+
const { threads = Math.min((0, node_os.availableParallelism)(), 256), ...opts } = options ?? {};
|
|
2674
|
+
if (!Number.isSafeInteger(threads) || threads < 1 || threads > 256) throw new RangeError("threads must be an integer from 1 to 256");
|
|
2675
|
+
return {
|
|
2676
|
+
options: flattenOptions(opts),
|
|
2677
|
+
threads
|
|
2678
|
+
};
|
|
2679
|
+
}
|
|
2680
|
+
async function loadAll(content, options, isDocument = true) {
|
|
2681
|
+
return loadAllWithPlans(content, options, isDocument);
|
|
2682
|
+
}
|
|
2683
|
+
/**
|
|
2684
|
+
* Loads documents in parallel. With `plans`, the parsing threads also run
|
|
2685
|
+
* them on each root and build each document's snapshot, which extraction
|
|
2686
|
+
* with callbacks would otherwise do on the calling thread when it needs them.
|
|
2687
|
+
*/
|
|
2688
|
+
async function loadAllWithPlans(content, options, isDocument, plans) {
|
|
2689
|
+
const { options: opts, threads } = parallelOptions(options);
|
|
2690
|
+
if (!Array.isArray(content) || content.some((item) => typeof item !== "string")) throw new TypeError("loadAll expects an array of HTML strings");
|
|
2691
|
+
if (!content.length) return [];
|
|
2692
|
+
const binding = require_nodes.requireNative();
|
|
2693
|
+
if (!binding.parseManyAsync || require_nodes.hasParserHooks(opts)) return Promise.all(content.map((html) => loadAsync(html, opts, isDocument)));
|
|
2694
|
+
return (await binding.parseManyAsync([...content], isDocument, threads, JSON.stringify(opts), plans && JSON.stringify(plans))).map((document) => {
|
|
2695
|
+
const routed = require_nodes.routeDocument(document, opts);
|
|
2696
|
+
if (plans) {
|
|
2697
|
+
require_nodes.usePreparedSnapshot(routed);
|
|
2698
|
+
prepareExtractPlans(routed, plans, document.takePreparedPlans?.());
|
|
2699
|
+
}
|
|
2700
|
+
return createAPI(routed, opts);
|
|
2701
|
+
});
|
|
2702
|
+
}
|
|
2703
|
+
function nativeQueries(map, options) {
|
|
2704
|
+
const queries = [];
|
|
2705
|
+
const plainProperties = /^(?:id|class|title|href|src|alt|name|data-.+|aria-.+)$/;
|
|
2706
|
+
for (const key in map) {
|
|
2707
|
+
const entry = map[key];
|
|
2708
|
+
const all = Array.isArray(entry);
|
|
2709
|
+
const descriptor = all ? entry[0] : entry;
|
|
2710
|
+
const selector = typeof descriptor === "string" ? descriptor : descriptor.selector;
|
|
2711
|
+
const value = typeof descriptor === "string" ? "textContent" : descriptor.value ?? "textContent";
|
|
2712
|
+
if (typeof value !== "string" || value !== "textContent" && !plainProperties.test(value) || options.baseURI && (value === "href" || value === "src") || options.pseudos) return void 0;
|
|
2713
|
+
queries.push({
|
|
2714
|
+
key,
|
|
2715
|
+
selector,
|
|
2716
|
+
all,
|
|
2717
|
+
...value === "textContent" ? {} : { attr: value }
|
|
2718
|
+
});
|
|
2719
|
+
}
|
|
2720
|
+
return queries;
|
|
2721
|
+
}
|
|
2722
|
+
async function extractAll(content, map, options) {
|
|
2723
|
+
const { options: opts, threads } = parallelOptions(options);
|
|
2724
|
+
if (!Array.isArray(content) || content.some((item) => typeof item !== "string")) throw new TypeError("extractAll expects an array of HTML strings");
|
|
2725
|
+
if (!content.length) return [];
|
|
2726
|
+
const binding = require_nodes.requireNative();
|
|
2727
|
+
const plan = require_nodes.hasParserHooks(opts) ? void 0 : compileExtractPlan(map, opts);
|
|
2728
|
+
if (binding.extractPlanManyAsync && plan) {
|
|
2729
|
+
const results = JSON.parse(await binding.extractPlanManyAsync([...content], JSON.stringify(plan), threads, JSON.stringify(opts)));
|
|
2730
|
+
for (const result of results) hydrateExtractResult(result, plan.entries);
|
|
2731
|
+
return results;
|
|
2732
|
+
}
|
|
2733
|
+
const queries = binding.extractPlanManyAsync || require_nodes.hasParserHooks(opts) ? void 0 : nativeQueries(map, opts);
|
|
2734
|
+
if (binding.extractManyAsync && queries) {
|
|
2735
|
+
const results = JSON.parse(await binding.extractManyAsync([...content], queries, threads, true, JSON.stringify(opts)));
|
|
2736
|
+
for (const result of results) for (const query of queries) {
|
|
2737
|
+
if (!Object.hasOwn(result, query.key)) Object.defineProperty(result, query.key, {
|
|
2738
|
+
value: query.all ? [] : void 0,
|
|
2739
|
+
configurable: true,
|
|
2740
|
+
writable: true,
|
|
2741
|
+
enumerable: true
|
|
2742
|
+
});
|
|
2743
|
+
if (query.all && Array.isArray(result[query.key])) result[query.key] = result[query.key].filter((value) => value != null);
|
|
2744
|
+
}
|
|
2745
|
+
return results;
|
|
2746
|
+
}
|
|
2747
|
+
const documents = await loadAllWithPlans(content, {
|
|
2748
|
+
...opts,
|
|
2749
|
+
threads
|
|
2750
|
+
}, true, require_nodes.hasParserHooks(opts) ? void 0 : rootExtractPlans(map, opts));
|
|
2751
|
+
const results = [];
|
|
2752
|
+
let yieldedAt = performance.now();
|
|
2753
|
+
for (const document of documents) {
|
|
2754
|
+
const root = document.root();
|
|
2755
|
+
try {
|
|
2756
|
+
results.push((await extractCooperatively(root, map)).value);
|
|
2757
|
+
} finally {
|
|
2758
|
+
releaseExtractPlans(root[require_nodes.DOCUMENT]);
|
|
2759
|
+
}
|
|
2760
|
+
if (performance.now() - yieldedAt >= 4) {
|
|
2761
|
+
await (0, node_timers_promises.setImmediate)();
|
|
2762
|
+
yieldedAt = performance.now();
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
return results;
|
|
2766
|
+
}
|
|
2767
|
+
Object.defineProperty(exports, "configureWorkers", {
|
|
2768
|
+
enumerable: true,
|
|
2769
|
+
get: function() {
|
|
2770
|
+
return configureWorkers;
|
|
2771
|
+
}
|
|
2772
|
+
});
|
|
2773
|
+
Object.defineProperty(exports, "contains", {
|
|
2774
|
+
enumerable: true,
|
|
2775
|
+
get: function() {
|
|
2776
|
+
return contains;
|
|
2777
|
+
}
|
|
2778
|
+
});
|
|
2779
|
+
Object.defineProperty(exports, "createAPI", {
|
|
2780
|
+
enumerable: true,
|
|
2781
|
+
get: function() {
|
|
2782
|
+
return createAPI;
|
|
2783
|
+
}
|
|
2784
|
+
});
|
|
2785
|
+
Object.defineProperty(exports, "decodeStream", {
|
|
2786
|
+
enumerable: true,
|
|
2787
|
+
get: function() {
|
|
2788
|
+
return decodeStream;
|
|
2789
|
+
}
|
|
2790
|
+
});
|
|
2791
|
+
Object.defineProperty(exports, "dom", {
|
|
2792
|
+
enumerable: true,
|
|
2793
|
+
get: function() {
|
|
2794
|
+
return dom;
|
|
2795
|
+
}
|
|
2796
|
+
});
|
|
2797
|
+
Object.defineProperty(exports, "extractAll", {
|
|
2798
|
+
enumerable: true,
|
|
2799
|
+
get: function() {
|
|
2800
|
+
return extractAll;
|
|
2801
|
+
}
|
|
2802
|
+
});
|
|
2803
|
+
Object.defineProperty(exports, "flattenOptions", {
|
|
2804
|
+
enumerable: true,
|
|
2805
|
+
get: function() {
|
|
2806
|
+
return flattenOptions;
|
|
2807
|
+
}
|
|
2808
|
+
});
|
|
2809
|
+
Object.defineProperty(exports, "fromURL", {
|
|
2810
|
+
enumerable: true,
|
|
2811
|
+
get: function() {
|
|
2812
|
+
return fromURL;
|
|
2813
|
+
}
|
|
2814
|
+
});
|
|
2815
|
+
Object.defineProperty(exports, "load", {
|
|
2816
|
+
enumerable: true,
|
|
2817
|
+
get: function() {
|
|
2818
|
+
return load;
|
|
2819
|
+
}
|
|
2820
|
+
});
|
|
2821
|
+
Object.defineProperty(exports, "loadAll", {
|
|
2822
|
+
enumerable: true,
|
|
2823
|
+
get: function() {
|
|
2824
|
+
return loadAll;
|
|
2825
|
+
}
|
|
2826
|
+
});
|
|
2827
|
+
Object.defineProperty(exports, "loadAsync", {
|
|
2828
|
+
enumerable: true,
|
|
2829
|
+
get: function() {
|
|
2830
|
+
return loadAsync;
|
|
2831
|
+
}
|
|
2832
|
+
});
|
|
2833
|
+
Object.defineProperty(exports, "loadBuffer", {
|
|
2834
|
+
enumerable: true,
|
|
2835
|
+
get: function() {
|
|
2836
|
+
return loadBuffer;
|
|
2837
|
+
}
|
|
2838
|
+
});
|
|
2839
|
+
Object.defineProperty(exports, "merge", {
|
|
2840
|
+
enumerable: true,
|
|
2841
|
+
get: function() {
|
|
2842
|
+
return merge;
|
|
2843
|
+
}
|
|
2844
|
+
});
|
|
2845
|
+
Object.defineProperty(exports, "stringStream", {
|
|
2846
|
+
enumerable: true,
|
|
2847
|
+
get: function() {
|
|
2848
|
+
return stringStream;
|
|
2849
|
+
}
|
|
2850
|
+
});
|
|
2851
|
+
Object.defineProperty(exports, "workerPoolInfo", {
|
|
2852
|
+
enumerable: true,
|
|
2853
|
+
get: function() {
|
|
2854
|
+
return workerPoolInfo;
|
|
2855
|
+
}
|
|
2856
|
+
});
|
|
2857
|
+
|
|
2858
|
+
//# sourceMappingURL=modern-CHQGP0Yt.cjs.map
|