Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the NodeIterator interface for WorkerDOM #1187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/worker-thread/dom/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { propagate as propagateResize } from '../ResizePropagation';
import { TransferrableKeys } from '../../transfer/TransferrableKeys';
import { WorkerDOMGlobalScope, GlobalScope } from '../WorkerDOMGlobalScope';
import { set as setPhase } from '../phase';
import { NodeIterator, NodeFilter } from './NodeIterator';


const DOCUMENT_NAME = '#document';

Expand Down Expand Up @@ -151,4 +153,12 @@ export class Document extends Element {
public getElementById(id: string): Element | null {
return matchChildElement(this.body, (element) => element.id === id);
}

/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator
* @return NodeIterator object.
*/
public createNodeIterator(root: Node, whatToShow: number = NodeFilter.SHOW_ALL, filter: ((node: Node) => number) | null = null): NodeIterator {
return new NodeIterator(root, whatToShow, filter);
}
}
91 changes: 91 additions & 0 deletions src/worker-thread/dom/NodeIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Node } from "./Node";

export const NodeFilter = {
FILTER_ACCEPT: 1,
FILTER_REJECT: 2,
FILTER_SKIP: 3,
SHOW_ALL: 0xFFFFFFFF,
SHOW_ELEMENT: 0x1,
SHOW_ATTRIBUTE: 0x2,
SHOW_TEXT: 0x4,
}

export class NodeIterator {
private currentNode: Node;
private root: Node;
private whatToShow: number;
private filter: ((node: Node) => number) | null;

constructor(root: Node, whatToShow: number = NodeFilter.SHOW_ALL, filter: ((node: Node) => number) | null = null) {
this.currentNode = root;
this.root = root;
this.whatToShow = whatToShow;
this.filter = filter;
}

nextNode(): Node | null {
let next = this._nextNode(this.currentNode);
while (next) {
if (this._acceptNode(next) === NodeFilter.FILTER_ACCEPT) {
this.currentNode = next;
return next;
}
next = this._nextNode(next);
}
return null;
}

private _nextNode(node: Node): Node | null {
if (node.firstChild) {
return node.firstChild;
}
while (node) {
if (node === this.root) {
return null;
}
if (node.nextSibling) {
return node.nextSibling;
}
(node as Node | null) = node.parentNode;
}
return null;
}

private _acceptNode(node: Node): number {
if ((this.whatToShow & (1 << node.nodeType - 1)) === 0) {
return NodeFilter.FILTER_REJECT;
}
if (this.filter) {
return this.filter(node);
}
return NodeFilter.FILTER_ACCEPT;
}

previousNode(): Node | null {
let prev = this._previousNode(this.currentNode);
while (prev) {
if (this._acceptNode(prev) === NodeFilter.FILTER_ACCEPT) {
this.currentNode = prev;
return prev;
}
prev = this._previousNode(prev);
}
return null;
}

private _previousNode(node: Node): Node | null {
if (node === this.root) {
return null;
}
if (node.previousSibling) {
node = node.previousSibling;
while (node.lastChild) {
node = node.lastChild;
}
return node;
}
return node.parentNode;
}
}