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

Walk from root through all children - change Attributes

Open SarahTrees opened this issue 2 years ago • 1 comments

I come from simplehtmldom and have found your library, thank you very much for OpenSource!

I need to go through all the elements in parent-child order, recursively. I really like how easy it is to read and change attributes with this library.

$dom= new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('
<div class="row">
       <div class="col-6" data-attr="redcolumn">
              <div class="row">
                      <div class="col-10" data-attr="bluecolumn"><p>Text in a p</p></div>
              </div>
       </div>
</div>');

When starting I can start with $dom->querySelectorAll('body > *'), because my fragments are automatically integrated into a body by the library. But how do I get all the direct divs which are included in my current elemts and not all the divs which are children of the children of the children ... ?

Per $childElement->querySelectorAll('*') I get all objects, not only the direct children - but I can very well read their attributes. Per $childElement->childNodes I get the direct children, but I can't read their attributes, because the result is an object from the DOMParster, right? Is it possible to convert DomNode elements into HTML5DomNode objects?

Thank you for the support!

SarahTrees avatar Feb 14 '22 07:02 SarahTrees

That's a really interesting situation. I expected that childNodes should handle that (because there is a DOMDocument::registerNodeClass call). Did you manage to find a solution?

ivopetkov avatar Mar 04 '22 12:03 ivopetkov