-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make dom findUpUntil util work within iframes (#99)
- Loading branch information
Showing
4 changed files
with
82 additions
and
7 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,34 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { isNode, isHTMLElement, isSVGElement } from '../element-types'; | ||
|
||
test('an HTMLElement is recognized as a Node and HTMLElement', () => { | ||
const div = document.createElement('div'); | ||
expect(isNode(div)).toBe(true); | ||
expect(isHTMLElement(div)).toBe(true); | ||
expect(isSVGElement(div)).toBe(false); | ||
}); | ||
|
||
test('an SVGElement is recognized as a Node and SVGElement', () => { | ||
const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect'); | ||
expect(isNode(rect)).toBe(true); | ||
expect(isHTMLElement(rect)).toBe(false); | ||
expect(isSVGElement(rect)).toBe(true); | ||
}); | ||
|
||
test('an object is recognized as Node', () => { | ||
expect(isNode({ nodeType: 3, nodeName: '', parentNode: {} })).toBe(true); | ||
}); | ||
|
||
test('an object is recognized as HTMLElement', () => { | ||
const node = { nodeType: 1, nodeName: '', parentNode: {} }; | ||
expect(isHTMLElement({ ...node, style: {}, ownerDocument: {} })).toBe(true); | ||
expect(isHTMLElement({ ...node, style: {}, ownerDocument: {}, ownerSVGElement: {} })).toBe(false); | ||
}); | ||
|
||
test('an object is recognized as SVGElement', () => { | ||
const node = { nodeType: 1, nodeName: '', parentNode: {} }; | ||
expect(isSVGElement({ ...node, style: {}, ownerDocument: {} })).toBe(false); | ||
expect(isSVGElement({ ...node, style: {}, ownerDocument: {}, ownerSVGElement: {} })).toBe(true); | ||
}); |
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,41 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// The instanceof Node/HTMLElement/SVGElement checks can fail if the target element | ||
// belongs to a different window than the respective type. | ||
|
||
export function isNode(target: unknown): target is Node { | ||
return ( | ||
target instanceof Node || | ||
(target !== null && | ||
typeof target === 'object' && | ||
'nodeType' in target && | ||
typeof target.nodeType === 'number' && | ||
'nodeName' in target && | ||
typeof target.nodeName === 'string' && | ||
'parentNode' in target && | ||
typeof target.parentNode === 'object') | ||
); | ||
} | ||
|
||
export function isHTMLElement(target: unknown): target is HTMLElement { | ||
return ( | ||
target instanceof HTMLElement || | ||
(isNode(target) && | ||
target.nodeType === Node.ELEMENT_NODE && | ||
'style' in target && | ||
typeof target.style === 'object' && | ||
typeof target.ownerDocument === 'object' && | ||
!isSVGElement(target)) | ||
); | ||
} | ||
|
||
export function isSVGElement(target: unknown): target is SVGElement { | ||
return ( | ||
target instanceof SVGElement || | ||
(isNode(target) && | ||
target.nodeType === Node.ELEMENT_NODE && | ||
'ownerSVGElement' in target && | ||
typeof target.ownerSVGElement === 'object') | ||
); | ||
} |
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
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 |
---|---|---|
@@ -1,18 +1,16 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { isNode } from './element-types'; | ||
|
||
/** | ||
* Checks whether the given node is a parent of the other descendant node. | ||
* @param parent Parent node | ||
* @param descendant Node that is checked to be a descendant of the parent node | ||
*/ | ||
export default function nodeContains(parent: Node | null, descendant: Node | EventTarget | null) { | ||
// ('nodeType' in descendant) is a workaround to check if descendant is a node | ||
// Node interface is tied to the window it's created in, if the descendant was moved to an iframe after it was created, | ||
// descendant instanceof Node will be false since Node has a different window | ||
if (!parent || !descendant || !('nodeType' in descendant)) { | ||
if (!parent || !descendant || !isNode(descendant)) { | ||
return false; | ||
} | ||
|
||
return parent.contains(descendant); | ||
} |