Skip to content

Commit 8a1f6c2

Browse files
authored
Merge pull request #1740 from thecrypticace/fix/normalization-append-error
Fix `append()` failing after mutating nodes through .parent
2 parents 2713d8d + 2955628 commit 8a1f6c2

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/container.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class Container extends Node {
326326
i.raws.before = sample.raws.before.replace(/\S/g, '')
327327
}
328328
}
329-
i.parent = this
329+
i.parent = this.proxyOf
330330
return i
331331
})
332332

test/visitor.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import postcss, {
77
Container,
88
Root,
99
Rule,
10+
AtRule,
1011
Declaration,
1112
Plugin,
1213
PluginCreator,
@@ -1552,4 +1553,50 @@ test('marks cleaned nodes as dirty on moving in a document', () => {
15521553
])
15531554
})
15541555

1556+
test('append works after reassigning nodes through .parent', async () => {
1557+
let plugin: Plugin = {
1558+
postcssPlugin: 'test',
1559+
1560+
Rule(rule) {
1561+
if (
1562+
!(
1563+
rule.selector === '.nested' &&
1564+
rule.nodes.length === 1 &&
1565+
rule.nodes[0].type === 'atrule'
1566+
)
1567+
) {
1568+
return
1569+
}
1570+
1571+
let atrule = rule.nodes[0]
1572+
1573+
atrule.append(rule.clone({ nodes: [] }).append(...atrule.nodes))
1574+
1575+
rule.after(atrule)
1576+
rule.remove()
1577+
},
1578+
1579+
OnceExit(root) {
1580+
let firstNode = root.nodes[0] as AtRule
1581+
let secondNode = root.nodes[1] as AtRule
1582+
let rule2 = secondNode.nodes[0]
1583+
rule2.parent!.nodes = rule2.parent!.nodes
1584+
firstNode.append(...secondNode.nodes)
1585+
secondNode.remove()
1586+
}
1587+
}
1588+
1589+
let { css } = await postcss([plugin]).process(
1590+
postcss.parse(
1591+
`@media (min-width: 640px) { .page { width: auto; } } .nested { @media (min-width: 640px) { width: auto; } }`
1592+
),
1593+
{ from: 'whatever' }
1594+
)
1595+
1596+
is(
1597+
css,
1598+
'@media (min-width: 640px) { .page { width: auto; } .nested { width: auto } }'
1599+
)
1600+
})
1601+
15551602
test.run()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy