Skip to content

Commit b3a31b7

Browse files
authored
fix: normalize tag selectors and sanitize key generation (#65)
- Convert tag selectors to lowercase, as tags are case-insensitive. - Sanitize key by replacing non-alphanumeric characters with underscores. - Set `lossless: false` in selector parser options for trimming. [ref](https://github.com/postcss/postcss-selector-parser/blob/HEAD/API.md#processoroptions)
1 parent eeffe5e commit b3a31b7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ function render(parsed: Parsed): string {
8686
function key(selector: selectorParser.Node): string {
8787
let key = ''
8888
if (selector.type === 'tag') {
89-
key += selector.toString()
89+
key += selector.toString().toLowerCase()
9090
}
9191
const next = selector.next()
9292
if (next?.type === 'attribute') {
9393
const { attribute, value } = next as selectorParser.Attribute
94-
key += `_${attribute.replace('-', '_')}${value?.replace(/^/, '_').replace(' ', '_').replace('-', '_')}`
94+
key += `_${attribute}_${value}`
9595
}
96-
return key
96+
return key.replace(/[^a-zA-Z0-9_]/g, '_')
9797
}
9898

9999
function initialParsedValue(): Parsed[keyof Parsed] {
@@ -120,7 +120,7 @@ const _mistcss: PluginCreator<{}> = (_opts = {}) => {
120120
selectors.walk((selector) => {
121121
if (selector.type === 'tag') {
122122
current = parsed[key(selector)] = initialParsedValue()
123-
current.tag = selector.toString()
123+
current.tag = selector.toString().toLowerCase()
124124
}
125125

126126
if (selector.type === 'attribute') {
@@ -134,7 +134,9 @@ const _mistcss: PluginCreator<{}> = (_opts = {}) => {
134134
}
135135
}
136136
})
137-
}).processSync(rule.selector)
137+
}).processSync(rule.selector, {
138+
lossless: false,
139+
})
138140

139141
rule.walkDecls(({ prop }) => {
140142
if (prop.startsWith('--') && prop !== '--apply')

0 commit comments

Comments
 (0)