Pian0_M4n
Pian0_M4n

Reputation: 2558

php DOMDocument element value can't be HTML

This is the code related to the problem:

$prep = "<select><option>Option 1</option><option selected>Option 1</option></select>"
$td = $dom->createElement('td',$prep);

Solution:

$f = $this->dom->createDocumentFragment();
$f->appendXML($prep);

BUT still a big problem. Any attributes without value ex: selected, disabled (which you can't write as selected="selected") the createElement doesn't allow you to do so.

How can use attributes without value, and not get 1000 erros like now: Warning: DOMDocumentFragment::appendXML() [domdocumentfragment.appendxml]: Entity: line 1: parser error : Specification mandate value for attribute selected Warning: DOMDocumentFragment::appendXML() [domdocumentfragment.appendxml]: Entity: line 2: parser error : chunk is not well balanced

This happens when passing a selected attribute, or disabled.

Upvotes: 1

Views: 2529

Answers (2)

S&#233;bastien Renauld
S&#233;bastien Renauld

Reputation: 19672

I cannot resist correcting. In PHP's implementation of the DOMDocument model, strings are also objects (this allows you to easily select them rather than to rely on nodeValue, which makes life easier in most cases).

To insert a table row with text, do this:

$f = new DOMDocument();
$table = $f->createElement("table");
$f->appendChild($table);
$tbody = $f->createElement("tbody");
$table->appendChild($tbody);
$tr = $f->createElement("tr");
$tbody->appendChild($tr);
$td = $f->createElement("td");
$tr->appendChild($td);

// Magic happens here
$td->appendChild(new DOMText("this is my text"));

Empty attributes are conserved when inserted properly, by the way - but you're free to use selected="selected", it is still valid provided that its content is the lowercase version of its attribute name ( What does it mean in HTML 5 when an attribute is a boolean attribute? ). If you'd still prefer to just flag it as boolean true, use:

$elem->setAttribute("selected","");

(I just ran the code + mode and got no errors whatsoever without the need to use shutop)

Upvotes: 1

Pian0_M4n
Pian0_M4n

Reputation: 2558

I found the answer my self.
$this->dom = new DOMDocument; all other code and what you build
$tbody = $this->dom->createElement('tbody');
$td = $this->dom->createElement('td');

To put a piece of html code in the TD you can't just paste it or set the value via ->nodeValue = ''. It will output as text, NOT as HTML.

$prep = "<strong>lalala</strong>"; or any other html string // create fragment and append it
@@$f = $this->dom->createDocumentFragment(); @@$f->appendXML($prep);

$td->appendChild($f);
-- it WORKS! but be careful :: Any sort of empty attribute to a HTML tag like <option selected> or <option disabled> will cause errors and fail.
Don't try to set attribute as selected="selected". That will also fail due to the HTML/XML parser.

SO createDocumentFragment() works very well as long as you dont have empty value attributes like selected,disabled or other

Upvotes: 0

Related Questions