mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6427dd7371 | ||
|
|
613a63c062 | ||
|
|
05089bbd03 | ||
|
|
5ac82f3c87 | ||
|
|
85fb92a042 | ||
|
|
f2a43b476c | ||
|
|
6def743902 | ||
|
|
8a44926392 | ||
|
|
8c7740f073 | ||
|
|
6a4720c951 | ||
|
|
7a9a82b543 | ||
|
|
8a91d36294 | ||
|
|
669adfb20f |
+1
-1
@@ -46,7 +46,7 @@ before_script:
|
|||||||
|
|
||||||
script:
|
script:
|
||||||
- mkdir -p build/logs
|
- mkdir -p build/logs
|
||||||
- phpunit -v --coverage-clover build/logs/clover.xml
|
- php vendor/bin/simple-phpunit -v --coverage-clover build/logs/clover.xml
|
||||||
- if [ "$CS_FIXER" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi;
|
- if [ "$CS_FIXER" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi;
|
||||||
|
|
||||||
after_script:
|
after_script:
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ The default php-readability lib is really old and needs to be improved. I found
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
By default, this lib will use the [Tidy extension](https://github.com/htacg/tidy-html5) if it's available. Tidy is only used to cleanup the given HTML and avoid problems with bad HTML structure, etc ..
|
By default, this lib will use the [Tidy extension](https://github.com/htacg/tidy-html5) if it's available. Tidy is only used to cleanup the given HTML and avoid problems with bad HTML structure, etc .. It'll be suggested by Composer.
|
||||||
|
|
||||||
Since Composer doesn't support suggestion on PHP extension, I write this suggestion here.
|
Also, if you got problem from parsing a content without Tidy installed, please install it and try again.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|||||||
+8
-2
@@ -25,12 +25,18 @@
|
|||||||
}],
|
}],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.3",
|
"php": ">=5.3.3",
|
||||||
"psr/log": "^1.0"
|
"ext-mbstring": "*",
|
||||||
|
"psr/log": "^1.0",
|
||||||
|
"electrolinux/php-html5lib": "^0.1.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"satooshi/php-coveralls": "~0.6",
|
"satooshi/php-coveralls": "~0.6",
|
||||||
"friendsofphp/php-cs-fixer": "<2",
|
"friendsofphp/php-cs-fixer": "<2",
|
||||||
"monolog/monolog": "^1.13"
|
"monolog/monolog": "^1.13",
|
||||||
|
"symfony/phpunit-bridge": "^3.2"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": { "Readability\\": "src/" }
|
"psr-4": { "Readability\\": "src/" }
|
||||||
|
|||||||
+20
-13
@@ -45,19 +45,31 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
*/
|
*/
|
||||||
public function __set($name, $value)
|
public function __set($name, $value)
|
||||||
{
|
{
|
||||||
if ($name === 'innerHTML') {
|
if ($name !== 'innerHTML') {
|
||||||
|
$trace = debug_backtrace();
|
||||||
|
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// first, empty the element
|
// first, empty the element
|
||||||
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
||||||
$this->removeChild($this->childNodes->item($x));
|
$this->removeChild($this->childNodes->item($x));
|
||||||
}
|
}
|
||||||
|
|
||||||
// $value holds our new inner HTML
|
// $value holds our new inner HTML
|
||||||
if ($value !== '') {
|
$value = trim($value);
|
||||||
|
if (empty($value)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure bad entity won't generate warning
|
||||||
|
$previousError = libxml_use_internal_errors(true);
|
||||||
|
|
||||||
$f = $this->ownerDocument->createDocumentFragment();
|
$f = $this->ownerDocument->createDocumentFragment();
|
||||||
|
|
||||||
// appendXML() expects well-formed markup (XHTML)
|
// appendXML() expects well-formed markup (XHTML)
|
||||||
// @ to suppress PHP warnings
|
$result = $f->appendXML($value);
|
||||||
$result = @$f->appendXML($value);
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
if ($f->hasChildNodes()) {
|
if ($f->hasChildNodes()) {
|
||||||
$this->appendChild($f);
|
$this->appendChild($f);
|
||||||
@@ -72,7 +84,7 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
// We use it (and suppress the warning) because an HTML fragment will
|
// We use it (and suppress the warning) because an HTML fragment will
|
||||||
// be wrapped around <html><body> tags which we don't really want to keep.
|
// be wrapped around <html><body> tags which we don't really want to keep.
|
||||||
// Note: despite the warning, if loadHTML succeeds it will return true.
|
// Note: despite the warning, if loadHTML succeeds it will return true.
|
||||||
$result = @$f->loadHTML('<htmlfragment>' . $value . '</htmlfragment>');
|
$result = $f->loadHTML('<htmlfragment>' . $value . '</htmlfragment>');
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
||||||
@@ -81,16 +93,11 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
$importedNode = $this->ownerDocument->importNode($child, true);
|
$importedNode = $this->ownerDocument->importNode($child, true);
|
||||||
$this->appendChild($importedNode);
|
$this->appendChild($importedNode);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// oh well, we tried, we really did. :(
|
|
||||||
// this element is now empty
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
libxml_clear_errors();
|
||||||
$trace = debug_backtrace();
|
libxml_use_internal_errors($previousError);
|
||||||
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+2
-1
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Readability;
|
namespace Readability;
|
||||||
|
|
||||||
|
use HTML5Lib\Parser;
|
||||||
use Psr\Log\LoggerAwareInterface;
|
use Psr\Log\LoggerAwareInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Psr\Log\NullLogger;
|
use Psr\Log\NullLogger;
|
||||||
@@ -284,7 +285,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
$this->html = mb_convert_encoding($this->html, 'HTML-ENTITIES', 'UTF-8');
|
$this->html = mb_convert_encoding($this->html, 'HTML-ENTITIES', 'UTF-8');
|
||||||
|
|
||||||
if (!($this->parser === 'html5lib' && ($this->dom = \HTML5_Parser::parse($this->html)))) {
|
if (!($this->parser === 'html5lib' && ($this->dom = Parser::parse($this->html)))) {
|
||||||
libxml_use_internal_errors(true);
|
libxml_use_internal_errors(true);
|
||||||
|
|
||||||
$this->dom = new \DOMDocument();
|
$this->dom = new \DOMDocument();
|
||||||
|
|||||||
@@ -33,6 +33,15 @@ class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConstructHtml5Parser()
|
||||||
|
{
|
||||||
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'html5lib');
|
||||||
|
|
||||||
|
$this->assertEquals('http://0.0.0.0', $readability->url);
|
||||||
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
|
$this->assertEquals('<html/>', $readability->original_html);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @requires extension tidy
|
* @requires extension tidy
|
||||||
*/
|
*/
|
||||||
@@ -326,13 +335,6 @@ class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertNotContains('This text is also an awesome text and you should know that', $readability->getContent()->innerHTML);
|
$this->assertNotContains('This text is also an awesome text and you should know that', $readability->getContent()->innerHTML);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public function testConstructParser()
|
|
||||||
// {
|
|
||||||
// $readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'html5lib');
|
|
||||||
|
|
||||||
// $this->assertEquals('http://0.0.0.0', $readability->url);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// dummy function to be used to the next test
|
// dummy function to be used to the next test
|
||||||
public function error2Exception($code, $string, $file, $line, $context)
|
public function error2Exception($code, $string, $file, $line, $context)
|
||||||
{
|
{
|
||||||
@@ -443,7 +445,7 @@ class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testPostFilters()
|
public function testPostFilters()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div>' . str_repeat('<p>This <b>is</b> the awesome content :)</p>', 7) . '</div>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<div>' . str_repeat('<p>This <b>is</b> the awesome content :)</p>', 10) . '</div>', 'http://0.0.0.0');
|
||||||
$readability->addPostFilter('!<strong[^>]*>(.*?)</strong>!is', '');
|
$readability->addPostFilter('!<strong[^>]*>(.*?)</strong>!is', '');
|
||||||
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|||||||
Reference in New Issue
Block a user