diff --git a/.travis.yml b/.travis.yml index 3bd07e4..b75da2c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ php: - 5.5 - 5.6 - 7.0 + - 7.1 - hhvm env: diff --git a/Tests/HtmlPageCrawlerTest.php b/Tests/HtmlPageCrawlerTest.php index 9c2880d..29f6f0f 100644 --- a/Tests/HtmlPageCrawlerTest.php +++ b/Tests/HtmlPageCrawlerTest.php @@ -43,7 +43,7 @@ public function testManipulationFunctions() $this->assertEquals('

Ein neuer Inhalt

', $content->getInnerHtml()); $content->prepend('

Neue Überschrift'); - $this->assertEquals('

Neue Überschrift

Ein neuer Inhalt

', $content->getInnerHtml()); + $this->assertEquals('

Neue Überschrift

Ein neuer Inhalt

', $content->getInnerHtml()); $h1 = $content->filter('h1'); $this->assertEquals('Neue Überschrift', $h1->text()); @@ -440,7 +440,7 @@ public function testUTF8Characters() $c = HtmlPageCrawler::create($text); $expected =<<< END -

Die Burse wurde unmittelbar (1478 bis 1482) nach der Universitätsgründung als Studentenwohnhaus und -lehranstalt errichtet. Hier lehrte der Humanist und Reformator Philipp Melanchthon bis zu seiner Berufung nach Wittenberg 1518, an ihn erinnert eine Gedenktafel. 1803 bis 1805 wurde das Gebäude im Stil des Klassizismus zum ersten Tübinger Klinikum umgebaut. Einer der ersten Patienten war Friedrich Hölderlin, der nach einer 231 Tage dauernden Behandlung am 3. Mai 1807 als unheilbar entlassen wurde.

Einst Badeanstalt vor der Stadtmauer. Wer durch das kleine Stadttor geht, hat – rückwärts gewandt – einen guten Blick auf die Stadtbefestigung mit "Pechnasen" und Spuren des alten Wehrgangs.

+

Die Burse wurde unmittelbar (1478 bis 1482) nach der Universitätsgründung als Studentenwohnhaus und -lehranstalt errichtet. Hier lehrte der Humanist und Reformator Philipp Melanchthon bis zu seiner Berufung nach Wittenberg 1518, an ihn erinnert eine Gedenktafel. 1803 bis 1805 wurde das Gebäude im Stil des Klassizismus zum ersten Tübinger Klinikum umgebaut. Einer der ersten Patienten war Friedrich Hölderlin, der nach einer 231 Tage dauernden Behandlung am 3. Mai 1807 als unheilbar entlassen wurde.

Einst Badeanstalt vor der Stadtmauer. Wer durch das kleine Stadttor geht, hat – rückwärts gewandt – einen guten Blick auf die Stadtbefestigung mit "Pechnasen" und Spuren des alten Wehrgangs.

END; $this->assertEquals($expected, $c->filter('p')->saveHTML()); diff --git a/Tests/HtmlPageTest.php b/Tests/HtmlPageTest.php index cf68a3f..3454e5a 100644 --- a/Tests/HtmlPageTest.php +++ b/Tests/HtmlPageTest.php @@ -34,7 +34,7 @@ public function testHtmlPage() $content = '

Überschrift

bla bla
fett

'; $hp->setHtmlById('content', $content); // echo $hp; - $this->assertEquals(mb_convert_encoding($content, 'HTML-ENTITIES', 'utf8'), $hp->getElementById('content')->getInnerHtml()); + $this->assertEquals($content, $hp->getElementById('content')->getInnerHtml()); $url = 'http://www.tuebingen.de/'; $hp->setBaseHref($url); diff --git a/src/HtmlPage.php b/src/HtmlPage.php index ac63bdf..63a52e4 100644 --- a/src/HtmlPage.php +++ b/src/HtmlPage.php @@ -50,6 +50,7 @@ public function __construct($content = '', $url = '', $charset = 'UTF-8') $disableEntities = libxml_disable_entity_loader(true); $this->dom = new \DOMDocument('1.0', $charset); + $this->dom->loadHTML(''); $this->dom->validateOnParse = true; @@ -254,7 +255,11 @@ public function getBody() public function __toString() { - return $this->dom->saveHTML(); + $html = $this->dom->saveHTML(); + if (function_exists('mb_convert_encoding') && in_array(strtolower($this->charset), array_map('strtolower', mb_list_encodings()))) { + $html = mb_convert_encoding($html, $this->charset, 'HTML-ENTITIES'); + } + return $html; } /** diff --git a/src/HtmlPageCrawler.php b/src/HtmlPageCrawler.php index e91cefc..52684b1 100644 --- a/src/HtmlPageCrawler.php +++ b/src/HtmlPageCrawler.php @@ -380,16 +380,11 @@ public function html($html = null) */ public function getInnerHtml() { - $node = $this->getNode(0); - if ($node instanceof \DOMNode) { - $doc = new \DOMDocument('1.0', 'UTF-8'); - $doc->appendChild($doc->importNode($node, true)); - $html = trim($doc->saveHTML()); - $tag = $node->nodeName; - return preg_replace('@^<' . $tag . '[^>]*>|$@', '', $html); - } else { - return ''; + $html = ''; + foreach ($this->getNode(0)->childNodes as $node) { + $html .= $node->ownerDocument->saveHTML($node); } + return $html; } /** @@ -875,25 +870,24 @@ public function wrapInner($content) /** * Get the HTML code fragment of all elements and their contents. * - * If the first node contains a complete HTML document return only - * the full code of this document. + * If the first node contains a complete HTML document return the + * DocType if exists * * @return string HTML code (fragment) * @api */ public function saveHTML() { - if ($this->isHtmlDocument()) { - return $this->getDOMDocument()->saveHTML(); - } else { - $doc = new \DOMDocument('1.0', 'UTF-8'); - $root = $doc->appendChild($doc->createElement('_root')); - foreach ($this as $node) { - $root->appendChild($doc->importNode($node, true)); - } - $html = trim($doc->saveHTML()); - return preg_replace('@^<'.self::FRAGMENT_ROOT_TAGNAME.'[^>]*>|$@', '', $html); + $html = ''; + if ( $this->isHtmlDocument() ) { + /* Output DocType if exists */ + $documentHtml = $this->getDOMDocument()->saveHTML(); + $html .= preg_match("//is", $documentHtml, $match) ? $match[0]."\n" : ''; + } + foreach ($this as $node) { + $html .= $node->ownerDocument->saveHTML($node); } + return $html; } public function __toString() @@ -928,7 +922,7 @@ public function isHtmlDocument() */ public function getDOMDocument() { - $node = $this->getNode(0); + $node = $this->getNode(0); $r = null; if ($node instanceof \DOMElement && $node->ownerDocument instanceof \DOMDocument @@ -1026,6 +1020,20 @@ public function getNode($position) return parent::getNode($position); } + /** + * + * get all nodes in Crawler in Array format, so that we can use + * foreach to loop through the elements + * + * @return Array HtmlPageCrawler + */ + public function getNodes() + { + return $this->each(function($node) { + return $node; + }); + } + /** * Returns the node name of the first node of the list. * @@ -1097,7 +1105,7 @@ public function isDisconnected() $parent = $this->getNode(0)->parentNode; return ($parent == null || $parent->tagName == self::FRAGMENT_ROOT_TAGNAME); } - + public function __get($name) { switch ($name) {