Skip to content
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
9 changes: 5 additions & 4 deletions src/Dom/findDOMNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import type React from 'react';

export function isDOM(node: any): node is HTMLElement | SVGElement {
// https://developer.mozilla.org/en-US/docs/Web/API/Element
// Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement
return node instanceof HTMLElement || node instanceof SVGElement;
// Cross-frame safe check: `instanceof` fails for elements created in a
// different frame (e.g. iframe via createPortal) because each frame has its
// own HTMLElement/SVGElement constructors.
return typeof node === 'object' && node !== null && node.nodeType === 1;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Dom/isVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default (element: Element): boolean => {
return false;
}

if (element instanceof Element) {
if (element.nodeType === 1) {
if ((element as HTMLElement).offsetParent) {
return true;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/findDOMNode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ describe('findDOMNode', () => {
expect(findDOMNode(svg)).toBe(svg);
});

it('supports DOM nodes from iframe', () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);

try {
const iframeWindow = iframe.contentWindow!;
const iframeDocument = iframeWindow.document;
const div = iframeDocument.createElement('div');
iframeDocument.body.appendChild(div);

expect(div).toBeInstanceOf(iframeWindow.HTMLElement);
expect(div).not.toBeInstanceOf(HTMLElement);
expect(isDOM(div)).toBe(true);
expect(getDOM(div)).toBe(div);
expect(findDOMNode(div)).toBe(div);
expect(getDOM({ nativeElement: div })).toBe(div);
} finally {
document.body.removeChild(iframe);
}
});

it('isDOM type', () => {
const svg: any = document.createElementNS(
'http://www.w3.org/2000/svg',
Expand Down
33 changes: 33 additions & 0 deletions tests/isVisible.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import isVisible from '../src/Dom/isVisible';

describe('isVisible', () => {
it('supports DOM nodes from iframe', () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);

try {
const iframeWindow = iframe.contentWindow!;
const iframeDocument = iframeWindow.document;
const div = iframeDocument.createElement('div');
iframeDocument.body.appendChild(div);

jest.spyOn(div, 'getBoundingClientRect').mockReturnValue({
bottom: 10,
height: 10,
left: 0,
right: 10,
toJSON: () => {},
top: 0,
width: 10,
x: 0,
y: 0,
});

expect(div).toBeInstanceOf(iframeWindow.HTMLElement);
expect(div).not.toBeInstanceOf(Element);
expect(isVisible(div)).toBe(true);
} finally {
document.body.removeChild(iframe);
}
});
});