in reply to Seemingly Valid HTML which crashes HTML::TreeBuilder::XPath

I think it's not a bug in either HTML::TreeBuilder, HTML::TreeBuilder::XPath or HTML::Tidy, but more in your expectations of what ->detach_content returns, especially for nodes that only contain text, like the string trololo.

I can't find documentation in HTML::TreeBuilder as to how it represents text nodes, but my guess is that if you receive a non-reference like the string trololo, then you should not call any methods on that.

Replies are listed 'Best First'.
Re^2: Seemingly Valid HTML which crashes HTML::TreeBuilder::XPath
by mldvx4 (Friar) on Nov 10, 2023 at 11:38 UTC

    Thanks.

    Would there be a better way of handling the character data (CDATA) found in odd places in the HTML?

    What I am trying to do is lift the contents out of an element. Specifically, HTML Tidy adds a body element around any elements and CDATA in a document so,

    <body> <p>foo</p> <p>bar</p> </body>

    would then after processing become

    <p>foo</p> <p>bar</p>

    This is so the block which had been body can be inserted into another document, without that new document ending up with multiple body elements. One alternative would have been to change the body to div, but then multiple passes though the work flow would cause multiple, unnecessary, nested div elements. Therefore it seems like the only option is to remove the element completely and leave just its contents.