/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/ffi/native-dom * * Element-level DOM API over the Rust bridge (html5ever + `selectors`). * * A {@link NativeDoc} owns one native tree. {@link NativeElement}s are * `(document, nodeId)` pairs, so nested queries, text and attribute reads run * against that tree without serialising and re-parsing HTML. Using an element * after its document was destroyed throws instead of touching freed memory. */ import { DomTree, isBridgeAvailable } from "../rust/bridge.ts"; /** `true` when the native bridge (and therefore this module) is usable. */ export function isNativeDomAvailable(): boolean { return isBridgeAvailable(); } export class NativeElement { readonly #doc: NativeDoc; readonly nodeId: number; constructor(doc: NativeDoc, nodeId: number) { this.#doc = doc; this.nodeId = nodeId; } textContent(): string { return this.#doc.tree.textContent(this.nodeId); } innerHTML(): string { return this.#doc.tree.innerHtml(this.nodeId); } outerHTML(): string { return this.#doc.tree.outerHtml(this.nodeId); } tagName(): string { return this.#doc.tree.tagName(this.nodeId); } /** Attribute value, or `""` when absent (matches the historical API). */ getAttribute(name: string): string { return this.#doc.tree.attribute(this.nodeId, name) ?? ""; } hasAttribute(name: string): boolean { return this.#doc.tree.attribute(this.nodeId, name) !== null; } attributes(): Record { return this.#doc.tree.attributes(this.nodeId); } async querySelector(selector: string): Promise { const id = this.#doc.tree.selectNode(selector, this.nodeId); return id === null ? null : new NativeElement(this.#doc, id); } async querySelectorAll(selector: string): Promise { return this.#doc.selectWithin(selector, this.nodeId); } /** Elements do not own native memory; kept for API compatibility. */ destroy(): void {} } export class NativeSelection implements Iterable { readonly #elements: NativeElement[]; constructor(elements: NativeElement[]) { this.#elements = elements; } get count(): number { return this.#elements.length; } at(i: number): NativeElement | null { return this.#elements[i] ?? null; } toArray(): NativeElement[] { return [...this.#elements]; } map(fn: (el: NativeElement, i: number) => T): T[] { return this.#elements.map(fn); } filter(fn: (el: NativeElement, i: number) => boolean): NativeSelection { return new NativeSelection(this.#elements.filter(fn)); } find(fn: (el: NativeElement, i: number) => boolean): NativeElement | undefined { return this.#elements.find(fn); } [Symbol.iterator](): Iterator { return this.#elements[Symbol.iterator](); } /** Selections do not own native memory; kept for API compatibility. */ destroy(): void {} } export class NativeDoc implements Disposable { readonly html: string; readonly tree: DomTree; constructor(html: string) { this.html = html; this.tree = DomTree.parse(html); } /** @internal Query inside `scope` (a node id of this document). */ selectWithin(selector: string, scope?: number): NativeSelection { if (this.tree.disposed) return new NativeSelection([]); const ids = this.tree.selectNodes(selector, scope); return new NativeSelection(ids.map((id) => new NativeElement(this, id))); } async find(selector: string): Promise { return this.selectWithin(selector); } async querySelector(selector: string): Promise { if (this.tree.disposed) return null; const id = this.tree.selectNode(selector); return id === null ? null : new NativeElement(this, id); } async querySelectorAll(selector: string): Promise { return this.selectWithin(selector); } get destroyed(): boolean { return this.tree.disposed; } /** Release the native tree. Idempotent. */ destroy(): void { this.tree.dispose(); } [Symbol.dispose](): void { this.destroy(); } } /** Parse `html` into a {@link NativeDoc}. Throws when the bridge is unavailable. */ export async function parseHtml(html: string): Promise { return new NativeDoc(html); }