Fix append()
failing after mutating nodes through .parent
#1740
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1738
I wasn't quite sure where to put the test but the gist of the issue is that
node.append
callsnode.normalize
node.normalize
sets the parent tothis
.The problem arises when
node
is a proxy. This means thatthis
ends up being a proxy instead of the raw node. Assigning the proxy to the "raw" child node's parent has a side-effect of causingproxyOf.nodes
to contain proxies rather than raw postcss nodes.This causes
indexOf
lookups to fail since they're sadly not transparent to Proxies b/c of object identity.Later on
append
tries to use this index assuming that the nodes passed in are definitely children and have been found and crashes.I'm not 100% sure this is the right fix because the interactions just to get this to happen are fairly specific. Change any number of things and it all magically works. But, it seems reasonable.
I noticed that the
.push
method sets the parent as well but I don't have a test case where it's affected so I've not updated it. I might be able to come up with a test case if needed. Also, if you have any suggestions for reducing the test case I've got in this PR I would love some because it seems like it's a bit much for such a "simple" fix.