Skip to content

Commit

Permalink
at-rule with ampersand (quick fix, needs review)
Browse files Browse the repository at this point in the history
  • Loading branch information
pix666 committed Dec 16, 2024
1 parent 265340b commit 8a66276
Show file tree
Hide file tree
Showing 3 changed files with 2,328 additions and 8 deletions.
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function interpolateAmpInSelector(nodes, parent) {
/**
* Combines parent and child selectors, in a SCSS-like way
*/
function mergeSelectors(parent, child) {
let merged = []
function mergeSelectors(parent, child, mergeSels = true) {
let merged = new Set()
for (let sel of parent.selectors) {
let parentNode = parse(sel, parent)

Expand All @@ -62,14 +62,14 @@ function mergeSelectors(parent, child) {
}
let node = parse(selector, child)
let replaced = interpolateAmpInSelector(node, parentNode)
if (!replaced) {
if (!replaced && mergeSels) {
node.prepend(parser.combinator({ value: ' ' }))
node.prepend(parentNode.clone({}))
}
merged.push(node.toString())
merged.add(node.toString())
}
}
return merged
return Array.from(merged)
}

/**
Expand Down Expand Up @@ -104,9 +104,7 @@ function createFnAtruleChilds(bubble) {
let children = []
atrule.each(child => {
if (child.type === 'rule' && bubbling) {
if (mergeSels) {
child.selectors = mergeSelectors(rule, child)
}
child.selectors = mergeSelectors(rule, child, mergeSels)
} else if (child.type === 'atrule' && child.nodes) {
if (bubble[child.name]) {
atruleChilds(rule, child, mergeSels)
Expand Down
33 changes: 33 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ test('nested at-root with nested media', () => {
)
})

test('at-root with ampersand', () => {
run('a { & {} @at-root { b & {} } }', 'a {} b a {}')
})

test('at-root with ampersand 2', () => {
run('a { & {} @at-root { & b {} } }', 'a {} a b {}')
})

test('nested at-root with ampersand', () => {
run(
`a {
& {}
@at-root {
b & {
@at-root {
c & {
& {}
}
@media y {
d {}
}
}
}
}
}`,
`a {}
c b a {}
@media y {
d {}
}`
)
})

test('tolerates immediately nested at-root', () => {
run(
`a {
Expand Down
Loading

0 comments on commit 8a66276

Please sign in to comment.