@dvaji/cheerox 0.0.0-stage → 0.1.2
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 +1091 -0
- package/Cargo.toml +47 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/RELEASING.md +114 -0
- package/THIRD_PARTY_NOTICES.md +7581 -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-BKkfJEAr.mjs +1648 -0
- package/dist/cheerio-compat-BKkfJEAr.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-DcnYK9OB.cjs +1719 -0
- package/dist/cheerio-compat-DcnYK9OB.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 +4120 -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 +35 -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/native/vendor/scraper-0.24.0/Cargo.toml +52 -0
- package/native/vendor/scraper-0.24.0/LICENSE +15 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/element.rs +212 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/mod.rs +236 -0
- package/native/vendor/scraper-0.24.0/src/element_ref/serializable.rs +15 -0
- package/native/vendor/scraper-0.24.0/src/error/utils.rs +91 -0
- package/native/vendor/scraper-0.24.0/src/error.rs +118 -0
- package/native/vendor/scraper-0.24.0/src/html/mod.rs +270 -0
- package/native/vendor/scraper-0.24.0/src/html/serializable.rs +27 -0
- package/native/vendor/scraper-0.24.0/src/html/tree_sink.rs +285 -0
- package/native/vendor/scraper-0.24.0/src/lib.rs +180 -0
- package/native/vendor/scraper-0.24.0/src/node/serializable.rs +52 -0
- package/native/vendor/scraper-0.24.0/src/node.rs +465 -0
- package/native/vendor/scraper-0.24.0/src/selectable.rs +70 -0
- package/native/vendor/scraper-0.24.0/src/selector.rs +290 -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 +194 -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,1719 @@
|
|
|
1
|
+
const require_nodes = require("./nodes-jbWWhssj.cjs");
|
|
2
|
+
// src/cheerio-compat/css.ts
|
|
3
|
+
function setCss(el, prop, val, idx) {
|
|
4
|
+
if (typeof prop === "string") {
|
|
5
|
+
const styles = parseStyles(el.attribs?.style);
|
|
6
|
+
if (typeof val === "function") val = val.call(el, idx, styles[prop]);
|
|
7
|
+
if (val === "") delete styles[prop];
|
|
8
|
+
else if (val != null) styles[prop] = val;
|
|
9
|
+
el.attribs.style = stringifyStyles(styles);
|
|
10
|
+
} else if (typeof prop === "object") for (const key of Object.keys(prop)) setCss(el, key, prop[key], idx);
|
|
11
|
+
}
|
|
12
|
+
function getCss(el, prop) {
|
|
13
|
+
const styles = parseStyles(el?.attribs?.style);
|
|
14
|
+
if (typeof prop === "string") return styles[prop];
|
|
15
|
+
else if (Array.isArray(prop)) return pick(styles, prop);
|
|
16
|
+
else return styles;
|
|
17
|
+
}
|
|
18
|
+
function pick(obj, props) {
|
|
19
|
+
const result = {};
|
|
20
|
+
for (const prop of props) if (prop in obj) result[prop] = obj[prop];
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
function stringifyStyles(obj) {
|
|
24
|
+
return Object.keys(obj).reduce((str, prop) => `${str}${str ? " " : ""}${prop}: ${obj[prop]};`, "");
|
|
25
|
+
}
|
|
26
|
+
function parseStyles(styles) {
|
|
27
|
+
styles = (styles || "").trim();
|
|
28
|
+
if (!styles) return {};
|
|
29
|
+
return styles.split(";").reduce((obj, str) => {
|
|
30
|
+
const n = str.indexOf(":");
|
|
31
|
+
if (n < 1 || n === str.length - 1) return obj;
|
|
32
|
+
obj[str.slice(0, n).trim()] = str.slice(n + 1).trim();
|
|
33
|
+
return obj;
|
|
34
|
+
}, {});
|
|
35
|
+
}
|
|
36
|
+
// src/cheerio-compat/selectors.ts
|
|
37
|
+
/**
|
|
38
|
+
* The pseudo-classes css-select 1.2.0 (Cheerio 0.22.0's selector engine)
|
|
39
|
+
* understands; anything else throws, exactly as the original library did.
|
|
40
|
+
*/
|
|
41
|
+
const legacyPseudos = /* @__PURE__ */ new Set([
|
|
42
|
+
"nth-child",
|
|
43
|
+
"nth-last-child",
|
|
44
|
+
"nth-of-type",
|
|
45
|
+
"nth-last-of-type",
|
|
46
|
+
"first-child",
|
|
47
|
+
"last-child",
|
|
48
|
+
"first-of-type",
|
|
49
|
+
"last-of-type",
|
|
50
|
+
"only-of-type",
|
|
51
|
+
"only-child",
|
|
52
|
+
"empty",
|
|
53
|
+
"link",
|
|
54
|
+
"visited",
|
|
55
|
+
"selected",
|
|
56
|
+
"disabled",
|
|
57
|
+
"enabled",
|
|
58
|
+
"checked",
|
|
59
|
+
"required",
|
|
60
|
+
"optional",
|
|
61
|
+
"parent",
|
|
62
|
+
"header",
|
|
63
|
+
"button",
|
|
64
|
+
"input",
|
|
65
|
+
"text",
|
|
66
|
+
"contains",
|
|
67
|
+
"icontains",
|
|
68
|
+
"checkbox",
|
|
69
|
+
"file",
|
|
70
|
+
"password",
|
|
71
|
+
"radio",
|
|
72
|
+
"reset",
|
|
73
|
+
"image",
|
|
74
|
+
"submit",
|
|
75
|
+
"root",
|
|
76
|
+
"not",
|
|
77
|
+
"has",
|
|
78
|
+
"matches",
|
|
79
|
+
"scope"
|
|
80
|
+
]);
|
|
81
|
+
/** Reject pseudo-classes that only exist in later Cheerio lines. */
|
|
82
|
+
function validateLegacySelector(selector) {
|
|
83
|
+
let depth = 0;
|
|
84
|
+
let quote = "";
|
|
85
|
+
let escape = false;
|
|
86
|
+
for (let i = 0; i < selector.length; i++) {
|
|
87
|
+
const character = selector[i];
|
|
88
|
+
if (escape) {
|
|
89
|
+
escape = false;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (character === "\\") {
|
|
93
|
+
escape = true;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (quote) {
|
|
97
|
+
if (character === quote) quote = "";
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (character === "\"" || character === "'") {
|
|
101
|
+
quote = character;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (character === "[") {
|
|
105
|
+
depth++;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (character === "]") {
|
|
109
|
+
depth = Math.max(0, depth - 1);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (depth || character !== ":") continue;
|
|
113
|
+
const match = /^:([a-zA-Z][a-zA-Z0-9-]*)/.exec(selector.slice(i));
|
|
114
|
+
if (!match) continue;
|
|
115
|
+
if (!legacyPseudos.has(match[1])) throw new SyntaxError(`unmatched pseudo-class ${match[0]}`);
|
|
116
|
+
if (selector[i + match[0].length] === "(") {
|
|
117
|
+
let parens = 0;
|
|
118
|
+
let innerQuote = "";
|
|
119
|
+
for (let j = i + match[0].length; j < selector.length; j++) {
|
|
120
|
+
const inner = selector[j];
|
|
121
|
+
if (innerQuote) {
|
|
122
|
+
if (inner === innerQuote) innerQuote = "";
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (inner === "\"" || inner === "'") {
|
|
126
|
+
innerQuote = inner;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (inner === "(") parens++;
|
|
130
|
+
else if (inner === ")") {
|
|
131
|
+
parens--;
|
|
132
|
+
if (!parens) {
|
|
133
|
+
i = j;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
} else i += match[0].length;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// src/cheerio-compat/render.ts
|
|
142
|
+
const isTag$1 = (node) => node.type === "tag" || node.type === "script" || node.type === "style";
|
|
143
|
+
const unencodedElements = {
|
|
144
|
+
style: true,
|
|
145
|
+
script: true,
|
|
146
|
+
xmp: true,
|
|
147
|
+
iframe: true,
|
|
148
|
+
noembed: true,
|
|
149
|
+
noframes: true,
|
|
150
|
+
plaintext: true,
|
|
151
|
+
noscript: true
|
|
152
|
+
};
|
|
153
|
+
const singleTag = {
|
|
154
|
+
area: true,
|
|
155
|
+
base: true,
|
|
156
|
+
basefont: true,
|
|
157
|
+
br: true,
|
|
158
|
+
col: true,
|
|
159
|
+
command: true,
|
|
160
|
+
embed: true,
|
|
161
|
+
frame: true,
|
|
162
|
+
hr: true,
|
|
163
|
+
img: true,
|
|
164
|
+
input: true,
|
|
165
|
+
isindex: true,
|
|
166
|
+
keygen: true,
|
|
167
|
+
link: true,
|
|
168
|
+
meta: true,
|
|
169
|
+
param: true,
|
|
170
|
+
source: true,
|
|
171
|
+
track: true,
|
|
172
|
+
wbr: true
|
|
173
|
+
};
|
|
174
|
+
const xmlReplacements = {
|
|
175
|
+
"&": "&",
|
|
176
|
+
"'": "'",
|
|
177
|
+
">": ">",
|
|
178
|
+
"<": "<",
|
|
179
|
+
"\"": """
|
|
180
|
+
};
|
|
181
|
+
const encodeXmlRegex = /[&'><"]/g;
|
|
182
|
+
const nonAsciiRegex = /[^\0-\x7F]/g;
|
|
183
|
+
const astralRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
|
184
|
+
function codePointReplacement(c) {
|
|
185
|
+
return `&#x${c.codePointAt(0).toString(16).toUpperCase()};`;
|
|
186
|
+
}
|
|
187
|
+
/** entities 1.1 `encodeXML`: the five XML characters plus every non-ASCII rune. */
|
|
188
|
+
function encodeXML(data) {
|
|
189
|
+
return data.replace(encodeXmlRegex, (c) => xmlReplacements[c]).replace(astralRegex, codePointReplacement).replace(nonAsciiRegex, codePointReplacement);
|
|
190
|
+
}
|
|
191
|
+
function formatAttrs(attribs, opts) {
|
|
192
|
+
if (!attribs) return "";
|
|
193
|
+
let output = "";
|
|
194
|
+
for (const key of Object.keys(attribs)) {
|
|
195
|
+
const value = attribs[key];
|
|
196
|
+
if (output) output += " ";
|
|
197
|
+
output += key;
|
|
198
|
+
if (value !== null && value !== "" || opts.xmlMode) output += `="${opts.decodeEntities ? encodeXML(value) : value}"`;
|
|
199
|
+
}
|
|
200
|
+
return output;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* dom-serializer 0.1.x rules run natively (one call per document); nodes
|
|
204
|
+
* outside the engine, or spanning several documents, fall back to the port.
|
|
205
|
+
*/
|
|
206
|
+
function renderWith(opts, dom, parentName) {
|
|
207
|
+
const nodes = Array.isArray(dom) || dom && typeof dom === "object" && "length" in dom ? Array.from(dom) : [dom];
|
|
208
|
+
if (nodes.length) {
|
|
209
|
+
const bindings = nodes.map((node) => require_nodes.nodeBinding(node));
|
|
210
|
+
const document = bindings[0]?.document;
|
|
211
|
+
if (document && bindings.every((binding) => binding?.document === document)) return require_nodes.operation(document, "render", {
|
|
212
|
+
ids: bindings.map((binding) => binding.id),
|
|
213
|
+
options: {
|
|
214
|
+
...opts,
|
|
215
|
+
_useHtmlParser2: true,
|
|
216
|
+
legacySerializer: true
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
let output = "";
|
|
221
|
+
for (const elem of nodes) {
|
|
222
|
+
if (!elem || !elem.type) continue;
|
|
223
|
+
if (elem.type === "root") output += renderWith(opts, elem.children);
|
|
224
|
+
else if (isTag$1(elem)) output += renderTag(elem, opts);
|
|
225
|
+
else if (elem.type === "directive") output += `<${elem.data}>`;
|
|
226
|
+
else if (elem.type === "comment") output += `<!--${elem.data}-->`;
|
|
227
|
+
else if (elem.type === "cdata") output += `<![CDATA[${elem.children && elem.children[0] ? elem.children[0].data : ""}]]>`;
|
|
228
|
+
else output += renderText(elem, opts, parentName);
|
|
229
|
+
}
|
|
230
|
+
return output;
|
|
231
|
+
}
|
|
232
|
+
function renderTag(elem, opts) {
|
|
233
|
+
const effective = elem.name === "svg" ? {
|
|
234
|
+
decodeEntities: opts.decodeEntities,
|
|
235
|
+
xmlMode: true
|
|
236
|
+
} : opts;
|
|
237
|
+
let tag = `<${elem.name}`;
|
|
238
|
+
const attribs = formatAttrs(elem.attribs, effective);
|
|
239
|
+
if (attribs) tag += ` ${attribs}`;
|
|
240
|
+
if (effective.xmlMode && (!elem.children || elem.children.length === 0)) tag += "/>";
|
|
241
|
+
else {
|
|
242
|
+
tag += ">";
|
|
243
|
+
if (elem.children) tag += renderWith(effective, elem.children, elem.name);
|
|
244
|
+
if (!singleTag[elem.name] || effective.xmlMode) tag += `</${elem.name}>`;
|
|
245
|
+
}
|
|
246
|
+
return tag;
|
|
247
|
+
}
|
|
248
|
+
function renderText(elem, opts, parentName) {
|
|
249
|
+
let data = elem.data || "";
|
|
250
|
+
if (opts.decodeEntities && !(parentName !== void 0 && parentName in unencodedElements)) data = encodeXML(data);
|
|
251
|
+
return data;
|
|
252
|
+
}
|
|
253
|
+
// package.json
|
|
254
|
+
var version = "0.1.2";
|
|
255
|
+
// src/cheerio-compat.ts
|
|
256
|
+
/**
|
|
257
|
+
* The Cheerio 0.22.0 facade, backed by the Rust engine.
|
|
258
|
+
*
|
|
259
|
+
* Every public behavior is ported from cheerio@0.22.0 (commit
|
|
260
|
+
* 49b477e7fe98b74f61f77acd06a0ef4edb5c333e): lib/cheerio.js, lib/static.js,
|
|
261
|
+
* lib/parse.js, lib/utils.js and lib/api/*.js. DOM storage, parsing and
|
|
262
|
+
* selection stay in the native arena; this file supplies the legacy object
|
|
263
|
+
* model (plain field semantics, `root` references, 0.22.0 serialization and
|
|
264
|
+
* css-select 1.2.0 selection semantics) on top of it.
|
|
265
|
+
*
|
|
266
|
+
* Structural rule: sibling and parent links are owned by the engine. All
|
|
267
|
+
* tree surgery installs whole child lists (see writeChildren), which rebuilds
|
|
268
|
+
* the links exactly as 0.22.0's raw `prev`/`next`/`parent` writes did
|
|
269
|
+
* observably; raw pointer writes on attached nodes would fight the engine's
|
|
270
|
+
* own invariants.
|
|
271
|
+
*/
|
|
272
|
+
/** Cheerio.prototype.options — the exact 0.22.0 defaults. */
|
|
273
|
+
const defaultOpts = {
|
|
274
|
+
withDomLvl1: true,
|
|
275
|
+
normalizeWhitespace: false,
|
|
276
|
+
xmlMode: false,
|
|
277
|
+
decodeEntities: true
|
|
278
|
+
};
|
|
279
|
+
/** lodash.defaults semantics: fill undefined keys from the sources. */
|
|
280
|
+
function defaults(target, ...sources) {
|
|
281
|
+
for (const source of sources) {
|
|
282
|
+
if (!source) continue;
|
|
283
|
+
for (const key of Object.keys(source)) {
|
|
284
|
+
const value = source[key];
|
|
285
|
+
if (target[key] === void 0 && value !== void 0) target[key] = value;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return target;
|
|
289
|
+
}
|
|
290
|
+
const isTag = (elem) => {
|
|
291
|
+
const type = typeof elem === "object" && elem !== null ? elem.type : elem;
|
|
292
|
+
return type !== void 0 && tags[type] || false;
|
|
293
|
+
};
|
|
294
|
+
const tags = {
|
|
295
|
+
tag: true,
|
|
296
|
+
script: true,
|
|
297
|
+
style: true
|
|
298
|
+
};
|
|
299
|
+
const camelCase = (str) => str.replace(/[_.-](\w|$)/g, (_, x) => x.toUpperCase());
|
|
300
|
+
const cssCase = (str) => str.replace(/[A-Z]/g, "-$&").toLowerCase();
|
|
301
|
+
const domEach = (cheerio, fn) => {
|
|
302
|
+
let i = 0;
|
|
303
|
+
const len = cheerio.length;
|
|
304
|
+
while (i < len && fn.call(cheerio, i, cheerio[i]) !== false) ++i;
|
|
305
|
+
return cheerio;
|
|
306
|
+
};
|
|
307
|
+
/** Create a deep copy by rendering and re-parsing, as 0.22.0 does. */
|
|
308
|
+
function cloneDom(dom, options) {
|
|
309
|
+
return evaluate(renderWith(options, dom), options);
|
|
310
|
+
}
|
|
311
|
+
const quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
|
|
312
|
+
const simpleTagExpr = /^\s*([A-Za-z][\w-]*)\s*$/;
|
|
313
|
+
const finalTagExpr = /^\s*(?:[A-Za-z][\w-]*\s+)*([A-Za-z][\w-]*)(?:\[[\w-]+\])?\s*$/;
|
|
314
|
+
const specialTagExpr = /^(?:script|style)$/i;
|
|
315
|
+
const isHtml = (str) => {
|
|
316
|
+
if (str.charAt(0) === "<" && str.charAt(str.length - 1) === ">" && str.length >= 3) return true;
|
|
317
|
+
const match = quickExpr.exec(str);
|
|
318
|
+
return !!(match && match[1]);
|
|
319
|
+
};
|
|
320
|
+
/** Legacy options with the native parser mode folded in. */
|
|
321
|
+
function legacyOptions(options) {
|
|
322
|
+
return defaults({ ...options ?? {} }, defaultOpts, {
|
|
323
|
+
_useHtmlParser2: true,
|
|
324
|
+
_legacyDom: true
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
function newDocument(options) {
|
|
328
|
+
return require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument("", false, JSON.stringify(require_nodes.cleanParserOptions(options))), options);
|
|
329
|
+
}
|
|
330
|
+
/** As nodes under the legacy model: views that read like plain objects. */
|
|
331
|
+
function asNodes(document, ids, knownType) {
|
|
332
|
+
if (knownType) return require_nodes.getNodesOfType(document, ids, knownType);
|
|
333
|
+
return ids.map((id) => require_nodes.getNode(document, id));
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* htmlparser2 3.9.1's `normalizeWhitespace`: collapse whitespace runs in
|
|
337
|
+
* regular text nodes to single spaces (script/style contents stay raw).
|
|
338
|
+
*/
|
|
339
|
+
function normalizeWhitespace(nodes) {
|
|
340
|
+
for (const node of nodes) if (node.type === "text") {
|
|
341
|
+
if (/\s\s|\s$|^\s/.test(node.data ?? "")) node.data = (node.data ?? "").replace(/\s+/g, " ");
|
|
342
|
+
} else if (node.type !== "script" && node.type !== "style" && node.children) normalizeWhitespace(node.children);
|
|
343
|
+
}
|
|
344
|
+
/** htmlparser.parseDOM(content, options): the parsed children, without a root wrapper. */
|
|
345
|
+
function evaluate(content, options) {
|
|
346
|
+
const opts = legacyOptions(options);
|
|
347
|
+
if (typeof content !== "string" && !Buffer.isBuffer(content)) {
|
|
348
|
+
const document = newDocument(opts);
|
|
349
|
+
const nodes = Array.isArray(content) || content && typeof content === "object" && "length" in content ? Array.from(content) : [content];
|
|
350
|
+
return asNodes(document, require_nodes.importNodes(document, nodes));
|
|
351
|
+
}
|
|
352
|
+
const document = require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument(Buffer.isBuffer(content) ? content.toString() : content, false, JSON.stringify(require_nodes.cleanParserOptions(opts))), opts);
|
|
353
|
+
const root = require_nodes.rootNode(document, document.rootId());
|
|
354
|
+
const children = Array.from(root.children ?? []);
|
|
355
|
+
if (opts.normalizeWhitespace) normalizeWhitespace(children);
|
|
356
|
+
return children;
|
|
357
|
+
}
|
|
358
|
+
/** One native write that installs a parent's child list and sibling links. */
|
|
359
|
+
function writeChildren(parent, list) {
|
|
360
|
+
const binding = bindingOf(parent);
|
|
361
|
+
if (!binding) return;
|
|
362
|
+
require_nodes.operation(binding.document, "setNode", {
|
|
363
|
+
id: binding.id,
|
|
364
|
+
field: "children",
|
|
365
|
+
value: require_nodes.importNodes(binding.document, list)
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Update the dom structure for one changed layer — lib/parse.js `update`.
|
|
370
|
+
* The new level is installed with one children write per changed parent;
|
|
371
|
+
* the engine rebuilds the `prev`/`next`/`parent` links.
|
|
372
|
+
*/
|
|
373
|
+
function update(arrInput, parent) {
|
|
374
|
+
const arr = Array.isArray(arrInput) ? arrInput : [arrInput];
|
|
375
|
+
const oldParents = arr.map((node) => node.parent || node.root);
|
|
376
|
+
if (parent) writeChildren(parent, arr);
|
|
377
|
+
for (let i = 0; i < arr.length; i++) {
|
|
378
|
+
const node = arr[i];
|
|
379
|
+
const oldParent = oldParents[i];
|
|
380
|
+
if (oldParent && oldParent !== parent) {
|
|
381
|
+
const list = Array.from(oldParent.children ?? []);
|
|
382
|
+
const index = list.indexOf(node);
|
|
383
|
+
if (index >= 0) writeChildren(oldParent, list.filter((item, at) => at !== index));
|
|
384
|
+
}
|
|
385
|
+
if (parent && parent.type === "root") node.root = parent;
|
|
386
|
+
else node.root = null;
|
|
387
|
+
}
|
|
388
|
+
return parent;
|
|
389
|
+
}
|
|
390
|
+
/** lib/parse.js: parse into a fresh document and return its `<root>` node. */
|
|
391
|
+
function parse(content, options) {
|
|
392
|
+
const opts = legacyOptions(options);
|
|
393
|
+
let document;
|
|
394
|
+
let root;
|
|
395
|
+
if (typeof content === "string" || Buffer.isBuffer(content)) {
|
|
396
|
+
document = require_nodes.routeDocument(new (require_nodes.requireNative()).NativeDocument(Buffer.isBuffer(content) ? content.toString() : content, false, JSON.stringify(require_nodes.cleanParserOptions(opts))), opts);
|
|
397
|
+
root = require_nodes.rootNode(document, document.rootId());
|
|
398
|
+
} else {
|
|
399
|
+
document = newDocument(opts);
|
|
400
|
+
const nodes = Array.isArray(content) || content && typeof content === "object" && "length" in content ? Array.from(content) : [content];
|
|
401
|
+
root = require_nodes.rootNode(document, document.rootId());
|
|
402
|
+
writeChildren(root, nodes);
|
|
403
|
+
}
|
|
404
|
+
let children;
|
|
405
|
+
if (opts.normalizeWhitespace) {
|
|
406
|
+
children = Array.from(root.children ?? []);
|
|
407
|
+
normalizeWhitespace(children);
|
|
408
|
+
} else {
|
|
409
|
+
const record = require_nodes.operation(document, "node", { id: require_nodes.viewId(root) });
|
|
410
|
+
children = asNodes(document, record.children ?? []);
|
|
411
|
+
}
|
|
412
|
+
for (const child of children) child.root = root;
|
|
413
|
+
return root;
|
|
414
|
+
}
|
|
415
|
+
function bindingOf(node) {
|
|
416
|
+
return node && typeof node === "object" ? require_nodes.nodeBinding(node) : void 0;
|
|
417
|
+
}
|
|
418
|
+
function idsOf(document, nodes) {
|
|
419
|
+
const len = nodes.length;
|
|
420
|
+
if (typeof len !== "number" || len < 0) return require_nodes.importNodes(document, nodes);
|
|
421
|
+
const out = new Array(len);
|
|
422
|
+
for (let i = 0; i < len; i++) {
|
|
423
|
+
const id = require_nodes.viewIdIn(document, nodes[i]);
|
|
424
|
+
if (id === void 0) return require_nodes.importNodes(document, nodes);
|
|
425
|
+
out[i] = id;
|
|
426
|
+
}
|
|
427
|
+
return out;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* css-select 1.2 `select(query, elems, options)`: candidates are the members
|
|
431
|
+
* of `elems` and their descendants, in list order (domutils.findAll, with
|
|
432
|
+
* removeSubsets applied). A candidate matches when it satisfies the selector
|
|
433
|
+
* and is a context node or a descendant of one — the absolutized `:scope`
|
|
434
|
+
* descendant rule, whose `acceptSelf` branch lets a context node match itself.
|
|
435
|
+
*/
|
|
436
|
+
function scopedSelect(selector, elems, options) {
|
|
437
|
+
if (!selector || !elems.length) return [];
|
|
438
|
+
const document = bindingOf(elems[0])?.document;
|
|
439
|
+
if (!document) return [];
|
|
440
|
+
const context = options.context ?? elems;
|
|
441
|
+
const contextIds = new Set(idsOf(document, context));
|
|
442
|
+
const roots = elems.filter((node, index) => !elems.some((other, otherIndex) => otherIndex !== index && isAncestorOf(other, node)));
|
|
443
|
+
const candidates = [];
|
|
444
|
+
const seen = /* @__PURE__ */ new Set();
|
|
445
|
+
for (const root of roots) {
|
|
446
|
+
const rootId = require_nodes.viewIdIn(document, root);
|
|
447
|
+
if (!seen.has(rootId)) {
|
|
448
|
+
seen.add(rootId);
|
|
449
|
+
candidates.push(rootId);
|
|
450
|
+
}
|
|
451
|
+
for (const id of document.select("*", [rootId])) if (!seen.has(id)) {
|
|
452
|
+
seen.add(id);
|
|
453
|
+
candidates.push(id);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
const leading = /^\s*([>+~])\s*/.exec(selector);
|
|
457
|
+
let rest = selector;
|
|
458
|
+
let combinator;
|
|
459
|
+
if (leading) {
|
|
460
|
+
combinator = leading[1];
|
|
461
|
+
rest = selector.slice(leading[0].length);
|
|
462
|
+
}
|
|
463
|
+
const matched = new Set(matchesSelector(document, candidates, rest, options));
|
|
464
|
+
const result = [];
|
|
465
|
+
for (const id of candidates) {
|
|
466
|
+
if (!matched.has(id)) continue;
|
|
467
|
+
const node = require_nodes.getNode(document, id);
|
|
468
|
+
if (combinator && !combinatorAnchored(node, combinator, contextIds)) continue;
|
|
469
|
+
if (!combinator && !withinContext(node, contextIds)) continue;
|
|
470
|
+
result.push(node);
|
|
471
|
+
}
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* The `:scope` anchor of a leading-combinator selector: the candidate's
|
|
476
|
+
* parent, previous element sibling, or any earlier element sibling must be a
|
|
477
|
+
* context node.
|
|
478
|
+
*/
|
|
479
|
+
function combinatorAnchored(node, combinator, contextIds) {
|
|
480
|
+
if (combinator === ">") {
|
|
481
|
+
const parent = node.parent;
|
|
482
|
+
return parent !== null && parent !== void 0 && contextIds.has(require_nodes.viewId(parent) ?? NaN);
|
|
483
|
+
}
|
|
484
|
+
if (combinator === "+") {
|
|
485
|
+
let sibling = node.prev;
|
|
486
|
+
while (sibling && !isTag(sibling)) sibling = sibling.prev;
|
|
487
|
+
return sibling !== null && sibling !== void 0 && contextIds.has(require_nodes.viewId(sibling) ?? NaN);
|
|
488
|
+
}
|
|
489
|
+
let sibling = node.prev;
|
|
490
|
+
while (sibling) {
|
|
491
|
+
if (isTag(sibling) && contextIds.has(require_nodes.viewId(sibling) ?? NaN)) return true;
|
|
492
|
+
sibling = sibling.prev;
|
|
493
|
+
}
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
/** Native matching for a batch of ids, with 0.22.0's SyntaxError behavior. */
|
|
497
|
+
function matchesSelector(document, ids, selector, options) {
|
|
498
|
+
validateLegacySelector(selector);
|
|
499
|
+
try {
|
|
500
|
+
return require_nodes.operation(document, "matches", {
|
|
501
|
+
ids,
|
|
502
|
+
selector,
|
|
503
|
+
options: { xmlMode: options.xmlMode }
|
|
504
|
+
}) ?? [];
|
|
505
|
+
} catch (error) {
|
|
506
|
+
throw error instanceof SyntaxError ? error : new SyntaxError(error?.message ?? String(error));
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
/** Whether the node or one of its ancestors belongs to the context set. */
|
|
510
|
+
function withinContext(node, contextIds) {
|
|
511
|
+
for (let current = node; current;) {
|
|
512
|
+
const id = require_nodes.viewId(current);
|
|
513
|
+
if (id !== void 0 && contextIds.has(id)) return true;
|
|
514
|
+
const parent = current.parent;
|
|
515
|
+
if (parent === null || parent === current) {
|
|
516
|
+
const root = current.root;
|
|
517
|
+
return root !== null && root !== void 0 && contextIds.has(require_nodes.viewId(root) ?? NaN);
|
|
518
|
+
}
|
|
519
|
+
current = parent;
|
|
520
|
+
}
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
function isAncestorOf(ancestor, node) {
|
|
524
|
+
if (node === ancestor) return false;
|
|
525
|
+
for (let current = node?.parent; current; current = current.parent) {
|
|
526
|
+
if (current === ancestor) return true;
|
|
527
|
+
if (current.parent === current) break;
|
|
528
|
+
}
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
/** htmlparser2 DomUtils.uniqueSort over views: document order, deduplicated. */
|
|
532
|
+
function uniqueSort(nodes) {
|
|
533
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
534
|
+
for (const node of nodes) {
|
|
535
|
+
const binding = bindingOf(node);
|
|
536
|
+
if (!binding) continue;
|
|
537
|
+
let list = grouped.get(binding.document);
|
|
538
|
+
if (!list) grouped.set(binding.document, list = []);
|
|
539
|
+
list.push({
|
|
540
|
+
id: binding.id,
|
|
541
|
+
node
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
const sorted = [];
|
|
545
|
+
for (const [document, list] of grouped) {
|
|
546
|
+
const order = require_nodes.operation(document, "uniqueSort", { ids: list.map((entry) => entry.id) });
|
|
547
|
+
for (const id of order ?? []) {
|
|
548
|
+
const entry = list.find((candidate) => candidate.id === id);
|
|
549
|
+
if (entry) sorted.push(entry.node);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return sorted;
|
|
553
|
+
}
|
|
554
|
+
const isNode = (obj) => {
|
|
555
|
+
if (require_nodes.viewId(obj) !== void 0) return true;
|
|
556
|
+
const node = obj;
|
|
557
|
+
return !!(obj && typeof obj === "object" && (node.name || node.type === "text" || node.type === "comment"));
|
|
558
|
+
};
|
|
559
|
+
function makeCheerio() {
|
|
560
|
+
const Cheerio = function(selector, context, root, options) {
|
|
561
|
+
if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root, options);
|
|
562
|
+
this.options = defaults(options || {}, this.options);
|
|
563
|
+
if (!selector) return this;
|
|
564
|
+
if (root) {
|
|
565
|
+
if (typeof root === "string") root = parse(root, this.options);
|
|
566
|
+
this._root = Cheerio.call(this, root);
|
|
567
|
+
}
|
|
568
|
+
if (selector.cheerio) return selector;
|
|
569
|
+
if (isNode(selector)) selector = [selector];
|
|
570
|
+
if (Array.isArray(selector)) {
|
|
571
|
+
selector.forEach((elem, idx) => {
|
|
572
|
+
this[idx] = elem;
|
|
573
|
+
});
|
|
574
|
+
this.length = selector.length;
|
|
575
|
+
return this;
|
|
576
|
+
}
|
|
577
|
+
if (typeof selector === "string" && isHtml(selector)) return Cheerio.call(this, parse(selector, this.options).children ?? []);
|
|
578
|
+
if (!context) context = this._root;
|
|
579
|
+
else if (typeof context === "string") {
|
|
580
|
+
if (isHtml(context)) context = Cheerio.call(this, parse(context, this.options));
|
|
581
|
+
else {
|
|
582
|
+
selector = [context, selector].join(" ");
|
|
583
|
+
context = this._root;
|
|
584
|
+
}
|
|
585
|
+
} else if (!context.cheerio) context = Cheerio.call(this, context);
|
|
586
|
+
if (!context) return this;
|
|
587
|
+
return context.find(selector);
|
|
588
|
+
};
|
|
589
|
+
Cheerio.prototype.cheerio = "[cheerio object]";
|
|
590
|
+
Cheerio.prototype.options = defaultOpts;
|
|
591
|
+
Cheerio.prototype.length = 0;
|
|
592
|
+
Cheerio.prototype.splice = Array.prototype.splice;
|
|
593
|
+
Cheerio.prototype._make = function(dom, context) {
|
|
594
|
+
const cheerio = new this.constructor(dom, context, this._root, this.options);
|
|
595
|
+
cheerio.prevObject = this;
|
|
596
|
+
return cheerio;
|
|
597
|
+
};
|
|
598
|
+
Cheerio.prototype.toArray = function() {
|
|
599
|
+
return this.get();
|
|
600
|
+
};
|
|
601
|
+
return Cheerio;
|
|
602
|
+
}
|
|
603
|
+
const hasOwn = Object.prototype.hasOwnProperty;
|
|
604
|
+
const rspace = /\s+/;
|
|
605
|
+
const dataAttrPrefix = "data-";
|
|
606
|
+
const primitives = {
|
|
607
|
+
null: null,
|
|
608
|
+
true: true,
|
|
609
|
+
false: false
|
|
610
|
+
};
|
|
611
|
+
const rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
|
|
612
|
+
const rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;
|
|
613
|
+
function getAttr(elem, name) {
|
|
614
|
+
if (!elem || !isTag(elem)) return;
|
|
615
|
+
if (!elem.attribs) elem.attribs = {};
|
|
616
|
+
if (!name) return elem.attribs;
|
|
617
|
+
if (hasOwn.call(elem.attribs, name)) return rboolean.test(name) ? name : elem.attribs[name];
|
|
618
|
+
if (elem.name === "option" && name === "value") return staticText(elem.children ?? []);
|
|
619
|
+
if (elem.name === "input" && (elem.attribs.type === "radio" || elem.attribs.type === "checkbox") && name === "value") return "on";
|
|
620
|
+
}
|
|
621
|
+
function setAttr(el, name, value) {
|
|
622
|
+
if (value === null) {
|
|
623
|
+
removeAttribute(el, name);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
const stringValue = value + "";
|
|
627
|
+
const binding = bindingOf(el);
|
|
628
|
+
const document = binding?.document;
|
|
629
|
+
if (binding && document?.setAttribute) document.setAttribute([binding.id], name, stringValue);
|
|
630
|
+
else el.attribs[name] = stringValue;
|
|
631
|
+
}
|
|
632
|
+
function removeAttribute(elem, name) {
|
|
633
|
+
if (!elem.attribs || !hasOwn.call(elem.attribs, name)) return;
|
|
634
|
+
const binding = bindingOf(elem);
|
|
635
|
+
const document = binding?.document;
|
|
636
|
+
if (binding && document?.setAttribute) document.setAttribute([binding.id], name, null);
|
|
637
|
+
else delete elem.attribs[name];
|
|
638
|
+
}
|
|
639
|
+
const splitClass = (className) => className ? className.trim().split(rspace) : [];
|
|
640
|
+
function setDataStore(el) {
|
|
641
|
+
if (!el.data || typeof el.data !== "object") el.data = {};
|
|
642
|
+
return el.data;
|
|
643
|
+
}
|
|
644
|
+
function readData(el, name) {
|
|
645
|
+
const readAll = arguments.length < 2;
|
|
646
|
+
let domNames, jsNames;
|
|
647
|
+
if (readAll) {
|
|
648
|
+
domNames = Object.keys(el.attribs ?? {}).filter((attrName) => attrName.slice(0, 5) === dataAttrPrefix);
|
|
649
|
+
jsNames = domNames.map((domName) => camelCase(domName.slice(5)));
|
|
650
|
+
} else {
|
|
651
|
+
domNames = [dataAttrPrefix + cssCase(name)];
|
|
652
|
+
jsNames = [name];
|
|
653
|
+
}
|
|
654
|
+
let value;
|
|
655
|
+
const store = setDataStore(el);
|
|
656
|
+
for (let idx = 0; idx < domNames.length; ++idx) {
|
|
657
|
+
const domName = domNames[idx];
|
|
658
|
+
const jsName = jsNames[idx];
|
|
659
|
+
if (hasOwn.call(el.attribs ?? {}, domName)) {
|
|
660
|
+
value = el.attribs[domName];
|
|
661
|
+
if (hasOwn.call(primitives, value)) value = primitives[value];
|
|
662
|
+
else if (value === String(Number(value))) value = Number(value);
|
|
663
|
+
else if (rbrace.test(value)) try {
|
|
664
|
+
value = JSON.parse(value);
|
|
665
|
+
} catch {}
|
|
666
|
+
store[jsName] = value;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return readAll ? store : value;
|
|
670
|
+
}
|
|
671
|
+
const getProp = (el, name) => {
|
|
672
|
+
if (!el || !isTag(el)) return;
|
|
673
|
+
return hasOwn.call(el, name) ? el[name] : rboolean.test(name) ? getAttr(el, name) !== void 0 : getAttr(el, name);
|
|
674
|
+
};
|
|
675
|
+
const setProp = (el, name, value) => {
|
|
676
|
+
el[name] = rboolean.test(name) ? !!value : value;
|
|
677
|
+
};
|
|
678
|
+
const attributesApi = {
|
|
679
|
+
attr(name, value) {
|
|
680
|
+
if (typeof name === "object" || value !== void 0) {
|
|
681
|
+
if (typeof value === "function") return domEach(this, function(i, el) {
|
|
682
|
+
setAttr(el, name, value.call(el, i, el.attribs[name]));
|
|
683
|
+
});
|
|
684
|
+
if (typeof name === "string") {
|
|
685
|
+
if (this.length === 1 && isTag(this[0])) {
|
|
686
|
+
setAttr(this[0], name, value);
|
|
687
|
+
return this;
|
|
688
|
+
}
|
|
689
|
+
const groups = /* @__PURE__ */ new Map();
|
|
690
|
+
let usable = this.length > 0;
|
|
691
|
+
for (let i = 0; i < this.length; i++) {
|
|
692
|
+
const el = this[i];
|
|
693
|
+
if (!isTag(el)) continue;
|
|
694
|
+
if (!el.attribs) el.attribs = {};
|
|
695
|
+
const binding = bindingOf(el);
|
|
696
|
+
if (!binding) {
|
|
697
|
+
usable = false;
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
let group = groups.get(binding.document);
|
|
701
|
+
if (!group) groups.set(binding.document, group = []);
|
|
702
|
+
group.push({
|
|
703
|
+
id: binding.id,
|
|
704
|
+
name,
|
|
705
|
+
value: value === null ? null : value + ""
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
if (usable) {
|
|
709
|
+
for (const [document, writes] of groups) {
|
|
710
|
+
const attributeDocument = document;
|
|
711
|
+
const firstValue = writes[0]?.value;
|
|
712
|
+
if (attributeDocument.setAttribute && writes.every((write) => write.value === firstValue)) attributeDocument.setAttribute(writes.map((write) => write.id), name, firstValue ?? null);
|
|
713
|
+
else require_nodes.operation(document, "setAttrs", { writes });
|
|
714
|
+
}
|
|
715
|
+
return this;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return domEach(this, function(_i, el) {
|
|
719
|
+
if (!isTag(el)) return;
|
|
720
|
+
if (typeof name === "object") for (const key of Object.keys(name)) setAttr(el, key, name[key]);
|
|
721
|
+
else setAttr(el, name, value);
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
return getAttr(this[0], name);
|
|
725
|
+
},
|
|
726
|
+
prop(name, value) {
|
|
727
|
+
if (typeof name === "string" && value === void 0) switch (name) {
|
|
728
|
+
case "style": {
|
|
729
|
+
const property = this.css();
|
|
730
|
+
let i = 0;
|
|
731
|
+
for (const p of Object.keys(property)) property[i++] = p;
|
|
732
|
+
property.length = i;
|
|
733
|
+
return property;
|
|
734
|
+
}
|
|
735
|
+
case "tagName":
|
|
736
|
+
case "nodeName": return this[0].name.toUpperCase();
|
|
737
|
+
default: return getProp(this[0], name);
|
|
738
|
+
}
|
|
739
|
+
if (typeof name === "object" || value !== void 0) {
|
|
740
|
+
if (typeof value === "function") return domEach(this, function(i, el) {
|
|
741
|
+
setProp(el, name, value.call(el, i, getProp(el, name)));
|
|
742
|
+
});
|
|
743
|
+
return domEach(this, function(_i, el) {
|
|
744
|
+
if (!isTag(el)) return;
|
|
745
|
+
if (typeof name === "object") for (const key of Object.keys(name)) setProp(el, key, name[key]);
|
|
746
|
+
else setProp(el, name, value);
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
},
|
|
750
|
+
data(name, value) {
|
|
751
|
+
const elem = this[0];
|
|
752
|
+
if (!elem || !isTag(elem)) return;
|
|
753
|
+
if (!name) return readData(elem);
|
|
754
|
+
if (typeof name === "object" || value !== void 0) {
|
|
755
|
+
domEach(this, function(_i, el) {
|
|
756
|
+
const store = setDataStore(el);
|
|
757
|
+
if (typeof name === "object") Object.assign(store, name);
|
|
758
|
+
else store[name] = value;
|
|
759
|
+
});
|
|
760
|
+
return this;
|
|
761
|
+
} else if (hasOwn.call(setDataStore(elem), name)) return setDataStore(elem)[name];
|
|
762
|
+
return readData(elem, name);
|
|
763
|
+
},
|
|
764
|
+
val(value) {
|
|
765
|
+
const querying = arguments.length === 0;
|
|
766
|
+
const element = this[0];
|
|
767
|
+
if (!element) return;
|
|
768
|
+
switch (element.name) {
|
|
769
|
+
case "textarea": return this.text(value);
|
|
770
|
+
case "input": switch (this.attr("type")) {
|
|
771
|
+
case "radio": if (querying) return this.attr("value");
|
|
772
|
+
else {
|
|
773
|
+
this.attr("value", value);
|
|
774
|
+
return this;
|
|
775
|
+
}
|
|
776
|
+
default: return this.attr("value", value);
|
|
777
|
+
}
|
|
778
|
+
case "select": {
|
|
779
|
+
const option = this.find("option:selected");
|
|
780
|
+
let returnValue;
|
|
781
|
+
if (option === void 0) return void 0;
|
|
782
|
+
if (!querying) {
|
|
783
|
+
if (!this.attr().hasOwnProperty("multiple") && typeof value === "object") return this;
|
|
784
|
+
if (typeof value !== "object") value = [value];
|
|
785
|
+
this.find("option").removeAttr("selected");
|
|
786
|
+
for (let i = 0; i < value.length; i++) this.find(`option[value="${value[i]}"]`).attr("selected", "");
|
|
787
|
+
return this;
|
|
788
|
+
}
|
|
789
|
+
returnValue = option.attr("value");
|
|
790
|
+
if (this.attr().hasOwnProperty("multiple")) {
|
|
791
|
+
returnValue = [];
|
|
792
|
+
domEach(option, function(_i, el) {
|
|
793
|
+
returnValue.push(getAttr(el, "value"));
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
return returnValue;
|
|
797
|
+
}
|
|
798
|
+
case "option":
|
|
799
|
+
if (!querying) {
|
|
800
|
+
this.attr("value", value);
|
|
801
|
+
return this;
|
|
802
|
+
}
|
|
803
|
+
return this.attr("value");
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
removeAttr(name) {
|
|
807
|
+
domEach(this, function(_i, elem) {
|
|
808
|
+
removeAttribute(elem, name);
|
|
809
|
+
});
|
|
810
|
+
return this;
|
|
811
|
+
},
|
|
812
|
+
hasClass(className) {
|
|
813
|
+
for (let i = 0; i < this.length; i++) {
|
|
814
|
+
const attrs = this[i].attribs;
|
|
815
|
+
const clazz = attrs && attrs["class"];
|
|
816
|
+
let idx = -1;
|
|
817
|
+
let end;
|
|
818
|
+
if (clazz) while ((idx = clazz.indexOf(className, idx + 1)) > -1) {
|
|
819
|
+
end = idx + className.length;
|
|
820
|
+
if ((idx === 0 || rspace.test(clazz[idx - 1])) && (end === clazz.length || rspace.test(clazz[end]))) return true;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return false;
|
|
824
|
+
},
|
|
825
|
+
addClass(value) {
|
|
826
|
+
if (typeof value === "function") return domEach(this, function(i, el) {
|
|
827
|
+
const className = el.attribs["class"] || "";
|
|
828
|
+
attributesApi.addClass.call([el], value.call(el, i, className));
|
|
829
|
+
});
|
|
830
|
+
if (!value || typeof value !== "string") return this;
|
|
831
|
+
const numElements = this.length;
|
|
832
|
+
if (numElements === 1 && isTag(this[0]) && !require_nodes.hasMaterializedAttributes(this[0])) {
|
|
833
|
+
const binding = bindingOf(this[0]);
|
|
834
|
+
const document = binding?.document;
|
|
835
|
+
if (binding && document?.addClass) {
|
|
836
|
+
document.addClass([binding.id], value);
|
|
837
|
+
return this;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
const groups = /* @__PURE__ */ new Map();
|
|
841
|
+
let native = true;
|
|
842
|
+
for (let i = 0; i < numElements; i++) {
|
|
843
|
+
if (!isTag(this[i])) continue;
|
|
844
|
+
const binding = bindingOf(this[i]);
|
|
845
|
+
const document = binding?.document;
|
|
846
|
+
if (!binding || !document?.addClass || require_nodes.hasMaterializedAttributes(this[i])) {
|
|
847
|
+
native = false;
|
|
848
|
+
break;
|
|
849
|
+
}
|
|
850
|
+
const ids = groups.get(document);
|
|
851
|
+
if (ids) ids.push(binding.id);
|
|
852
|
+
else groups.set(document, [binding.id]);
|
|
853
|
+
}
|
|
854
|
+
if (native) {
|
|
855
|
+
for (const [document, ids] of groups) document.addClass(ids, value);
|
|
856
|
+
return this;
|
|
857
|
+
}
|
|
858
|
+
const classNames = value.split(rspace);
|
|
859
|
+
for (let i = 0; i < numElements; i++) {
|
|
860
|
+
if (!isTag(this[i])) continue;
|
|
861
|
+
const className = getAttr(this[i], "class");
|
|
862
|
+
if (!className) setAttr(this[i], "class", classNames.join(" ").trim());
|
|
863
|
+
else {
|
|
864
|
+
let setClass = ` ${className} `;
|
|
865
|
+
const numClasses = classNames.length;
|
|
866
|
+
for (let j = 0; j < numClasses; j++) {
|
|
867
|
+
const appendClass = `${classNames[j]} `;
|
|
868
|
+
if (setClass.indexOf(` ${appendClass}`) < 0) setClass += appendClass;
|
|
869
|
+
}
|
|
870
|
+
setAttr(this[i], "class", setClass.trim());
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
return this;
|
|
874
|
+
},
|
|
875
|
+
removeClass(value) {
|
|
876
|
+
if (typeof value === "function") return domEach(this, function(i, el) {
|
|
877
|
+
attributesApi.removeClass.call([el], value.call(el, i, el.attribs["class"] || ""));
|
|
878
|
+
});
|
|
879
|
+
const classes = splitClass(value);
|
|
880
|
+
const numClasses = classes.length;
|
|
881
|
+
const removeAll = arguments.length === 0;
|
|
882
|
+
return domEach(this, function(_i, el) {
|
|
883
|
+
if (!isTag(el)) return;
|
|
884
|
+
if (removeAll) el.attribs.class = "";
|
|
885
|
+
else {
|
|
886
|
+
const elClasses = splitClass(el.attribs.class);
|
|
887
|
+
let changed;
|
|
888
|
+
for (let j = 0; j < numClasses; j++) {
|
|
889
|
+
const index = elClasses.indexOf(classes[j]);
|
|
890
|
+
if (index >= 0) {
|
|
891
|
+
elClasses.splice(index, 1);
|
|
892
|
+
changed = true;
|
|
893
|
+
j--;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (changed) el.attribs.class = elClasses.join(" ");
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
},
|
|
900
|
+
toggleClass(value, stateVal) {
|
|
901
|
+
if (typeof value === "function") return domEach(this, function(i, el) {
|
|
902
|
+
attributesApi.toggleClass.call([el], value.call(el, i, el.attribs["class"] || "", stateVal), stateVal);
|
|
903
|
+
});
|
|
904
|
+
if (!value || typeof value !== "string") return this;
|
|
905
|
+
const classNames = value.split(rspace);
|
|
906
|
+
const numClasses = classNames.length;
|
|
907
|
+
const state = typeof stateVal === "boolean" ? stateVal ? 1 : -1 : 0;
|
|
908
|
+
const numElements = this.length;
|
|
909
|
+
for (let i = 0; i < numElements; i++) {
|
|
910
|
+
if (!isTag(this[i])) continue;
|
|
911
|
+
const elementClasses = splitClass(this[i].attribs.class);
|
|
912
|
+
for (let j = 0; j < numClasses; j++) {
|
|
913
|
+
const index = elementClasses.indexOf(classNames[j]);
|
|
914
|
+
if (state >= 0 && index < 0) elementClasses.push(classNames[j]);
|
|
915
|
+
else if (state <= 0 && index >= 0) elementClasses.splice(index, 1);
|
|
916
|
+
}
|
|
917
|
+
this[i].attribs.class = elementClasses.join(" ");
|
|
918
|
+
}
|
|
919
|
+
return this;
|
|
920
|
+
},
|
|
921
|
+
is(selector) {
|
|
922
|
+
if (selector) return traversingApi.filter.call(this, selector).length > 0;
|
|
923
|
+
return false;
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
const traversingApi = {
|
|
927
|
+
find(selectorOrHaystack) {
|
|
928
|
+
const self = this;
|
|
929
|
+
if (selectorOrHaystack && typeof selectorOrHaystack !== "string") {
|
|
930
|
+
const haystack = selectorOrHaystack.cheerio ? selectorOrHaystack.get() : [selectorOrHaystack];
|
|
931
|
+
const contains = self.constructor.contains;
|
|
932
|
+
return this._make(haystack.filter(function(elem) {
|
|
933
|
+
for (let idx = 0, len = self.length; idx < len; ++idx) if (contains(self[idx], elem)) return true;
|
|
934
|
+
return false;
|
|
935
|
+
}));
|
|
936
|
+
}
|
|
937
|
+
const binding = bindingOf(this[0]);
|
|
938
|
+
const document = binding?.document;
|
|
939
|
+
if (document && selectorOrHaystack) {
|
|
940
|
+
const contexts = this.length === 1 && binding ? [binding.id] : idsOf(document, this.toArray());
|
|
941
|
+
if (contexts.length) try {
|
|
942
|
+
const simpleTag = simpleTagExpr.exec(selectorOrHaystack);
|
|
943
|
+
if (!simpleTag) validateLegacySelector(selectorOrHaystack);
|
|
944
|
+
const finalTag = simpleTag ?? finalTagExpr.exec(selectorOrHaystack);
|
|
945
|
+
const knownType = finalTag && !specialTagExpr.test(finalTag[1]) ? "tag" : void 0;
|
|
946
|
+
const found = document.select(selectorOrHaystack, contexts);
|
|
947
|
+
if (found) return this._make(asNodes(document, found, knownType));
|
|
948
|
+
} catch (error) {
|
|
949
|
+
throw error instanceof SyntaxError ? error : new SyntaxError(error?.message ?? String(error));
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
const elems = [];
|
|
953
|
+
for (let i = 0; i < this.length; i++) for (const child of this[i].children ?? []) if (isTag(child)) elems.push(child);
|
|
954
|
+
const options = {
|
|
955
|
+
...this.options,
|
|
956
|
+
context: this.toArray()
|
|
957
|
+
};
|
|
958
|
+
return this._make(scopedSelect(selectorOrHaystack, elems, options));
|
|
959
|
+
},
|
|
960
|
+
parent(selector) {
|
|
961
|
+
const set = [];
|
|
962
|
+
domEach(this, function(_idx, elem) {
|
|
963
|
+
const parentElem = elem.parent;
|
|
964
|
+
if (parentElem && set.indexOf(parentElem) < 0) set.push(parentElem);
|
|
965
|
+
});
|
|
966
|
+
const filtered = arguments.length ? traversingApi.filter.call(set, selector, this) : set;
|
|
967
|
+
return this._make(filtered);
|
|
968
|
+
},
|
|
969
|
+
parents(selector) {
|
|
970
|
+
const parentNodes = [];
|
|
971
|
+
this.get().reverse().forEach(function(elem) {
|
|
972
|
+
traverseParents(this, elem.parent, selector, Infinity).forEach((node) => {
|
|
973
|
+
if (parentNodes.indexOf(node) === -1) parentNodes.push(node);
|
|
974
|
+
});
|
|
975
|
+
}, this);
|
|
976
|
+
return this._make(parentNodes);
|
|
977
|
+
},
|
|
978
|
+
parentsUntil(selector, filter) {
|
|
979
|
+
const parentNodes = [];
|
|
980
|
+
let untilNode;
|
|
981
|
+
let untilNodes;
|
|
982
|
+
if (typeof selector === "string") untilNode = scopedSelect(selector, this.parents().toArray(), this.options)[0];
|
|
983
|
+
else if (selector && selector.cheerio) untilNodes = selector.toArray();
|
|
984
|
+
else if (selector) untilNode = selector;
|
|
985
|
+
this.toArray().reverse().forEach(function(elem) {
|
|
986
|
+
while (elem = elem.parent) if (untilNode && elem !== untilNode || untilNodes && untilNodes.indexOf(elem) === -1 || !untilNode && !untilNodes) {
|
|
987
|
+
if (isTag(elem) && parentNodes.indexOf(elem) === -1) parentNodes.push(elem);
|
|
988
|
+
} else break;
|
|
989
|
+
});
|
|
990
|
+
return this._make(filter ? scopedSelect(filter, parentNodes, this.options) : parentNodes);
|
|
991
|
+
},
|
|
992
|
+
closest(selector) {
|
|
993
|
+
const set = [];
|
|
994
|
+
if (!selector) return this._make(set);
|
|
995
|
+
domEach(this, function(_idx, elem) {
|
|
996
|
+
const closestElem = traverseParents(this, elem, selector, 1)[0];
|
|
997
|
+
if (closestElem && set.indexOf(closestElem) < 0) set.push(closestElem);
|
|
998
|
+
});
|
|
999
|
+
return this._make(set);
|
|
1000
|
+
},
|
|
1001
|
+
next(selector) {
|
|
1002
|
+
if (!this[0]) return this;
|
|
1003
|
+
const elems = [];
|
|
1004
|
+
for (let i = 0; i < this.length; i++) {
|
|
1005
|
+
let elem = this[i];
|
|
1006
|
+
while (elem = elem.next) if (isTag(elem)) {
|
|
1007
|
+
elems.push(elem);
|
|
1008
|
+
break;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return selector ? traversingApi.filter.call(elems, selector, this) : this._make(elems);
|
|
1012
|
+
},
|
|
1013
|
+
nextAll(selector) {
|
|
1014
|
+
if (!this[0]) return this;
|
|
1015
|
+
const elems = [];
|
|
1016
|
+
for (let i = 0; i < this.length; i++) {
|
|
1017
|
+
let elem = this[i];
|
|
1018
|
+
while (elem = elem.next) if (isTag(elem) && elems.indexOf(elem) === -1) elems.push(elem);
|
|
1019
|
+
}
|
|
1020
|
+
return selector ? traversingApi.filter.call(elems, selector, this) : this._make(elems);
|
|
1021
|
+
},
|
|
1022
|
+
nextUntil(selector, filterSelector) {
|
|
1023
|
+
if (!this[0]) return this;
|
|
1024
|
+
const elems = [];
|
|
1025
|
+
let untilNode;
|
|
1026
|
+
let untilNodes;
|
|
1027
|
+
if (typeof selector === "string") untilNode = scopedSelect(selector, this.nextAll().get(), this.options)[0];
|
|
1028
|
+
else if (selector && selector.cheerio) untilNodes = selector.get();
|
|
1029
|
+
else if (selector) untilNode = selector;
|
|
1030
|
+
for (let i = 0; i < this.length; i++) {
|
|
1031
|
+
let elem = this[i];
|
|
1032
|
+
while (elem = elem.next) if (untilNode && elem !== untilNode || untilNodes && untilNodes.indexOf(elem) === -1 || !untilNode && !untilNodes) {
|
|
1033
|
+
if (isTag(elem) && elems.indexOf(elem) === -1) elems.push(elem);
|
|
1034
|
+
} else break;
|
|
1035
|
+
}
|
|
1036
|
+
return filterSelector ? traversingApi.filter.call(elems, filterSelector, this) : this._make(elems);
|
|
1037
|
+
},
|
|
1038
|
+
prev(selector) {
|
|
1039
|
+
if (!this[0]) return this;
|
|
1040
|
+
const elems = [];
|
|
1041
|
+
for (let i = 0; i < this.length; i++) {
|
|
1042
|
+
let elem = this[i];
|
|
1043
|
+
while (elem = elem.prev) if (isTag(elem)) {
|
|
1044
|
+
elems.push(elem);
|
|
1045
|
+
break;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
return selector ? traversingApi.filter.call(elems, selector, this) : this._make(elems);
|
|
1049
|
+
},
|
|
1050
|
+
prevAll(selector) {
|
|
1051
|
+
if (!this[0]) return this;
|
|
1052
|
+
const elems = [];
|
|
1053
|
+
for (let i = 0; i < this.length; i++) {
|
|
1054
|
+
let elem = this[i];
|
|
1055
|
+
while (elem = elem.prev) if (isTag(elem) && elems.indexOf(elem) === -1) elems.push(elem);
|
|
1056
|
+
}
|
|
1057
|
+
return selector ? traversingApi.filter.call(elems, selector, this) : this._make(elems);
|
|
1058
|
+
},
|
|
1059
|
+
prevUntil(selector, filterSelector) {
|
|
1060
|
+
if (!this[0]) return this;
|
|
1061
|
+
const elems = [];
|
|
1062
|
+
let untilNode;
|
|
1063
|
+
let untilNodes;
|
|
1064
|
+
if (typeof selector === "string") untilNode = scopedSelect(selector, this.prevAll().get(), this.options)[0];
|
|
1065
|
+
else if (selector && selector.cheerio) untilNodes = selector.get();
|
|
1066
|
+
else if (selector) untilNode = selector;
|
|
1067
|
+
for (let i = 0; i < this.length; i++) {
|
|
1068
|
+
let elem = this[i];
|
|
1069
|
+
while (elem = elem.prev) if (untilNode && elem !== untilNode || untilNodes && untilNodes.indexOf(elem) === -1 || !untilNode && !untilNodes) {
|
|
1070
|
+
if (isTag(elem) && elems.indexOf(elem) === -1) elems.push(elem);
|
|
1071
|
+
} else break;
|
|
1072
|
+
}
|
|
1073
|
+
return filterSelector ? traversingApi.filter.call(elems, filterSelector, this) : this._make(elems);
|
|
1074
|
+
},
|
|
1075
|
+
siblingsAndMe() {
|
|
1076
|
+
const elems = [];
|
|
1077
|
+
const parent = this[0] ? this[0].parent || this[0].root : void 0;
|
|
1078
|
+
if (parent) for (const sibling of parent.children ?? []) elems.push(sibling);
|
|
1079
|
+
return this._make(elems);
|
|
1080
|
+
},
|
|
1081
|
+
siblings(selector) {
|
|
1082
|
+
const parent = this.parent();
|
|
1083
|
+
const elems = (parent.length ? parent.children() : this.siblingsAndMe()).toArray().filter((elem) => isTag(elem) && !this.is(elem));
|
|
1084
|
+
if (selector !== void 0) return traversingApi.filter.call(elems, selector, this);
|
|
1085
|
+
else return this._make(elems);
|
|
1086
|
+
},
|
|
1087
|
+
children(selector) {
|
|
1088
|
+
const elems = [];
|
|
1089
|
+
for (let i = 0; i < this.length; i++) for (const child of this[i].children ?? []) if (isTag(child)) elems.push(child);
|
|
1090
|
+
if (selector === void 0) return this._make(elems);
|
|
1091
|
+
return traversingApi.filter.call(elems, selector, this);
|
|
1092
|
+
},
|
|
1093
|
+
contents() {
|
|
1094
|
+
const all = [];
|
|
1095
|
+
for (let i = 0; i < this.length; i++) all.push(...this[i].children ?? []);
|
|
1096
|
+
return this._make(all);
|
|
1097
|
+
},
|
|
1098
|
+
each(fn) {
|
|
1099
|
+
let i = 0;
|
|
1100
|
+
const len = this.length;
|
|
1101
|
+
while (i < len && fn.call(this[i], i, this[i]) !== false) ++i;
|
|
1102
|
+
return this;
|
|
1103
|
+
},
|
|
1104
|
+
map(fn) {
|
|
1105
|
+
const memo = [];
|
|
1106
|
+
for (let i = 0; i < this.length; i++) {
|
|
1107
|
+
const el = this[i];
|
|
1108
|
+
const val = fn.call(el, i, el);
|
|
1109
|
+
if (val != null) {
|
|
1110
|
+
if (Array.isArray(val)) memo.push(...val);
|
|
1111
|
+
else memo.push(val);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
return this._make(memo);
|
|
1115
|
+
},
|
|
1116
|
+
filter(match, container) {
|
|
1117
|
+
return makeFilterMethod((list, testFn) => list.filter((el, i) => testFn(el, i))).call(this, match, container);
|
|
1118
|
+
},
|
|
1119
|
+
not(match, container) {
|
|
1120
|
+
return makeFilterMethod((list, testFn) => list.filter((el, i) => !testFn(el, i))).call(this, match, container);
|
|
1121
|
+
},
|
|
1122
|
+
has(selectorOrHaystack) {
|
|
1123
|
+
const that = this;
|
|
1124
|
+
return traversingApi.filter.call(this, function() {
|
|
1125
|
+
return that._make(this).find(selectorOrHaystack).length > 0;
|
|
1126
|
+
});
|
|
1127
|
+
},
|
|
1128
|
+
first() {
|
|
1129
|
+
return this.length > 1 ? this._make(this[0]) : this;
|
|
1130
|
+
},
|
|
1131
|
+
last() {
|
|
1132
|
+
return this.length > 1 ? this._make(this[this.length - 1]) : this;
|
|
1133
|
+
},
|
|
1134
|
+
eq(i) {
|
|
1135
|
+
i = +i;
|
|
1136
|
+
if (i === 0 && this.length <= 1) return this;
|
|
1137
|
+
if (i < 0) i = this.length + i;
|
|
1138
|
+
return this[i] ? this._make(this[i]) : this._make([]);
|
|
1139
|
+
},
|
|
1140
|
+
get(i) {
|
|
1141
|
+
if (i == null) return Array.prototype.slice.call(this);
|
|
1142
|
+
else return this[i < 0 ? this.length + i : i];
|
|
1143
|
+
},
|
|
1144
|
+
index(selectorOrNeedle) {
|
|
1145
|
+
let $haystack, needle;
|
|
1146
|
+
if (arguments.length === 0) {
|
|
1147
|
+
$haystack = this.parent().children();
|
|
1148
|
+
needle = this[0];
|
|
1149
|
+
} else if (typeof selectorOrNeedle === "string") {
|
|
1150
|
+
$haystack = this._make(selectorOrNeedle);
|
|
1151
|
+
needle = this[0];
|
|
1152
|
+
} else {
|
|
1153
|
+
$haystack = this;
|
|
1154
|
+
needle = selectorOrNeedle.cheerio ? selectorOrNeedle[0] : selectorOrNeedle;
|
|
1155
|
+
}
|
|
1156
|
+
return $haystack.get().indexOf(needle);
|
|
1157
|
+
},
|
|
1158
|
+
slice(...args) {
|
|
1159
|
+
return this._make(Array.prototype.slice.apply(this, args));
|
|
1160
|
+
},
|
|
1161
|
+
end() {
|
|
1162
|
+
return this.prevObject || this._make([]);
|
|
1163
|
+
},
|
|
1164
|
+
add(other, context) {
|
|
1165
|
+
const selection = this._make(other, context);
|
|
1166
|
+
const merged = uniqueSort(selection.get().concat(this.get()));
|
|
1167
|
+
for (let i = 0; i < merged.length; ++i) selection[i] = merged[i];
|
|
1168
|
+
selection.length = merged.length;
|
|
1169
|
+
return selection;
|
|
1170
|
+
},
|
|
1171
|
+
addBack(selector) {
|
|
1172
|
+
return this.add(arguments.length ? this.prevObject.filter(selector) : this.prevObject);
|
|
1173
|
+
}
|
|
1174
|
+
};
|
|
1175
|
+
function makeFilterMethod(filterFn) {
|
|
1176
|
+
return function(match, container) {
|
|
1177
|
+
let testFn;
|
|
1178
|
+
const self = this;
|
|
1179
|
+
const list = typeof this.toArray === "function" ? this.toArray() : Array.from(this);
|
|
1180
|
+
container = container || (typeof this._make === "function" ? self : void 0);
|
|
1181
|
+
if (typeof match === "string") {
|
|
1182
|
+
const binding = bindingOf(list[0] ?? container?.[0]);
|
|
1183
|
+
let compiled;
|
|
1184
|
+
testFn = (el) => {
|
|
1185
|
+
if (!binding) return false;
|
|
1186
|
+
if (!compiled) {
|
|
1187
|
+
const ids = idsOf(binding.document, list);
|
|
1188
|
+
compiled = new Set(matchesSelector(binding.document, ids, match, { xmlMode: container?.options?.xmlMode }));
|
|
1189
|
+
}
|
|
1190
|
+
const id = require_nodes.viewId(el);
|
|
1191
|
+
return id !== void 0 && compiled.has(id);
|
|
1192
|
+
};
|
|
1193
|
+
} else if (typeof match === "function") testFn = (el, i) => !!match.call(el, i, el);
|
|
1194
|
+
else if (match.cheerio) testFn = (el) => match.toArray().includes(el);
|
|
1195
|
+
else testFn = (el) => match === el;
|
|
1196
|
+
if (!container) return new (this.constructor && this.constructor.prototype?._make ? this.constructor : moduleCheerio)(filterFn(list, testFn));
|
|
1197
|
+
return container._make(filterFn(list, testFn));
|
|
1198
|
+
};
|
|
1199
|
+
}
|
|
1200
|
+
function traverseParents(self, elem, selector, limit) {
|
|
1201
|
+
const elems = [];
|
|
1202
|
+
while (elem && elems.length < limit) {
|
|
1203
|
+
if (!selector || traversingApi.filter.call([elem], selector, self).length) elems.push(elem);
|
|
1204
|
+
elem = elem.parent;
|
|
1205
|
+
}
|
|
1206
|
+
return elems;
|
|
1207
|
+
}
|
|
1208
|
+
function insertAdjacentStatic(elems, position) {
|
|
1209
|
+
if (elems.length !== 1 || typeof elems[0] !== "string") return false;
|
|
1210
|
+
const options = legacyOptions(this.options);
|
|
1211
|
+
if (options.normalizeWhitespace || require_nodes.hasParserHooks(options)) return false;
|
|
1212
|
+
let document;
|
|
1213
|
+
const targetIds = [];
|
|
1214
|
+
for (let i = 0; i < this.length; i++) {
|
|
1215
|
+
const el = this[i];
|
|
1216
|
+
if (!isTag(el)) return false;
|
|
1217
|
+
const binding = bindingOf(el);
|
|
1218
|
+
if (!binding || document && binding.document !== document) return false;
|
|
1219
|
+
document ??= binding.document;
|
|
1220
|
+
const parent = el.parent || el.root;
|
|
1221
|
+
if (!parent || bindingOf(parent)?.document !== document || require_nodes.hasMaterializedChildren(parent)) return false;
|
|
1222
|
+
targetIds.push(binding.id);
|
|
1223
|
+
}
|
|
1224
|
+
if (!document) return false;
|
|
1225
|
+
const fused = document.insertHtml?.(targetIds, elems[0], position);
|
|
1226
|
+
if (fused) {
|
|
1227
|
+
for (let i = 0; i < fused.length; i++) require_nodes.setRootIfCached(document, fused[i], null);
|
|
1228
|
+
return true;
|
|
1229
|
+
}
|
|
1230
|
+
const nodeIds = require_nodes.operation(document, "create", {
|
|
1231
|
+
html: elems[0],
|
|
1232
|
+
options
|
|
1233
|
+
});
|
|
1234
|
+
if (!nodeIds.length) return true;
|
|
1235
|
+
const inserted = require_nodes.operation(document, "insert", {
|
|
1236
|
+
targetIds,
|
|
1237
|
+
nodeIds,
|
|
1238
|
+
position
|
|
1239
|
+
});
|
|
1240
|
+
for (let i = 0; i < inserted.length; i++) require_nodes.setRootIfCached(document, inserted[i], null);
|
|
1241
|
+
return true;
|
|
1242
|
+
}
|
|
1243
|
+
function removeNative(elems) {
|
|
1244
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1245
|
+
for (let i = 0; i < elems.length; i++) {
|
|
1246
|
+
const el = elems[i];
|
|
1247
|
+
const binding = bindingOf(el);
|
|
1248
|
+
if (!binding) return false;
|
|
1249
|
+
const parent = el.parent || el.root;
|
|
1250
|
+
if (!parent) continue;
|
|
1251
|
+
if (bindingOf(parent)?.document !== binding.document || require_nodes.hasMaterializedChildren(parent)) return false;
|
|
1252
|
+
let group = groups.get(binding.document);
|
|
1253
|
+
if (!group) groups.set(binding.document, group = {
|
|
1254
|
+
ids: [],
|
|
1255
|
+
nodes: []
|
|
1256
|
+
});
|
|
1257
|
+
group.ids.push(binding.id);
|
|
1258
|
+
group.nodes.push(el);
|
|
1259
|
+
}
|
|
1260
|
+
for (const [document, group] of groups) {
|
|
1261
|
+
const nativeDocument = document;
|
|
1262
|
+
if (nativeDocument.remove) nativeDocument.remove(group.ids);
|
|
1263
|
+
else require_nodes.operation(document, "remove", { ids: group.ids });
|
|
1264
|
+
for (const node of group.nodes) node.root = null;
|
|
1265
|
+
}
|
|
1266
|
+
return true;
|
|
1267
|
+
}
|
|
1268
|
+
const manipulationApi = {
|
|
1269
|
+
_makeDomArray(elem, clone) {
|
|
1270
|
+
if (elem == null) return [];
|
|
1271
|
+
else if (elem.cheerio) return clone ? cloneDom(elem.get(), elem.options) : elem.get();
|
|
1272
|
+
else if (Array.isArray(elem)) return elem.reduce((memo, el) => memo.concat(this._makeDomArray(el, clone)), []);
|
|
1273
|
+
else if (typeof elem === "string") return evaluate(elem, this.options);
|
|
1274
|
+
else return clone ? cloneDom([elem], this.options) : [elem];
|
|
1275
|
+
},
|
|
1276
|
+
appendTo(target) {
|
|
1277
|
+
if (!target.cheerio) target = this.constructor.call(this.constructor, target, null, this._originalRoot);
|
|
1278
|
+
target.append(this);
|
|
1279
|
+
return this;
|
|
1280
|
+
},
|
|
1281
|
+
prependTo(target) {
|
|
1282
|
+
if (!target.cheerio) target = this.constructor.call(this.constructor, target, null, this._originalRoot);
|
|
1283
|
+
target.prepend(this);
|
|
1284
|
+
return this;
|
|
1285
|
+
},
|
|
1286
|
+
append(...elems) {
|
|
1287
|
+
return insert.call(this, (dom, children, parent) => {
|
|
1288
|
+
uniqueSplice(children, children.length, 0, dom, parent);
|
|
1289
|
+
}, elems);
|
|
1290
|
+
},
|
|
1291
|
+
prepend(...elems) {
|
|
1292
|
+
return insert.call(this, (dom, children, parent) => {
|
|
1293
|
+
uniqueSplice(children, 0, 0, dom, parent);
|
|
1294
|
+
}, elems);
|
|
1295
|
+
},
|
|
1296
|
+
wrap(wrapper) {
|
|
1297
|
+
const wrapperFn = typeof wrapper === "function" && wrapper;
|
|
1298
|
+
const lastIdx = this.length - 1;
|
|
1299
|
+
for (let i = 0; i < this.length; i++) {
|
|
1300
|
+
const el = this[i];
|
|
1301
|
+
const parent = el.parent || el.root;
|
|
1302
|
+
if (!parent) continue;
|
|
1303
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1304
|
+
let currentWrapper = wrapper;
|
|
1305
|
+
if (wrapperFn) currentWrapper = wrapperFn.call(el, i);
|
|
1306
|
+
if (typeof currentWrapper === "string" && !isHtml(currentWrapper)) currentWrapper = this.parents().last().find(currentWrapper).clone();
|
|
1307
|
+
const dom = this._makeDomArray(currentWrapper, i < lastIdx).slice(0, 1);
|
|
1308
|
+
const index = siblings.indexOf(el);
|
|
1309
|
+
update([el], dom[0]);
|
|
1310
|
+
siblings.splice(siblings.indexOf(el), 1);
|
|
1311
|
+
uniqueSplice(siblings, index, 0, dom, parent);
|
|
1312
|
+
}
|
|
1313
|
+
return this;
|
|
1314
|
+
},
|
|
1315
|
+
after(...elems) {
|
|
1316
|
+
if (insertAdjacentStatic.call(this, elems, "after")) return this;
|
|
1317
|
+
const lastIdx = this.length - 1;
|
|
1318
|
+
domEach(this, function(i, el) {
|
|
1319
|
+
const parent = el.parent || el.root;
|
|
1320
|
+
if (!parent) return;
|
|
1321
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1322
|
+
const index = siblings.indexOf(el);
|
|
1323
|
+
let domSrc;
|
|
1324
|
+
let dom;
|
|
1325
|
+
if (index < 0) return;
|
|
1326
|
+
if (typeof elems[0] === "function") domSrc = elems[0].call(el, i, staticHtml.call(this, el.children));
|
|
1327
|
+
else domSrc = elems;
|
|
1328
|
+
dom = this._makeDomArray(domSrc, i < lastIdx);
|
|
1329
|
+
uniqueSplice(siblings, index + 1, 0, dom, parent);
|
|
1330
|
+
});
|
|
1331
|
+
return this;
|
|
1332
|
+
},
|
|
1333
|
+
insertAfter(target) {
|
|
1334
|
+
const self = this;
|
|
1335
|
+
if (typeof target === "string") target = this.constructor.call(this.constructor, target, null, this._originalRoot);
|
|
1336
|
+
const targetNodes = this._makeDomArray(target);
|
|
1337
|
+
self.remove();
|
|
1338
|
+
const clones = [];
|
|
1339
|
+
domEach(targetNodes, function(_i, el) {
|
|
1340
|
+
const clonedSelf = self._makeDomArray(self.clone());
|
|
1341
|
+
const parent = el.parent || el.root;
|
|
1342
|
+
if (!parent) return;
|
|
1343
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1344
|
+
const index = siblings.indexOf(el);
|
|
1345
|
+
if (index < 0) return;
|
|
1346
|
+
uniqueSplice(siblings, index + 1, 0, clonedSelf, parent);
|
|
1347
|
+
clones.push(clonedSelf);
|
|
1348
|
+
});
|
|
1349
|
+
return this.constructor.call(this.constructor, this._makeDomArray(clones));
|
|
1350
|
+
},
|
|
1351
|
+
before(...elems) {
|
|
1352
|
+
if (insertAdjacentStatic.call(this, elems, "before")) return this;
|
|
1353
|
+
const lastIdx = this.length - 1;
|
|
1354
|
+
domEach(this, function(i, el) {
|
|
1355
|
+
const parent = el.parent || el.root;
|
|
1356
|
+
if (!parent) return;
|
|
1357
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1358
|
+
const index = siblings.indexOf(el);
|
|
1359
|
+
let domSrc;
|
|
1360
|
+
let dom;
|
|
1361
|
+
if (index < 0) return;
|
|
1362
|
+
if (typeof elems[0] === "function") domSrc = elems[0].call(el, i, staticHtml.call(this, el.children));
|
|
1363
|
+
else domSrc = elems;
|
|
1364
|
+
dom = this._makeDomArray(domSrc, i < lastIdx);
|
|
1365
|
+
uniqueSplice(siblings, index, 0, dom, parent);
|
|
1366
|
+
});
|
|
1367
|
+
return this;
|
|
1368
|
+
},
|
|
1369
|
+
insertBefore(target) {
|
|
1370
|
+
const self = this;
|
|
1371
|
+
if (typeof target === "string") target = this.constructor.call(this.constructor, target, null, this._originalRoot);
|
|
1372
|
+
const targetNodes = this._makeDomArray(target);
|
|
1373
|
+
self.remove();
|
|
1374
|
+
const clones = [];
|
|
1375
|
+
domEach(targetNodes, function(_i, el) {
|
|
1376
|
+
const clonedSelf = self._makeDomArray(self.clone());
|
|
1377
|
+
const parent = el.parent || el.root;
|
|
1378
|
+
if (!parent) return;
|
|
1379
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1380
|
+
const index = siblings.indexOf(el);
|
|
1381
|
+
if (index < 0) return;
|
|
1382
|
+
uniqueSplice(siblings, index, 0, clonedSelf, parent);
|
|
1383
|
+
clones.push(clonedSelf);
|
|
1384
|
+
});
|
|
1385
|
+
return this.constructor.call(this.constructor, this._makeDomArray(clones));
|
|
1386
|
+
},
|
|
1387
|
+
remove(selector) {
|
|
1388
|
+
const elems = selector ? this.filter(selector) : this;
|
|
1389
|
+
if (removeNative(elems)) return this;
|
|
1390
|
+
domEach(elems, function(_i, el) {
|
|
1391
|
+
const parent = el.parent || el.root;
|
|
1392
|
+
if (!parent) return;
|
|
1393
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1394
|
+
const index = siblings.indexOf(el);
|
|
1395
|
+
if (index < 0) return;
|
|
1396
|
+
writeChildren(parent, siblings.filter((item, at) => at !== index));
|
|
1397
|
+
el.root = null;
|
|
1398
|
+
});
|
|
1399
|
+
return this;
|
|
1400
|
+
},
|
|
1401
|
+
replaceWith(content) {
|
|
1402
|
+
const self = this;
|
|
1403
|
+
domEach(this, function(i, el) {
|
|
1404
|
+
const parent = el.parent || el.root;
|
|
1405
|
+
if (!parent) return;
|
|
1406
|
+
const siblings = Array.from(parent.children ?? []);
|
|
1407
|
+
const dom = self._makeDomArray(typeof content === "function" ? content.call(el, i, el) : content);
|
|
1408
|
+
const index = siblings.indexOf(el);
|
|
1409
|
+
for (const node of dom) update(node, null);
|
|
1410
|
+
uniqueSplice(siblings, index, 1, dom, parent);
|
|
1411
|
+
el.root = null;
|
|
1412
|
+
});
|
|
1413
|
+
return this;
|
|
1414
|
+
},
|
|
1415
|
+
empty() {
|
|
1416
|
+
domEach(this, function(_i, el) {
|
|
1417
|
+
writeChildren(el, []);
|
|
1418
|
+
});
|
|
1419
|
+
return this;
|
|
1420
|
+
},
|
|
1421
|
+
html(str) {
|
|
1422
|
+
if (str === void 0) {
|
|
1423
|
+
if (!this[0] || !this[0].children) return null;
|
|
1424
|
+
return staticHtml.call(this, this[0].children, this.options);
|
|
1425
|
+
}
|
|
1426
|
+
const opts = this.options;
|
|
1427
|
+
domEach(this, function(_i, el) {
|
|
1428
|
+
update(str.cheerio ? str.clone().get() : evaluate(`${str}`, opts), el);
|
|
1429
|
+
});
|
|
1430
|
+
return this;
|
|
1431
|
+
},
|
|
1432
|
+
toString() {
|
|
1433
|
+
return staticHtml.call(this, this, this.options);
|
|
1434
|
+
},
|
|
1435
|
+
text(str) {
|
|
1436
|
+
if (str === void 0) return staticText.call(this, this);
|
|
1437
|
+
else if (typeof str === "function") return domEach(this, function(i, el) {
|
|
1438
|
+
const $el = [el];
|
|
1439
|
+
return manipulationApi.text.call($el, str.call(el, i, staticText($el)));
|
|
1440
|
+
});
|
|
1441
|
+
domEach(this, function(_i, el) {
|
|
1442
|
+
const document = bindingOf(el).document;
|
|
1443
|
+
const textId = require_nodes.operation(document, "createNode", {
|
|
1444
|
+
type: "text",
|
|
1445
|
+
data: `${str}`,
|
|
1446
|
+
options: legacyOptions(this.options)
|
|
1447
|
+
});
|
|
1448
|
+
update(require_nodes.getNode(document, textId), el);
|
|
1449
|
+
});
|
|
1450
|
+
return this;
|
|
1451
|
+
},
|
|
1452
|
+
clone() {
|
|
1453
|
+
return this._make(cloneDom(this.get(), this.options));
|
|
1454
|
+
}
|
|
1455
|
+
};
|
|
1456
|
+
function insert(concatenator, elems) {
|
|
1457
|
+
const lastIdx = this.length - 1;
|
|
1458
|
+
return domEach(this, function(i, el) {
|
|
1459
|
+
let domSrc;
|
|
1460
|
+
let dom;
|
|
1461
|
+
if (typeof elems[0] === "function") domSrc = elems[0].call(el, i, staticHtml.call(this, el.children));
|
|
1462
|
+
else domSrc = elems;
|
|
1463
|
+
dom = this._makeDomArray(domSrc, i < lastIdx);
|
|
1464
|
+
concatenator(dom, el.children, el);
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* Modify an array in-place, removing some number of elements and adding new
|
|
1469
|
+
* elements directly following them — lib/api/manipulation.js `uniqueSplice`.
|
|
1470
|
+
* Structural changes install whole child lists (see writeChildren); the
|
|
1471
|
+
* engine rebuilds the sibling links.
|
|
1472
|
+
*/
|
|
1473
|
+
function uniqueSplice(array, spliceIdx, spliceCount, newElems, parent) {
|
|
1474
|
+
const current = Array.from(array);
|
|
1475
|
+
for (let idx = 0; idx < newElems.length; ++idx) {
|
|
1476
|
+
const node = newElems[idx];
|
|
1477
|
+
const oldParent = node.parent || node.root;
|
|
1478
|
+
const prevIdx = oldParent ? Array.from(oldParent.children ?? []).indexOf(node) : -1;
|
|
1479
|
+
if (oldParent && prevIdx > -1) {
|
|
1480
|
+
if (oldParent === parent) {
|
|
1481
|
+
current.splice(prevIdx, 1);
|
|
1482
|
+
if (spliceIdx > prevIdx) spliceIdx--;
|
|
1483
|
+
} else writeChildren(oldParent, Array.from(oldParent.children ?? []).filter((item, at) => at !== prevIdx));
|
|
1484
|
+
}
|
|
1485
|
+
node.root = null;
|
|
1486
|
+
}
|
|
1487
|
+
writeChildren(parent, current.slice(0, spliceIdx).concat(newElems, current.slice(spliceIdx + spliceCount)));
|
|
1488
|
+
}
|
|
1489
|
+
const cssApi = { css(prop, val) {
|
|
1490
|
+
if (arguments.length === 2 || Object.prototype.toString.call(prop) === "[object Object]") return domEach(this, function(idx, el) {
|
|
1491
|
+
setCss(el, prop, val, idx);
|
|
1492
|
+
});
|
|
1493
|
+
else return getCss(this[0], prop);
|
|
1494
|
+
} };
|
|
1495
|
+
const submittableSelector = "input,select,textarea,keygen";
|
|
1496
|
+
const r20 = /%20/g;
|
|
1497
|
+
const rCRLF = /\r?\n/g;
|
|
1498
|
+
const formsApi = {
|
|
1499
|
+
serialize() {
|
|
1500
|
+
return formsApi.serializeArray.call(this).map((data) => `${encodeURIComponent(data.name)}=${encodeURIComponent(data.value)}`).join("&").replace(r20, "+");
|
|
1501
|
+
},
|
|
1502
|
+
serializeArray() {
|
|
1503
|
+
const Cheerio = this.constructor;
|
|
1504
|
+
return this.map(function() {
|
|
1505
|
+
const elem = this;
|
|
1506
|
+
const $elem = Cheerio(elem);
|
|
1507
|
+
if (elem.name === "form") return $elem.find(submittableSelector).toArray();
|
|
1508
|
+
else return $elem.filter(submittableSelector).toArray();
|
|
1509
|
+
}).filter("[name!=\"\"]:not(:disabled):not(:submit, :button, :image, :reset, :file):matches([checked], :not(:checkbox, :radio))").map(function(_i, elem) {
|
|
1510
|
+
const $elem = Cheerio(elem);
|
|
1511
|
+
const name = $elem.attr("name");
|
|
1512
|
+
const val = $elem.val();
|
|
1513
|
+
if (val == null) return null;
|
|
1514
|
+
else if (Array.isArray(val)) return val.map((value) => ({
|
|
1515
|
+
name,
|
|
1516
|
+
value: value.replace(rCRLF, "\r\n")
|
|
1517
|
+
}));
|
|
1518
|
+
else return {
|
|
1519
|
+
name,
|
|
1520
|
+
value: val.replace(rCRLF, "\r\n")
|
|
1521
|
+
};
|
|
1522
|
+
}).get();
|
|
1523
|
+
}
|
|
1524
|
+
};
|
|
1525
|
+
function render(that, dom, options) {
|
|
1526
|
+
if (!dom) {
|
|
1527
|
+
if (that._root && that._root.children) dom = that._root.children;
|
|
1528
|
+
else return "";
|
|
1529
|
+
} else if (typeof dom === "string") {
|
|
1530
|
+
const roots = (that._root ?? {}).children ?? [];
|
|
1531
|
+
dom = scopedSelect(dom, roots, {
|
|
1532
|
+
...options,
|
|
1533
|
+
context: roots
|
|
1534
|
+
});
|
|
1535
|
+
}
|
|
1536
|
+
return renderWith(options, dom);
|
|
1537
|
+
}
|
|
1538
|
+
/** $.html([selector | dom], [options]) */
|
|
1539
|
+
function staticHtml(dom, options) {
|
|
1540
|
+
const Cheerio = moduleCheerio;
|
|
1541
|
+
if (Object.prototype.toString.call(dom) === "[object Object]" && !options && !("length" in dom) && !("type" in dom)) {
|
|
1542
|
+
options = dom;
|
|
1543
|
+
dom = void 0;
|
|
1544
|
+
}
|
|
1545
|
+
options = defaults(options ?? {}, this._options, Cheerio.prototype.options);
|
|
1546
|
+
return render(this, dom, options);
|
|
1547
|
+
}
|
|
1548
|
+
/** $.xml([selector | dom]) */
|
|
1549
|
+
function staticXml(dom) {
|
|
1550
|
+
const options = defaults({ xmlMode: true }, this._options);
|
|
1551
|
+
return render(this, dom, options);
|
|
1552
|
+
}
|
|
1553
|
+
/** $.text(dom) */
|
|
1554
|
+
function staticText(elems) {
|
|
1555
|
+
if (!elems) elems = staticRoot.call(this);
|
|
1556
|
+
const src = elems ?? [];
|
|
1557
|
+
const len = src.length >>> 0;
|
|
1558
|
+
if (len > 0) {
|
|
1559
|
+
const first = src[0];
|
|
1560
|
+
const document = bindingOf(first)?.document;
|
|
1561
|
+
if (document) {
|
|
1562
|
+
const ids = new Array(len);
|
|
1563
|
+
let ok = true;
|
|
1564
|
+
for (let i = 0; i < len; i++) {
|
|
1565
|
+
const id = require_nodes.viewIdIn(document, src[i]);
|
|
1566
|
+
if (id === void 0) {
|
|
1567
|
+
ok = false;
|
|
1568
|
+
break;
|
|
1569
|
+
}
|
|
1570
|
+
ids[i] = id;
|
|
1571
|
+
}
|
|
1572
|
+
if (ok) {
|
|
1573
|
+
const text = document.text(ids);
|
|
1574
|
+
if (typeof text === "string") return text;
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
let ret = "";
|
|
1579
|
+
for (let i = 0; i < len; i++) {
|
|
1580
|
+
const elem = src[i];
|
|
1581
|
+
if (elem.type === "text") ret += elem.data;
|
|
1582
|
+
else if (elem.children && elem.type !== "comment") ret += staticText(elem.children);
|
|
1583
|
+
}
|
|
1584
|
+
return ret;
|
|
1585
|
+
}
|
|
1586
|
+
/** $.parseHTML(data [, context ] [, keepScripts ]) */
|
|
1587
|
+
function staticParseHTML(data, context, keepScripts = typeof context === "boolean" ? context : false) {
|
|
1588
|
+
let parsed;
|
|
1589
|
+
if (!data || typeof data !== "string") return null;
|
|
1590
|
+
if (typeof context === "boolean") keepScripts = context;
|
|
1591
|
+
parsed = this.load(data);
|
|
1592
|
+
if (!keepScripts) parsed("script").remove();
|
|
1593
|
+
return parsed.root()[0].children.slice();
|
|
1594
|
+
}
|
|
1595
|
+
/** $.root() */
|
|
1596
|
+
function staticRoot() {
|
|
1597
|
+
return this(this._root);
|
|
1598
|
+
}
|
|
1599
|
+
/** $.contains(container, contained) */
|
|
1600
|
+
function staticContains(container, contained) {
|
|
1601
|
+
if (contained === container) return false;
|
|
1602
|
+
while (contained && contained !== contained.parent) {
|
|
1603
|
+
contained = contained.parent;
|
|
1604
|
+
if (contained === container) return true;
|
|
1605
|
+
}
|
|
1606
|
+
return false;
|
|
1607
|
+
}
|
|
1608
|
+
/** $.load(str) */
|
|
1609
|
+
function load(content, options) {
|
|
1610
|
+
const Cheerio = moduleCheerio;
|
|
1611
|
+
options = defaults({ ...options ?? {} }, Cheerio.prototype.options);
|
|
1612
|
+
const root = parse(content, options);
|
|
1613
|
+
const initialize = function(selector, context, r, opts) {
|
|
1614
|
+
if (!(this instanceof initialize)) return new initialize(selector, context, r, opts);
|
|
1615
|
+
opts = defaults(opts || {}, options);
|
|
1616
|
+
return Cheerio.call(this, selector, context, r || root, opts);
|
|
1617
|
+
};
|
|
1618
|
+
initialize.prototype = Object.create(Cheerio.prototype);
|
|
1619
|
+
initialize.prototype.constructor = initialize;
|
|
1620
|
+
initialize.fn = initialize.prototype;
|
|
1621
|
+
initialize.prototype._originalRoot = root;
|
|
1622
|
+
Object.assign(initialize, staticExports);
|
|
1623
|
+
initialize._root = root;
|
|
1624
|
+
initialize._options = options;
|
|
1625
|
+
return initialize;
|
|
1626
|
+
}
|
|
1627
|
+
const staticExports = {
|
|
1628
|
+
load,
|
|
1629
|
+
html: staticHtml,
|
|
1630
|
+
xml: staticXml,
|
|
1631
|
+
text: staticText,
|
|
1632
|
+
parseHTML: staticParseHTML,
|
|
1633
|
+
root: staticRoot,
|
|
1634
|
+
contains: staticContains
|
|
1635
|
+
};
|
|
1636
|
+
const moduleCheerio = makeCheerio();
|
|
1637
|
+
for (const api of [
|
|
1638
|
+
attributesApi,
|
|
1639
|
+
traversingApi,
|
|
1640
|
+
manipulationApi,
|
|
1641
|
+
cssApi,
|
|
1642
|
+
formsApi
|
|
1643
|
+
]) Object.assign(moduleCheerio.prototype, api);
|
|
1644
|
+
Object.assign(moduleCheerio, staticExports);
|
|
1645
|
+
moduleCheerio.version = version;
|
|
1646
|
+
Object.defineProperty(exports, "evaluate", {
|
|
1647
|
+
enumerable: true,
|
|
1648
|
+
get: function() {
|
|
1649
|
+
return evaluate;
|
|
1650
|
+
}
|
|
1651
|
+
});
|
|
1652
|
+
Object.defineProperty(exports, "load", {
|
|
1653
|
+
enumerable: true,
|
|
1654
|
+
get: function() {
|
|
1655
|
+
return load;
|
|
1656
|
+
}
|
|
1657
|
+
});
|
|
1658
|
+
Object.defineProperty(exports, "moduleCheerio", {
|
|
1659
|
+
enumerable: true,
|
|
1660
|
+
get: function() {
|
|
1661
|
+
return moduleCheerio;
|
|
1662
|
+
}
|
|
1663
|
+
});
|
|
1664
|
+
Object.defineProperty(exports, "parse", {
|
|
1665
|
+
enumerable: true,
|
|
1666
|
+
get: function() {
|
|
1667
|
+
return parse;
|
|
1668
|
+
}
|
|
1669
|
+
});
|
|
1670
|
+
Object.defineProperty(exports, "staticContains", {
|
|
1671
|
+
enumerable: true,
|
|
1672
|
+
get: function() {
|
|
1673
|
+
return staticContains;
|
|
1674
|
+
}
|
|
1675
|
+
});
|
|
1676
|
+
Object.defineProperty(exports, "staticExports", {
|
|
1677
|
+
enumerable: true,
|
|
1678
|
+
get: function() {
|
|
1679
|
+
return staticExports;
|
|
1680
|
+
}
|
|
1681
|
+
});
|
|
1682
|
+
Object.defineProperty(exports, "staticHtml", {
|
|
1683
|
+
enumerable: true,
|
|
1684
|
+
get: function() {
|
|
1685
|
+
return staticHtml;
|
|
1686
|
+
}
|
|
1687
|
+
});
|
|
1688
|
+
Object.defineProperty(exports, "staticParseHTML", {
|
|
1689
|
+
enumerable: true,
|
|
1690
|
+
get: function() {
|
|
1691
|
+
return staticParseHTML;
|
|
1692
|
+
}
|
|
1693
|
+
});
|
|
1694
|
+
Object.defineProperty(exports, "staticRoot", {
|
|
1695
|
+
enumerable: true,
|
|
1696
|
+
get: function() {
|
|
1697
|
+
return staticRoot;
|
|
1698
|
+
}
|
|
1699
|
+
});
|
|
1700
|
+
Object.defineProperty(exports, "staticText", {
|
|
1701
|
+
enumerable: true,
|
|
1702
|
+
get: function() {
|
|
1703
|
+
return staticText;
|
|
1704
|
+
}
|
|
1705
|
+
});
|
|
1706
|
+
Object.defineProperty(exports, "staticXml", {
|
|
1707
|
+
enumerable: true,
|
|
1708
|
+
get: function() {
|
|
1709
|
+
return staticXml;
|
|
1710
|
+
}
|
|
1711
|
+
});
|
|
1712
|
+
Object.defineProperty(exports, "update", {
|
|
1713
|
+
enumerable: true,
|
|
1714
|
+
get: function() {
|
|
1715
|
+
return update;
|
|
1716
|
+
}
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1719
|
+
//# sourceMappingURL=cheerio-compat-DcnYK9OB.cjs.map
|