import { expect, fixture } from '@open-wc/testing'; import { html } from 'lit'; import { deepContains } from '@lion/ui/overlays.js'; describe('deepContains()', () => { it('returns true if element contains a target element with a shadow boundary in between', async () => { const shadowElement = /** @type {HTMLElement} */ (await fixture('
')); const shadowRoot = shadowElement.attachShadow({ mode: 'open' }); shadowRoot.innerHTML = ` Href `; const shadowElementChild = /** @type {HTMLElement} */ (shadowRoot.querySelector('#el-1')); const element = /** @type {HTMLElement} */ ( await fixture(html`
${shadowElement}
`) ); const lightChildEl = /** @type {HTMLElement} */ (element.querySelector('#light-el-1')); expect(deepContains(element, element)).to.be.true; expect(deepContains(element, shadowElement)).to.be.true; expect(deepContains(element, shadowElementChild)).to.be.true; expect(deepContains(element, lightChildEl)).to.be.true; expect(deepContains(shadowElement, shadowElement)).to.be.true; expect(deepContains(shadowElement, shadowElementChild)).to.be.true; expect(deepContains(shadowRoot, shadowElementChild)).to.be.true; // Siblings expect( deepContains( /** @type {HTMLElement} */ (element.firstElementChild), /** @type {HTMLElement} */ (element.lastElementChild), ), ).to.be.false; // Unrelated expect(deepContains(lightChildEl, shadowElementChild)).to.be.false; }); it('returns true if element contains a target element with a shadow boundary in between, for multiple shadowroots', async () => { const shadowElement = /** @type {HTMLElement} */ (await fixture('
')); const shadowRoot = shadowElement.attachShadow({ mode: 'open' }); shadowRoot.innerHTML = ` Href `; const shadowElement2 = /** @type {HTMLElement} */ (await fixture('
')); const shadowRoot2 = shadowElement2.attachShadow({ mode: 'open' }); shadowRoot2.innerHTML = ` Href `; const shadowElementChild = /** @type {HTMLElement} */ (shadowRoot.querySelector('#el-2')); const shadowElementChild2 = /** @type {HTMLElement} */ (shadowRoot2.querySelector('#el-2')); const element = /** @type {HTMLElement} */ ( await fixture(html`
${shadowElement} ${shadowElement2}
`) ); expect(deepContains(element, shadowElementChild)).to.be.true; expect(deepContains(shadowElement, shadowElementChild)).to.be.true; expect(deepContains(shadowRoot, shadowElementChild)).to.be.true; expect(deepContains(element, shadowElementChild2)).to.be.true; expect(deepContains(shadowElement2, shadowElementChild2)).to.be.true; expect(deepContains(shadowRoot2, shadowElementChild2)).to.be.true; }); it('can detect containing elements inside multiple nested shadow roots', async () => { const shadowNestedElement = await fixture('
'); const shadowNestedRoot = shadowNestedElement.attachShadow({ mode: 'open' }); shadowNestedRoot.innerHTML = ` Href `; const shadowElement = /** @type {HTMLElement} */ (await fixture('
')); const shadowRoot = shadowElement.attachShadow({ mode: 'open' }); shadowRoot.innerHTML = ` `; shadowRoot.insertBefore(shadowNestedElement, shadowRoot.lastElementChild); const element = /** @type {HTMLElement} */ ( await fixture(html`
${shadowElement}
`) ); const elementFirstChild = /** @type {HTMLElement} */ (element.firstElementChild); const elementFirstChildShadow = /** @type {ShadowRoot} */ (elementFirstChild.shadowRoot); const elementFirstChildShadowChildren = /** @type {HTMLElement[]} */ ( Array.from(elementFirstChildShadow.children) ); const elementFirstChildShadowChildShadow = /** @type {ShadowRoot} */ ( elementFirstChildShadowChildren[1].shadowRoot ); const elementFirstChildShadowChildShadowLastChild = /** @type {HTMLElement} */ ( elementFirstChildShadowChildShadow.lastElementChild ); expect(deepContains(element, elementFirstChild)).to.be.true; expect(deepContains(element, elementFirstChildShadow)).to.be.true; expect(deepContains(element, elementFirstChildShadowChildren[1])).to.be.true; expect(deepContains(element, elementFirstChildShadowChildShadow)).to.be.true; expect(deepContains(element, elementFirstChildShadowChildShadowLastChild)).to.be.true; }); });