taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

Should .appendChild not first remove itself from its parent? #171

Closed cammytown closed 2 years ago

cammytown commented 2 years ago

Thanks for this library; it's extremely useful.

appendChild does not currently behave as it does in browsers; moving to its new parent by first removing itself from its current one.

As per https://www.w3.org/TR/DOM-Level-2-Core/core.html

Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.

It may be better to have the functionality of appendChild in this library reflect the W3 specification.

For now, I'm simply calling .remove on the element before calling .appendChild. Presumably patching this into the actual library would be essentially the same concept.

I suppose this could be a breaking change for some implementations that rely on the duplication.

Cheers.

nonara commented 2 years ago

Thank you for the report! I agree with you that we should conform to MDN spec. This is definitely something that should be implemented.

If you or anyone are interested in doing a PR, it is welcomed! Otherwise, I will look into it when I find some time.