|
|
|
@@ -4,27 +4,34 @@ namespace Tests\Readability;
|
|
|
|
|
|
|
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Readability\JSLikeHTMLElement;
|
|
|
|
|
use Readability\Readability;
|
|
|
|
|
|
|
|
|
|
class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
{
|
|
|
|
|
/** @var TestHandler */
|
|
|
|
|
public $logHandler;
|
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
public $logger;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @requires extension tidy
|
|
|
|
|
*/
|
|
|
|
|
public function testConstructDefault()
|
|
|
|
|
public function testConstructDefault(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('');
|
|
|
|
|
$this->assertSame('utf8', $readability->tidy_config['input-encoding']);
|
|
|
|
|
$readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertNull($readability->url);
|
|
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructHtml5Parser()
|
|
|
|
|
public function testConstructHtml5Parser(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'html5lib');
|
|
|
|
|
$readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertSame('http://0.0.0.0', $readability->url);
|
|
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
|
|
|
@@ -34,9 +41,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
/**
|
|
|
|
|
* @requires extension tidy
|
|
|
|
|
*/
|
|
|
|
|
public function testConstructSimple()
|
|
|
|
|
public function testConstructSimple(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
|
|
|
|
$readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertSame('http://0.0.0.0', $readability->url);
|
|
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
|
|
|
@@ -44,9 +52,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertTrue($readability->tidied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructDefaultWithoutTidy()
|
|
|
|
|
public function testConstructDefaultWithoutTidy(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('', null, 'libxml', false);
|
|
|
|
|
$readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertNull($readability->url);
|
|
|
|
|
$this->assertSame('', $readability->original_html);
|
|
|
|
@@ -55,9 +64,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructSimpleWithoutTidy()
|
|
|
|
|
public function testConstructSimpleWithoutTidy(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'libxml', false);
|
|
|
|
|
$readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertSame('http://0.0.0.0', $readability->url);
|
|
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
|
|
|
@@ -65,66 +75,66 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertFalse($readability->tidied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInitNoContent()
|
|
|
|
|
public function testInitNoContent(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('Sorry, Readability was unable to parse this page for content.', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInitP()
|
|
|
|
|
public function testInitP(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability(str_repeat('<p>This is the awesome content :)</p>', 7), 'http://0.0.0.0');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInitDivP()
|
|
|
|
|
public function testInitDivP(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<div>' . str_repeat('<p>This is the awesome content :)</p>', 7) . '</div>', 'http://0.0.0.0');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInitDiv()
|
|
|
|
|
public function testInitDiv(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<div>' . str_repeat('This is the awesome content :)', 7) . '</div>', 'http://0.0.0.0');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithFootnotes()
|
|
|
|
|
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->convertLinksToFootnotes = true;
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
@@ -132,15 +142,15 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertStringContainsString('readabilityLink-3', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testStandardClean()
|
|
|
|
|
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->lightClean = false;
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
@@ -148,178 +158,175 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertStringNotContainsString('<h2>', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithIframe()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('nofollow', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithArticle()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('nofollow', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithAside()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('<aside>', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('<footer readability="5"/>', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('<footer readability="9"/>', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithClasses()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text should be removed', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithClassesWithoutLightClean()
|
|
|
|
|
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->lightClean = false;
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text should be removed', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithTd()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithSameClasses()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithScript()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitle()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertSame('this is my title', $readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitleWithDash()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertSame('title2 - title3', $readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitleWithDoubleDot()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertSame('title2 : title3', $readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitleTooShortUseH1()
|
|
|
|
|
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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertSame('this is my h1 title !', $readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// dummy function to be used to the next test
|
|
|
|
|
public function error2Exception($code, $string, $file, $line)
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception($string, $code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAutoClosingIframeNotThrowingException()
|
|
|
|
|
public function testAutoClosingIframeNotThrowingException(): void
|
|
|
|
|
{
|
|
|
|
|
$oldErrorReporting = error_reporting(\E_ALL);
|
|
|
|
|
$oldDisplayErrors = ini_set('display_errors', true);
|
|
|
|
|
set_error_handler([$this, 'error2Exception']);
|
|
|
|
|
$oldDisplayErrors = ini_set('display_errors', '1');
|
|
|
|
|
// dummy function to be used to the next test
|
|
|
|
|
set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) {
|
|
|
|
|
throw new \Exception($errstr, $errno);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
@@ -344,8 +351,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>';
|
|
|
|
|
|
|
|
|
@@ -354,8 +359,8 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertStringContainsString('<iframe src="https://www.youtube.com/embed/PUep6xNeKjA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"> </iframe>', $readability->getContent()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('3D Touch', $readability->getTitle()->getInnerHtml());
|
|
|
|
|
} finally {
|
|
|
|
@@ -370,7 +375,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
/**
|
|
|
|
|
* This should generate an Exception "DOMElement::setAttribute(): ID post-60 already defined".
|
|
|
|
|
*/
|
|
|
|
|
public function testAppendIdAlreadyHere()
|
|
|
|
|
public function testAppendIdAlreadyHere(): void
|
|
|
|
|
{
|
|
|
|
|
$data = '<!DOCTYPE html>
|
|
|
|
|
<html lang="fr">
|
|
|
|
@@ -421,11 +426,11 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPostFilters()
|
|
|
|
|
public function testPostFilters(): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability('<div>' . str_repeat('<p>This <strong>is</strong> the awesome content :)</p>', 10) . '</div>', 'http://0.0.0.0');
|
|
|
|
|
$readability->addPostFilter('!<strong[^>]*>(.*?)</strong>!is', '');
|
|
|
|
@@ -436,10 +441,8 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertStringContainsString('This the awesome content :)', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPreFilters()
|
|
|
|
|
public function testPreFilters(): void
|
|
|
|
|
{
|
|
|
|
|
$this->markTestSkipped('Won\'t work until loadHtml() is moved in init() instead of __construct()');
|
|
|
|
|
|
|
|
|
|
$readability = $this->getReadability('<div>' . str_repeat('<p>This <b>is</b> the awesome and WONDERFUL content :)</p>', 7) . '</div>', 'http://0.0.0.0');
|
|
|
|
|
$readability->addPreFilter('!<b[^>]*>(.*?)</b>!is', '');
|
|
|
|
|
|
|
|
|
@@ -449,10 +452,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertStringContainsString('This the awesome and WONDERFUL content :)', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testChildNodeGoneNull()
|
|
|
|
|
public function testChildNodeGoneNull(): void
|
|
|
|
|
{
|
|
|
|
|
// from http://www.ayyaantuu.net/ethiopia-targets-opposition-lawmakers/
|
|
|
|
|
$html = 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->convertLinksToFootnotes = true;
|
|
|
|
@@ -461,10 +464,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testKeepFootnotes()
|
|
|
|
|
public function testKeepFootnotes(): void
|
|
|
|
|
{
|
|
|
|
|
// from https://www.schreibdichte.de/blog/feed-aggregator-und-spaeter-lesen-dienst-im-team
|
|
|
|
|
$html = 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');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
@@ -474,10 +477,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertStringContainsString('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWithWipedBody()
|
|
|
|
|
public function testWithWipedBody(): void
|
|
|
|
|
{
|
|
|
|
|
// from https://www.cs.cmu.edu/~rgs/alice-table.html
|
|
|
|
|
$html = 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);
|
|
|
|
|
$res = $readability->init();
|
|
|
|
@@ -486,16 +489,57 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertStringContainsString('<a href="alice-I.html">Down the Rabbit-Hole</a>', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dataForVisibleNode(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'visible node' => [
|
|
|
|
|
'<div>' . str_repeat('<p>This <b>is</b> the awesome and WONDERFUL content :)</p>', 7) . '</div>',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
'display=none' => [
|
|
|
|
|
'<div style="display:none;">' . str_repeat('<p>This <b>is</b> the awesome and WONDERFUL content :)</p>', 7) . '</div>',
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
'display=inline' => [
|
|
|
|
|
'<div style="display:inline;">' . str_repeat('<p>This <b>is</b> the awesome and WONDERFUL content :)</p>', 7) . '</div>',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
'hidden attribute' => [
|
|
|
|
|
'<div hidden>' . str_repeat('<p>This <b>is</b> the awesome and WONDERFUL content :)</p>', 7) . '</div>',
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
'missing display' => [
|
|
|
|
|
'<div style="color:#ccc;">' . str_repeat('<p>This <b>is</b> the awesome and WONDERFUL content :)</p>', 7) . '</div>',
|
|
|
|
|
true,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataForVisibleNode
|
|
|
|
|
*/
|
|
|
|
|
public function testVisibleNode(string $content, bool $shouldBeVisible): void
|
|
|
|
|
{
|
|
|
|
|
$readability = $this->getReadability($content, 'http://0.0.0.0');
|
|
|
|
|
$res = $readability->init();
|
|
|
|
|
|
|
|
|
|
if ($shouldBeVisible) {
|
|
|
|
|
$this->assertStringContainsString('WONDERFUL content', $readability->getContent()->getInnerHtml());
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertStringNotContainsString('WONDERFUL content', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://github.com/wallabag/wallabag/issues/8158
|
|
|
|
|
public function testCharsetAfterTitle()
|
|
|
|
|
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->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
|
|
|
|
$this->assertSame('Tõde ja õigus I', $readability->getTitle()->getInnerHtml());
|
|
|
|
|
$this->assertStringContainsString('Päike lähenes', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
@@ -503,7 +547,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
/**
|
|
|
|
|
* @return array<string, array{0: string, 1: string, 2?: bool}>
|
|
|
|
|
*/
|
|
|
|
|
public function dataForHtmlLang()
|
|
|
|
|
public function dataForHtmlLang(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'meta' => [
|
|
|
|
@@ -532,7 +576,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataForHtmlLang
|
|
|
|
|
*/
|
|
|
|
|
public function testHtmlLang($html, $lang, $useTidy = true)
|
|
|
|
|
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();
|
|
|
|
@@ -540,11 +584,11 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
|
|
|
|
$this->assertSame($lang, $readability->dom->documentElement->getAttribute('lang'));
|
|
|
|
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
|
|
|
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
|
|
|
|
$this->assertStringContainsString('êtres', $readability->getContent()->getInnerHtml());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getReadability($html, $url = null, $parser = 'libxml', $useTidy = true)
|
|
|
|
|
private function getReadability(string $html, ?string $url = null, string $parser = 'libxml', bool $useTidy = true): Readability
|
|
|
|
|
{
|
|
|
|
|
$readability = new Readability($html, $url, $parser, $useTidy);
|
|
|
|
|
|
|
|
|
|