@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,308 @@
|
|
|
1
|
+
import type { AnyNode, CheerioOptions, Element, SerializedField } from './types.js';
|
|
2
|
+
import type { Cheerio } from './selection.js';
|
|
3
|
+
import type { NativeDocument } from './native.js';
|
|
4
|
+
import { getNode, hydrateLocation, importNodes, nodeRecord } from './nodes.js';
|
|
5
|
+
import { attributeAt, DOCUMENT, plainSnapshot, recordAt, VIEW_LOCAL, VIEW_SELF, VIEW_STATE, type DocumentState } from './router.js';
|
|
6
|
+
|
|
7
|
+
export interface StyleProp {
|
|
8
|
+
length: number;
|
|
9
|
+
[key: string]: string | number;
|
|
10
|
+
[index: number]: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AttributeMethods<T = AnyNode> {
|
|
14
|
+
attr<U extends AnyNode>(this: Cheerio<U>, name: string): string | undefined;
|
|
15
|
+
attr<U extends AnyNode>(this: Cheerio<U>): Record<string, string> | undefined;
|
|
16
|
+
attr<U extends AnyNode>(this: Cheerio<U>, name: string, value?: string | null | ((this: Element, index: number, value: string) => string | null)): Cheerio<U>;
|
|
17
|
+
attr<U extends AnyNode>(this: Cheerio<U>, values: Record<string, string | null>): Cheerio<U>;
|
|
18
|
+
prop<U extends AnyNode>(this: Cheerio<U>, name: 'tagName' | 'nodeName'): string | undefined;
|
|
19
|
+
prop<U extends AnyNode>(this: Cheerio<U>, name: 'innerHTML' | 'outerHTML' | 'innerText' | 'textContent'): string | null;
|
|
20
|
+
prop<U extends AnyNode>(this: Cheerio<U>, name: 'style'): StyleProp | undefined;
|
|
21
|
+
prop<U extends AnyNode>(this: Cheerio<U>, name: 'href' | 'src'): string | undefined;
|
|
22
|
+
prop<U extends AnyNode, K extends keyof Element>(this: Cheerio<U>, name: K): Element[K];
|
|
23
|
+
prop<U extends AnyNode, K extends keyof Element>(this: Cheerio<U>, name: K, value: Element[K] | ((this: Element, index: number, property: K) => Element[keyof Element])): Cheerio<U>;
|
|
24
|
+
prop<U extends AnyNode>(this: Cheerio<U>, values: Record<string, string | Element[keyof Element] | boolean>): Cheerio<U>;
|
|
25
|
+
prop<U extends AnyNode>(this: Cheerio<U>, name: string, value: string | boolean | null | ((this: Element, index: number, property: string) => string | boolean)): Cheerio<U>;
|
|
26
|
+
prop<U extends AnyNode>(this: Cheerio<U>, name: string): string;
|
|
27
|
+
data<U extends AnyNode>(this: Cheerio<U>, name: string): unknown;
|
|
28
|
+
data<U extends AnyNode>(this: Cheerio<U>): Record<string, unknown>;
|
|
29
|
+
data<U extends AnyNode>(this: Cheerio<U>, name: string, value: unknown): Cheerio<U>;
|
|
30
|
+
data<U extends AnyNode>(this: Cheerio<U>, values: Record<string, unknown>): Cheerio<U>;
|
|
31
|
+
val<U extends AnyNode>(this: Cheerio<U>): string | string[] | undefined;
|
|
32
|
+
val<U extends AnyNode>(this: Cheerio<U>, value: string | string[]): Cheerio<U>;
|
|
33
|
+
removeAttr<U extends AnyNode>(this: Cheerio<U>, name: string): Cheerio<U>;
|
|
34
|
+
hasClass<U extends AnyNode>(this: Cheerio<U>, name: string): boolean;
|
|
35
|
+
addClass<U extends AnyNode, R extends ArrayLike<U>>(this: R, name?: string | ((this: Element, index: number, current: string) => string | undefined)): R;
|
|
36
|
+
removeClass<U extends AnyNode, R extends ArrayLike<U>>(this: R, name?: string | ((this: Element, index: number, current: string) => string | undefined)): R;
|
|
37
|
+
toggleClass<U extends AnyNode, R extends ArrayLike<U>>(this: R, name?: string | ((this: Element, index: number, current: string, state?: boolean) => string), state?: boolean): R;
|
|
38
|
+
css<U extends AnyNode>(this: Cheerio<U>, names?: string[]): Record<string, string> | undefined;
|
|
39
|
+
css<U extends AnyNode>(this: Cheerio<U>, name: string): string | undefined;
|
|
40
|
+
css<U extends AnyNode>(this: Cheerio<U>, name: string, value: string | ((this: Element, index: number, current: string) => string | undefined)): Cheerio<U>;
|
|
41
|
+
css<U extends AnyNode>(this: Cheerio<U>, values: Record<string, string>): Cheerio<U>;
|
|
42
|
+
serializeArray<U extends AnyNode>(this: Cheerio<U>): SerializedField[];
|
|
43
|
+
serialize<U extends AnyNode>(this: Cheerio<U>): string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type Selection = Cheerio<AnyNode>;
|
|
47
|
+
type DataRecord = Record<string, unknown>;
|
|
48
|
+
type DataDocument = NativeDocument & {
|
|
49
|
+
retainNodeData?: (id: number, data: DataRecord) => void;
|
|
50
|
+
nodeData?: (id: number) => DataRecord | undefined;
|
|
51
|
+
nodeDataOperation?: (id: number) => (command: string, args: unknown) => unknown;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const elementTypes = new Set(['tag', 'script', 'style']);
|
|
55
|
+
/** Boolean attributes read as their name in HTML, matched case-insensitively as Cheerio does. */
|
|
56
|
+
const booleanAttribute = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
|
|
57
|
+
|
|
58
|
+
const absent = Symbol('absent');
|
|
59
|
+
const deferred = Symbol('deferred');
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Reads an attribute of the selection's first node, as Cheerio reads it from
|
|
63
|
+
* `this[0]`, from cached records; `deferred` leaves the read to the native
|
|
64
|
+
* `attr` operation and `absent` means there is no such attribute.
|
|
65
|
+
*/
|
|
66
|
+
function readAttribute(selection: Selection, name: string): string | typeof absent | typeof deferred {
|
|
67
|
+
const node = selection[0] as unknown as Record<string | symbol, unknown> | null | undefined;
|
|
68
|
+
if (typeof node !== 'object' || node === null) return absent;
|
|
69
|
+
const state = node[VIEW_STATE] as DocumentState | undefined;
|
|
70
|
+
if (state === undefined || node[VIEW_SELF] !== node) return typeof node.type === 'string' ? deferred : absent;
|
|
71
|
+
const local = node[VIEW_LOCAL] as number;
|
|
72
|
+
const snapshot = plainSnapshot(state, local);
|
|
73
|
+
const record = snapshot ? undefined : recordAt(state, local);
|
|
74
|
+
const type = snapshot ? snapshot.typeOf(local) : record?.type;
|
|
75
|
+
if (type === undefined || !elementTypes.has(type)) return absent;
|
|
76
|
+
const value = attributeAt(state, local, name);
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
// Snapshot attributes are all strings; a record may hold another value, which the native read interprets.
|
|
79
|
+
const attributes = record?.attribs as Record<string, unknown> | undefined;
|
|
80
|
+
if (attributes && Object.hasOwn(attributes, name)) return deferred;
|
|
81
|
+
return name === 'value' ? deferred : absent;
|
|
82
|
+
}
|
|
83
|
+
const options = selection.options ?? {};
|
|
84
|
+
const isXml = options.xmlMode ?? (options.xml === true || (typeof options.xml === 'object' && options.xml !== null));
|
|
85
|
+
return !isXml && booleanAttribute.test(name) ? name : value;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Writes one attribute through the routed document's typed path when it has one. */
|
|
89
|
+
function setAttribute(selection: Selection, ids: number[], name: string, value: string | null): void {
|
|
90
|
+
const document = selection[DOCUMENT] as NativeDocument & { setAttribute?(ids: number[], name: string, value: string | null): void };
|
|
91
|
+
if (document.setAttribute) document.setAttribute(ids, name, value);
|
|
92
|
+
else selection.op('attr', { ids, name, value });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isElement(selection: Selection, id: number): boolean {
|
|
96
|
+
return ['tag', 'script', 'style'].includes(nodeRecord(selection[DOCUMENT], id)?.type);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function nativeValue(value: unknown): unknown {
|
|
100
|
+
// Preserve JSON's primitive normalization without serializing and parsing it.
|
|
101
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') return value;
|
|
102
|
+
if (typeof value === 'number') return Number.isFinite(value) ? (value === 0 ? 0 : value) : { __nativeNumber: String(value) };
|
|
103
|
+
const seen = new WeakSet<object>();
|
|
104
|
+
const json = JSON.stringify(value, (_key, value: unknown) => {
|
|
105
|
+
if (typeof value === 'function' || typeof value === 'symbol' || typeof value === 'bigint' || value === undefined) return { __opaqueJavaScriptValue: true };
|
|
106
|
+
if (typeof value === 'number' && !Number.isFinite(value)) return { __nativeNumber: String(value) };
|
|
107
|
+
if (value && typeof value === 'object') {
|
|
108
|
+
if (seen.has(value)) return { __opaqueJavaScriptValue: true };
|
|
109
|
+
seen.add(value);
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
});
|
|
113
|
+
return json === undefined ? { __opaqueJavaScriptValue: true } : JSON.parse(json);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function hydrateValue(value: any): any {
|
|
117
|
+
if (value && typeof value === 'object') {
|
|
118
|
+
if (Object.keys(value).length === 1 && typeof value.__nativeNumber === 'string') return Number(value.__nativeNumber);
|
|
119
|
+
for (const key of Object.keys(value)) value[key] = hydrateValue(value[key]);
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function dataObject(selection: Selection, id: number): Record<string, unknown> {
|
|
125
|
+
// The router stores data per node and carries it along when a node moves
|
|
126
|
+
// between documents, preserving data identities and callback references.
|
|
127
|
+
const document = selection[DOCUMENT] as DataDocument;
|
|
128
|
+
let data = document.nodeData?.(id);
|
|
129
|
+
if (!data) {
|
|
130
|
+
const dispatch = document.nodeDataOperation?.(id) ?? ((command: string, args: unknown) => JSON.parse(document.op(command, JSON.stringify(args))));
|
|
131
|
+
const target: Record<string, unknown> = {};
|
|
132
|
+
data = new Proxy(target, {
|
|
133
|
+
set(object, name, value) {
|
|
134
|
+
Reflect.set(object, name, value);
|
|
135
|
+
if (typeof name === 'string') dispatch('data', { ids: [id], name, value: nativeValue(value) });
|
|
136
|
+
return true;
|
|
137
|
+
},
|
|
138
|
+
defineProperty(object, name, descriptor) {
|
|
139
|
+
Reflect.defineProperty(object, name, descriptor);
|
|
140
|
+
if (typeof name === 'string' && 'value' in descriptor) dispatch('data', { ids: [id], name, value: nativeValue(descriptor.value) });
|
|
141
|
+
return true;
|
|
142
|
+
},
|
|
143
|
+
deleteProperty(object, name) {
|
|
144
|
+
Reflect.deleteProperty(object, name);
|
|
145
|
+
if (typeof name === 'string') dispatch('removeData', { ids: [id], name });
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
document.retainNodeData?.(id, data);
|
|
150
|
+
}
|
|
151
|
+
const node = getNode(selection[DOCUMENT], id);
|
|
152
|
+
// JS data can contain callbacks, cycles, and object identities. The DOM and
|
|
153
|
+
// parsed data values live in Rust; the node exposes the JS values.
|
|
154
|
+
if (!Object.hasOwn(node, 'data')) {
|
|
155
|
+
const values = data;
|
|
156
|
+
Object.defineProperty(node, 'data', { configurable: true, enumerable: true, get: () => values, set: (value: Record<string, unknown>) => { for (const key of Object.keys(values)) delete values[key]; for (const key of Object.keys(value ?? {})) values[key] = value[key]; } });
|
|
157
|
+
}
|
|
158
|
+
return data;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function property(selection: Selection, id: number, name: string, raw = false): any {
|
|
162
|
+
const result = selection.op('prop', { ids: [id], name, ...(raw ? { raw: true } : {}) });
|
|
163
|
+
if (name === 'sourceCodeLocation') return hydrateLocation(result);
|
|
164
|
+
return result?.__nativeNodeProperty ? (getNode(selection[DOCUMENT], id) as any)[result.__nativeNodeProperty] : result === null ? undefined : result;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function propertyValue(selection: Selection, name: string, value: any): any {
|
|
168
|
+
if (name === 'children' || name === 'childNodes') return importNodes(selection[DOCUMENT], value);
|
|
169
|
+
if (['parent', 'parentNode', 'prev', 'previousSibling', 'next', 'nextSibling'].includes(name)) return value == null ? null : importNodes(selection[DOCUMENT], [value])[0] ?? null;
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function attr(this: Selection, name?: any, value?: any): any {
|
|
174
|
+
if (typeof name === 'object' || value !== undefined) {
|
|
175
|
+
if (typeof value === 'function') {
|
|
176
|
+
if (typeof name !== 'string') throw new Error('Bad combination of arguments.');
|
|
177
|
+
this.ids.forEach((id, index) => {
|
|
178
|
+
if (!isElement(this, id)) return;
|
|
179
|
+
const node = getNode(this[DOCUMENT], id) as Element;
|
|
180
|
+
const current = node.attribs[name];
|
|
181
|
+
const next = value.call(node, index, current);
|
|
182
|
+
setAttribute(this, [id], name, next === null ? null : String(next));
|
|
183
|
+
});
|
|
184
|
+
} else if (typeof name === 'object') {
|
|
185
|
+
for (const key of Object.keys(name)) setAttribute(this, this.ids, key, name[key] === null ? null : String(name[key]));
|
|
186
|
+
} else {
|
|
187
|
+
setAttribute(this, this.ids, name, value === null ? null : String(value));
|
|
188
|
+
}
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
if (arguments.length > 1) return this;
|
|
192
|
+
if (typeof name === 'string') {
|
|
193
|
+
const cached = readAttribute(this, name);
|
|
194
|
+
if (cached === absent) return undefined;
|
|
195
|
+
if (cached !== deferred) return cached;
|
|
196
|
+
}
|
|
197
|
+
const result = this.op('attr', { ids: this.ids, ...(typeof name === 'string' ? { name } : {}) });
|
|
198
|
+
if (result === null) return undefined;
|
|
199
|
+
return !name && this.ids.length ? (getNode(this[DOCUMENT], this.ids[0]) as Element).attribs : result;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function prop(this: Selection, name: any, value?: any): any {
|
|
203
|
+
if (typeof name === 'string' && value === undefined) return this.ids.length ? property(this, this.ids[0], name) : undefined;
|
|
204
|
+
if (typeof name === 'object' || value !== undefined) {
|
|
205
|
+
if (typeof value === 'function') {
|
|
206
|
+
if (typeof name === 'object') throw new TypeError('Bad combination of arguments.');
|
|
207
|
+
this.ids.forEach((id, index) => {
|
|
208
|
+
if (!isElement(this, id)) return;
|
|
209
|
+
const next = value.call(getNode(this[DOCUMENT], id), index, property(this, id, name, true));
|
|
210
|
+
this.op('prop', { ids: [id], name, value: propertyValue(this, name, next) });
|
|
211
|
+
});
|
|
212
|
+
} else if (typeof name === 'object') {
|
|
213
|
+
for (const key of Object.keys(name)) this.op('prop', { ids: this.ids, name: key, value: propertyValue(this, key, name[key]) });
|
|
214
|
+
} else {
|
|
215
|
+
this.op('prop', { ids: this.ids, name, value: propertyValue(this, name, value) });
|
|
216
|
+
}
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function data(this: Selection, name?: any, value?: unknown): any {
|
|
223
|
+
const first = this.ids[0];
|
|
224
|
+
if (first === undefined || !isElement(this, first)) return undefined;
|
|
225
|
+
if (name != null && (typeof name === 'object' || value !== undefined)) {
|
|
226
|
+
for (const id of this.ids) {
|
|
227
|
+
if (!isElement(this, id)) continue;
|
|
228
|
+
const record = dataObject(this, id);
|
|
229
|
+
if (typeof name === 'object') for (const key of Object.keys(name)) record[key] = name[key];
|
|
230
|
+
else if (typeof name === 'string') record[name] = value;
|
|
231
|
+
}
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
const record = dataObject(this, first);
|
|
235
|
+
if (typeof name === 'string' && Object.hasOwn(record, name)) return record[name];
|
|
236
|
+
const found = this.op<{ found: boolean; value?: any }>('data', { ids: [first], ...(name == null ? {} : { name }) });
|
|
237
|
+
if (!found.found) return undefined;
|
|
238
|
+
if (name == null) {
|
|
239
|
+
for (const key of Object.keys(found.value)) if (!Object.hasOwn(record, key)) record[key] = hydrateValue(found.value[key]);
|
|
240
|
+
return record;
|
|
241
|
+
}
|
|
242
|
+
record[name] = hydrateValue(found.value);
|
|
243
|
+
return record[name];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function css(this: Selection, name?: any, value?: any): any {
|
|
247
|
+
if (name != null && value != null || typeof name === 'object' && !Array.isArray(name)) {
|
|
248
|
+
if (typeof name === 'object') {
|
|
249
|
+
for (const [propertyIndex, key] of Object.keys(name).entries()) {
|
|
250
|
+
if (typeof name[key] === 'function') {
|
|
251
|
+
for (const id of this.ids) {
|
|
252
|
+
if (!isElement(this, id)) continue;
|
|
253
|
+
const current = this.op('css', { ids: [id], name: key }) ?? undefined;
|
|
254
|
+
const next = name[key].call(getNode(this[DOCUMENT], id), propertyIndex, current);
|
|
255
|
+
this.op('css', { ids: [id], name: key, value: next == null ? null : String(next) });
|
|
256
|
+
}
|
|
257
|
+
} else this.op('css', { ids: this.ids, name: key, value: name[key] == null ? null : String(name[key]) });
|
|
258
|
+
}
|
|
259
|
+
} else if (typeof name === 'string' && typeof value === 'function') {
|
|
260
|
+
this.ids.forEach((id, index) => {
|
|
261
|
+
if (!isElement(this, id)) return;
|
|
262
|
+
const current = this.op('css', { ids: [id], name }) ?? undefined;
|
|
263
|
+
const next = value.call(getNode(this[DOCUMENT], id), index, current);
|
|
264
|
+
this.op('css', { ids: [id], name, value: next == null ? null : String(next) });
|
|
265
|
+
});
|
|
266
|
+
} else if (typeof name === 'string') this.op('css', { ids: this.ids, name, value: value == null ? null : String(value) });
|
|
267
|
+
return this;
|
|
268
|
+
}
|
|
269
|
+
return this.op('css', { ids: this.ids, ...(name === undefined ? {} : { name }) }) ?? undefined;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function classMethod(command: 'addClass' | 'removeClass' | 'toggleClass') {
|
|
273
|
+
return function(this: Selection, name?: any, state?: boolean): Selection {
|
|
274
|
+
if (typeof name === 'function') {
|
|
275
|
+
this.ids.forEach((id, index) => {
|
|
276
|
+
if (!isElement(this, id)) return;
|
|
277
|
+
const node = getNode(this[DOCUMENT], id) as Element;
|
|
278
|
+
const next = command === 'toggleClass'
|
|
279
|
+
? name.call(node, index, node.attribs.class ?? '', state)
|
|
280
|
+
: name.call(node, index, node.attribs.class ?? '');
|
|
281
|
+
if (typeof next === 'string') this.op(command, { ids: [id], name: next, ...(typeof state === 'boolean' ? { state } : {}) });
|
|
282
|
+
});
|
|
283
|
+
} else if (typeof name === 'string' || command === 'removeClass' && arguments.length === 0) {
|
|
284
|
+
this.op(command, { ids: this.ids, ...(typeof name === 'string' ? { name } : { all: true }), ...(typeof state === 'boolean' ? { state } : {}) });
|
|
285
|
+
}
|
|
286
|
+
return this;
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function installAttributes(CheerioClass: { prototype: object }): void {
|
|
291
|
+
Object.assign(CheerioClass.prototype, {
|
|
292
|
+
attr, prop, data, css,
|
|
293
|
+
removeAttr(this: Selection, name: string) {
|
|
294
|
+
const ids = this.ids;
|
|
295
|
+
for (const single of String(name).split(/\s+/)) if (single) setAttribute(this, ids, single, null);
|
|
296
|
+
return this;
|
|
297
|
+
},
|
|
298
|
+
hasClass(this: Selection, name: string) { return this.op<boolean>('hasClass', { ids: this.ids, name }); },
|
|
299
|
+
addClass: classMethod('addClass'), removeClass: classMethod('removeClass'), toggleClass: classMethod('toggleClass'),
|
|
300
|
+
val(this: Selection, value?: unknown) {
|
|
301
|
+
if (arguments.length === 0) return this.op('val', { ids: this.ids }) ?? undefined;
|
|
302
|
+
const result = this.op('val', { ids: this.ids, value: value === undefined ? { __undefinedJavaScriptValue: true } : value });
|
|
303
|
+
return result?.chain === false ? result.returnValue : result === false ? undefined : this;
|
|
304
|
+
},
|
|
305
|
+
serializeArray(this: Selection) { return this.op<SerializedField[]>('serializeArray', { ids: this.ids }); },
|
|
306
|
+
serialize(this: Selection) { return this.op<string>('serialize', { ids: this.ids }); },
|
|
307
|
+
});
|
|
308
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Cheerio 0.22 style attribute parsing and mutation.
|
|
2
|
+
import type { LegacyNode } from './types.js';
|
|
3
|
+
|
|
4
|
+
export function setCss(el: LegacyNode, prop: any, val: any, idx: number): void {
|
|
5
|
+
if (typeof prop === 'string') {
|
|
6
|
+
const styles = parseStyles(el.attribs?.style);
|
|
7
|
+
if (typeof val === 'function') {
|
|
8
|
+
val = val.call(el, idx, styles[prop]);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (val === '') {
|
|
12
|
+
delete styles[prop];
|
|
13
|
+
} else if (val != null) {
|
|
14
|
+
styles[prop] = val;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
el.attribs!.style = stringifyStyles(styles);
|
|
18
|
+
} else if (typeof prop === 'object') {
|
|
19
|
+
for (const key of Object.keys(prop)) {
|
|
20
|
+
setCss(el, key, prop[key], idx);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getCss(el: LegacyNode | undefined, prop?: any): any {
|
|
26
|
+
const styles = parseStyles(el?.attribs?.style);
|
|
27
|
+
if (typeof prop === 'string') {
|
|
28
|
+
return styles[prop];
|
|
29
|
+
} else if (Array.isArray(prop)) {
|
|
30
|
+
return pick(styles, prop);
|
|
31
|
+
} else {
|
|
32
|
+
return styles;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function pick(obj: Record<string, string>, props: string[]): Record<string, string> {
|
|
37
|
+
const result: Record<string, string> = {};
|
|
38
|
+
for (const prop of props) if (prop in obj) result[prop] = obj[prop];
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function stringifyStyles(obj: Record<string, string>): string {
|
|
43
|
+
return Object.keys(obj).reduce((str, prop) => `${str}${str ? ' ' : ''}${prop}: ${obj[prop]};`, '');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parseStyles(styles: string | undefined): Record<string, string> {
|
|
47
|
+
styles = (styles || '').trim();
|
|
48
|
+
|
|
49
|
+
if (!styles) return {};
|
|
50
|
+
|
|
51
|
+
return styles.split(';').reduce<Record<string, string>>((obj, str) => {
|
|
52
|
+
const n = str.indexOf(':');
|
|
53
|
+
// skip if there is no :, or if it is the first/last character
|
|
54
|
+
if (n < 1 || n === str.length - 1) return obj;
|
|
55
|
+
obj[str.slice(0, n).trim()] = str.slice(n + 1).trim();
|
|
56
|
+
return obj;
|
|
57
|
+
}, {});
|
|
58
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { nodeBinding, operation } from '../nodes.js';
|
|
2
|
+
import type { LegacyNode, LegacyOptions } from './types.js';
|
|
3
|
+
|
|
4
|
+
const isTag = (node: LegacyNode): boolean => node.type === 'tag' || node.type === 'script' || node.type === 'style';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* dom-serializer 0.1.0, over node views.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const unencodedElements: Record<string, boolean | undefined> = { style: true, script: true, xmp: true, iframe: true, noembed: true, noframes: true, plaintext: true, noscript: true };
|
|
11
|
+
|
|
12
|
+
const singleTag: Record<string, boolean | undefined> = {
|
|
13
|
+
area: true, base: true, basefont: true, br: true, col: true, command: true, embed: true, frame: true,
|
|
14
|
+
hr: true, img: true, input: true, isindex: true, keygen: true, link: true, meta: true, param: true,
|
|
15
|
+
source: true, track: true, wbr: true,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const xmlReplacements: Record<string, string> = { '&': '&', "'": ''', '>': '>', '<': '<', '"': '"' };
|
|
19
|
+
const encodeXmlRegex = /[&'><"]/g;
|
|
20
|
+
const nonAsciiRegex = /[^\0-\x7F]/g;
|
|
21
|
+
const astralRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
|
22
|
+
|
|
23
|
+
function codePointReplacement(c: string): string {
|
|
24
|
+
return `&#x${c.codePointAt(0)!.toString(16).toUpperCase()};`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** entities 1.1 `encodeXML`: the five XML characters plus every non-ASCII rune. */
|
|
28
|
+
function encodeXML(data: string): string {
|
|
29
|
+
return data
|
|
30
|
+
.replace(encodeXmlRegex, (c) => xmlReplacements[c])
|
|
31
|
+
.replace(astralRegex, codePointReplacement)
|
|
32
|
+
.replace(nonAsciiRegex, codePointReplacement);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function formatAttrs(attribs: Record<string, string> | undefined, opts: LegacyOptions): string {
|
|
36
|
+
if (!attribs) return '';
|
|
37
|
+
let output = '';
|
|
38
|
+
for (const key of Object.keys(attribs)) {
|
|
39
|
+
const value = attribs[key];
|
|
40
|
+
if (output) output += ' ';
|
|
41
|
+
output += key;
|
|
42
|
+
if ((value !== null && value !== '') || opts.xmlMode) {
|
|
43
|
+
output += `="${opts.decodeEntities ? encodeXML(value) : value}"`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return output;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* dom-serializer 0.1.x rules run natively (one call per document); nodes
|
|
51
|
+
* outside the engine, or spanning several documents, fall back to the port.
|
|
52
|
+
*/
|
|
53
|
+
export function renderWith(opts: LegacyOptions, dom: unknown, parentName?: string): string {
|
|
54
|
+
const nodes = Array.isArray(dom) || (dom && typeof dom === 'object' && 'length' in (dom as object))
|
|
55
|
+
? Array.from(dom as ArrayLike<LegacyNode>)
|
|
56
|
+
: [dom as LegacyNode];
|
|
57
|
+
void parentName;
|
|
58
|
+
if (nodes.length) {
|
|
59
|
+
const bindings = nodes.map((node) => nodeBinding(node));
|
|
60
|
+
const document = bindings[0]?.document;
|
|
61
|
+
if (document && bindings.every((binding) => binding?.document === document)) {
|
|
62
|
+
return operation<string>(document, 'render', {
|
|
63
|
+
ids: bindings.map((binding) => binding!.id),
|
|
64
|
+
// The options go through exactly as 0.22.0's render received them:
|
|
65
|
+
// defaults already applied by the static, `decodeEntities` possibly
|
|
66
|
+
// absent (bare-module `cheerio.xml()` leaves text raw).
|
|
67
|
+
options: { ...opts, _useHtmlParser2: true, legacySerializer: true },
|
|
68
|
+
}) as string;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let output = '';
|
|
72
|
+
for (const elem of nodes) {
|
|
73
|
+
if (!elem || !elem.type) continue;
|
|
74
|
+
if (elem.type === 'root') output += renderWith(opts, elem.children);
|
|
75
|
+
else if (isTag(elem)) output += renderTag(elem, opts);
|
|
76
|
+
else if (elem.type === 'directive') output += `<${elem.data}>`;
|
|
77
|
+
else if (elem.type === 'comment') output += `<!--${elem.data}-->`;
|
|
78
|
+
else if (elem.type === 'cdata') output += `<![CDATA[${elem.children && elem.children[0] ? elem.children[0].data : ''}]]>`;
|
|
79
|
+
else output += renderText(elem, opts, parentName);
|
|
80
|
+
}
|
|
81
|
+
return output;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function renderTag(elem: LegacyNode, opts: LegacyOptions): string {
|
|
85
|
+
// Handle SVG as in 0.22.0: it renders like XML.
|
|
86
|
+
const effective = elem.name === 'svg' ? ({ decodeEntities: opts.decodeEntities, xmlMode: true } as LegacyOptions) : opts;
|
|
87
|
+
let tag = `<${elem.name}`;
|
|
88
|
+
const attribs = formatAttrs(elem.attribs, effective);
|
|
89
|
+
if (attribs) tag += ` ${attribs}`;
|
|
90
|
+
if (effective.xmlMode && (!elem.children || elem.children.length === 0)) {
|
|
91
|
+
tag += '/>';
|
|
92
|
+
} else {
|
|
93
|
+
tag += '>';
|
|
94
|
+
if (elem.children) tag += renderWith(effective, elem.children, elem.name);
|
|
95
|
+
if (!singleTag[elem.name as string] || effective.xmlMode) tag += `</${elem.name}>`;
|
|
96
|
+
}
|
|
97
|
+
return tag;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function renderText(elem: LegacyNode, opts: LegacyOptions, parentName?: string): string {
|
|
101
|
+
let data = elem.data || '';
|
|
102
|
+
if (opts.decodeEntities && !(parentName !== undefined && parentName in unencodedElements)) {
|
|
103
|
+
data = encodeXML(data);
|
|
104
|
+
}
|
|
105
|
+
return data;
|
|
106
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pseudo-classes css-select 1.2.0 (Cheerio 0.22.0's selector engine)
|
|
3
|
+
* understands; anything else throws, exactly as the original library did.
|
|
4
|
+
*/
|
|
5
|
+
const legacyPseudos = new Set([
|
|
6
|
+
// filters
|
|
7
|
+
'nth-child', 'nth-last-child', 'nth-of-type', 'nth-last-of-type',
|
|
8
|
+
'first-child', 'last-child', 'first-of-type', 'last-of-type',
|
|
9
|
+
'only-of-type', 'only-child',
|
|
10
|
+
// pseudos and jQuery extensions of the time
|
|
11
|
+
'empty', 'link', 'visited', 'selected', 'disabled', 'enabled', 'checked',
|
|
12
|
+
'required', 'optional', 'parent', 'header', 'button', 'input', 'text',
|
|
13
|
+
'contains', 'icontains', 'checkbox', 'file', 'password', 'radio', 'reset',
|
|
14
|
+
'image', 'submit', 'root', 'not', 'has', 'matches', 'scope',
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
/** Reject pseudo-classes that only exist in later Cheerio lines. */
|
|
18
|
+
export function validateLegacySelector(selector: string): void {
|
|
19
|
+
let depth = 0;
|
|
20
|
+
let quote = '';
|
|
21
|
+
let escape = false;
|
|
22
|
+
for (let i = 0; i < selector.length; i++) {
|
|
23
|
+
const character = selector[i];
|
|
24
|
+
if (escape) { escape = false; continue; }
|
|
25
|
+
if (character === '\\') { escape = true; continue; }
|
|
26
|
+
if (quote) { if (character === quote) quote = ''; continue; }
|
|
27
|
+
if (character === '"' || character === "'") { quote = character; continue; }
|
|
28
|
+
if (character === '[') { depth++; continue; }
|
|
29
|
+
if (character === ']') { depth = Math.max(0, depth - 1); continue; }
|
|
30
|
+
if (depth || character !== ':') continue;
|
|
31
|
+
const match = /^:([a-zA-Z][a-zA-Z0-9-]*)/.exec(selector.slice(i));
|
|
32
|
+
if (!match) continue;
|
|
33
|
+
if (!legacyPseudos.has(match[1])) {
|
|
34
|
+
throw new SyntaxError(`unmatched pseudo-class ${match[0]}`);
|
|
35
|
+
}
|
|
36
|
+
// Skip a balanced argument so nested selectors are not scanned twice.
|
|
37
|
+
if (selector[i + match[0].length] === '(') {
|
|
38
|
+
let parens = 0;
|
|
39
|
+
let innerQuote = '';
|
|
40
|
+
for (let j = i + match[0].length; j < selector.length; j++) {
|
|
41
|
+
const inner = selector[j];
|
|
42
|
+
if (innerQuote) { if (inner === innerQuote) innerQuote = ''; continue; }
|
|
43
|
+
if (inner === '"' || inner === "'") { innerQuote = inner; continue; }
|
|
44
|
+
if (inner === '(') parens++;
|
|
45
|
+
else if (inner === ')') { parens--; if (!parens) { i = j; break; } }
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
i += match[0].length;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { CheerioOptions } from '../types.js';
|
|
2
|
+
|
|
3
|
+
export type LegacyOptions = CheerioOptions & { withDomLvl1?: boolean; normalizeWhitespace?: boolean };
|
|
4
|
+
|
|
5
|
+
/** A node under the 0.22.0 object model: plain, mutable fields on the views. */
|
|
6
|
+
export type LegacyNode = {
|
|
7
|
+
type: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
data?: string;
|
|
10
|
+
attribs?: Record<string, string>;
|
|
11
|
+
children?: LegacyNode[];
|
|
12
|
+
parent?: LegacyNode | null;
|
|
13
|
+
prev?: LegacyNode | null;
|
|
14
|
+
next?: LegacyNode | null;
|
|
15
|
+
root?: LegacyNode | null;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface CheerioInstance {
|
|
20
|
+
options: LegacyOptions;
|
|
21
|
+
_root?: CheerioInstance;
|
|
22
|
+
length: number;
|
|
23
|
+
prevObject?: CheerioInstance;
|
|
24
|
+
[index: number]: LegacyNode;
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type CheerioConstructor = {
|
|
29
|
+
(selector?: any, context?: any, root?: any, options?: LegacyOptions): CheerioInstance;
|
|
30
|
+
new (selector?: any, context?: any, root?: any, options?: LegacyOptions): CheerioInstance;
|
|
31
|
+
prototype: CheerioPrototype;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type CheerioPrototype = Record<string, any>;
|
|
35
|
+
/**
|
|
36
|
+
* A loaded `$`: the callable initialize function with the static methods,
|
|
37
|
+
* the stored root and options, and `fn` for plugins — as in 0.22.0.
|
|
38
|
+
*/
|
|
39
|
+
export type CheerioStatic = CheerioConstructor & {
|
|
40
|
+
load(content: string | Buffer | ArrayLike<LegacyNode> | LegacyNode, options?: LegacyOptions | null): CheerioStatic;
|
|
41
|
+
html(dom?: any, options?: LegacyOptions): string;
|
|
42
|
+
xml(dom?: any): string;
|
|
43
|
+
text(elems?: ArrayLike<LegacyNode>): string;
|
|
44
|
+
parseHTML(data?: string | null, context?: unknown, keepScripts?: boolean): LegacyNode[] | null;
|
|
45
|
+
root(): CheerioInstance;
|
|
46
|
+
contains(container?: LegacyNode, contained?: LegacyNode): boolean;
|
|
47
|
+
version: string;
|
|
48
|
+
_root: LegacyNode;
|
|
49
|
+
_options: LegacyOptions;
|
|
50
|
+
fn: CheerioPrototype;
|
|
51
|
+
prototype: CheerioPrototype;
|
|
52
|
+
};
|