testing-library__dom
Version:
Shadow DOM support for the simple and complete DOM testing utilities that encourage good testing practices you've come to know and love from Dom Testing Library.
55 lines (51 loc) • 1.84 kB
JavaScript
HTMLDocument.prototype.querySelectorAllWithShadowDOM = function(query) {
const isNotCustomElementParent = true;
const children = [...(this.children || [])].filter(
(child) => isNotCustomElementParent || !!child.assignedSlot
);
let results = children.filter((el) => el.matches(query));
if (this.shadowRoot) {
results = results.concat([
...(this.shadowRoot.querySelectorAllWithShadowDOM(query) || []),
]);
}
children.map((child) => {
if (child.querySelectorAllWithShadowDOM) {
results = results.concat([
...(child.querySelectorAllWithShadowDOM(query) || []),
]);
}
});
return results;
};
HTMLElement.prototype.querySelectorAllWithShadowDOM = function(query) {
const isNotCustomElementParent = this.tagName.search('-') === -1;
const children = [...(this.children || [])].filter(
(child) => isNotCustomElementParent || !!child.assignedSlot
);
let results = children.filter((el) => el.matches(query));
if (this.shadowRoot) {
results = results.concat([
...(this.shadowRoot.querySelectorAllWithShadowDOM(query) || []),
]);
}
children.map((child) => {
if (child.querySelectorAllWithShadowDOM) {
results = results.concat([
...(child.querySelectorAllWithShadowDOM(query) || []),
]);
}
});
return results;
};
ShadowRoot.prototype.querySelectorAllWithShadowDOM = function(query) {
const children = [...(this.children || [])];
let results = children.filter((el) => el.matches(query));
children.map((child) => {
results = results.concat([
...(child.querySelectorAllWithShadowDOM(query) || []),
]);
});
return results;
};
export * from './dom.js';