diff --git a/crates/domparser_napi/index.d.ts b/crates/domparser_napi/index.d.ts deleted file mode 100644 index 1a9c0e8..0000000 --- a/crates/domparser_napi/index.d.ts +++ /dev/null @@ -1,176 +0,0 @@ -/* auto-generated by NAPI-RS */ -/* eslint-disable */ -export declare class NodeRepr { - /** - * Append a child node to this node, after existing children. - * - * The child node will be remove from its previous position. - */ - append(newChild: NodeRepr): void - appendChild(newChild: NodeRepr): NodeRepr - removeChild(child: NodeRepr): NodeRepr - /** - * Prepend a child node to this node, before existing children. - * - * The child node will be remove from its previous position. - */ - prepend(newChild: NodeRepr): void - /** - * Insert a new sibling after this node. - * - * The sibling node will be remove from its previous position. - */ - after(newSibling: NodeRepr): void - /** - * Insert a new sibling before this node. - * - * The sibling node will be remove from its previous position. - */ - before(newSibling: NodeRepr): void - insertBefore(newNode: NodeRepr, refNode?: NodeRepr | undefined | null): NodeRepr - /** Remove a node from its parent and siblings. Children are not affected. */ - remove(): void - /** Assign an attribute K-V to this node */ - setAttribute(name: string, value: string): void - /** Remove an attribute of this node by name. */ - removeAttribute(name: string): void - toggleAttribute(name: string, force?: boolean | undefined | null): boolean - setAttributeNS(namespace: string | undefined | null, name: string, value: string): void - removeAttributeNS(namespace: string | undefined | null, localName: string): void - createElement(tagName: string): NodeRepr - createTextNode(data: string): NodeRepr - createComment(data: string): NodeRepr - createDocumentFragment(): NodeRepr - createProcessingInstruction(target: string, data: string): NodeRepr - importNode(externalNode: NodeRepr, deep?: boolean | undefined | null): NodeRepr - adoptNode(externalNode: NodeRepr): NodeRepr - replaceChild(newChild: NodeRepr, oldChild: NodeRepr): NodeRepr - replaceWith(newNode: NodeRepr): void - insertAdjacentElement(position: string, element: NodeRepr): NodeRepr | null - insertAdjacentText(position: string, data: string): void - insertAdjacentHTML(position: string, html: string): void - normalize(): void - splitText(offset: number): NodeRepr - substringData(offset: number, count: number): string - appendData(data: string): void - insertData(offset: number, data: string): void - deleteData(offset: number, count: number): void - replaceData(offset: number, count: number, data: string): void - get nodeType(): number - get nodeName(): string - get tagName(): string | null - get namespaceURI(): string | null - get prefix(): string | null - get localName(): string | null - get id(): string - set id(id: string) - get className(): string - set className(className: string) - get parentNode(): NodeRepr | null - get firstChild(): NodeRepr | null - get lastChild(): NodeRepr | null - get previousSibling(): NodeRepr | null - get nextSibling(): NodeRepr | null - get parentElement(): NodeRepr | null - get firstElementChild(): NodeRepr | null - get lastElementChild(): NodeRepr | null - get previousElementSibling(): NodeRepr | null - get nextElementSibling(): NodeRepr | null - get children(): Array - get childElementCount(): number - getRootNode(): NodeRepr - get nodeValue(): string | null - set nodeValue(value?: string | undefined | null) - get data(): string | null - set data(value: string) - get textContent(): string - set textContent(text: string) - isSameNode(otherNode: NodeRepr): boolean - get innerHTML(): string - set innerHTML(html: string) - get outerHTML(): string - set outerHTML(html: string) - get ownerDocument(): NodeRepr | null - get childNodes(): Array - get isConnected(): boolean - get doctype(): NodeRepr | null - get publicId(): string - get systemId(): string - get name(): string - get target(): string - classListAdd(className: string): void - classListRemove(className: string): void - classListToggle(className: string, force?: boolean | undefined | null): boolean - classListContains(className: string): boolean - datasetGet(): Record - datasetSet(key: string, value: string): void - datasetRemove(key: string): void - /** Select the the fist node that match the given css selector, like document.querySelector. */ - select(selectors: string): NodeRepr | null - /** Select all nodes that match the given css selector, like document.querySelectorAll. */ - selectAll(selectors: string): Array - /** Get all children nodes of this node. */ - getChildren(): Array - /** Get attribute value of this node by given name. */ - getAttribute(name: string): string | null - /** Get attributes K-V object of this node. */ - getAttributes(): Record - getAttributeNames(): Array - hasAttributes(): boolean - hasChildNodes(): boolean - compareDocumentPosition(other: NodeRepr): number - /** Get the serialized html of this node, including its all descendants and itelf. */ - outerHtml(): string - /** Get the serialized html of this node, only including its all descendants. */ - innerHtml(): string - /** Get all text nodes content of this node, including its all descendants and itelf. */ - text(): string - querySelector(selectors: string): NodeRepr | null - querySelectorAll(selectors: string): Array - getAttribute(name: string): string | null - hasAttribute(name: string): boolean - getAttributeNS(namespace: string | undefined | null, localName: string): string | null - hasAttributeNS(namespace: string | undefined | null, localName: string): boolean - lookupPrefix(namespace?: string | undefined | null): string | null - lookupNamespaceURI(prefix?: string | undefined | null): string | null - isDefaultNamespace(namespace?: string | undefined | null): boolean - getElementById(id: string): NodeRepr | null - getElementsByClassName(classNames: string): Array - getElementsByTagName(tagName: string): Array - contains(otherNode: NodeRepr): boolean - isEqualNode(otherNode: NodeRepr): boolean - get head(): NodeRepr | null - get body(): NodeRepr | null - get title(): string - get documentElement(): NodeRepr | null - matches(selectors: string): boolean - closest(selectors: string): NodeRepr | null - /** The node object, cann't be instantiated in javascript. So call the constructor will throw an error. */ - constructor(): void - get elementNode(): number - get attributeNode(): number - get textNode(): number - get cdataSectionNode(): number - get entityReferenceNode(): number - get entityNode(): number - get processingInstructionNode(): number - get commentNode(): number - get documentNode(): number - get documentTypeNode(): number - get documentFragmentNode(): number - get notationNode(): number - get documentPositionDisconnected(): number - get documentPositionPreceding(): number - get documentPositionFollowing(): number - get documentPositionContains(): number - get documentPositionContainedBy(): number - get documentPositionImplementationSpecific(): number - /** Clone this node to a new instance, not clone its descendants. */ - clone(): NodeRepr - /** Clone this node to a new instance, including its all descendants. */ - cloneRecursive(): NodeRepr - cloneNode(deep?: boolean | undefined | null): NodeRepr -} - -/** Parse string input to a html tree, return the root node. */ -export declare function parse(html: string): NodeRepr diff --git a/crates/domparser_napi/index.js b/crates/domparser_napi/index.js deleted file mode 100644 index 8661879..0000000 --- a/crates/domparser_napi/index.js +++ /dev/null @@ -1,576 +0,0 @@ -// prettier-ignore -/* eslint-disable */ -// @ts-nocheck -/* auto-generated by NAPI-RS */ - -const { readFileSync } = require('node:fs') -let nativeBinding = null -const loadErrors = [] - -const isMusl = () => { - let musl = false - if (process.platform === 'linux') { - musl = isMuslFromFilesystem() - if (musl === null) { - musl = isMuslFromReport() - } - if (musl === null) { - musl = isMuslFromChildProcess() - } - } - return musl -} - -const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-') - -const isMuslFromFilesystem = () => { - try { - return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl') - } catch { - return null - } -} - -const isMuslFromReport = () => { - let report = null - if (typeof process.report?.getReport === 'function') { - process.report.excludeNetwork = true - report = process.report.getReport() - } - if (!report) { - return null - } - if (report.header && report.header.glibcVersionRuntime) { - return false - } - if (Array.isArray(report.sharedObjects)) { - if (report.sharedObjects.some(isFileMusl)) { - return true - } - } - return false -} - -const isMuslFromChildProcess = () => { - try { - return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl') - } catch (e) { - // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false - return false - } -} - -function requireNative() { - if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) { - try { - return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH); - } catch (err) { - loadErrors.push(err) - } - } else if (process.platform === 'android') { - if (process.arch === 'arm64') { - try { - return require('./domparser.android-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-android-arm64') - const bindingPackageVersion = require('domparser-android-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 'arm') { - try { - return require('./domparser.android-arm-eabi.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-android-arm-eabi') - const bindingPackageVersion = require('domparser-android-arm-eabi/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`)) - } - } else if (process.platform === 'win32') { - if (process.arch === 'x64') { - if (process.config?.variables?.shlib_suffix === 'dll.a' || process.config?.variables?.node_target_type === 'shared_library') { - try { - return require('./domparser.win32-x64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-win32-x64-gnu') - const bindingPackageVersion = require('domparser-win32-x64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - try { - return require('./domparser.win32-x64-msvc.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-win32-x64-msvc') - const bindingPackageVersion = require('domparser-win32-x64-msvc/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } - } else if (process.arch === 'ia32') { - try { - return require('./domparser.win32-ia32-msvc.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-win32-ia32-msvc') - const bindingPackageVersion = require('domparser-win32-ia32-msvc/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 'arm64') { - try { - return require('./domparser.win32-arm64-msvc.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-win32-arm64-msvc') - const bindingPackageVersion = require('domparser-win32-arm64-msvc/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`)) - } - } else if (process.platform === 'darwin') { - try { - return require('./domparser.darwin-universal.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-darwin-universal') - const bindingPackageVersion = require('domparser-darwin-universal/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - if (process.arch === 'x64') { - try { - return require('./domparser.darwin-x64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-darwin-x64') - const bindingPackageVersion = require('domparser-darwin-x64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 'arm64') { - try { - return require('./domparser.darwin-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-darwin-arm64') - const bindingPackageVersion = require('domparser-darwin-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`)) - } - } else if (process.platform === 'freebsd') { - if (process.arch === 'x64') { - try { - return require('./domparser.freebsd-x64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-freebsd-x64') - const bindingPackageVersion = require('domparser-freebsd-x64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 'arm64') { - try { - return require('./domparser.freebsd-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-freebsd-arm64') - const bindingPackageVersion = require('domparser-freebsd-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`)) - } - } else if (process.platform === 'linux') { - if (process.arch === 'x64') { - if (isMusl()) { - try { - return require('./domparser.linux-x64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-x64-musl') - const bindingPackageVersion = require('domparser-linux-x64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - try { - return require('./domparser.linux-x64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-x64-gnu') - const bindingPackageVersion = require('domparser-linux-x64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } - } else if (process.arch === 'arm64') { - if (isMusl()) { - try { - return require('./domparser.linux-arm64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-arm64-musl') - const bindingPackageVersion = require('domparser-linux-arm64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - try { - return require('./domparser.linux-arm64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-arm64-gnu') - const bindingPackageVersion = require('domparser-linux-arm64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } - } else if (process.arch === 'arm') { - if (isMusl()) { - try { - return require('./domparser.linux-arm-musleabihf.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-arm-musleabihf') - const bindingPackageVersion = require('domparser-linux-arm-musleabihf/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - try { - return require('./domparser.linux-arm-gnueabihf.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-arm-gnueabihf') - const bindingPackageVersion = require('domparser-linux-arm-gnueabihf/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } - } else if (process.arch === 'loong64') { - if (isMusl()) { - try { - return require('./domparser.linux-loong64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-loong64-musl') - const bindingPackageVersion = require('domparser-linux-loong64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - try { - return require('./domparser.linux-loong64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-loong64-gnu') - const bindingPackageVersion = require('domparser-linux-loong64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } - } else if (process.arch === 'riscv64') { - if (isMusl()) { - try { - return require('./domparser.linux-riscv64-musl.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-riscv64-musl') - const bindingPackageVersion = require('domparser-linux-riscv64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - try { - return require('./domparser.linux-riscv64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-riscv64-gnu') - const bindingPackageVersion = require('domparser-linux-riscv64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } - } else if (process.arch === 'ppc64') { - try { - return require('./domparser.linux-ppc64-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-ppc64-gnu') - const bindingPackageVersion = require('domparser-linux-ppc64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 's390x') { - try { - return require('./domparser.linux-s390x-gnu.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-linux-s390x-gnu') - const bindingPackageVersion = require('domparser-linux-s390x-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`)) - } - } else if (process.platform === 'openharmony') { - if (process.arch === 'arm64') { - try { - return require('./domparser.openharmony-arm64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-openharmony-arm64') - const bindingPackageVersion = require('domparser-openharmony-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 'x64') { - try { - return require('./domparser.openharmony-x64.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-openharmony-x64') - const bindingPackageVersion = require('domparser-openharmony-x64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else if (process.arch === 'arm') { - try { - return require('./domparser.openharmony-arm.node') - } catch (e) { - loadErrors.push(e) - } - try { - const binding = require('domparser-openharmony-arm') - const bindingPackageVersion = require('domparser-openharmony-arm/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) - } - return binding - } catch (e) { - loadErrors.push(e) - } - } else { - loadErrors.push(new Error(`Unsupported architecture on OpenHarmony: ${process.arch}`)) - } - } else { - loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`)) - } -} - -nativeBinding = requireNative() - -if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { - let wasiBinding = null - let wasiBindingError = null - try { - wasiBinding = require('./domparser.wasi.cjs') - nativeBinding = wasiBinding - } catch (err) { - if (process.env.NAPI_RS_FORCE_WASI) { - wasiBindingError = err - } - } - if (!nativeBinding) { - try { - wasiBinding = require('domparser-wasm32-wasi') - nativeBinding = wasiBinding - } catch (err) { - if (process.env.NAPI_RS_FORCE_WASI) { - wasiBindingError.cause = err - loadErrors.push(err) - } - } - } - if (process.env.NAPI_RS_FORCE_WASI === 'error' && !wasiBinding) { - const error = new Error('WASI binding not found and NAPI_RS_FORCE_WASI is set to error') - error.cause = wasiBindingError - throw error - } -} - -if (!nativeBinding) { - if (loadErrors.length > 0) { - throw new Error( - `Cannot find native binding. ` + - `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` + - 'Please try `npm i` again after removing both package-lock.json and node_modules directory.', - { - cause: loadErrors.reduce((err, cur) => { - cur.cause = err - return cur - }), - }, - ) - } - throw new Error(`Failed to load native binding`) -} - -module.exports = nativeBinding -module.exports.NodeRepr = nativeBinding.NodeRepr -module.exports.parse = nativeBinding.parse diff --git a/crates/domparser_napi/src/node_repr/mod.rs b/crates/domparser_napi/src/node_repr/mod.rs index 6d68b84..3dd2d52 100644 --- a/crates/domparser_napi/src/node_repr/mod.rs +++ b/crates/domparser_napi/src/node_repr/mod.rs @@ -118,7 +118,6 @@ impl NodeRepr { impl NodeRepr { /// Clone this node to a new instance, not clone its descendants. /// - #[napi(js_name = "clone")] pub fn clone_self_only(&self) -> NodeRepr { let new_node_ref = NodeRef::new(self.0.data().clone()); NodeRepr::from(new_node_ref) @@ -126,7 +125,6 @@ impl NodeRepr { /// Clone this node to a new instance, including its all descendants. /// - #[napi] pub fn clone_recursive(&self) -> NodeRepr { NodeRepr::from(clone_node_ref_recursive(&self.0)) } diff --git a/crates/domparser_napi/src/node_repr/query.rs b/crates/domparser_napi/src/node_repr/query.rs index b266017..a089a40 100644 --- a/crates/domparser_napi/src/node_repr/query.rs +++ b/crates/domparser_napi/src/node_repr/query.rs @@ -1,6 +1,5 @@ use html5ever::serialize::{self, serialize, SerializeOpts}; use html5ever::{local_name, namespace_url, ns, LocalName, Namespace}; -use indexmap::IndexMap; use kuchikiki::ExpandedName; use crate::serializer::serialize_text_only; @@ -11,14 +10,12 @@ use super::NodeRepr; impl NodeRepr { /// Select the the fist node that match the given css selector, like document.querySelector. /// - #[napi] pub fn select(&self, selectors: String) -> Option { self.0.select_first(&selectors).ok().map(Into::into) } /// Select all nodes that match the given css selector, like document.querySelectorAll. /// - #[napi] pub fn select_all(&self, selectors: String) -> Vec { self .0 @@ -26,37 +23,33 @@ impl NodeRepr { .map_or(vec![], |els| els.map(Into::into).collect()) } - /// Get all children nodes of this node. - /// - #[napi] - pub fn get_children(&self) -> Vec { - self.0.children().map(Into::into).collect() - } - /// Get attribute value of this node by given name. /// #[napi] pub fn get_attribute(&self, name: String) -> Option { - self - .0 - .as_element() - .and_then(|e| e.attributes.borrow().get(name).map(|v| v.to_string())) - } + self.0.as_element().and_then(|e| { + let attrs = e.attributes.borrow(); + if let Some(val) = attrs.get(name.as_str()) { + return Some(val.to_string()); + } - /// Get attributes K-V object of this node. - /// - #[napi] - pub fn get_attributes(&self) -> IndexMap { - self.0.as_element().map_or_else(IndexMap::new, |e| { - e.attributes - .borrow() - .map - .iter() - .map(|(expanded_name, attr)| { - let ExpandedName { local, ns: _ } = expanded_name; - (local.to_string(), attr.value.to_string()) - }) - .collect::>() + // Fallback: search by qualified name + for (key, attr) in attrs.map.iter() { + let qname = if let Some(prefix) = &attr.prefix { + if prefix.is_empty() { + key.local.to_string() + } else { + format!("{}:{}", prefix, key.local) + } + } else { + key.local.to_string() + }; + + if qname == name { + return Some(attr.value.to_string()); + } + } + None }) } @@ -66,8 +59,18 @@ impl NodeRepr { e.attributes .borrow() .map - .keys() - .map(|expanded_name| expanded_name.local.to_string()) + .iter() + .map(|(key, attr)| { + if let Some(prefix) = &attr.prefix { + if prefix.is_empty() { + key.local.to_string() + } else { + format!("{}:{}", prefix, key.local) + } + } else { + key.local.to_string() + } + }) .collect() }) } @@ -171,7 +174,6 @@ impl NodeRepr { /// Get the serialized html of this node, including its all descendants and itelf. /// - #[napi] pub fn outer_html(&self) -> String { let mut u8_vec = Vec::new(); serialize( @@ -189,7 +191,6 @@ impl NodeRepr { /// Get the serialized html of this node, only including its all descendants. /// - #[napi] pub fn inner_html(&self) -> String { let mut buf = Vec::::new(); serialize( @@ -207,7 +208,6 @@ impl NodeRepr { /// Get all text nodes content of this node, including its all descendants and itelf. /// - #[napi] pub fn text(&self) -> String { let mut buf = Vec::::new(); serialize_text_only(&self.0, &mut buf).unwrap(); @@ -224,11 +224,6 @@ impl NodeRepr { self.select_all(selectors) } - #[napi(js_name = "getAttribute")] - pub fn get_attribute_js(&self, name: String) -> Option { - self.get_attribute(name) - } - #[napi(js_name = "hasAttribute")] pub fn has_attribute(&self, name: String) -> bool { self.get_attribute(name).is_some() diff --git a/domparser.js b/domparser.js index b3b2ad4..f4b7f95 100644 --- a/domparser.js +++ b/domparser.js @@ -93,7 +93,6 @@ class DOMParser { } module.exports = { - parse, NodeRepr, DOMParser }; diff --git a/index.d.ts b/index.d.ts index d30870f..f538b93 100644 --- a/index.d.ts +++ b/index.d.ts @@ -105,29 +105,14 @@ export declare class NodeRepr { datasetGet(): Record datasetSet(key: string, value: string): void datasetRemove(key: string): void - /** Select the the fist node that match the given css selector, like document.querySelector. */ - select(selectors: string): NodeRepr | null - /** Select all nodes that match the given css selector, like document.querySelectorAll. */ - selectAll(selectors: string): Array - /** Get all children nodes of this node. */ - getChildren(): Array /** Get attribute value of this node by given name. */ getAttribute(name: string): string | null - /** Get attributes K-V object of this node. */ - getAttributes(): Record getAttributeNames(): Array hasAttributes(): boolean hasChildNodes(): boolean compareDocumentPosition(other: NodeRepr): number - /** Get the serialized html of this node, including its all descendants and itelf. */ - outerHtml(): string - /** Get the serialized html of this node, only including its all descendants. */ - innerHtml(): string - /** Get all text nodes content of this node, including its all descendants and itelf. */ - text(): string querySelector(selectors: string): NodeRepr | null querySelectorAll(selectors: string): Array - getAttribute(name: string): string | null hasAttribute(name: string): boolean getAttributeNS(namespace: string | undefined | null, localName: string): string | null hasAttributeNS(namespace: string | undefined | null, localName: string): boolean @@ -165,22 +150,8 @@ export declare class NodeRepr { get documentPositionContains(): number get documentPositionContainedBy(): number get documentPositionImplementationSpecific(): number - /** Clone this node to a new instance, not clone its descendants. */ - clone(): NodeRepr - /** Clone this node to a new instance, including its all descendants. */ - cloneRecursive(): NodeRepr cloneNode(deep?: boolean | undefined | null): NodeRepr } /** Parse string input to a html tree, return the root node. */ export declare function parse(html: string): NodeRepr -export declare interface NodeRepr { - select(selectors: "html"): NodeRepr; - select(selectors: "head"): NodeRepr; - select(selectors: "body"): NodeRepr; -} - -export declare class DOMParser { - parseFromString(string: string, mimeType: string): NodeRepr; -} - diff --git a/index.js b/index.js index 35a7612..9a3e545 100644 --- a/index.js +++ b/index.js @@ -77,8 +77,8 @@ function requireNative() { try { const binding = require('domparser-android-arm64') const bindingPackageVersion = require('domparser-android-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -93,8 +93,8 @@ function requireNative() { try { const binding = require('domparser-android-arm-eabi') const bindingPackageVersion = require('domparser-android-arm-eabi/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -114,8 +114,8 @@ function requireNative() { try { const binding = require('domparser-win32-x64-gnu') const bindingPackageVersion = require('domparser-win32-x64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -130,8 +130,8 @@ function requireNative() { try { const binding = require('domparser-win32-x64-msvc') const bindingPackageVersion = require('domparser-win32-x64-msvc/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -147,8 +147,8 @@ function requireNative() { try { const binding = require('domparser-win32-ia32-msvc') const bindingPackageVersion = require('domparser-win32-ia32-msvc/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -163,8 +163,8 @@ function requireNative() { try { const binding = require('domparser-win32-arm64-msvc') const bindingPackageVersion = require('domparser-win32-arm64-msvc/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -182,8 +182,8 @@ function requireNative() { try { const binding = require('domparser-darwin-universal') const bindingPackageVersion = require('domparser-darwin-universal/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -198,8 +198,8 @@ function requireNative() { try { const binding = require('domparser-darwin-x64') const bindingPackageVersion = require('domparser-darwin-x64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -214,8 +214,8 @@ function requireNative() { try { const binding = require('domparser-darwin-arm64') const bindingPackageVersion = require('domparser-darwin-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -234,8 +234,8 @@ function requireNative() { try { const binding = require('domparser-freebsd-x64') const bindingPackageVersion = require('domparser-freebsd-x64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -250,8 +250,8 @@ function requireNative() { try { const binding = require('domparser-freebsd-arm64') const bindingPackageVersion = require('domparser-freebsd-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -271,8 +271,8 @@ function requireNative() { try { const binding = require('domparser-linux-x64-musl') const bindingPackageVersion = require('domparser-linux-x64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -287,8 +287,8 @@ function requireNative() { try { const binding = require('domparser-linux-x64-gnu') const bindingPackageVersion = require('domparser-linux-x64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -305,8 +305,8 @@ function requireNative() { try { const binding = require('domparser-linux-arm64-musl') const bindingPackageVersion = require('domparser-linux-arm64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -321,8 +321,42 @@ function requireNative() { try { const binding = require('domparser-linux-arm64-gnu') const bindingPackageVersion = require('domparser-linux-arm64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding + } catch (e) { + loadErrors.push(e) + } + } + } else if (process.arch === 'arm') { + if (isMusl()) { + try { + return require('./domparser.linux-arm-musleabihf.node') + } catch (e) { + loadErrors.push(e) + } + try { + const binding = require('domparser-linux-arm-musleabihf') + const bindingPackageVersion = require('domparser-linux-arm-musleabihf/package.json').version + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding + } catch (e) { + loadErrors.push(e) + } + } else { + try { + return require('./domparser.linux-arm-gnueabihf.node') + } catch (e) { + loadErrors.push(e) + } + try { + const binding = require('domparser-linux-arm-gnueabihf') + const bindingPackageVersion = require('domparser-linux-arm-gnueabihf/package.json').version + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -339,8 +373,8 @@ function requireNative() { try { const binding = require('domparser-linux-loong64-musl') const bindingPackageVersion = require('domparser-linux-loong64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -355,8 +389,8 @@ function requireNative() { try { const binding = require('domparser-linux-loong64-gnu') const bindingPackageVersion = require('domparser-linux-loong64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -373,8 +407,8 @@ function requireNative() { try { const binding = require('domparser-linux-riscv64-musl') const bindingPackageVersion = require('domparser-linux-riscv64-musl/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -389,8 +423,8 @@ function requireNative() { try { const binding = require('domparser-linux-riscv64-gnu') const bindingPackageVersion = require('domparser-linux-riscv64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -406,8 +440,8 @@ function requireNative() { try { const binding = require('domparser-linux-ppc64-gnu') const bindingPackageVersion = require('domparser-linux-ppc64-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -422,8 +456,8 @@ function requireNative() { try { const binding = require('domparser-linux-s390x-gnu') const bindingPackageVersion = require('domparser-linux-s390x-gnu/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -442,8 +476,8 @@ function requireNative() { try { const binding = require('domparser-openharmony-arm64') const bindingPackageVersion = require('domparser-openharmony-arm64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -458,8 +492,8 @@ function requireNative() { try { const binding = require('domparser-openharmony-x64') const bindingPackageVersion = require('domparser-openharmony-x64/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { @@ -474,8 +508,8 @@ function requireNative() { try { const binding = require('domparser-openharmony-arm') const bindingPackageVersion = require('domparser-openharmony-arm/package.json').version - if (bindingPackageVersion !== '0.0.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { - throw new Error(`Native binding package version mismatch, expected 0.0.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + if (bindingPackageVersion !== '0.0.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) } return binding } catch (e) { diff --git a/scripts/postbuild.js b/scripts/postbuild.js index ea8e52d..6043cae 100644 --- a/scripts/postbuild.js +++ b/scripts/postbuild.js @@ -15,6 +15,15 @@ generatedFiles.forEach(file => { } }); +// Move .node files +const files = fs.readdirSync(crateDir); +files.filter(f => f.endsWith('.node')).forEach(file => { + const src = path.join(crateDir, file); + const dest = path.join(rootDir, file); + console.log(`Moving ${file} to root...`); + fs.renameSync(src, dest); +}); + // 1. Patch index.d.ts const patchFile = path.join(rootDir, 'patch.d.ts'); const indexDts = path.join(rootDir, 'index.d.ts'); diff --git a/test/domparser.spec.mjs b/test/domparser.spec.mjs index 7180f85..b7e846e 100644 --- a/test/domparser.spec.mjs +++ b/test/domparser.spec.mjs @@ -6,7 +6,7 @@ test('DOMParser should parse html string', () => { const parser = new DOMParser() const doc = parser.parseFromString('
', 'text/html') assert.strictEqual( - doc.outerHtml(), + doc.documentElement.outerHTML, '
', ) }) @@ -24,7 +24,7 @@ test('DOMParser should throw on unsupported mime type', () => { test('should support innerHTML setter', () => { const parser = new DOMParser() const doc = parser.parseFromString('
', 'text/html') - const div = doc.select('div') + const div = doc.querySelector('div') div.innerHTML = 'hello' assert.strictEqual(div.innerHTML, 'hello') }) @@ -32,7 +32,7 @@ test('should support innerHTML setter', () => { test('should support textContent setter', () => { const parser = new DOMParser() const doc = parser.parseFromString('
', 'text/html') - const div = doc.select('div') + const div = doc.querySelector('div') div.textContent = 'hello' assert.strictEqual(div.innerHTML, 'hello') }) @@ -40,8 +40,8 @@ test('should support textContent setter', () => { test('should support replaceChild', () => { const parser = new DOMParser() const doc = parser.parseFromString('
old
', 'text/html') - const div = doc.select('div') - const oldSpan = doc.select('span') + const div = doc.querySelector('div') + const oldSpan = doc.querySelector('span') const newSpan = doc.createElement('b') newSpan.textContent = 'new' @@ -52,8 +52,8 @@ test('should support replaceChild', () => { test('should support contains', () => { const parser = new DOMParser() const doc = parser.parseFromString('
hello
', 'text/html') - const div = doc.select('div') - const span = doc.select('span') + const div = doc.querySelector('div') + const span = doc.querySelector('span') assert.ok(div.contains(span)) assert.ok(doc.contains(span)) assert.strictEqual(span.contains(div), false) @@ -70,16 +70,16 @@ test('should support head/body/title', () => { test('should support element traversal', () => { const parser = new DOMParser() const doc = parser.parseFromString('
12
', 'text/html') - const div = doc.select('div') - const span = doc.select('span') - const b = doc.select('b') + const div = doc.querySelector('div') + const span = doc.querySelector('span') + const b = doc.querySelector('b') - assert.strictEqual(div.firstElementChild.outerHtml(), '1') - assert.strictEqual(div.lastElementChild.outerHtml(), '2') - assert.strictEqual(span.nextElementSibling.outerHtml(), '2') - assert.strictEqual(b.previousElementSibling.outerHtml(), '1') + assert.strictEqual(div.firstElementChild.outerHTML, '1') + assert.strictEqual(div.lastElementChild.outerHTML, '2') + assert.strictEqual(span.nextElementSibling.outerHTML, '2') + assert.strictEqual(b.previousElementSibling.outerHTML, '1') assert.strictEqual( - span.parentElement.outerHtml(), + span.parentElement.outerHTML, '
12
', ) assert.strictEqual(div.childElementCount, 2) @@ -89,7 +89,7 @@ test('should support element traversal', () => { test('should support matches and closest', () => { const parser = new DOMParser() const doc = parser.parseFromString('
hello
', 'text/html') - const span = doc.select('#bar') + const span = doc.querySelector('#bar') assert.ok(span.matches('#bar')) assert.ok(span.matches('span')) @@ -107,7 +107,7 @@ test('should support matches and closest', () => { test('should support getRootNode', () => { const parser = new DOMParser() const doc = parser.parseFromString('
', 'text/html') - const div = doc.select('div') + const div = doc.querySelector('div') assert.strictEqual(div.getRootNode().nodeType, 9) // Document node }) @@ -128,8 +128,8 @@ test('should support createComment and createDocumentFragment', () => { test('should support isSameNode', () => { const parser = new DOMParser() const doc = parser.parseFromString('
', 'text/html') - const div1 = doc.select('div') - const div2 = doc.select('div') + const div1 = doc.querySelector('div') + const div2 = doc.querySelector('div') assert.ok(div1.isSameNode(div2)) assert.strictEqual(div1.isSameNode(doc), false) @@ -138,7 +138,7 @@ test('should support isSameNode', () => { test('should support attribute methods', () => { const parser = new DOMParser() const doc = parser.parseFromString('
', 'text/html') - const div = doc.select('div') + const div = doc.querySelector('div') assert.ok(div.hasAttributes()) assert.deepStrictEqual(div.getAttributeNames().sort(), ['class', 'id']) diff --git a/test/index.spec.mjs b/test/index.spec.mjs index c901f42..736f30e 100644 --- a/test/index.spec.mjs +++ b/test/index.spec.mjs @@ -5,11 +5,24 @@ import path from "path"; import { fileURLToPath } from "url"; import beautify from "js-beautify"; -import { parse } from "../domparser.js"; +import { DOMParser } from "../domparser.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); +function parse(html) { + return new DOMParser().parseFromString(html, "text/html"); +} + +function getAttributes(node) { + const names = node.getAttributeNames(); + const attrs = {}; + for (const name of names) { + attrs[name] = node.getAttribute(name); + } + return attrs; +} + function formatHtml(html) { return beautify .html(html, { preserve_newlines: false }) @@ -21,7 +34,7 @@ test("should sanitise html", () => { '
first
Bad body
second
', ); assert.strictEqual( - $.outerHtml(), + $.outerHTML, '
first
Bad body
second
', ); }); @@ -31,7 +44,7 @@ test("should not change the original html structure", () => { encoding: "utf8", }); const $ = parse(jqueryHtml); - assert.strictEqual(formatHtml($.outerHtml()), formatHtml(jqueryHtml)); + assert.strictEqual(formatHtml($.outerHTML), formatHtml(jqueryHtml)); }); test("should select node and get attributes with ns correctly", () => { @@ -50,13 +63,13 @@ test("should select node and get attributes with ns correctly", () => { `); - assert.deepStrictEqual($.select("svg").getAttributes(), { + assert.deepStrictEqual(getAttributes($.querySelector("svg")), { xmlns: "http://www.w3.org/2000/svg", width: "100", height: "100", }); - assert.strictEqual($.select("svg").getAttribute("width"), "100"); + assert.strictEqual($.querySelector("svg").getAttribute("width"), "100"); }); test("should select first correctly", () => { @@ -65,7 +78,7 @@ test("should select first correctly", () => { ); assert.strictEqual( - $.select(".one").select("#two").outerHtml(), + $.querySelector(".one").querySelector("#two").outerHTML, '
second
', ); }); @@ -76,12 +89,12 @@ test("should select all correctly", () => { ); assert.deepStrictEqual( - $.selectAll("div").map((e) => e.outerHtml()), + $.querySelectorAll("div").map((e) => e.outerHTML), ['
first
', '
second
'], ); assert.deepStrictEqual( - $.selectAll("body>*").map((e) => e.outerHtml()), + $.querySelectorAll("body>*").map((e) => e.outerHTML), ['
first
', '
second
'], ); }); @@ -92,9 +105,9 @@ test("should get all childs correctly", () => { ); assert.deepStrictEqual( - $.select("body") - .getChildren() - .map((e) => e.outerHtml()), + $.querySelector("body") + .children + .map((e) => e.outerHTML), ['
first
', '
second
'], ); }); @@ -104,10 +117,7 @@ test("should get text correctly", () => { '
first
second
', ); - assert.deepStrictEqual( - $.select("body").text()["firstsecond"], - undefined, - ); + assert.strictEqual($.querySelector("body").textContent, "firstsecond"); }); test("should append child correctly", () => { @@ -118,10 +128,10 @@ test("should append child correctly", () => { '
second
', ); - $1.select(".one").append($2.select("#two")); + $1.querySelector(".one").appendChild($2.querySelector("#two")); assert.strictEqual( - $1.select(".one").outerHtml(), + $1.querySelector(".one").outerHTML, '
first
second
', ); }); @@ -134,10 +144,10 @@ test("should prepend child correctly", () => { '
second
', ); - $1.select(".one").prepend($2.select("#two")); + $1.querySelector(".one").prepend($2.querySelector("#two")); assert.strictEqual( - $1.select(".one").outerHtml(), + $1.querySelector(".one").outerHTML, '
second
first
', ); }); @@ -150,10 +160,10 @@ test("should insert child after correctly", () => { '
second
', ); - $1.select(".one").after($2.select("#two")); + $1.querySelector(".one").after($2.querySelector("#two")); assert.strictEqual( - $1.select("body").outerHtml(), + $1.querySelector("body").outerHTML, '
first
second
three
', ); }); @@ -166,10 +176,10 @@ test("should insert child before correctly", () => { '
second
', ); - $1.select(".one").before($2.select("#two")); + $1.querySelector(".one").before($2.querySelector("#two")); assert.strictEqual( - $1.select("body").outerHtml(), + $1.querySelector("body").outerHTML, '
second
first
three
', ); }); @@ -179,10 +189,10 @@ test("should remove node correctly", () => { '
first
second
three
', ); - $.select(".one").remove(); + $.querySelector(".one").remove(); assert.strictEqual( - $.select("body").outerHtml(), + $.querySelector("body").outerHTML, "
three
", ); }); @@ -192,9 +202,9 @@ test("should set attribute correctly", () => { '
first
', ); - $.select(".one").setAttribute("id", "Hello"); + $.querySelector(".one").setAttribute("id", "Hello"); - assert.deepStrictEqual($.select(".one").getAttribute("id"), "Hello"); + assert.deepStrictEqual($.querySelector(".one").getAttribute("id"), "Hello"); }); test("should remove attribute correctly", () => { @@ -202,9 +212,9 @@ test("should remove attribute correctly", () => { '
first
', ); - $.select(".one").removeAttribute("class"); + $.querySelector(".one").removeAttribute("class"); - assert.deepStrictEqual($.select("#Hello").getAttributes(), { + assert.deepStrictEqual(getAttributes($.querySelector("#Hello")), { id: "Hello", }); }); @@ -215,22 +225,22 @@ test("should clone correctly", () => { ); assert.strictEqual( - $.select(".one").clone().outerHtml(), + $.querySelector(".one").cloneNode(false).outerHTML, '
', ); assert.strictEqual( - $.select(".one").cloneRecursive().outerHtml(), + $.querySelector(".one").cloneNode(true).outerHTML, '
first
', ); - const $cloned = $.select(".one").cloneRecursive(); - $cloned.select(".one").getChildren()[0].remove(); + const $cloned = $.querySelector(".one").cloneNode(true); + $cloned.childNodes[0].remove(); assert.strictEqual( - $cloned.select(".one").outerHtml(), + $cloned.outerHTML, '
', ); assert.strictEqual( - $.select(".one").outerHtml(), + $.querySelector(".one").outerHTML, '
first
', ); });