-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Shadow DOM support when root is not defined
- Loading branch information
Showing
5 changed files
with
117 additions
and
19 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,9 @@ | ||
const libraryName = 'CssSelectorGenerator' | ||
|
||
/** | ||
* Convenient wrapper for `console.warn` using consistent formatting. | ||
*/ | ||
export function showWarning (id = 'unknown problem', ...args: unknown[]): void { | ||
// eslint-disable-next-line no-console | ||
console.warn(`${libraryName}: ${id}`, ...args) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {assert} from 'chai' | ||
import {sanitizeRoot} from '../src/utilities-options.ts' | ||
|
||
describe('utilities - sanitizeRoot', () => { | ||
let root | ||
|
||
beforeEach(() => { | ||
root = document.body.appendChild(document.createElement('div')) | ||
}) | ||
|
||
afterEach(() => { | ||
root.parentNode.removeChild(root) | ||
}) | ||
|
||
it('should return provided node if it is a document', () => { | ||
const element = root.appendChild(document.createElement('div')) | ||
const result = sanitizeRoot(document, element) | ||
assert.equal(result, document) | ||
}) | ||
|
||
it('should return provided node if it is an element', () => { | ||
const element = root.appendChild(document.createElement('div')) | ||
const result = sanitizeRoot(root, element) | ||
assert.equal(result, root) | ||
}) | ||
|
||
it('should return provided node if it is a fragment', () => { | ||
const fragment = root.appendChild(document.createDocumentFragment()) | ||
const element = fragment.appendChild(document.createElement('div')) | ||
const result = sanitizeRoot(fragment, element) | ||
assert.equal(result, fragment) | ||
}) | ||
|
||
it('should return document root if not provided', () => { | ||
const element = root.appendChild(document.createElement('div')) | ||
const result = sanitizeRoot(undefined, element) | ||
assert.equal(result, document) | ||
}) | ||
|
||
it('should return shadow root if element is part of shadow DOM', () => { | ||
const shadowRoot = root.attachShadow({mode: 'open'}) | ||
const wrapper = shadowRoot.appendChild(document.createElement('div')) | ||
wrapper.className = 'wrapper' | ||
const element = wrapper.appendChild(document.createElement('div')) | ||
element.className = 'element' | ||
const result = sanitizeRoot(undefined, element) | ||
assert.equal(result, shadowRoot) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {assert} from 'chai' | ||
import {getCssSelector} from '../src/index.ts' | ||
|
||
describe('Shadow DOM', () => { | ||
let root | ||
let shadowRoot | ||
let shadowElement | ||
|
||
beforeEach(() => { | ||
root = document.body.appendChild(document.createElement('div')) | ||
shadowRoot = root.attachShadow({mode: 'open'}) | ||
shadowElement = shadowRoot.appendChild(document.createElement('div')) | ||
shadowElement.className = 'shadowElement' | ||
}) | ||
|
||
afterEach(() => { | ||
root.parentNode.removeChild(root) | ||
}) | ||
|
||
it('should match shadow element within shadow root', () => { | ||
const result = getCssSelector(shadowElement, {root: shadowRoot}) | ||
assert.equal(result, '.shadowElement') | ||
}) | ||
|
||
it('should match shadow element without specifying root', () => { | ||
const result = getCssSelector(shadowElement) | ||
assert.equal(result, '.shadowElement') | ||
}) | ||
}) |