@dvaji/cheerox 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +74 -0
- package/Cargo.lock +1094 -0
- package/Cargo.toml +46 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/RELEASING.md +114 -0
- package/THIRD_PARTY_NOTICES.md +7579 -0
- package/build.rs +3 -0
- package/cheerox.darwin-arm64.node +0 -0
- package/cheerox.darwin-x64.node +0 -0
- package/cheerox.linux-arm64-gnu.node +0 -0
- package/cheerox.linux-arm64-musl.node +0 -0
- package/cheerox.linux-x64-gnu.node +0 -0
- package/cheerox.linux-x64-musl.node +0 -0
- package/cheerox.win32-arm64-msvc.node +0 -0
- package/cheerox.win32-x64-msvc.node +0 -0
- package/dist/cheerio-compat-BS--qPzw.mjs +1648 -0
- package/dist/cheerio-compat-BS--qPzw.mjs.map +1 -0
- package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
- package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
- package/dist/cheerio-compat-DSwBAJ1s.cjs +1719 -0
- package/dist/cheerio-compat-DSwBAJ1s.cjs.map +1 -0
- package/dist/cheerio-compat.cjs +18 -0
- package/dist/cheerio-compat.d.cts +2 -0
- package/dist/cheerio-compat.d.mts +2 -0
- package/dist/cheerio-compat.d.ts +2 -0
- package/dist/cheerio-compat.mjs +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.mjs +53 -0
- package/dist/index.mjs.map +1 -0
- package/dist/legacy-parse.cjs +5 -0
- package/dist/legacy-parse.d.mts +8 -0
- package/dist/legacy-parse.d.ts +7 -0
- package/dist/legacy-parse.mjs +1 -0
- package/dist/modern-B7hyW4cI.d.cts +102 -0
- package/dist/modern-B7hyW4cI.d.ts +102 -0
- package/dist/modern-CHQGP0Yt.cjs +2858 -0
- package/dist/modern-CHQGP0Yt.cjs.map +1 -0
- package/dist/modern-CJvSD_y5.d.mts +102 -0
- package/dist/modern-CvXcTn82.mjs +2769 -0
- package/dist/modern-CvXcTn82.mjs.map +1 -0
- package/dist/modern.cjs +19 -0
- package/dist/modern.d.cts +3 -0
- package/dist/modern.d.mts +3 -0
- package/dist/modern.d.ts +3 -0
- package/dist/modern.mjs +3 -0
- package/dist/nodes-jbWWhssj.cjs +4210 -0
- package/dist/nodes-jbWWhssj.cjs.map +1 -0
- package/dist/nodes-sYxb5qyZ.mjs +3803 -0
- package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
- package/dist/slim.cjs +16 -0
- package/dist/slim.cjs.map +1 -0
- package/dist/slim.d.cts +6 -0
- package/dist/slim.d.mts +6 -0
- package/dist/slim.d.ts +6 -0
- package/dist/slim.mjs +12 -0
- package/dist/slim.mjs.map +1 -0
- package/dist/types-CA01Zg3r.d.cts +526 -0
- package/dist/types-CA01Zg3r.d.mts +526 -0
- package/dist/types-CA01Zg3r.d.ts +526 -0
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.mts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.mjs +25 -0
- package/dist/utils.mjs.map +1 -0
- package/native/src/arena.rs +2109 -0
- package/native/src/attributes.rs +946 -0
- package/native/src/codec.rs +298 -0
- package/native/src/diagnostics.rs +1249 -0
- package/native/src/document_order.rs +711 -0
- package/native/src/extraction.rs +321 -0
- package/native/src/gc.rs +175 -0
- package/native/src/hook_sink.rs +801 -0
- package/native/src/hook_stream.rs +513 -0
- package/native/src/id_hash.rs +42 -0
- package/native/src/immutable.rs +333 -0
- package/native/src/lib.rs +4119 -0
- package/native/src/location.rs +392 -0
- package/native/src/markup_tokenizer.rs +668 -0
- package/native/src/runtime.rs +368 -0
- package/native/src/selector.rs +1596 -0
- package/native/src/sink.rs +274 -0
- package/native/src/snapshot.rs +672 -0
- package/native/src/stream.rs +334 -0
- package/native/src/tokenizer.rs +581 -0
- package/native/src/tracker.rs +232 -0
- package/native/vendor/README.md +26 -0
- package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
- package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/html5ever-0.35.0/README.md +60 -0
- package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
- package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
- package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
- package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
- package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
- package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
- package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
- package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
- package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
- package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
- package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
- package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
- package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
- package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
- package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
- package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
- package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
- package/native/vendor/html5gum-0.8.4/README.md +110 -0
- package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
- package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
- package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
- package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
- package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
- package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
- package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
- package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
- package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
- package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
- package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
- package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
- package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
- package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
- package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
- package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
- package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
- package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
- package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
- package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
- package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
- package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
- package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
- package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
- package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
- package/package.json +167 -4
- package/scripts/check-native.mjs +15 -0
- package/scripts/check-release.mjs +234 -0
- package/scripts/fix-declarations.mjs +39 -0
- package/scripts/generate-notices.mjs +188 -0
- package/scripts/notices/README.md +13 -0
- package/scripts/notices/cheerio-MIT.txt +21 -0
- package/scripts/notices/html5ever-AUTHORS.txt +20 -0
- package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
- package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
- package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
- package/scripts/notices/htmlparser2-MIT.txt +18 -0
- package/scripts/notices/napi-rs-MIT.txt +43 -0
- package/scripts/notices/neon-MIT.txt +19 -0
- package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
- package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
- package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
- package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
- package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
- package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
- package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
- package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
- package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
- package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
- package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
- package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
- package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
- package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
- package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
- package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
- package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
- package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
- package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
- package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
- package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
- package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
- package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
- package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
- package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
- package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
- package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
- package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
- package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
- package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
- package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
- package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
- package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
- package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
- package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
- package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
- package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
- package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
- package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
- package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
- package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
- package/scripts/notices/runtime.json +1632 -0
- package/scripts/notices/sources.json +561 -0
- package/scripts/notices/xray-MIT.txt +21 -0
- package/src/attribute-object.ts +71 -0
- package/src/attributes.ts +308 -0
- package/src/cheerio-compat/css.ts +58 -0
- package/src/cheerio-compat/render.ts +106 -0
- package/src/cheerio-compat/selectors.ts +51 -0
- package/src/cheerio-compat/types.ts +52 -0
- package/src/cheerio-compat.ts +2055 -0
- package/src/codec.ts +96 -0
- package/src/dom.ts +87 -0
- package/src/extract-plan.ts +97 -0
- package/src/extract.ts +322 -0
- package/src/hooks.ts +369 -0
- package/src/index.ts +48 -0
- package/src/io.ts +491 -0
- package/src/match.ts +207 -0
- package/src/modern.ts +259 -0
- package/src/native.ts +149 -0
- package/src/nodes.ts +831 -0
- package/src/parse.ts +53 -0
- package/src/parser-types.ts +157 -0
- package/src/router.ts +1129 -0
- package/src/selection.ts +682 -0
- package/src/slim.ts +12 -0
- package/src/snapshot.ts +291 -0
- package/src/transport.ts +113 -0
- package/src/types.ts +186 -0
- package/src/utils.ts +29 -0
- package/src/workers.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.mjs +17 -0
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { requireNative, type NativeDocument } from './native.js';
|
|
2
|
+
import { attachParsedNodes, getNode, hydrateLocation, operation } from './nodes.js';
|
|
3
|
+
import { plainSnapshot, recordAt, routeDocument, type DocumentState, type RoutedDocument } from './router.js';
|
|
4
|
+
import type { CheerioOptions } from './types.js';
|
|
5
|
+
import type { TokenizerCallbacks } from './parser-types.js';
|
|
6
|
+
|
|
7
|
+
const tokenCallbacks = ['onattribdata', 'onattribentity', 'onattribend', 'onattribname', 'oncdata', 'onclosetag', 'oncomment', 'ondeclaration', 'onend', 'onopentagend', 'onopentagname', 'onprocessinginstruction', 'onselfclosingtag', 'ontext', 'ontextentity'] as const;
|
|
8
|
+
|
|
9
|
+
export function createTokenizerSession(options: CheerioOptions): { write(chunk: string): void; end(): CheerioOptions } {
|
|
10
|
+
const events: unknown[][] = [];
|
|
11
|
+
const callbacks = Object.fromEntries(tokenCallbacks.map((name) => [name, (...args: number[]) => {
|
|
12
|
+
if (name === 'ontextentity' || name === 'onattribentity') {
|
|
13
|
+
const codePoint = +args[0];
|
|
14
|
+
// Validate through an indirect call: a bare known-builtin statement is
|
|
15
|
+
// optimized away by the bundler's pure-built-in pass, which would drop
|
|
16
|
+
// the RangeError the 0.22.0-facing behavior depends on.
|
|
17
|
+
(0, String.fromCodePoint)(codePoint);
|
|
18
|
+
args[0] = codePoint;
|
|
19
|
+
}
|
|
20
|
+
events.push([name, ...args]);
|
|
21
|
+
}]));
|
|
22
|
+
const Constructor = options.Tokenizer!;
|
|
23
|
+
const tokenizer = new Constructor(options, callbacks as unknown as TokenizerCallbacks);
|
|
24
|
+
return {
|
|
25
|
+
write(chunk) { if (tokenizer.running) tokenizer.write(chunk); },
|
|
26
|
+
end() { tokenizer.end(); const result = cleanParserOptions(options); result.__tokenEvents = events; return result; },
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function cleanParserOptions(options: CheerioOptions): CheerioOptions {
|
|
31
|
+
const result = { ...options };
|
|
32
|
+
delete result.Tokenizer; delete result.treeAdapter; delete result.onParseError;
|
|
33
|
+
delete result.__tokenEvents;
|
|
34
|
+
if (!options._useHtmlParser2 && options.onParseError) { result.__collectParseErrors = true; result.sourceCodeLocationInfo = true; }
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** User tokenizers run on JavaScript; their actual events are consumed by Rust. */
|
|
39
|
+
export function parserOptions(source: string, options: CheerioOptions): CheerioOptions {
|
|
40
|
+
if (options._useHtmlParser2 && options.Tokenizer) {
|
|
41
|
+
const session = createTokenizerSession(options);
|
|
42
|
+
session.write(source);
|
|
43
|
+
return session.end();
|
|
44
|
+
}
|
|
45
|
+
return cleanParserOptions(options);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function deliverParseErrors(document: NativeDocument, options: CheerioOptions): void {
|
|
49
|
+
if (options._useHtmlParser2 || !options.onParseError) return;
|
|
50
|
+
const diagnostics = (document as NativeDocument & { parserDiagnostics?(): string | unknown[] }).parserDiagnostics?.();
|
|
51
|
+
const errors = typeof diagnostics === 'string' ? JSON.parse(diagnostics) : diagnostics ?? [];
|
|
52
|
+
for (const error of errors) options.onParseError(error);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function hasParserHooks(options: CheerioOptions): boolean {
|
|
56
|
+
return !!(options._useHtmlParser2 ? options.Tokenizer : options.treeAdapter || options.onParseError);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A live tree adapter bridge. Every tree-builder decision consults the user's adapter. */
|
|
60
|
+
function treeAdapterBridge(adapter: any, options: CheerioOptions) {
|
|
61
|
+
const deliveredErrors = new Set<string>();
|
|
62
|
+
const nodes: any[] = [];
|
|
63
|
+
const ids = new WeakMap<object, number>();
|
|
64
|
+
const identify = (node: any): number | null => {
|
|
65
|
+
if (node == null) return null;
|
|
66
|
+
if (typeof node !== 'object') throw new TypeError('treeAdapter methods must return DOM node objects');
|
|
67
|
+
let id = ids.get(node);
|
|
68
|
+
if (id === undefined) { id = nodes.length; ids.set(node, id); nodes.push(node); }
|
|
69
|
+
return id;
|
|
70
|
+
};
|
|
71
|
+
const nodeArgs: Record<string, number[]> = {
|
|
72
|
+
appendChild: [0, 1], insertBefore: [0, 1, 2], setTemplateContent: [0, 1], getTemplateContent: [0],
|
|
73
|
+
setDocumentType: [0], setDocumentMode: [0], getDocumentMode: [0], detachNode: [0], insertText: [0],
|
|
74
|
+
insertTextBefore: [0, 2], adoptAttributes: [0], getFirstChild: [0], getChildNodes: [0], getParentNode: [0],
|
|
75
|
+
getAttrList: [0], getTagName: [0], getNamespaceURI: [0], getTextNodeContent: [0], getCommentNodeContent: [0],
|
|
76
|
+
getDocumentTypeNodeName: [0], getDocumentTypeNodePublicId: [0], getDocumentTypeNodeSystemId: [0],
|
|
77
|
+
isTextNode: [0], isCommentNode: [0], isElementNode: [0], isDocumentTypeNode: [0],
|
|
78
|
+
setNodeSourceCodeLocation: [0], getNodeSourceCodeLocation: [0], updateNodeSourceCodeLocation: [0],
|
|
79
|
+
onItemPop: [0, 1], onItemPush: [0],
|
|
80
|
+
};
|
|
81
|
+
const nodeReturns = new Set(['createDocument', 'createDocumentFragment', 'createElement', 'createCommentNode', 'createTextNode', 'getTemplateContent', 'getFirstChild', 'getParentNode']);
|
|
82
|
+
const invoke = (method: string, args: any[]): any => {
|
|
83
|
+
const fn = adapter[method];
|
|
84
|
+
if (typeof fn !== 'function') {
|
|
85
|
+
if (method === 'onItemPop' || method === 'onItemPush') return undefined;
|
|
86
|
+
throw new TypeError(`treeAdapter.${method} must be a function`);
|
|
87
|
+
}
|
|
88
|
+
return Reflect.apply(fn, adapter, args);
|
|
89
|
+
};
|
|
90
|
+
const snapshot = () => {
|
|
91
|
+
const records: any[] = [];
|
|
92
|
+
for (let id = 0; id < nodes.length; id++) {
|
|
93
|
+
const node = nodes[id];
|
|
94
|
+
const element = invoke('isElementNode', [node]);
|
|
95
|
+
const text = invoke('isTextNode', [node]);
|
|
96
|
+
const comment = invoke('isCommentNode', [node]);
|
|
97
|
+
const doctype = invoke('isDocumentTypeNode', [node]);
|
|
98
|
+
const name = element ? invoke('getTagName', [node]) : doctype ? '!doctype' : node.name;
|
|
99
|
+
const type = element ? name === 'script' || name === 'style' ? name : 'tag' : text ? 'text' : comment ? 'comment' : doctype ? 'directive' : node.type ?? 'root';
|
|
100
|
+
const children = element || type === 'root' || type === 'cdata' ? invoke('getChildNodes', [node]) ?? [] : [];
|
|
101
|
+
const record: any = { id, type, children: Array.from(children, identify), parent: identify(invoke('getParentNode', [node])) };
|
|
102
|
+
if (name !== undefined) record.name = name;
|
|
103
|
+
if (element) {
|
|
104
|
+
record.namespace = invoke('getNamespaceURI', [node]);
|
|
105
|
+
record.attribs = Object.create(null); record.attribsNamespace = Object.create(null); record.attribsPrefix = Object.create(null);
|
|
106
|
+
for (const attr of invoke('getAttrList', [node])) {
|
|
107
|
+
Object.defineProperty(record.attribs, attr.name, { value: attr.value, enumerable: true, configurable: true, writable: true });
|
|
108
|
+
record.attribsNamespace[attr.name] = attr.namespace; record.attribsPrefix[attr.name] = attr.prefix;
|
|
109
|
+
}
|
|
110
|
+
} else if (text || comment) record.data = invoke(text ? 'getTextNodeContent' : 'getCommentNodeContent', [node]);
|
|
111
|
+
else if (doctype) {
|
|
112
|
+
record.data = node.data;
|
|
113
|
+
for (const [field, method] of [['x-name', 'getDocumentTypeNodeName'], ['x-publicId', 'getDocumentTypeNodePublicId'], ['x-systemId', 'getDocumentTypeNodeSystemId']]) record[field] = invoke(method, [node]);
|
|
114
|
+
}
|
|
115
|
+
if (type === 'root' && node['x-mode'] !== undefined) record['x-mode'] = invoke('getDocumentMode', [node]);
|
|
116
|
+
for (const field of ['startIndex', 'endIndex', 'sourceCodeLocation']) if (node[field] !== undefined) record[field] = node[field];
|
|
117
|
+
records.push(record);
|
|
118
|
+
}
|
|
119
|
+
return records;
|
|
120
|
+
};
|
|
121
|
+
const bridge = (request: string): string => {
|
|
122
|
+
const { method, args = [] } = JSON.parse(request);
|
|
123
|
+
if (method === '__parseError') {
|
|
124
|
+
deliveredErrors.add(JSON.stringify(args[0]));
|
|
125
|
+
options.onParseError?.(args[0]);
|
|
126
|
+
return 'null';
|
|
127
|
+
}
|
|
128
|
+
if (method === '__snapshot') return JSON.stringify(snapshot());
|
|
129
|
+
if (method === '__locations') {
|
|
130
|
+
for (const entry of args[0]) invoke('setNodeSourceCodeLocation', [nodes[entry.id], hydrateLocation(entry.location)]);
|
|
131
|
+
return 'null';
|
|
132
|
+
}
|
|
133
|
+
const values = args.slice();
|
|
134
|
+
if (method === 'setNodeSourceCodeLocation' || method === 'updateNodeSourceCodeLocation') hydrateLocation(values[1]);
|
|
135
|
+
for (const index of nodeArgs[method] ?? []) values[index] = values[index] == null ? null : nodes[values[index]];
|
|
136
|
+
const result = invoke(method, values);
|
|
137
|
+
return JSON.stringify(method === 'getChildNodes' ? Array.from(result ?? [], identify) : nodeReturns.has(method) ? identify(result) : result ?? null);
|
|
138
|
+
};
|
|
139
|
+
return { bridge, nodes, deliveredErrors };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function bindAdapterDocument(document: NativeDocument, options: CheerioOptions, adapter: ReturnType<typeof treeAdapterBridge>): NativeDocument {
|
|
143
|
+
if (options.onParseError) {
|
|
144
|
+
const errors = (document as NativeDocument & { parserDiagnostics?(): string }).parserDiagnostics?.();
|
|
145
|
+
for (const error of errors ? JSON.parse(errors) : []) if (!adapter.deliveredErrors.has(JSON.stringify(error))) options.onParseError(error);
|
|
146
|
+
}
|
|
147
|
+
const routed = routeDocument(document, options);
|
|
148
|
+
const globalId = (routed as NativeDocument & { localNodeId(id: number): number }).localNodeId.bind(routed);
|
|
149
|
+
for (let id = 0; id < adapter.nodes.length; id++) getNode(routed, globalId(id), adapter.nodes[id]);
|
|
150
|
+
return routed;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function createTreeAdapterSession(options: CheerioOptions, isDocument: boolean): { write(chunk: string): Promise<void>; end(): Promise<NativeDocument>; abort(): void } {
|
|
154
|
+
const binding = requireNative() as ReturnType<typeof requireNative> & {
|
|
155
|
+
NativeHookStream?: new (isDocument: boolean, options: string, bridge: (request: string) => string) => { write(chunk: string): Promise<void> | void; finish(): Promise<NativeDocument> | NativeDocument; abort(): void };
|
|
156
|
+
};
|
|
157
|
+
if (!binding.NativeHookStream) throw new Error('This native addon does not support streaming treeAdapter hooks; rebuild the Rust addon.');
|
|
158
|
+
const adapter = treeAdapterBridge(options.treeAdapter, options);
|
|
159
|
+
let hasFailure = false;
|
|
160
|
+
let failure: unknown;
|
|
161
|
+
let aborted = false;
|
|
162
|
+
const bridge = (request: string): string => {
|
|
163
|
+
if (hasFailure || aborted) return '{"__cheeroxHookError":true}';
|
|
164
|
+
try { return adapter.bridge(request); }
|
|
165
|
+
catch (error) { hasFailure = true; failure = error; return '{"__cheeroxHookError":true}'; }
|
|
166
|
+
};
|
|
167
|
+
const parser = new binding.NativeHookStream(isDocument, JSON.stringify(cleanParserOptions(options)), bridge);
|
|
168
|
+
const restoreFailure = (error: unknown): never => { if (hasFailure) throw failure; throw error; };
|
|
169
|
+
return {
|
|
170
|
+
async write(chunk) {
|
|
171
|
+
try { await parser.write(chunk); if (hasFailure) throw failure; }
|
|
172
|
+
catch (error) { restoreFailure(error); }
|
|
173
|
+
},
|
|
174
|
+
async end() {
|
|
175
|
+
try {
|
|
176
|
+
const document = await parser.finish();
|
|
177
|
+
if (hasFailure) throw failure;
|
|
178
|
+
return bindAdapterDocument(document, options, adapter);
|
|
179
|
+
} catch (error) { return restoreFailure(error); }
|
|
180
|
+
},
|
|
181
|
+
abort() { aborted = true; parser.abort(); },
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function parseNativeDocument(source: string, options: CheerioOptions, isDocument: boolean): NativeDocument {
|
|
186
|
+
const binding = requireNative();
|
|
187
|
+
const nativeOptions = parserOptions(source, options);
|
|
188
|
+
if (!options._useHtmlParser2 && options.treeAdapter && canReplay(options)) {
|
|
189
|
+
const document = routeDocument(new binding.NativeDocument(source, isDocument, JSON.stringify(nativeOptions)), options) as RoutedDocument;
|
|
190
|
+
const nodes = replayIntoAdapter(document, options.treeAdapter, isDocument);
|
|
191
|
+
if (nodes) { attachParsedNodes(document.state, nodes); return document; }
|
|
192
|
+
}
|
|
193
|
+
if (!options._useHtmlParser2 && options.treeAdapter) {
|
|
194
|
+
const parse = (binding as typeof binding & { parseWithTreeAdapter?(source: string, isDocument: boolean, options: string, bridge: (request: string) => string): NativeDocument }).parseWithTreeAdapter;
|
|
195
|
+
if (!parse) throw new Error('This native addon does not support treeAdapter hooks; rebuild the Rust addon.');
|
|
196
|
+
const adapter = treeAdapterBridge(options.treeAdapter, options);
|
|
197
|
+
return bindAdapterDocument(parse(source, isDocument, JSON.stringify(nativeOptions), adapter.bridge), options, adapter);
|
|
198
|
+
}
|
|
199
|
+
const document = new binding.NativeDocument(source, isDocument, JSON.stringify(nativeOptions));
|
|
200
|
+
deliverParseErrors(document, options);
|
|
201
|
+
return routeDocument(document, options);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Error-only streams keep the Rust DOM and deliver diagnostics during writes. */
|
|
205
|
+
export function createDiagnosticSession(options: CheerioOptions, isDocument = true): { write(chunk: string): Promise<void>; end(): Promise<NativeDocument>; abort(): void } {
|
|
206
|
+
const binding = requireNative() as ReturnType<typeof requireNative> & {
|
|
207
|
+
NativeStream?: new (options: string, isDocument: boolean, bridge: (request: string) => string) => { write(chunk: string): Promise<void>; finish(): Promise<NativeDocument>; abort(): void };
|
|
208
|
+
};
|
|
209
|
+
if (!binding.NativeStream) throw new Error('This native addon does not support incremental parser diagnostics; rebuild the Rust addon.');
|
|
210
|
+
let hasFailure = false;
|
|
211
|
+
let failure: unknown;
|
|
212
|
+
let aborted = false;
|
|
213
|
+
const bridge = (request: string): string => {
|
|
214
|
+
if (hasFailure || aborted) return '{"__cheeroxHookError":true}';
|
|
215
|
+
try {
|
|
216
|
+
const { method, args } = JSON.parse(request);
|
|
217
|
+
if (method === '__parseError') options.onParseError?.(args[0]);
|
|
218
|
+
return 'null';
|
|
219
|
+
} catch (error) { hasFailure = true; failure = error; return '{"__cheeroxHookError":true}'; }
|
|
220
|
+
};
|
|
221
|
+
const parser = new binding.NativeStream(JSON.stringify(cleanParserOptions(options)), isDocument, bridge);
|
|
222
|
+
const restoreFailure = (error: unknown): never => { if (hasFailure) throw failure; throw error; };
|
|
223
|
+
return {
|
|
224
|
+
async write(chunk) { try { await parser.write(chunk); if (hasFailure) throw failure; } catch (error) { restoreFailure(error); } },
|
|
225
|
+
async end() { try { const document = await parser.finish(); if (hasFailure) throw failure; return routeDocument(document, options); } catch (error) { return restoreFailure(error); } },
|
|
226
|
+
abort() { aborted = true; parser.abort(); },
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Whether an adapter can be given the finished tree instead of every parser
|
|
232
|
+
* step: it observes neither the open-element stack nor source locations, and
|
|
233
|
+
* no parse-error callback can change it while parsing.
|
|
234
|
+
*/
|
|
235
|
+
function canReplay(options: CheerioOptions): boolean {
|
|
236
|
+
const adapter = options.treeAdapter as unknown as Record<string, unknown>;
|
|
237
|
+
return !options.onParseError && !options.sourceCodeLocationInfo && adapter.onItemPush === undefined && adapter.onItemPop === undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Builds the adapter's tree from a native parse by calling its construction
|
|
242
|
+
* methods in document order, then checks every node against the parse. An
|
|
243
|
+
* adapter whose nodes differ from what it was given (renamed elements,
|
|
244
|
+
* rewritten text, filtered children) could have steered parsing differently,
|
|
245
|
+
* so it returns `undefined` and the caller runs the parser with the adapter
|
|
246
|
+
* live instead.
|
|
247
|
+
*/
|
|
248
|
+
function replayIntoAdapter(document: RoutedDocument, adapter: any, isDocument: boolean): (object | undefined)[] | undefined {
|
|
249
|
+
const state = document.state;
|
|
250
|
+
const root = document.rootId() - state.base;
|
|
251
|
+
const size = state.baseline;
|
|
252
|
+
const nodes: any[] = new Array(size);
|
|
253
|
+
// Reading every record once builds the snapshot; plain nodes are then read from its arrays.
|
|
254
|
+
for (let local = 0; local < Math.min(size, 65); local++) recordAt(state, local);
|
|
255
|
+
for (let local = 0; local < size; local++) {
|
|
256
|
+
const snapshot = plainSnapshot(state, local);
|
|
257
|
+
const record = snapshot ? undefined : recordAt(state, local);
|
|
258
|
+
if (!snapshot && !record) continue;
|
|
259
|
+
const type = snapshot ? snapshot.typeOf(local) : record!.type;
|
|
260
|
+
if (local === root) {
|
|
261
|
+
nodes[local] = isDocument ? adapter.createDocument() : adapter.createDocumentFragment();
|
|
262
|
+
const mode = record?.['x-mode'];
|
|
263
|
+
if (isDocument && mode !== undefined) adapter.setDocumentMode(nodes[local], mode);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const parentLocal = snapshot ? snapshot.linkOf(local, 1) : record!.parent;
|
|
267
|
+
const parent = parentLocal == null ? undefined : nodes[parentLocal];
|
|
268
|
+
if (parent === undefined) return undefined;
|
|
269
|
+
switch (type) {
|
|
270
|
+
case 'tag': case 'script': case 'style': {
|
|
271
|
+
const element = snapshot
|
|
272
|
+
? adapter.createElement(snapshot.nameOf(local), snapshot.namespaceOf(local), snapshot.attributeObjects(local))
|
|
273
|
+
: adapter.createElement(record!.name, record!.namespace, attributeList(record));
|
|
274
|
+
adapter.appendChild(parent, element);
|
|
275
|
+
nodes[local] = element;
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
case 'root': {
|
|
279
|
+
const content = adapter.createDocumentFragment();
|
|
280
|
+
adapter.setTemplateContent(parent, content);
|
|
281
|
+
nodes[local] = content;
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
case 'text': {
|
|
285
|
+
adapter.insertText(parent, (snapshot ? snapshot.dataOf(local) : record!.data) ?? '');
|
|
286
|
+
const children = adapter.getChildNodes(parent);
|
|
287
|
+
nodes[local] = children[children.length - 1];
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
case 'comment': {
|
|
291
|
+
const comment = adapter.createCommentNode((snapshot ? snapshot.dataOf(local) : record!.data) ?? '');
|
|
292
|
+
adapter.appendChild(parent, comment);
|
|
293
|
+
nodes[local] = comment;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case 'directive': {
|
|
297
|
+
if (!record || record.name !== '!doctype' || !isDocument) return undefined;
|
|
298
|
+
adapter.setDocumentType(parent, record['x-name'] ?? '', record['x-publicId'] ?? '', record['x-systemId'] ?? '');
|
|
299
|
+
nodes[local] = Array.from(adapter.getChildNodes(parent) ?? []).find((child) => adapter.isDocumentTypeNode(child));
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
default: return undefined;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const changes = differencesFromParse(adapter, nodes, state, root);
|
|
306
|
+
if (!changes) return undefined;
|
|
307
|
+
// Attribute and text rewrites do not steer parsing; the document takes the adapter's values.
|
|
308
|
+
for (const change of changes) operation(document, 'setNode', { id: state.base + change.local, field: change.field, value: change.value });
|
|
309
|
+
return nodes;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function attributeList(record: any): { name: string; value: string; namespace?: string; prefix?: string }[] {
|
|
313
|
+
const attributes = record.attribs ?? {};
|
|
314
|
+
return Object.keys(attributes).map((name) => {
|
|
315
|
+
const namespace = record.attribsNamespace?.[name];
|
|
316
|
+
const prefix = record.attribsPrefix?.[name];
|
|
317
|
+
return { name, value: attributes[name], ...(namespace === undefined ? {} : { namespace }), ...(prefix === undefined ? {} : { prefix }) };
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface DataChange { local: number; field: 'attribs' | 'attribsNamespace' | 'attribsPrefix' | 'data'; value: unknown }
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The attribute and text values in which the adapter's nodes differ from the
|
|
325
|
+
* parse, or `undefined` when a name, namespace or link differs, since those
|
|
326
|
+
* could have steered parsing.
|
|
327
|
+
*/
|
|
328
|
+
function differencesFromParse(adapter: any, nodes: any[], state: DocumentState, root: number): DataChange[] | undefined {
|
|
329
|
+
const changes: DataChange[] = [];
|
|
330
|
+
for (let local = 0; local < nodes.length; local++) {
|
|
331
|
+
const node = nodes[local];
|
|
332
|
+
const snapshot = plainSnapshot(state, local);
|
|
333
|
+
const record = snapshot ? undefined : recordAt(state, local);
|
|
334
|
+
if (!snapshot && !record) continue;
|
|
335
|
+
if (!node) return undefined;
|
|
336
|
+
const type = snapshot ? snapshot.typeOf(local) : record!.type;
|
|
337
|
+
if (local !== root && adapter.getParentNode(node) !== nodes[(snapshot ? snapshot.linkOf(local, 1) : record!.parent)!]) return undefined;
|
|
338
|
+
if (type !== 'text' && type !== 'comment' && type !== 'directive') {
|
|
339
|
+
const children = adapter.getChildNodes(node) ?? [];
|
|
340
|
+
const count = snapshot ? snapshot.childCount(local) : record!.children.length;
|
|
341
|
+
if (children.length !== count) return undefined;
|
|
342
|
+
for (let index = 0; index < count; index++) if (children[index] !== nodes[snapshot ? snapshot.childAt(local, index) : record!.children[index]]) return undefined;
|
|
343
|
+
}
|
|
344
|
+
if (type === 'tag' || type === 'script' || type === 'style') {
|
|
345
|
+
const name = snapshot ? snapshot.nameOf(local) : record!.name;
|
|
346
|
+
const namespace = snapshot ? snapshot.namespaceOf(local) : record!.namespace;
|
|
347
|
+
if (!adapter.isElementNode(node) || adapter.getTagName(node) !== name || adapter.getNamespaceURI(node) !== namespace) return undefined;
|
|
348
|
+
const attributes: { name: string; value: string; namespace?: string; prefix?: string }[] = adapter.getAttrList(node) ?? [];
|
|
349
|
+
const expected = snapshot ? snapshot.attributeObjects(local) : attributeList(record);
|
|
350
|
+
const isSame = attributes.length === expected.length && attributes.every((attribute, index) => attribute.name === expected[index].name && attribute.value === expected[index].value);
|
|
351
|
+
if (!isSame) {
|
|
352
|
+
const values: Record<string, string> = Object.create(null);
|
|
353
|
+
const namespaces: Record<string, string> = Object.create(null);
|
|
354
|
+
const prefixes: Record<string, string> = Object.create(null);
|
|
355
|
+
for (const attribute of attributes) {
|
|
356
|
+
values[attribute.name] = attribute.value;
|
|
357
|
+
if (attribute.namespace !== undefined) namespaces[attribute.name] = attribute.namespace;
|
|
358
|
+
if (attribute.prefix !== undefined) prefixes[attribute.name] = attribute.prefix;
|
|
359
|
+
}
|
|
360
|
+
changes.push({ local, field: 'attribs', value: values }, { local, field: 'attribsNamespace', value: namespaces }, { local, field: 'attribsPrefix', value: prefixes });
|
|
361
|
+
}
|
|
362
|
+
} else if (type === 'text' || type === 'comment') {
|
|
363
|
+
if (type === 'text' ? !adapter.isTextNode(node) : !adapter.isCommentNode(node)) return undefined;
|
|
364
|
+
const data = type === 'text' ? adapter.getTextNodeContent(node) : adapter.getCommentNodeContent(node);
|
|
365
|
+
if (data !== (snapshot ? snapshot.dataOf(local) : record!.data)) changes.push({ local, field: 'data', value: data });
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return changes;
|
|
369
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cheerox root entry: the Cheerio 0.22.0-compatible API.
|
|
3
|
+
*
|
|
4
|
+
* The built CommonJS entry point is the callable legacy `cheerio` module
|
|
5
|
+
* itself (scripts/write-entrypoints.mjs installs the wrapper), so calling
|
|
6
|
+
* the required module directly and using its `load` property both work as in
|
|
7
|
+
* the original library. ESM consumers get named exports plus the callable
|
|
8
|
+
* default. Cheerox's modern asynchronous extensions remain available as
|
|
9
|
+
* additive exports; the 1.2.0-style facade itself lives behind the modern
|
|
10
|
+
* entry point.
|
|
11
|
+
*/
|
|
12
|
+
import legacy from './cheerio-compat.js';
|
|
13
|
+
|
|
14
|
+
export default legacy;
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
cheerio,
|
|
18
|
+
load,
|
|
19
|
+
html,
|
|
20
|
+
xml,
|
|
21
|
+
text,
|
|
22
|
+
parseHTML,
|
|
23
|
+
root,
|
|
24
|
+
contains,
|
|
25
|
+
evaluate,
|
|
26
|
+
update,
|
|
27
|
+
parse,
|
|
28
|
+
} from './cheerio-compat.js';
|
|
29
|
+
export type { CheerioInstance, CheerioConstructor, LegacyNode, LegacyOptions } from './cheerio-compat.js';
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
loadAsync,
|
|
33
|
+
loadAll,
|
|
34
|
+
extractAll,
|
|
35
|
+
configureWorkers,
|
|
36
|
+
workerPoolInfo,
|
|
37
|
+
nativeStatus,
|
|
38
|
+
dom,
|
|
39
|
+
merge,
|
|
40
|
+
flattenOptions,
|
|
41
|
+
createAPI,
|
|
42
|
+
loadBuffer,
|
|
43
|
+
stringStream,
|
|
44
|
+
decodeStream,
|
|
45
|
+
fromURL,
|
|
46
|
+
} from './modern.js';
|
|
47
|
+
|
|
48
|
+
export const version: string = (legacy as unknown as { version: string }).version;
|