-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* The function checks if a given node is contained within a root node, using either the native | ||
* `contains` method or a fallback method for older browsers. | ||
* @param {Node | null | undefined} root - The `root` parameter represents the root node of a tree or | ||
* subtree. | ||
* @param {Node | null} n - The parameter `n` represents a Node object that we want to check if it is | ||
* contained within the `root` Node object. | ||
* @returns a boolean value. It returns `true` if the `root` node contains the `n` node, and `false` | ||
* otherwise. | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/contains.ts | ||
*/ | ||
export function domContains(root: Node | null | undefined, n: Node | null) { | ||
if (!root) | ||
return false | ||
|
||
// Use native if support | ||
if (root.contains) | ||
return root.contains(n) | ||
|
||
// `document.contains` not support with IE11 | ||
let node = n | ||
while (node) { | ||
if (node === root) | ||
return true | ||
|
||
node = node.parentNode | ||
} | ||
|
||
return false | ||
} |