@react-aria/utils
Version:
Spectrum UI components in React
195 lines (189 loc) • 8.12 kB
JavaScript
import {nodeContains as $d4ee10de306f2510$export$4282f70798064fe0} from "./DOMFunctions.module.js";
import {shadowDOM as $bJKXg$shadowDOM} from "@react-stately/flags";
// https://github.com/microsoft/tabster/blob/a89fc5d7e332d48f68d03b1ca6e344489d1c3898/src/Shadowdomize/ShadowTreeWalker.ts
class $dfc540311bf7f109$export$63eb3ababa9c55c4 {
get currentNode() {
return this._currentNode;
}
set currentNode(node) {
if (!(0, $d4ee10de306f2510$export$4282f70798064fe0)(this.root, node)) throw new Error('Cannot set currentNode to a node that is not contained by the root node.');
const walkers = [];
let curNode = node;
let currentWalkerCurrentNode = node;
this._currentNode = node;
while(curNode && curNode !== this.root)if (curNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
const shadowRoot = curNode;
const walker = this._doc.createTreeWalker(shadowRoot, this.whatToShow, {
acceptNode: this._acceptNode
});
walkers.push(walker);
walker.currentNode = currentWalkerCurrentNode;
this._currentSetFor.add(walker);
curNode = currentWalkerCurrentNode = shadowRoot.host;
} else curNode = curNode.parentNode;
const walker = this._doc.createTreeWalker(this.root, this.whatToShow, {
acceptNode: this._acceptNode
});
walkers.push(walker);
walker.currentNode = currentWalkerCurrentNode;
this._currentSetFor.add(walker);
this._walkerStack = walkers;
}
get doc() {
return this._doc;
}
firstChild() {
let currentNode = this.currentNode;
let newNode = this.nextNode();
if (!(0, $d4ee10de306f2510$export$4282f70798064fe0)(currentNode, newNode)) {
this.currentNode = currentNode;
return null;
}
if (newNode) this.currentNode = newNode;
return newNode;
}
lastChild() {
let walker = this._walkerStack[0];
let newNode = walker.lastChild();
if (newNode) this.currentNode = newNode;
return newNode;
}
nextNode() {
const nextNode = this._walkerStack[0].nextNode();
if (nextNode) {
const shadowRoot = nextNode.shadowRoot;
if (shadowRoot) {
var _this_filter;
let nodeResult;
if (typeof this.filter === 'function') nodeResult = this.filter(nextNode);
else if ((_this_filter = this.filter) === null || _this_filter === void 0 ? void 0 : _this_filter.acceptNode) nodeResult = this.filter.acceptNode(nextNode);
if (nodeResult === NodeFilter.FILTER_ACCEPT) {
this.currentNode = nextNode;
return nextNode;
}
// _acceptNode should have added new walker for this shadow,
// go in recursively.
let newNode = this.nextNode();
if (newNode) this.currentNode = newNode;
return newNode;
}
if (nextNode) this.currentNode = nextNode;
return nextNode;
} else {
if (this._walkerStack.length > 1) {
this._walkerStack.shift();
let newNode = this.nextNode();
if (newNode) this.currentNode = newNode;
return newNode;
} else return null;
}
}
previousNode() {
const currentWalker = this._walkerStack[0];
if (currentWalker.currentNode === currentWalker.root) {
if (this._currentSetFor.has(currentWalker)) {
this._currentSetFor.delete(currentWalker);
if (this._walkerStack.length > 1) {
this._walkerStack.shift();
let newNode = this.previousNode();
if (newNode) this.currentNode = newNode;
return newNode;
} else return null;
}
return null;
}
const previousNode = currentWalker.previousNode();
if (previousNode) {
const shadowRoot = previousNode.shadowRoot;
if (shadowRoot) {
var _this_filter;
let nodeResult;
if (typeof this.filter === 'function') nodeResult = this.filter(previousNode);
else if ((_this_filter = this.filter) === null || _this_filter === void 0 ? void 0 : _this_filter.acceptNode) nodeResult = this.filter.acceptNode(previousNode);
if (nodeResult === NodeFilter.FILTER_ACCEPT) {
if (previousNode) this.currentNode = previousNode;
return previousNode;
}
// _acceptNode should have added new walker for this shadow,
// go in recursively.
let newNode = this.lastChild();
if (newNode) this.currentNode = newNode;
return newNode;
}
if (previousNode) this.currentNode = previousNode;
return previousNode;
} else {
if (this._walkerStack.length > 1) {
this._walkerStack.shift();
let newNode = this.previousNode();
if (newNode) this.currentNode = newNode;
return newNode;
} else return null;
}
}
/**
* @deprecated
*/ nextSibling() {
// if (__DEV__) {
// throw new Error("Method not implemented.");
// }
return null;
}
/**
* @deprecated
*/ previousSibling() {
// if (__DEV__) {
// throw new Error("Method not implemented.");
// }
return null;
}
/**
* @deprecated
*/ parentNode() {
// if (__DEV__) {
// throw new Error("Method not implemented.");
// }
return null;
}
constructor(doc, root, whatToShow, filter){
this._walkerStack = [];
this._currentSetFor = new Set();
this._acceptNode = (node)=>{
if (node.nodeType === Node.ELEMENT_NODE) {
const shadowRoot = node.shadowRoot;
if (shadowRoot) {
const walker = this._doc.createTreeWalker(shadowRoot, this.whatToShow, {
acceptNode: this._acceptNode
});
this._walkerStack.unshift(walker);
return NodeFilter.FILTER_ACCEPT;
} else {
var _this_filter;
if (typeof this.filter === 'function') return this.filter(node);
else if ((_this_filter = this.filter) === null || _this_filter === void 0 ? void 0 : _this_filter.acceptNode) return this.filter.acceptNode(node);
else if (this.filter === null) return NodeFilter.FILTER_ACCEPT;
}
}
return NodeFilter.FILTER_SKIP;
};
this._doc = doc;
this.root = root;
this.filter = filter !== null && filter !== void 0 ? filter : null;
this.whatToShow = whatToShow !== null && whatToShow !== void 0 ? whatToShow : NodeFilter.SHOW_ALL;
this._currentNode = root;
this._walkerStack.unshift(doc.createTreeWalker(root, whatToShow, this._acceptNode));
const shadowRoot = root.shadowRoot;
if (shadowRoot) {
const walker = this._doc.createTreeWalker(shadowRoot, this.whatToShow, {
acceptNode: this._acceptNode
});
this._walkerStack.unshift(walker);
}
}
}
function $dfc540311bf7f109$export$4d0f8be8b12a7ef6(doc, root, whatToShow, filter) {
if ((0, $bJKXg$shadowDOM)()) return new $dfc540311bf7f109$export$63eb3ababa9c55c4(doc, root, whatToShow, filter);
return doc.createTreeWalker(root, whatToShow, filter);
}
export {$dfc540311bf7f109$export$63eb3ababa9c55c4 as ShadowTreeWalker, $dfc540311bf7f109$export$4d0f8be8b12a7ef6 as createShadowTreeWalker};
//# sourceMappingURL=ShadowTreeWalker.module.js.map