I generate some non-standard-tag HTML with DomDocument and the result is this:
/* Input HTML
<div id="toobar_top">
<widget id="flag_holder"></widget>
<widget id="horizontal_menu"></widget>
</div>
<div id="header">
<widget name="header"></widget>
</div>
*/
What I want to do is to "translate" each widget in something useful... they are simple placeholders with params.
The function extract from the class is:
private function widgeter($doc) { //Give it an DomDocument HTML containing <widget> elements and will translate them into usable stuff
$this->_widgetList = $doc->getElementsByTagName($this->_widgetTransformTo);
foreach ($this->_widgetList as $widget) {
$data = array();
if ($widget->hasAttributes()) {
foreach ($widget->attributes as $attribute) {
$data[][$attribute->name] = $attribute->value;
// @TODO: Implements Widget Transformation
}
}
// Next 2 lines are just for debug
$string = serialize($data);
$newWidget = $doc->createElement('p', $string);
$widget->parentNode->replaceChild($newWidget, $widget);
}
return $doc;
}
then when I saveHTML() the $doc I see:
/* Output HTML
<div id="toobar_top">
<p>[{"id":"flag_holder"}]</p>
<widget id="horizontal_menu"></widget>
</div>
<div id="header">
<p>[{"id":"header"}]</p>
</div>
*/
why "horizontal_menu" wasn't translated?
It doesn't matter where widgets are (I tried with only one div with all widgets in and with a div per widget).
I can't figure out it...
It happens because you are replacing the elements in the DOMNodeList while looping on them. A DOMNodeList is not an array, so foreach does not operate on a copy, but on the object itself.
Basically, what I think is happening is:
<widget>
(Item 0).What you need to do is to save the elements in an array and then change them, instead of looping on the DOMNodeList:
$this->_widgetList = array();
foreach ($domNodeList as $node) {
$this->_widgetList[] = $node;
}
foreach ($this->_widgetList as $widget) {
// do stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With