Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gchumillas committed Jan 21, 2015
1 parent 602ac77 commit ffa3f70
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 43 deletions.
26 changes: 16 additions & 10 deletions classes/dom/node/dom-node-content-capable.php
Expand Up @@ -154,9 +154,13 @@ private function _getInnerText()
*/
private function _setInnerText($value)
{
foreach ($this->elements() as $element) {
$doc = $element->ownerDocument;
$element->appendChild($doc->createTextNode($value));
$this->clear();

if (!TextHelper::isEmpty($value)) {
foreach ($this->elements() as $element) {
$doc = $element->ownerDocument;
$element->appendChild($doc->createTextNode($value));
}
}

return $this;
Expand Down Expand Up @@ -196,14 +200,16 @@ private function _setInnerHtml($value)
{
$this->clear();

foreach ($this->elements() as $element) {
$doc = $element->ownerDocument;
$fragment = $doc->createDocumentFragment();
@$fragment->appendXML($value);
$node = @$element->appendChild($fragment);
if (!TextHelper::isEmpty($value)) {
foreach ($this->elements() as $element) {
$doc = $element->ownerDocument;
$fragment = $doc->createDocumentFragment();
@$fragment->appendXML($value);
$node = @$element->appendChild($fragment);

if ($node === false) {
throw new DomNodeException("Invalid XML fragment");
if ($node === false) {
throw new DomNodeException("Invalid XML fragment");
}
}
}

Expand Down
52 changes: 19 additions & 33 deletions classes/dom/node/dom-node.php
Expand Up @@ -55,19 +55,16 @@ class DomNode extends DomNodeIterable
/**
* Creates a node.
*
* Example 1:
* Examples:
* ```php
* // creates a simple node with two attributes and inner text
* $item = new DomNode("item", array("id" => 101, "title" => "Title 101"), "Inner text here...");
* echo $item;
* ```
*
* Example 2:
* ```php
* // creates a complex node
* // in this case we use a callback function to add complex structures into the node
* $root = new DomNode("root", function ($target) {
* // adds three subnodes
* // adds three subnodes
* for ($i = 0; $i < 3; $i++) {
* $target->append(
* new DomNode("item", array("id" => $i, "title" => "Title $i"), "This is the item $i")
Expand All @@ -82,29 +79,21 @@ class DomNode extends DomNodeIterable
* });
* echo $root;
* ```
* Example 3:
* ```php
* // if not specified, DomNode creates a default DOMDocument instance. But you can pass to the
* // constructor a given document.
* $doc = new DOMDocument("1.0", "utf-8");
* $root = new DomNode($doc, "root", "Inner text...");
* echo $doc->saveXML();
* ```
*
* @param DOMDocument $document DOM Document (not required)
* @param string $nodeName Node name (not required)
* @param array $attributes List of attributes (not required)
* @param string $text Inner text (not required)
* @param string $callback Callback function (not required)
* @param string $nodeName Node name (not required)
* @param string $document Document context (not required)
* @param array $attributes List of attributes (not required)
* @param string $text Inner text (not required)
* @param string $callback Callback function (not required)
*/
public function __construct(
$document = null, $nodeName = null, $attributes = null, $text = null, $callback = null
$nodeName = null, $document = null, $attributes = null, $text = null, $callback = null
) {
$args = ArrHelper::fetch(
func_get_args(),
array(
"document" => "\DOMDocument",
"nodeName" => "string",
"document" => "\DOMDocument",
"attributes" => "array",
"text" => "scalar",
"callback" => "function"
Expand Down Expand Up @@ -146,27 +135,24 @@ public function __construct(
/**
* Creates an instance from a given string.
*
* @param string $str Well formed document
* @param string $contentType Content Type (default is "text/xml")
* @param DOMDocument $document DOM Document (not required)
* @param string $str Well formed document
* @param string $contentType Content Type (default is "text/xml")
*
* @return DomNode
*/
public static function createFromString($str, $contentType = "text/xml", $document = null)
public static function createFromString($str, $contentType = "text/xml")
{
if ($document == null) {
$document = new DOMDocument("1.0");
$document->preserveWhiteSpace = false;
$document->formatOutput = true;
}
$doc = new DOMDocument("1.0");
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;

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

if ($contentType == "text/html") {
$document->loadHTML($str);
$doc->loadHTML($str);
} else {
$document->loadXML($str);
$doc->loadXML($str);
}

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

$node = new static();
$node->document = $document;
$node->elements = array($document->documentElement);
$node->document = $doc;
$node->elements = array($doc->documentElement);

return $node;
}
Expand Down

0 comments on commit ffa3f70

Please sign in to comment.