Skip to content

Commit ffa3f70

Browse files
author
Gonzalo Chumillas
committed
update
1 parent 602ac77 commit ffa3f70

File tree

2 files changed

+35
-43
lines changed

2 files changed

+35
-43
lines changed

classes/dom/node/dom-node-content-capable.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,13 @@ private function _getInnerText()
154154
*/
155155
private function _setInnerText($value)
156156
{
157-
foreach ($this->elements() as $element) {
158-
$doc = $element->ownerDocument;
159-
$element->appendChild($doc->createTextNode($value));
157+
$this->clear();
158+
159+
if (!TextHelper::isEmpty($value)) {
160+
foreach ($this->elements() as $element) {
161+
$doc = $element->ownerDocument;
162+
$element->appendChild($doc->createTextNode($value));
163+
}
160164
}
161165

162166
return $this;
@@ -196,14 +200,16 @@ private function _setInnerHtml($value)
196200
{
197201
$this->clear();
198202

199-
foreach ($this->elements() as $element) {
200-
$doc = $element->ownerDocument;
201-
$fragment = $doc->createDocumentFragment();
202-
@$fragment->appendXML($value);
203-
$node = @$element->appendChild($fragment);
203+
if (!TextHelper::isEmpty($value)) {
204+
foreach ($this->elements() as $element) {
205+
$doc = $element->ownerDocument;
206+
$fragment = $doc->createDocumentFragment();
207+
@$fragment->appendXML($value);
208+
$node = @$element->appendChild($fragment);
204209

205-
if ($node === false) {
206-
throw new DomNodeException("Invalid XML fragment");
210+
if ($node === false) {
211+
throw new DomNodeException("Invalid XML fragment");
212+
}
207213
}
208214
}
209215

classes/dom/node/dom-node.php

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,16 @@ class DomNode extends DomNodeIterable
5555
/**
5656
* Creates a node.
5757
*
58-
* Example 1:
58+
* Examples:
5959
* ```php
6060
* // creates a simple node with two attributes and inner text
6161
* $item = new DomNode("item", array("id" => 101, "title" => "Title 101"), "Inner text here...");
6262
* echo $item;
63-
* ```
6463
*
65-
* Example 2:
66-
* ```php
6764
* // creates a complex node
6865
* // in this case we use a callback function to add complex structures into the node
6966
* $root = new DomNode("root", function ($target) {
70-
* // adds three subnodes
67+
* // adds three subnodes
7168
* for ($i = 0; $i < 3; $i++) {
7269
* $target->append(
7370
* new DomNode("item", array("id" => $i, "title" => "Title $i"), "This is the item $i")
@@ -82,29 +79,21 @@ class DomNode extends DomNodeIterable
8279
* });
8380
* echo $root;
8481
* ```
85-
* Example 3:
86-
* ```php
87-
* // if not specified, DomNode creates a default DOMDocument instance. But you can pass to the
88-
* // constructor a given document.
89-
* $doc = new DOMDocument("1.0", "utf-8");
90-
* $root = new DomNode($doc, "root", "Inner text...");
91-
* echo $doc->saveXML();
92-
* ```
9382
*
94-
* @param DOMDocument $document DOM Document (not required)
95-
* @param string $nodeName Node name (not required)
96-
* @param array $attributes List of attributes (not required)
97-
* @param string $text Inner text (not required)
98-
* @param string $callback Callback function (not required)
83+
* @param string $nodeName Node name (not required)
84+
* @param string $document Document context (not required)
85+
* @param array $attributes List of attributes (not required)
86+
* @param string $text Inner text (not required)
87+
* @param string $callback Callback function (not required)
9988
*/
10089
public function __construct(
101-
$document = null, $nodeName = null, $attributes = null, $text = null, $callback = null
90+
$nodeName = null, $document = null, $attributes = null, $text = null, $callback = null
10291
) {
10392
$args = ArrHelper::fetch(
10493
func_get_args(),
10594
array(
106-
"document" => "\DOMDocument",
10795
"nodeName" => "string",
96+
"document" => "\DOMDocument",
10897
"attributes" => "array",
10998
"text" => "scalar",
11099
"callback" => "function"
@@ -146,27 +135,24 @@ public function __construct(
146135
/**
147136
* Creates an instance from a given string.
148137
*
149-
* @param string $str Well formed document
150-
* @param string $contentType Content Type (default is "text/xml")
151-
* @param DOMDocument $document DOM Document (not required)
138+
* @param string $str Well formed document
139+
* @param string $contentType Content Type (default is "text/xml")
152140
*
153141
* @return DomNode
154142
*/
155-
public static function createFromString($str, $contentType = "text/xml", $document = null)
143+
public static function createFromString($str, $contentType = "text/xml")
156144
{
157-
if ($document == null) {
158-
$document = new DOMDocument("1.0");
159-
$document->preserveWhiteSpace = false;
160-
$document->formatOutput = true;
161-
}
145+
$doc = new DOMDocument("1.0");
146+
$doc->preserveWhiteSpace = false;
147+
$doc->formatOutput = true;
162148

163149
// use internal errors
164150
$useInternalErrors = libxml_use_internal_errors(true);
165151

166152
if ($contentType == "text/html") {
167-
$document->loadHTML($str);
153+
$doc->loadHTML($str);
168154
} else {
169-
$document->loadXML($str);
155+
$doc->loadXML($str);
170156
}
171157

172158
// retrieves the errors
@@ -188,8 +174,8 @@ public static function createFromString($str, $contentType = "text/xml", $docume
188174
}
189175

190176
$node = new static();
191-
$node->document = $document;
192-
$node->elements = array($document->documentElement);
177+
$node->document = $doc;
178+
$node->elements = array($doc->documentElement);
193179

194180
return $node;
195181
}

0 commit comments

Comments
 (0)