Skip to content

Commit

Permalink
fix: normalize tag selectors and sanitize key generation (#65)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
honeymaro authored Nov 10, 2024
1 parent eeffe5e commit b3a31b7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ function render(parsed: Parsed): string {
function key(selector: selectorParser.Node): string {
let key = ''
if (selector.type === 'tag') {
key += selector.toString()
key += selector.toString().toLowerCase()
}
const next = selector.next()
if (next?.type === 'attribute') {
const { attribute, value } = next as selectorParser.Attribute
key += `_${attribute.replace('-', '_')}${value?.replace(/^/, '_').replace(' ', '_').replace('-', '_')}`
key += `_${attribute}_${value}`
}
return key
return key.replace(/[^a-zA-Z0-9_]/g, '_')
}

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

if (selector.type === 'attribute') {
Expand All @@ -134,7 +134,9 @@ const _mistcss: PluginCreator<{}> = (_opts = {}) => {
}
}
})
}).processSync(rule.selector)
}).processSync(rule.selector, {
lossless: false,
})

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

0 comments on commit b3a31b7

Please sign in to comment.