Fix append() failing after mutating nodes through .parent

This commit is contained in:
Jordan Pittman
2022-04-29 13:42:14 -04:00
parent 9264eb28f0
commit 1d6117c754
2 changed files with 48 additions and 1 deletions
+1 -1
View File
@@ -326,7 +326,7 @@ class Container extends Node {
i.raws.before = sample.raws.before.replace(/\S/g, '')
}
}
i.parent = this
i.parent = this.proxyOf
return i
})
+47
View File
@@ -7,6 +7,7 @@ import postcss, {
Container,
Root,
Rule,
AtRule,
Declaration,
Plugin,
PluginCreator,
@@ -1552,4 +1553,50 @@ test('marks cleaned nodes as dirty on moving in a document', () => {
])
})
test('append works after reassigning nodes through .parent', async () => {
let plugin: Plugin = {
postcssPlugin: 'test',
Rule(rule) {
if (
!(
rule.selector === '.nested' &&
rule.nodes.length === 1 &&
rule.nodes[0].type === 'atrule'
)
) {
return
}
let atrule = rule.nodes[0]
atrule.append(rule.clone({ nodes: [] }).append(...atrule.nodes))
rule.after(atrule)
rule.remove()
},
OnceExit(root) {
let firstNode = root.nodes[0] as AtRule
let secondNode = root.nodes[1] as AtRule
let rule2 = secondNode.nodes[0]
rule2.parent!.nodes = rule2.parent!.nodes
firstNode.append(...secondNode.nodes)
secondNode.remove()
}
}
let { css } = await postcss([plugin]).process(
postcss.parse(
`@media (min-width: 640px) { .page { width: auto; } } .nested { @media (min-width: 640px) { width: auto; } }`
),
{ from: 'whatever' }
)
is(
css,
'@media (min-width: 640px) { .page { width: auto; } .nested { width: auto } }'
)
})
test.run()