diff --git a/src/Readability.php b/src/Readability.php index 1d2d2f5..05a6c9f 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -1396,7 +1396,10 @@ private function loadHtml() $this->logger->debug('Parsing URL: ' . $this->url); if ($this->url) { - $this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', parse_url($this->url, \PHP_URL_HOST)), ['.' => '\.']) . '/'; + $host = parse_url($this->url, \PHP_URL_HOST); + if (null !== $host) { + $this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', $host), ['.' => '\.']) . '/'; + } } mb_internal_encoding('UTF-8'); @@ -1432,7 +1435,7 @@ private function loadHtml() unset($tidy); } - $this->html = '' . (string) $this->html; + $this->html = self::ensureMetaCharset((string) $this->html); if ('html5lib' === $this->parser || 'html5' === $this->parser) { $this->dom = (new HTML5())->loadHTML($this->html); @@ -1450,4 +1453,45 @@ private function loadHtml() $this->dom->registerNodeClass('DOMElement', 'Readability\JSLikeHTMLElement'); } + + /** + * Tries to insert `meta[charset]` tag into the proper place in the passed HTML document. + * + * `DOMDocument::loadHTML` will parse HTML documents as ISO-8859-1 if there is no `meta[charset]` tag. + * This means that UTF-8-encoded HTML fragments such as those coming from JSON-LD `articleBody` field would be parsed with incorrect encoding. + * Unfortunately, we cannot just put the tag at the start of the HTML fragment, since that would cause parser to auto-insert a `html` element, losing the attributes of the original `html` tag. + * + * @param string $html UTF-8 encoded document + */ + private static function ensureMetaCharset($html) + { + $charsetTag = ''; + + // Only look at first 1024 bytes since, according to HTML5 specification, + // that’s where elements declaring a character encoding must be located. + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#charset + $start = substr($html, 0, 1000); + + if (1 === preg_match('/]+charset/i', $start)) { + // tag is already present, no need for modification. + return $html; + } + + if (1 === preg_match('/]*>/i', $start)) { + // tag was located, tags go there. + $html = preg_replace('/]*>/i', '$0' . $charsetTag, $html, 1); + + return $html; + } + + if (1 === preg_match('/]*>/i', $start)) { + // tag was located, let’s put it inside and have parser create . + $html = preg_replace('/]*>/i', '$0' . $charsetTag, $html, 1); + + return $html; + } + + // Fallback – just plop the at the start of the fragment. + return $charsetTag . $html; + } } diff --git a/tests/ReadabilityTest.php b/tests/ReadabilityTest.php index b190a8e..fc9f6d5 100644 --- a/tests/ReadabilityTest.php +++ b/tests/ReadabilityTest.php @@ -19,7 +19,7 @@ public function testConstructDefault() $readability = $this->getReadability(''); $this->assertNull($readability->url); - $this->assertInstanceOf('DomDocument', $readability->dom); + $this->assertInstanceOf(\DOMDocument::class, $readability->dom); } public function testConstructHtml5Parser() @@ -27,7 +27,7 @@ public function testConstructHtml5Parser() $readability = $this->getReadability('', 'http://0.0.0.0', 'html5lib'); $this->assertSame('http://0.0.0.0', $readability->url); - $this->assertInstanceOf('DomDocument', $readability->dom); + $this->assertInstanceOf(\DOMDocument::class, $readability->dom); $this->assertSame('', $readability->original_html); } @@ -39,7 +39,7 @@ public function testConstructSimple() $readability = $this->getReadability('', 'http://0.0.0.0'); $this->assertSame('http://0.0.0.0', $readability->url); - $this->assertInstanceOf('DomDocument', $readability->dom); + $this->assertInstanceOf(\DOMDocument::class, $readability->dom); $this->assertSame('', $readability->original_html); $this->assertTrue($readability->tidied); } @@ -52,7 +52,7 @@ public function testConstructDefaultWithoutTidy() $this->assertSame('', $readability->original_html); $this->assertFalse($readability->tidied); - $this->assertInstanceOf('DomDocument', $readability->dom); + $this->assertInstanceOf(\DOMDocument::class, $readability->dom); } public function testConstructSimpleWithoutTidy() @@ -60,7 +60,7 @@ public function testConstructSimpleWithoutTidy() $readability = $this->getReadability('', 'http://0.0.0.0', 'libxml', false); $this->assertSame('http://0.0.0.0', $readability->url); - $this->assertInstanceOf('DomDocument', $readability->dom); + $this->assertInstanceOf(\DOMDocument::class, $readability->dom); $this->assertSame('', $readability->original_html); $this->assertFalse($readability->tidied); } @@ -106,7 +106,6 @@ public function testInitDivP() public function testInitDiv() { $readability = $this->getReadability('
' . str_repeat('This is the awesome content :)', 7) . '
', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -120,7 +119,6 @@ public function testInitDiv() public function testWithFootnotes() { $readability = $this->getReadability('
' . str_repeat('

This is an awesome text with some links, here there are: the awesome

', 7) . '
', 'http://0.0.0.0'); - $readability->debug = true; $readability->convertLinksToFootnotes = true; $res = $readability->init(); @@ -137,7 +135,6 @@ public function testWithFootnotes() public function testStandardClean() { $readability = $this->getReadability('

Title

' . str_repeat('

This is an awesome text with some links, here there are: the awesome

', 7) . 'will NOT be removed
', 'http://0.0.0.0'); - $readability->debug = true; $readability->lightClean = false; $res = $readability->init(); @@ -154,7 +151,6 @@ public function testStandardClean() public function testWithIframe() { $readability = $this->getReadability('

Title

' . str_repeat('

This is an awesome text with some links, here there are: the awesome

', 7) . '

This is an awesome text with some links, here there are

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -169,7 +165,6 @@ public function testWithIframe() public function testWithArticle() { $readability = $this->getReadability('

' . str_repeat('This is an awesome text with some links, here there are: the awesome', 20) . '

This is an awesome text with some links, here there are

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -184,7 +179,6 @@ public function testWithArticle() public function testWithAside() { $readability = $this->getReadability('
' . str_repeat('

This is an awesome text with some links, here there are: the awesome

', 7) . '
', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -199,7 +193,6 @@ public function testWithAside() public function testWithClasses() { $readability = $this->getReadability('
' . str_repeat('

This is an awesome text with some links, here there are: the awesome

', 7) . '
' . str_repeat('

This text should be removed

', 10) . '
', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -214,7 +207,6 @@ public function testWithClasses() public function testWithClassesWithoutLightClean() { $readability = $this->getReadability('
' . str_repeat('

This is an awesome text with some links, here there are: the awesome

', 7) . '
' . str_repeat('

This text should be removed

', 10) . '
', 'http://0.0.0.0'); - $readability->debug = true; $readability->lightClean = false; $res = $readability->init(); @@ -230,7 +222,6 @@ public function testWithClassesWithoutLightClean() public function testWithTd() { $readability = $this->getReadability('' . str_repeat('', 7) . '

This is an awesome text with some links, here there are the awesome

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -243,7 +234,6 @@ public function testWithTd() public function testWithSameClasses() { $readability = $this->getReadability('
' . str_repeat('

This is an awesome text with some links, here there are the awesome

', 7) . '
This text is also an awesome text and you should know that !
', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -257,7 +247,6 @@ public function testWithSameClasses() public function testWithScript() { $readability = $this->getReadability('
' . str_repeat('

This is an awesome text with some links, here there are the awesome

', 7) . '

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -271,7 +260,6 @@ public function testWithScript() public function testTitle() { $readability = $this->getReadability('this is my title
' . str_repeat('

This is an awesome text with some links, here there are the awesome

', 7) . '

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -285,7 +273,6 @@ public function testTitle() public function testTitleWithDash() { $readability = $this->getReadability(' title2 - title3
' . str_repeat('

This is an awesome text with some links, here there are the awesome

', 7) . '

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -299,7 +286,6 @@ public function testTitleWithDash() public function testTitleWithDoubleDot() { $readability = $this->getReadability(' title2 : title3
' . str_repeat('

This is an awesome text with some links, here there are the awesome

', 7) . '

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -313,7 +299,6 @@ public function testTitleWithDoubleDot() public function testTitleTooShortUseH1() { $readability = $this->getReadability('too short

this is my h1 title !

' . str_repeat('

This is an awesome text with some links, here there are the awesome

', 7) . '

', 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -365,7 +350,6 @@ public function testAutoClosingIframeNotThrowingException() '; $readability = $this->getReadability($data, 'http://iosgames.ru/?p=22030'); - $readability->debug = true; $res = $readability->init(); @@ -433,7 +417,6 @@ public function testAppendIdAlreadyHere() '; $readability = $this->getReadability($data, 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); @@ -472,7 +455,6 @@ public function testChildNodeGoneNull() $html = file_get_contents('tests/fixtures/childNodeGoesNull.html'); $readability = $this->getReadability($html, 'http://0.0.0.0'); - $readability->debug = true; $readability->convertLinksToFootnotes = true; $res = $readability->init(); @@ -485,7 +467,6 @@ public function testKeepFootnotes() $html = file_get_contents('tests/fixtures/keepFootnotes.html'); $readability = $this->getReadability($html, 'http://0.0.0.0'); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); @@ -499,13 +480,54 @@ public function testWithWipedBody() $html = file_get_contents('tests/fixtures/wipedBody.html'); $readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', false); - $readability->debug = true; $res = $readability->init(); $this->assertTrue($res); $this->assertStringContainsString('Down the Rabbit-Hole', $readability->getContent()->getInnerHtml()); } + /** + * @return array + */ + public function dataForHtmlLang() + { + return [ + 'meta' => [ + '
' . str_repeat('

This is the awesome content :)

', 7) . '
', + 'fr', + ], + 'head' => [ + 'Foo
' . str_repeat('

This is the awesome content :)

', 7) . '
', + 'fr', + ], + 'headless' => [ + '
' . str_repeat('

This is the awesome content :)

', 7) . '
', + 'fr', + // tidy would add tag. + false, + ], + 'fragment' => [ + '
' . str_repeat('

This is the awesome content :)

', 7) . '
', + '', + // tidy would add . + false, + ], + ]; + } + + /** + * @dataProvider dataForHtmlLang + */ + public function testHtmlLang($html, $lang, $useTidy = true) + { + $readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', $useTidy); + $res = $readability->init(); + + $this->assertTrue($res); + $this->assertInstanceOf(\DOMDocument::class, $readability->dom); + $this->assertSame($lang, $readability->dom->documentElement->getAttribute('lang')); + } + private function getReadability($html, $url = null, $parser = 'libxml', $useTidy = true) { $readability = new Readability($html, $url, $parser, $useTidy);