Skip to content

Commit

Permalink
fix: getOwnerDocument(elementFromTemplate) (#871)
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Hoffman <git@fustrate.com>
  • Loading branch information
Fustrate committed Dec 10, 2020
1 parent 8d0d3aa commit 38caf47
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/dom-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export function getOwnerDocument(
elementOrElements: Element | Element[]
): Document {
const [element] = normalizeToArray(elementOrElements);
return element ? element.ownerDocument || document : document;

// Elements created via a <template> have an ownerDocument with no reference to the body
return element?.ownerDocument?.body ? element.ownerDocument : document;
}

export function isCursorOutsideInteractiveBorder(
Expand Down
16 changes: 16 additions & 0 deletions test/unit/dom-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,19 @@ describe('isReferenceElement', () => {
expect(DomUtils.isReferenceElement(instance.popper)).toBe(false);
});
});

describe('getOwnerDocument', () => {
it('finds the ownerDocument of an element', () => {
expect(DomUtils.getOwnerDocument(document.createElement('div'))).toBe(document);
});

it('uses the default document if the element was created from a template', () => {
const template = document.createElement('template');

template.innerHTML = '<div></div>';

const div = template.content.firstChild;

expect(DomUtils.getOwnerDocument(div)).toBe(document);
});
});

0 comments on commit 38caf47

Please sign in to comment.