-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match selectors without using css-select where possible. (#19)
* Do simple matches without external module. * Updated test case.
- Loading branch information
1 parent
0fe9ae3
commit e141521
Showing
6 changed files
with
152 additions
and
23 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
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,75 @@ | ||
import { CSSRule, CSSSelector, CSSSelectorSequence } from '../../lib/css.js'; | ||
import { createElement } from './testutils.js'; | ||
|
||
/** | ||
* @typedef {import('../../lib/css.js').SimpleSelector} SimpleSelector | ||
*/ | ||
|
||
describe('test parsing and stringifying of selectors', function () { | ||
/** @type {{ | ||
* selectorInfo:SimpleSelector[], | ||
* elData:{elName:string,atts:[string,string][]}, | ||
* expected:boolean|null | ||
* }[]} */ | ||
const tests = [ | ||
{ | ||
selectorInfo: [{ type: 'ClassSelector', name: 'class1' }], | ||
elData: { elName: 'path', atts: [] }, | ||
expected: false, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'ClassSelector', name: 'class1' }], | ||
elData: { elName: 'path', atts: [['class', 'class2']] }, | ||
expected: false, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'ClassSelector', name: 'class1' }], | ||
elData: { elName: 'path', atts: [['class', 'class1']] }, | ||
expected: true, | ||
}, | ||
{ | ||
selectorInfo: [ | ||
{ type: 'ClassSelector', name: 'class1' }, | ||
{ type: 'IdSelector', name: 'id1' }, | ||
], | ||
elData: { elName: 'path', atts: [['class', 'class1']] }, | ||
expected: null, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'IdSelector', name: 'id1' }], | ||
elData: { elName: 'path', atts: [] }, | ||
expected: false, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'IdSelector', name: 'id1' }], | ||
elData: { elName: 'path', atts: [['id', 'id1']] }, | ||
expected: true, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'IdSelector', name: 'id1' }], | ||
elData: { elName: 'path', atts: [['id', 'class1']] }, | ||
expected: false, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'TypeSelector', name: 'path' }], | ||
elData: { elName: 'path', atts: [] }, | ||
expected: true, | ||
}, | ||
{ | ||
selectorInfo: [{ type: 'TypeSelector', name: 'path' }], | ||
elData: { elName: 'circle', atts: [] }, | ||
expected: false, | ||
}, | ||
]; | ||
const declarations = new Map(); | ||
|
||
for (const test of tests) { | ||
const sequences = [new CSSSelectorSequence(undefined, test.selectorInfo)]; | ||
const selector = new CSSSelector(sequences); | ||
it(`test ${selector.getString()}`, function () { | ||
const rule = new CSSRule(selector, [0, 0, 0], declarations, false); | ||
const element = createElement(test.elData); | ||
expect(rule._matches(element)).toBe(test.expected); | ||
}); | ||
} | ||
}); |
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
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