html5-dom-document-php icon indicating copy to clipboard operation
html5-dom-document-php copied to clipboard

Feature request: `HTML5DOMDocumentFragment` with `appendHTML()`

Open rulatir opened this issue 2 years ago • 5 comments

Since XML is not HTML, using native DOMDocumentFragment and its appendXML() method is risky and prone to all the very same issues this library is trying to address.

rulatir avatar Jan 02 '23 14:01 rulatir

There are already two methods to help you with this. Please take a look at HTML5DOMDocument::insertHTML() and HTML5DOMDocument::insertHTMLMulti(). There is an example at https://github.com/ivopetkov/html5-dom-document-php (in the README section).

ivopetkov avatar Jan 03 '23 06:01 ivopetkov

I am implementing a contract that requires me to return a DOMDocumentFragment.

rulatir avatar Jan 03 '23 12:01 rulatir

Would you provide some example code so I can better understand the problem you're trying to solve and the API you're requesting?

ivopetkov avatar Jan 12 '23 07:01 ivopetkov

I must return a DOMDocumentFragment, because that is what a contract enforced by a 3rd-party library requires of me. I must also be able to assemble the contents of the resulting DOMDocumentFragment from pieces, which can be:

  • DOMNodes, including other DOMDocumentFragments,
  • strings of raw HTML

YES, I can do this:

  • for each $piece
    • if $piece is a DOMNode, just append it to the fragment
    • else
      • create a temporary HTML5DOMDocument
      • load a HTML skeleton containing <div id="processing-container">$piece</div> into the document
      • iterate over the children of the processing container, clone them, and append them to the DOMDocumentFragment

But that is a huge amount of work for what should be much easier to do, and with the requested feature, it would be. If the purpose of this library is to make HTML manipulation easy, then this feature is missing.

rulatir avatar Jan 12 '23 14:01 rulatir

Currently, I do not have plans to add fragments support. Maybe someday. Feel free to contribute such code to the library.

ivopetkov avatar Jan 24 '23 12:01 ivopetkov