Closed iamakulov closed 2 years ago
Interesting! Good catch. Thank you for the investigation and detail.
Thanks for your analysis, the same issue occurs when passing the 'afterbegin' parameter to the `insertAdjacentHTML' method. For this case, my workaround is to get the first child element and call insertAdjacentHTML on it, then passing the 'beforebegin' parameter to the method.
Hey there,
I stumbled upon a bug with node removal:
(executable notebook)
Why this happens
This happens because
innerHTML
,set_content()
,replaceWith
(and perhaps other methods?) fail to senda
’sparentNode
when adding it to children:https://github.com/taoqf/node-html-parser/blob/b1ef785fc7ffe6b882f4fe7403c4638bada4dee5/src/nodes/html.ts#L338-L342
As a result, when
remove()
is invoked,querySelector('a').parentNode
returnsnull
, and nothing happens:https://github.com/taoqf/node-html-parser/blob/b1ef785fc7ffe6b882f4fe7403c4638bada4dee5/src/nodes/html.ts#L207-L214
Workaround
A workaround for this is to use
insertAdjacentHTML("beforeend", "...")
. Internally, it adds elements throughappendChild
instead of a direct assignment, andappendChild
takes care of settingparentNode
:https://github.com/taoqf/node-html-parser/blob/b1ef785fc7ffe6b882f4fe7403c4638bada4dee5/src/nodes/html.ts#L770-L774