appendChild() public method

Add new child at the end of the children.
public appendChild ( Element | DOMNode | array $nodes ) : Document
$nodes Element | DOMNode | array The appended child
return Document
Example #1
0
 public function findNextNodeByText(DiDomElement $element, $expression, $grep = false)
 {
     $childNodes = $element->getNode()->childNodes;
     $length = $childNodes->length;
     $stop = false;
     for ($i = 0; $i < $length; $i++) {
         $node = $childNodes->item($i);
         if ($stop && trim($node->textContent)) {
             $document = new DiDomDocument();
             $document->appendChild($node);
             return $document;
         } else {
             if (!$grep && $expression == $node->textContent) {
                 $stop = true;
             } else {
                 if ($grep && preg_match($expression, $node->textContent)) {
                     $stop = true;
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Get the DOM document with the current element.
  *
  * @param  string $encoding The document encoding
  * 
  * @return \DiDom\Document
  */
 public function toDocument($encoding = 'UTF-8')
 {
     $document = new Document(null, false, $encoding);
     $document->appendChild($this->node);
     return $document;
 }
Example #3
0
 /**
  * Get the DOM document with the current element.
  * 
  * @return \DiDom\Document
  */
 public function toDocument()
 {
     $document = new Document();
     $document->appendChild($this->domElement);
     return $document;
 }
Example #4
0
 public function testAppendChildException()
 {
     $this->setExpectedException('InvalidArgumentException');
     $document = new Document('', true);
     $document->appendChild(null);
 }
Example #5
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testAppendChildWithInvalidArgument()
 {
     $document = new Document('');
     $document->appendChild(null);
 }
Example #6
0
    public function testAppendChild()
    {
        $html = '<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Document</title>
        </head>
        <body>

        </body>
        </html>';
        $document = new Document($html);
        $this->assertCount(0, $document->find('span'));
        $node = $document->createElement('span');
        $document->appendChild($node);
        $this->assertCount(1, $document->find('span'));
        $nodes = [];
        $nodes[] = $document->createElement('span');
        $nodes[] = $document->createElement('span');
        $document->appendChild($nodes);
        $this->assertCount(3, $document->find('span'));
    }