PHPHtmlParser\Dom\HtmlNode::innerHtml PHP Méthode

innerHtml() public méthode

Gets the inner html of this node.
public innerHtml ( ) : string
Résultat string
    public function innerHtml()
    {
        if (!$this->hasChildren()) {
            // no children
            return '';
        }
        if (!is_null($this->innerHtml)) {
            // we already know the result.
            return $this->innerHtml;
        }
        $child = $this->firstChild();
        $string = '';
        // continue to loop until we are out of children
        while (!is_null($child)) {
            if ($child instanceof TextNode) {
                $string .= $child->text();
            } elseif ($child instanceof HtmlNode) {
                $string .= $child->outerHtml();
            } else {
                throw new UnknownChildTypeException('Unknown child type "' . get_class($child) . '" found in node');
            }
            try {
                $child = $this->nextChild($child->id());
            } catch (ChildNotFoundException $e) {
                // no more children
                $child = null;
            }
        }
        // remember the results
        $this->innerHtml = $string;
        return $string;
    }

Usage Example

 /**
  * @expectedException PHPHtmlParser\Exceptions\UnknownChildTypeException
  */
 public function testInnerHtmlUnkownChild()
 {
     $div = new Tag('div');
     $div->setAttributes(['class' => ['value' => 'all', 'doubleQuote' => true]]);
     $a = new Tag('a');
     $a->setAttributes(['href' => ['value' => 'http://google.com', 'doubleQuote' => false]]);
     $br = new Tag('br');
     $br->selfClosing();
     $parent = new HtmlNode($div);
     $childa = new HtmlNode($a);
     $childbr = new MockNode($br);
     $parent->addChild($childa);
     $parent->addChild($childbr);
     $childa->addChild(new TextNode('link'));
     $inner = $parent->innerHtml();
     $this->assertEquals($inner, $parent->innerHtml());
 }
All Usage Examples Of PHPHtmlParser\Dom\HtmlNode::innerHtml