-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
21 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
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,34 @@ | ||
import assert from 'node:assert/strict' | ||
import test from 'node:test' | ||
import selectorParser = require('postcss-selector-parser'); | ||
import key = require('./key'); | ||
|
||
const parser = selectorParser() | ||
|
||
test("key", async (t) => { | ||
const arr: [string, string | ErrorConstructor][] = [ | ||
['div', 'div'], | ||
['div[data-foo="bar"]', 'div_data_foo_bar'], | ||
['div[data-foo]', 'div_data_foo'], | ||
['Div', 'div'], | ||
['div[data-Foo]', 'div_data_Foo'], | ||
['div[data-foo="1"]', 'div_data_foo_1'], | ||
['div[data-1]', 'div_data_1'], | ||
[' div[ data-foo ] ', 'div_data_foo'], | ||
['div:not([data-component])', 'div'], | ||
['div[data-foo=" bar"]', 'div_data_foo__bar'] | ||
] | ||
for (const [input, expected] of arr) { | ||
await t.test(`${input} → ${expected}`, () => { | ||
const selector = parser.astSync(input, { lossless: false }) | ||
if (typeof expected === 'string') { | ||
// @ts-ignore | ||
const actual = key(selector.nodes[0].nodes[0]) | ||
assert.equal(actual, expected) | ||
} else { | ||
// @ts-ignore | ||
assert.throws(() => key(selector.nodes[0].nodes[0])) | ||
} | ||
}) | ||
} | ||
}) |
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,18 @@ | ||
import selectorParser = require('postcss-selector-parser'); | ||
|
||
// Turn button[data-component='foo'] into a key that will be used for the interface name | ||
function key(selector: selectorParser.Node): string { | ||
let key = '' | ||
if (selector.type === 'tag') { | ||
key += selector.toString().toLowerCase() | ||
} | ||
const next = selector.next() | ||
if (next?.type === 'attribute') { | ||
const { attribute, value } = next as selectorParser.Attribute | ||
key += `_${attribute}` | ||
if (value) key += `_${value}` | ||
} | ||
return key.replace(/[^a-zA-Z0-9_]/g, '_') | ||
} | ||
|
||
module.exports = key |