Replaces subscribing feeds through a self-hosted Full-Text RSS proxy. Feed URLs go back to being real feed URLs and extraction happens inside tt-rss, using the same ftr-site-config rules the proxy used. The engine in lib/ has no tt-rss dependencies, so rules can be developed and audited from the command line; init.php is a thin adapter over it. Two findings from measuring the real subscription first, both of which shaped the design: - Firecrawl's own onlyMainContent is far too coarse to extract with (73KB of chrome on a Cloudflare post), but it is an excellent renderer. So it is used for rawHtml only and the rule engine does the extraction. - A body rule that stops matching after a redesign falls through to Readability and still produces a plausible article, so the breakage is invisible. Every extraction now records which rule matched and whether it fell back; auditing the 34 live feeds surfaced five community rules that match nothing. Custom rules included for the sites that needed them, including three comics where the article is an image and text-scoring extractors return the wrong thing or nothing at all. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011sSJdftQx5bW5HZHgUKF3i
137 lines
4.0 KiB
PHP
137 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Masterminds\HTML5\Tests\Serializer;
|
|
|
|
use Masterminds\HTML5\Serializer\OutputRules;
|
|
use Masterminds\HTML5\Serializer\Traverser;
|
|
|
|
class TraverserTest extends \Masterminds\HTML5\Tests\TestCase
|
|
{
|
|
protected $markup = '<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Test</title>
|
|
</head>
|
|
<body>
|
|
<p>This is a test.</p>
|
|
</body>
|
|
</html>';
|
|
|
|
public function setUp()
|
|
{
|
|
$this->html5 = $this->getInstance();
|
|
}
|
|
|
|
/**
|
|
* Using reflection we make a protected method accessible for testing.
|
|
*
|
|
* @param string $name
|
|
* The name of the method on the Traverser class to test
|
|
*
|
|
* @return \ReflectionMethod \ReflectionMethod for the specified method
|
|
*/
|
|
public function getProtectedMethod($name)
|
|
{
|
|
$class = new \ReflectionClass('\Masterminds\HTML5\Serializer\Traverser');
|
|
$method = $class->getMethod($name);
|
|
$method->setAccessible(true);
|
|
|
|
return $method;
|
|
}
|
|
|
|
public function getTraverser()
|
|
{
|
|
$stream = fopen('php://temp', 'w');
|
|
|
|
$dom = $this->html5->loadHTML($this->markup);
|
|
$t = new Traverser($dom, $stream, $html5->getOptions());
|
|
|
|
// We return both the traverser and stream so we can pull from it.
|
|
return array(
|
|
$t,
|
|
$stream,
|
|
);
|
|
}
|
|
|
|
public function testConstruct()
|
|
{
|
|
// The traverser needs a place to write the output to. In our case we
|
|
// use a stream in temp space.
|
|
$stream = fopen('php://temp', 'w');
|
|
|
|
$html5 = $this->getInstance();
|
|
|
|
$r = new OutputRules($stream, $this->html5->getOptions());
|
|
$dom = $this->html5->loadHTML($this->markup);
|
|
|
|
$t = new Traverser($dom, $stream, $r, $html5->getOptions());
|
|
|
|
$this->assertInstanceOf('\Masterminds\HTML5\Serializer\Traverser', $t);
|
|
}
|
|
|
|
public function testFragmentDeprecated()
|
|
{
|
|
$html = '<span class="bar">foo</span><span></span><div>bar</div>';
|
|
$input = new \Masterminds\HTML5\Parser\StringInputStream($html);
|
|
$dom = $this->html5->parseFragment($input);
|
|
|
|
$this->assertInstanceOf('\DOMDocumentFragment', $dom);
|
|
|
|
$stream = fopen('php://temp', 'w');
|
|
$r = new OutputRules($stream, $this->html5->getOptions());
|
|
$t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
|
|
$t->walk();
|
|
|
|
$this->assertEquals($html, stream_get_contents($stream, -1, 0));
|
|
}
|
|
|
|
public function testFragment()
|
|
{
|
|
$html = '<span class="bar">foo</span><span></span><div>bar</div>';
|
|
$dom = $this->html5->parseFragment($html);
|
|
|
|
$this->assertInstanceOf('\DOMDocumentFragment', $dom);
|
|
|
|
$stream = fopen('php://temp', 'w');
|
|
$r = new OutputRules($stream, $this->html5->getOptions());
|
|
$t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
|
|
$t->walk();
|
|
|
|
$this->assertEquals($html, stream_get_contents($stream, -1, 0));
|
|
}
|
|
|
|
public function testProcessorInstructionDeprecated()
|
|
{
|
|
$html = '<?foo bar ?>';
|
|
$input = new \Masterminds\HTML5\Parser\StringInputStream($html);
|
|
$dom = $this->html5->parseFragment($input);
|
|
|
|
$this->assertInstanceOf('\DOMDocumentFragment', $dom);
|
|
|
|
$stream = fopen('php://temp', 'w');
|
|
$r = new OutputRules($stream, $this->html5->getOptions());
|
|
|
|
$t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
|
|
$t->walk();
|
|
|
|
$this->assertEquals($html, stream_get_contents($stream, -1, 0));
|
|
}
|
|
|
|
public function testProcessorInstruction()
|
|
{
|
|
$html = '<?foo bar ?>';
|
|
$dom = $this->html5->parseFragment($html);
|
|
|
|
$this->assertInstanceOf('\DOMDocumentFragment', $dom);
|
|
|
|
$stream = fopen('php://temp', 'w');
|
|
$r = new OutputRules($stream, $this->html5->getOptions());
|
|
|
|
$t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
|
|
$t->walk();
|
|
|
|
$this->assertEquals($html, stream_get_contents($stream, -1, 0));
|
|
}
|
|
}
|