mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 14:36:23 +00:00
Compare commits
9
Commits
2.0.5
...
3042990efc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3042990efc | ||
|
|
8b89d70b1a | ||
|
|
3e9b15db46 | ||
|
|
7413a38ff0 | ||
|
|
a18cd0f2a9 | ||
|
|
efbbc86df9 | ||
|
|
541fab34a0 | ||
|
|
90869d877e | ||
|
|
c7208f6ad2 |
+22
-2
@@ -1383,7 +1383,10 @@ class Readability implements LoggerAwareInterface
|
|||||||
$this->logger->debug('Parsing URL: ' . $this->url);
|
$this->logger->debug('Parsing URL: ' . $this->url);
|
||||||
|
|
||||||
if ($this->url) {
|
if ($this->url) {
|
||||||
$this->domainRegExp = '/' . strtr((string) preg_replace('/www\d*\./', '', (string) parse_url($this->url, \PHP_URL_HOST)), ['.' => '\.']) . '/';
|
$host = parse_url($this->url, \PHP_URL_HOST);
|
||||||
|
if (null !== $host) {
|
||||||
|
$this->domainRegExp = '/' . strtr((string) preg_replace('/www\d*\./', '', $host), ['.' => '\.']) . '/';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mb_internal_encoding('UTF-8');
|
mb_internal_encoding('UTF-8');
|
||||||
@@ -1419,7 +1422,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
unset($tidy);
|
unset($tidy);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->html = '<meta charset="utf-8">' . (string) $this->html;
|
$this->html = self::entitizeNonAscii((string) $this->html);
|
||||||
|
|
||||||
if ('html5lib' === $this->parser || 'html5' === $this->parser) {
|
if ('html5lib' === $this->parser || 'html5' === $this->parser) {
|
||||||
$this->dom = (new HTML5())->loadHTML($this->html);
|
$this->dom = (new HTML5())->loadHTML($this->html);
|
||||||
@@ -1507,4 +1510,21 @@ class Readability implements LoggerAwareInterface
|
|||||||
)
|
)
|
||||||
&& !$node->hasAttribute('hidden');
|
&& !$node->hasAttribute('hidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts non-ASCII UTF-8 characters to numeric HTML entities.
|
||||||
|
*
|
||||||
|
* `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.
|
||||||
|
*
|
||||||
|
* @param string $html UTF-8 encoded document
|
||||||
|
*/
|
||||||
|
private static function entitizeNonAscii(string $html): string
|
||||||
|
{
|
||||||
|
$convmap = [
|
||||||
|
0x80, 0x1FFFFF, 0, 0x10FFFF,
|
||||||
|
];
|
||||||
|
|
||||||
|
return mb_encode_numericentity($html, $convmap, 'utf8', true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+63
-26
@@ -24,7 +24,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$readability->init();
|
$readability->init();
|
||||||
|
|
||||||
$this->assertNull($readability->url);
|
$this->assertNull($readability->url);
|
||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructHtml5Parser(): void
|
public function testConstructHtml5Parser(): void
|
||||||
@@ -33,7 +33,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$readability->init();
|
$readability->init();
|
||||||
|
|
||||||
$this->assertSame('http://0.0.0.0', $readability->url);
|
$this->assertSame('http://0.0.0.0', $readability->url);
|
||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||||
$this->assertSame('<html/>', $readability->original_html);
|
$this->assertSame('<html/>', $readability->original_html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$readability->init();
|
$readability->init();
|
||||||
|
|
||||||
$this->assertSame('http://0.0.0.0', $readability->url);
|
$this->assertSame('http://0.0.0.0', $readability->url);
|
||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||||
$this->assertSame('<html/>', $readability->original_html);
|
$this->assertSame('<html/>', $readability->original_html);
|
||||||
$this->assertTrue($readability->tidied);
|
$this->assertTrue($readability->tidied);
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertSame('', $readability->original_html);
|
$this->assertSame('', $readability->original_html);
|
||||||
$this->assertFalse($readability->tidied);
|
$this->assertFalse($readability->tidied);
|
||||||
|
|
||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructSimpleWithoutTidy(): void
|
public function testConstructSimpleWithoutTidy(): void
|
||||||
@@ -69,7 +69,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$readability->init();
|
$readability->init();
|
||||||
|
|
||||||
$this->assertSame('http://0.0.0.0', $readability->url);
|
$this->assertSame('http://0.0.0.0', $readability->url);
|
||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||||
$this->assertSame('<html/>', $readability->original_html);
|
$this->assertSame('<html/>', $readability->original_html);
|
||||||
$this->assertFalse($readability->tidied);
|
$this->assertFalse($readability->tidied);
|
||||||
}
|
}
|
||||||
@@ -115,7 +115,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testInitDiv(): void
|
public function testInitDiv(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div>' . str_repeat('This is the awesome content :)', 7) . '</div>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<div>' . str_repeat('This is the awesome content :)', 7) . '</div>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -129,7 +128,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithFootnotes(): void
|
public function testWithFootnotes(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '</div>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<div>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '</div>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$readability->convertLinksToFootnotes = true;
|
$readability->convertLinksToFootnotes = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
@@ -146,7 +144,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testStandardClean(): void
|
public function testStandardClean(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div><h2>Title</h2>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<a href="#nofollow" rel="nofollow">will NOT be removed</a></div>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<div><h2>Title</h2>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<a href="#nofollow" rel="nofollow">will NOT be removed</a></div>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$readability->lightClean = false;
|
$readability->lightClean = false;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
@@ -163,7 +160,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithIframe(): void
|
public function testWithIframe(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div><h2>Title</h2>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<p>This is an awesome text with some links, here there are <iframe src="http://youtube.com/test" href="#nofollow" rel="nofollow"></iframe><iframe>http://soundcloud.com/test</iframe></p></div>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<div><h2>Title</h2>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<p>This is an awesome text with some links, here there are <iframe src="http://youtube.com/test" href="#nofollow" rel="nofollow"></iframe><iframe>http://soundcloud.com/test</iframe></p></div>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -178,7 +174,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithArticle(): void
|
public function testWithArticle(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<article><p>' . str_repeat('This is an awesome text with some links, here there are: the awesome', 20) . '</p><p>This is an awesome text with some links, here there are <iframe src="http://youtube.com/test" href="#nofollow" rel="nofollow"></iframe></p></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<article><p>' . str_repeat('This is an awesome text with some links, here there are: the awesome', 20) . '</p><p>This is an awesome text with some links, here there are <iframe src="http://youtube.com/test" href="#nofollow" rel="nofollow"></iframe></p></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -193,7 +188,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithAside(): void
|
public function testWithAside(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<article>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<footer><aside>' . str_repeat('<p>This is an awesome text with some links, here there are</p>', 8) . '</aside></footer></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<article>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<footer><aside>' . str_repeat('<p>This is an awesome text with some links, here there are</p>', 8) . '</aside></footer></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -208,7 +202,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithClasses(): void
|
public function testWithClasses(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<article>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<div style="display:none">' . str_repeat('<p class="clock">This text should be removed</p>', 10) . '</div></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<article>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<div style="display:none">' . str_repeat('<p class="clock">This text should be removed</p>', 10) . '</div></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -223,7 +216,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithClassesWithoutLightClean(): void
|
public function testWithClassesWithoutLightClean(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<article>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<div style="display:none">' . str_repeat('<p class="clock">This text should be removed</p>', 10) . '</div></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<article>' . str_repeat('<p>This is an awesome text with some links, here there are: <a href="http://0.0.0.0/test.html">the awesome</a></p>', 7) . '<div style="display:none">' . str_repeat('<p class="clock">This text should be removed</p>', 10) . '</div></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$readability->lightClean = false;
|
$readability->lightClean = false;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
@@ -239,7 +231,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithTd(): void
|
public function testWithTd(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<table><tr>' . str_repeat('<td><p>This is an awesome text with some links, here there are the awesome</td>', 7) . '</tr></table>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<table><tr>' . str_repeat('<td><p>This is an awesome text with some links, here there are the awesome</td>', 7) . '</tr></table>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -252,7 +243,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithSameClasses(): void
|
public function testWithSameClasses(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<div class="awesomecontent">This text is also an awesome text and you should know that !</div></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<div class="awesomecontent">This text is also an awesome text and you should know that !</div></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -266,7 +256,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testWithScript(): void
|
public function testWithScript(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p><script>This text is also an awesome text and you should know that !</script></p></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p><script>This text is also an awesome text and you should know that !</script></p></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -280,7 +269,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testTitle(): void
|
public function testTitle(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<title>this is my title</title><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<title>this is my title</title><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -294,7 +282,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testTitleWithDash(): void
|
public function testTitleWithDash(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<title> title2 - title3 </title><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<title> title2 - title3 </title><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -308,7 +295,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testTitleWithDoubleDot(): void
|
public function testTitleWithDoubleDot(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<title> title2 : title3 </title><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<title> title2 : title3 </title><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -322,7 +308,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testTitleTooShortUseH1(): void
|
public function testTitleTooShortUseH1(): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<title>too short</title><h1>this is my h1 title !</h1><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<title>too short</title><h1>this is my h1 title !</h1><article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<p></p></article>', 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -369,7 +354,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
</html>';
|
</html>';
|
||||||
|
|
||||||
$readability = $this->getReadability($data, 'http://iosgames.ru/?p=22030');
|
$readability = $this->getReadability($data, 'http://iosgames.ru/?p=22030');
|
||||||
$readability->debug = true;
|
|
||||||
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
@@ -437,7 +421,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
</html>';
|
</html>';
|
||||||
|
|
||||||
$readability = $this->getReadability($data, 'http://0.0.0.0');
|
$readability = $this->getReadability($data, 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
@@ -474,7 +457,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$html = (string) file_get_contents('tests/fixtures/childNodeGoesNull.html');
|
$html = (string) file_get_contents('tests/fixtures/childNodeGoesNull.html');
|
||||||
|
|
||||||
$readability = $this->getReadability($html, 'http://0.0.0.0');
|
$readability = $this->getReadability($html, 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$readability->convertLinksToFootnotes = true;
|
$readability->convertLinksToFootnotes = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
@@ -487,7 +469,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$html = (string) file_get_contents('tests/fixtures/keepFootnotes.html');
|
$html = (string) file_get_contents('tests/fixtures/keepFootnotes.html');
|
||||||
|
|
||||||
$readability = $this->getReadability($html, 'http://0.0.0.0');
|
$readability = $this->getReadability($html, 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -501,7 +482,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$html = (string) file_get_contents('tests/fixtures/wipedBody.html');
|
$html = (string) file_get_contents('tests/fixtures/wipedBody.html');
|
||||||
|
|
||||||
$readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', false);
|
$readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', false);
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
@@ -540,7 +520,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
public function testVisibleNode(string $content, bool $shouldBeVisible): void
|
public function testVisibleNode(string $content, bool $shouldBeVisible): void
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability($content, 'http://0.0.0.0');
|
$readability = $this->getReadability($content, 'http://0.0.0.0');
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
if ($shouldBeVisible) {
|
if ($shouldBeVisible) {
|
||||||
@@ -550,6 +529,64 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/wallabag/wallabag/issues/8158
|
||||||
|
public function testCharsetAfterTitle(): void
|
||||||
|
{
|
||||||
|
$readability = $this->getReadability('<!DOCTYPE html><html lang="et"><head><title>Tõde ja õigus I</title> <meta charset="utf-8"></head><body><p>See oli läinud aastasaja kolmanda veerandi lõpul. Päike lähenes silmapiirile, seistes sedavõrd madalas, et enam ei ulatunud valgustama ei mäkke ronivat hobust, kes puutelgedega vankrit vedas, ei vankril istuvat noort naist ega ka ligi kolmekümnelist meest, kes kõndis vankri kõrval.</p></body></html>', 'https://et.wikisource.org/wiki/T%C3%B5de_ja_%C3%B5igus_I/I');
|
||||||
|
$readability->convertLinksToFootnotes = true;
|
||||||
|
$res = $readability->init();
|
||||||
|
|
||||||
|
$this->assertTrue($res);
|
||||||
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
|
$this->assertSame('Tõde ja õigus I', $readability->getTitle()->getInnerHtml());
|
||||||
|
$this->assertStringContainsString('Päike lähenes', $readability->getContent()->getInnerHtml());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, array{0: string, 1: string, 2?: bool}>
|
||||||
|
*/
|
||||||
|
public function dataForHtmlLang(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'meta' => [
|
||||||
|
'<html lang="fr"><head><meta charset="utf-8"></head><body><article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article></body></html>',
|
||||||
|
'fr',
|
||||||
|
],
|
||||||
|
'head' => [
|
||||||
|
'<html lang="fr"><head><title>Foo</title></head><body><article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article></body></html>',
|
||||||
|
'fr',
|
||||||
|
],
|
||||||
|
'headless' => [
|
||||||
|
'<html lang="fr"><body><article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article></body></html>',
|
||||||
|
'fr',
|
||||||
|
// tidy would add <head> tag.
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'fragment' => [
|
||||||
|
'<article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article>',
|
||||||
|
'',
|
||||||
|
// tidy would add <html>.
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataForHtmlLang
|
||||||
|
*/
|
||||||
|
public function testHtmlLang(string $html, string $lang, bool $useTidy = true): void
|
||||||
|
{
|
||||||
|
$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'));
|
||||||
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
|
$this->assertStringContainsString('êtres', $readability->getContent()->getInnerHtml());
|
||||||
|
}
|
||||||
|
|
||||||
private function getReadability(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability
|
private function getReadability(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability
|
||||||
{
|
{
|
||||||
$readability = new Readability($html, $url, $parser, $useTidy);
|
$readability = new Readability($html, $url, $parser, $useTidy);
|
||||||
|
|||||||
Reference in New Issue
Block a user