2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-05-27 09:20:34 -04:00
|
|
|
// Utility functions for DOM nodes
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as domTypes from "./dom_types.ts";
|
2019-05-27 09:20:34 -04:00
|
|
|
|
|
|
|
export function isNode(nodeImpl: domTypes.EventTarget | null): boolean {
|
|
|
|
return Boolean(nodeImpl && "nodeType" in nodeImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isShadowRoot(nodeImpl: domTypes.EventTarget | null): boolean {
|
|
|
|
return Boolean(
|
|
|
|
nodeImpl &&
|
2019-07-16 00:19:26 -04:00
|
|
|
nodeImpl[domTypes.eventTargetNodeType] ===
|
|
|
|
domTypes.NodeType.DOCUMENT_FRAGMENT_NODE &&
|
|
|
|
nodeImpl[domTypes.eventTargetHost] != null
|
2019-05-27 09:20:34 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isSlotable(nodeImpl: domTypes.EventTarget | null): boolean {
|
|
|
|
return Boolean(
|
|
|
|
nodeImpl &&
|
2019-07-16 00:19:26 -04:00
|
|
|
(nodeImpl[domTypes.eventTargetNodeType] ===
|
|
|
|
domTypes.NodeType.ELEMENT_NODE ||
|
|
|
|
nodeImpl[domTypes.eventTargetNodeType] === domTypes.NodeType.TEXT_NODE)
|
2019-05-27 09:20:34 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#node-trees
|
|
|
|
// const domSymbolTree = Symbol("DOM Symbol Tree");
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
|
|
|
|
export function isShadowInclusiveAncestor(
|
|
|
|
ancestor: domTypes.EventTarget | null,
|
|
|
|
node: domTypes.EventTarget | null
|
|
|
|
): boolean {
|
|
|
|
while (isNode(node)) {
|
|
|
|
if (node === ancestor) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isShadowRoot(node)) {
|
2019-07-16 00:19:26 -04:00
|
|
|
node = node && node[domTypes.eventTargetHost];
|
2019-05-27 09:20:34 -04:00
|
|
|
} else {
|
|
|
|
node = null; // domSymbolTree.parent(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRoot(
|
|
|
|
node: domTypes.EventTarget | null
|
|
|
|
): domTypes.EventTarget | null {
|
2019-09-07 12:27:18 -04:00
|
|
|
const root = node;
|
2019-05-27 09:20:34 -04:00
|
|
|
|
|
|
|
// for (const ancestor of domSymbolTree.ancestorsIterator(node)) {
|
|
|
|
// root = ancestor;
|
|
|
|
// }
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#retarget
|
|
|
|
export function retarget(
|
|
|
|
a: domTypes.EventTarget | null,
|
|
|
|
b: domTypes.EventTarget
|
|
|
|
): domTypes.EventTarget | null {
|
|
|
|
while (true) {
|
|
|
|
if (!isNode(a)) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
const aRoot = getRoot(a);
|
|
|
|
|
|
|
|
if (aRoot) {
|
|
|
|
if (
|
|
|
|
!isShadowRoot(aRoot) ||
|
|
|
|
(isNode(b) && isShadowInclusiveAncestor(aRoot, b))
|
|
|
|
) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2019-07-16 00:19:26 -04:00
|
|
|
a = aRoot[domTypes.eventTargetHost];
|
2019-05-27 09:20:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|