Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for :defined pseudo class #64

Merged
merged 3 commits into from
Mar 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Returns **[Array][62]<([object][60] \| [undefined][63])>** array of matched n
|E\[foo$="bar"\]|✓| |
|E\[foo*="bar"\]|✓| |
|E\[foo\|="en"\]|✓| |
|E:defined|Unsupported| |
|E:defined|| |
|E:dir(ltr)|✓| |
|E:lang(en)|Partially supported|Comma-separated list of language codes, e.g. `:lang(en, fr)`, is not yet supported.|
|E:any‑link|✓| |
Expand Down
17 changes: 16 additions & 1 deletion src/js/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,22 @@ export class Finder {
}
break;
}
case 'defined': {
const attr = node.getAttribute('is');
if (attr) {
if (isCustomElementName(attr) &&
this.#window.customElements.get(attr)) {
matched.add(node);
}
} else if (isCustomElementName(localName)) {
if (this.#window.customElements.get(localName)) {
matched.add(node);
}
} else if (node instanceof this.#window.HTMLElement) {
matched.add(node);
}
break;
}
case 'host':
case 'host-context': {
// ignore
Expand All @@ -1518,7 +1534,6 @@ export class Finder {
case 'blank':
case 'buffering':
case 'current':
case 'defined':
case 'focus-visible':
case 'fullscreen':
case 'future':
Expand Down
154 changes: 127 additions & 27 deletions test/finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6554,124 +6554,224 @@ describe('Finder', () => {
], 'result');
});

it('should get matched node', () => {
const leaf = {
children: null,
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('p');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [
node
], 'result');
});

it('should get matched node', () => {
const leaf = {
children: null,
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('asdf');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [
node
], 'result');
});

it('should get matched node', () => {
const leaf = {
children: null,
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
window.customElements.define('sw-rey',
class extends window.HTMLElement {});
const node = document.createElement('sw-rey');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [
node
], 'result');
});

it('should get matched node', () => {
const leaf = {
children: null,
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
window.customElements.define('sw-finn',
class extends window.HTMLElement {}, { extends: 'p' });
const node = document.createElement('p');
node.setAttribute('is', 'sw-finn');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [
node
], 'result');
});

it('should not match', () => {
const leaf = {
children: null,
name: 'host',
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
const node = document.createElement('sw-han');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':host', node);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

it('should not match', () => {
const leaf = {
children: null,
name: 'host-context',
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
const node = document.createElement('p');
node.setAttribute('is', 'sw-luke');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':host-context', node);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

// legacy pseudo-element
it('should not match', () => {
const leaf = {
children: null,
name: 'after',
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('p');
node.setAttribute('is', 'asdf');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

it('should get matched node', () => {
const leaf = {
children: null,
name: 'defined',
type: SELECTOR_PSEUDO_CLASS
};
window.customElements.define('foo-', class extends window.HTMLElement {});
const node = document.createElement('foo-');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [
node
], 'result');
});

it('should not match', () => {
const leaf = {
children: null,
name: 'host',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':after', node);
finder._setup(':host', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

it('should throw', () => {
it('should not match', () => {
const leaf = {
children: null,
name: 'after',
name: 'host-context',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':after', node, {
warn: true
});
assert.throws(() => finder._matchPseudoClassSelector(leaf, node),
DOMException, 'Unsupported pseudo-element ::after');
finder._setup(':host-context', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

// not supported
// legacy pseudo-element
it('should not match', () => {
const leaf = {
children: null,
name: 'active',
name: 'after',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':active', node);
finder._setup(':after', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

it('should throw', () => {
const leaf = {
children: null,
name: 'active',
name: 'after',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':active', node, {
finder._setup(':after', node, {
warn: true
});
assert.throws(() => finder._matchPseudoClassSelector(leaf, node),
DOMException, 'Unsupported pseudo-class :active');
DOMException, 'Unsupported pseudo-element ::after');
});

// not supported
it('should not match', () => {
const leaf = {
children: null,
name: 'defined',
name: 'active',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node);
finder._setup(':active', node);
const res = finder._matchPseudoClassSelector(leaf, node);
assert.deepEqual([...res], [], 'result');
});

it('should throw', () => {
const leaf = {
children: null,
name: 'defined',
name: 'active',
type: SELECTOR_PSEUDO_CLASS
};
const node = document.createElement('div');
document.getElementById('div0').appendChild(node);
const finder = new Finder(window);
finder._setup(':defined', node, {
finder._setup(':active', node, {
warn: true
});
assert.throws(() => finder._matchPseudoClassSelector(leaf, node),
DOMException, 'Unsupported pseudo-class :defined');
DOMException, 'Unsupported pseudo-class :active');
});

// unknown
Expand Down
Loading
Loading