mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
487ce3a517 | ||
|
|
0bae41248f | ||
|
|
ae0c20ab18 | ||
|
|
84220bff99 | ||
|
|
2ae758555a | ||
|
|
f5e25f3c9c | ||
|
|
a209429e8b | ||
|
|
8421ed5962 | ||
|
|
66864279fd | ||
|
|
ae87c8ca8c | ||
|
|
4adb0a96aa | ||
|
|
baa0a78ab6 | ||
|
|
2112dd95d1 | ||
|
|
f4550ccc46 | ||
|
|
6f4404030b | ||
|
|
5638357306 | ||
|
|
651e8a6bb0 |
@@ -6,6 +6,5 @@
|
|||||||
/.php_cs export-ignore
|
/.php_cs export-ignore
|
||||||
/phpunit.xml.dist export-ignore
|
/phpunit.xml.dist export-ignore
|
||||||
/phpstan.neon export-ignore
|
/phpstan.neon export-ignore
|
||||||
/rector.php export-ignore
|
|
||||||
/.github export-ignore
|
/.github export-ignore
|
||||||
/tests export-ignore
|
/tests export-ignore
|
||||||
|
|||||||
@@ -4,14 +4,19 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- 1.x
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- 1.x
|
||||||
|
|
||||||
|
env:
|
||||||
|
SYMFONY_PHPUNIT_VERSION: 7.5
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
coding-standards:
|
coding-standards:
|
||||||
name: "CS Fixer & PHPStan"
|
name: "CS Fixer & PHPStan"
|
||||||
runs-on: "ubuntu-20.04"
|
runs-on: "ubuntu-22.04"
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -20,7 +25,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v4"
|
||||||
|
|
||||||
- name: "Install PHP"
|
- name: "Install PHP"
|
||||||
uses: "shivammathur/setup-php@v2"
|
uses: "shivammathur/setup-php@v2"
|
||||||
@@ -32,8 +37,11 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: "Add PHPStan"
|
||||||
|
run: "composer require phpstan/phpstan phpstan/phpstan-phpunit --dev --no-progress --no-suggest"
|
||||||
|
|
||||||
- name: "Install dependencies with Composer"
|
- name: "Install dependencies with Composer"
|
||||||
uses: "ramsey/composer-install@v1"
|
uses: "ramsey/composer-install@v3"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- "master"
|
- "master"
|
||||||
|
- "1.x"
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- "master"
|
- "master"
|
||||||
|
- "1.x"
|
||||||
|
|
||||||
env:
|
env:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
@@ -14,20 +16,26 @@ env:
|
|||||||
jobs:
|
jobs:
|
||||||
phpunit:
|
phpunit:
|
||||||
name: "PHPUnit (PHP ${{ matrix.php }})"
|
name: "PHPUnit (PHP ${{ matrix.php }})"
|
||||||
runs-on: "ubuntu-20.04"
|
runs-on: "ubuntu-22.04"
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php:
|
php:
|
||||||
|
- "5.6"
|
||||||
|
- "7.0"
|
||||||
|
- "7.1"
|
||||||
- "7.2"
|
- "7.2"
|
||||||
- "7.3"
|
- "7.3"
|
||||||
- "7.4"
|
- "7.4"
|
||||||
- "8.0"
|
- "8.0"
|
||||||
- "8.1"
|
- "8.1"
|
||||||
|
- "8.2"
|
||||||
|
- "8.3"
|
||||||
|
- "8.4"
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v4"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -46,7 +54,7 @@ jobs:
|
|||||||
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
||||||
|
|
||||||
- name: "Install dependencies with Composer"
|
- name: "Install dependencies with Composer"
|
||||||
uses: "ramsey/composer-install@v1"
|
uses: "ramsey/composer-install@v3"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
@@ -58,7 +66,7 @@ jobs:
|
|||||||
|
|
||||||
phpunit-coverage:
|
phpunit-coverage:
|
||||||
name: "PHPUnit coverage (PHP ${{ matrix.php }})"
|
name: "PHPUnit coverage (PHP ${{ matrix.php }})"
|
||||||
runs-on: "ubuntu-20.04"
|
runs-on: "ubuntu-22.04"
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -67,7 +75,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v4"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -86,7 +94,7 @@ jobs:
|
|||||||
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
||||||
|
|
||||||
- name: "Install dependencies with Composer"
|
- name: "Install dependencies with Composer"
|
||||||
uses: "ramsey/composer-install@v1"
|
uses: "ramsey/composer-install@v3"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
@@ -109,7 +117,7 @@ jobs:
|
|||||||
|
|
||||||
phpunit-lowest:
|
phpunit-lowest:
|
||||||
name: "PHPUnit lowest deps (PHP ${{ matrix.php }})"
|
name: "PHPUnit lowest deps (PHP ${{ matrix.php }})"
|
||||||
runs-on: "ubuntu-20.04"
|
runs-on: "ubuntu-22.04"
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -118,7 +126,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v4"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -137,7 +145,7 @@ jobs:
|
|||||||
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
||||||
|
|
||||||
- name: "Install dependencies with Composer"
|
- name: "Install dependencies with Composer"
|
||||||
uses: "ramsey/composer-install@v1"
|
uses: "ramsey/composer-install@v3"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
dependency-versions: "lowest"
|
dependency-versions: "lowest"
|
||||||
@@ -147,3 +155,43 @@ jobs:
|
|||||||
|
|
||||||
- name: "Run PHPUnit"
|
- name: "Run PHPUnit"
|
||||||
run: "php vendor/bin/simple-phpunit -v"
|
run: "php vendor/bin/simple-phpunit -v"
|
||||||
|
|
||||||
|
phpunit-composerv2:
|
||||||
|
name: "PHPUnit with Composer v1 (PHP ${{ matrix.php }})"
|
||||||
|
runs-on: "ubuntu-20.04"
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
php:
|
||||||
|
- "7.4"
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: "Checkout"
|
||||||
|
uses: "actions/checkout@v2"
|
||||||
|
with:
|
||||||
|
fetch-depth: 2
|
||||||
|
|
||||||
|
- name: "Install PHP"
|
||||||
|
uses: "shivammathur/setup-php@v2"
|
||||||
|
with:
|
||||||
|
php-version: "${{ matrix.php }}"
|
||||||
|
coverage: "none"
|
||||||
|
tools: composer:v1
|
||||||
|
extensions: tidy
|
||||||
|
ini-values: "date.timezone=Europe/Paris"
|
||||||
|
env:
|
||||||
|
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: "Remove useless deps"
|
||||||
|
run: "composer remove friendsofphp/php-cs-fixer --dev --no-progress --no-update"
|
||||||
|
|
||||||
|
- name: "Install dependencies with Composer"
|
||||||
|
uses: "ramsey/composer-install@v1"
|
||||||
|
with:
|
||||||
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
|
- name: "Setup logs"
|
||||||
|
run: "mkdir -p build/logs"
|
||||||
|
|
||||||
|
- name: "Run PHPUnit"
|
||||||
|
run: "php vendor/bin/simple-phpunit -v"
|
||||||
|
|||||||
+1
-1
@@ -2,5 +2,5 @@ vendor/
|
|||||||
coverage/
|
coverage/
|
||||||
composer.lock
|
composer.lock
|
||||||
.php_cs.cache
|
.php_cs.cache
|
||||||
.php-cs-fixer.cache
|
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
|
phpstan.neon
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ return (new PhpCsFixer\Config())
|
|||||||
'strict_comparison' => true,
|
'strict_comparison' => true,
|
||||||
'strict_param' => true,
|
'strict_param' => true,
|
||||||
'concat_space' => ['spacing' => 'one'],
|
'concat_space' => ['spacing' => 'one'],
|
||||||
|
// Pulled in by @Symfony, we cannot add property types until we bump PHP to ≥ 7.4
|
||||||
|
'no_null_property_initialization' => false,
|
||||||
|
// Pulled in by @Symfony with `const` but const visibility requires PHP ≥ 7.1
|
||||||
|
'visibility_required' => ['elements' => ['method', 'property']],
|
||||||
])
|
])
|
||||||
->setFinder($finder)
|
->setFinder($finder)
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
tools:
|
||||||
|
external_code_coverage: false
|
||||||
|
|
||||||
|
build:
|
||||||
|
nodes:
|
||||||
|
analysis:
|
||||||
|
tests:
|
||||||
|
override:
|
||||||
|
- php-scrutinizer-run
|
||||||
|
environment:
|
||||||
|
php:
|
||||||
|
version: 7.2
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
[](https://packagist.org/packages/j0k3r/php-readability)
|
[](https://packagist.org/packages/j0k3r/php-readability)
|
||||||
[](https://packagist.org/packages/j0k3r/php-readability)
|
[](https://packagist.org/packages/j0k3r/php-readability)
|
||||||
|
|
||||||
This is an extract of the Readability class from this [full-text-rss](https://github.com/Dither/full-text-rss) fork. It can be defined as a better version of the original [php-readability](https://bitbucket.org/fivefilters/php-readability).
|
This is an extract of the Readability class from this [full-text-rss](https://github.com/Dither/full-text-rss) fork. It can be defined as a better version of the original [php-readability](https://bitbucket.org/fivefilters/php-readability/overview).
|
||||||
|
|
||||||
## Differences
|
## Differences
|
||||||
|
|
||||||
|
|||||||
+7
-10
@@ -24,18 +24,15 @@
|
|||||||
"role": "Developer (original JS version)"
|
"role": "Developer (original JS version)"
|
||||||
}],
|
}],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.2.0",
|
"php": ">=5.6.0",
|
||||||
"ext-mbstring": "*",
|
"ext-mbstring": "*",
|
||||||
"psr/log": "^1.0",
|
"psr/log": "^1.0",
|
||||||
"masterminds/html5": "^2.7"
|
"masterminds/html5": "^2.7"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"friendsofphp/php-cs-fixer": "^3.0",
|
"friendsofphp/php-cs-fixer": "^2.14",
|
||||||
"monolog/monolog": "^1.24|^2.1",
|
"monolog/monolog": "^1.24|^2.1",
|
||||||
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0",
|
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0|^7.0"
|
||||||
"phpstan/phpstan": "^1.3",
|
|
||||||
"phpstan/phpstan-phpunit": "^1.0",
|
|
||||||
"rector/rector": "^0.12.15"
|
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."
|
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."
|
||||||
@@ -46,9 +43,9 @@
|
|||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": { "Tests\\Readability\\": "tests/" }
|
"psr-4": { "Tests\\Readability\\": "tests/" }
|
||||||
},
|
},
|
||||||
"config":{
|
"scripts": {
|
||||||
"platform": {
|
"fix": "php-cs-fixer fix --verbose --diff",
|
||||||
"php": "7.2.34"
|
"phpstan": "phpstan analyze --memory-limit 512M",
|
||||||
}
|
"test": "simple-phpunit -v"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,7 @@ parameters:
|
|||||||
|
|
||||||
# https://github.com/phpstan/phpstan/issues/694#issuecomment-350724288
|
# https://github.com/phpstan/phpstan/issues/694#issuecomment-350724288
|
||||||
bootstrapFiles:
|
bootstrapFiles:
|
||||||
- vendor/bin/.phpunit/phpunit-8.5-0/vendor/autoload.php
|
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
|
||||||
|
|
||||||
checkMissingIterableValueType: false
|
|
||||||
|
|
||||||
includes:
|
includes:
|
||||||
- vendor/phpstan/phpstan-phpunit/extension.neon
|
- vendor/phpstan/phpstan-phpunit/extension.neon
|
||||||
+1
-1
@@ -11,7 +11,7 @@
|
|||||||
>
|
>
|
||||||
|
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="Readability">
|
<testsuite name="Readability Test Suite">
|
||||||
<directory>./tests/</directory>
|
<directory>./tests/</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|||||||
-32
@@ -1,32 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
use Rector\Core\Configuration\Option;
|
|
||||||
use Rector\Core\ValueObject\PhpVersion;
|
|
||||||
use Rector\Set\ValueObject\LevelSetList;
|
|
||||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
|
||||||
|
|
||||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
|
||||||
$parameters = $containerConfigurator->parameters();
|
|
||||||
|
|
||||||
// paths to refactor; solid alternative to CLI arguments
|
|
||||||
$parameters->set(Option::PATHS, [
|
|
||||||
__DIR__ . '/src',
|
|
||||||
__DIR__ . '/tests',
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Path to phpstan with extensions, that PHPSTan in Rector uses to determine types
|
|
||||||
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan.neon');
|
|
||||||
|
|
||||||
$parameters->set(Option::BOOTSTRAP_FILES, [
|
|
||||||
__DIR__ . '/vendor/bin/.phpunit/phpunit-8.5-0/vendor/autoload.php',
|
|
||||||
__DIR__ . '/vendor/autoload.php',
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Define what rule sets will be applied
|
|
||||||
$containerConfigurator->import(LevelSetList::UP_TO_PHP_72);
|
|
||||||
|
|
||||||
// is your PHP version different from the one your refactor to?
|
|
||||||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_72);
|
|
||||||
};
|
|
||||||
@@ -39,9 +39,9 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
/**
|
/**
|
||||||
* Used for setting innerHTML like it's done in JavaScript:.
|
* Used for setting innerHTML like it's done in JavaScript:.
|
||||||
*
|
*
|
||||||
* @code
|
* ```php
|
||||||
* $div->innerHTML = '<h2>Chapter 2</h2><p>The story begins...</p>';
|
* $div->innerHTML = '<h2>Chapter 2</h2><p>The story begins...</p>';
|
||||||
* @endcode
|
* ```
|
||||||
*/
|
*/
|
||||||
public function __set($name, $value)
|
public function __set($name, $value)
|
||||||
{
|
{
|
||||||
@@ -79,14 +79,13 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
} else {
|
} else {
|
||||||
// $value is probably ill-formed
|
// $value is probably ill-formed
|
||||||
$f = new \DOMDocument();
|
$f = new \DOMDocument();
|
||||||
$value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');
|
|
||||||
|
|
||||||
// Using <htmlfragment> will generate a warning, but so will bad HTML
|
// Using <htmlfragment> will generate a warning, but so will bad HTML
|
||||||
// (and by this point, bad HTML is what we've got).
|
// (and by this point, bad HTML is what we've got).
|
||||||
// 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('<meta charset="utf-8"><htmlfragment>' . $value . '</htmlfragment>');
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
||||||
@@ -105,9 +104,9 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
/**
|
/**
|
||||||
* Used for getting innerHTML like it's done in JavaScript:.
|
* Used for getting innerHTML like it's done in JavaScript:.
|
||||||
*
|
*
|
||||||
* @code
|
* ```php
|
||||||
* $string = $div->innerHTML;
|
* $string = $div->innerHTML;
|
||||||
* @endcode
|
* ```
|
||||||
*/
|
*/
|
||||||
public function __get($name)
|
public function __get($name)
|
||||||
{
|
{
|
||||||
|
|||||||
+252
-299
@@ -2,31 +2,73 @@
|
|||||||
|
|
||||||
namespace Readability;
|
namespace Readability;
|
||||||
|
|
||||||
use DOMElement;
|
|
||||||
use Masterminds\HTML5;
|
use Masterminds\HTML5;
|
||||||
use Psr\Log\LoggerAwareInterface;
|
use Psr\Log\LoggerAwareInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Psr\Log\NullLogger;
|
use Psr\Log\NullLogger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Arc90's Readability ported to PHP for FiveFilters.org
|
||||||
|
* Based on readability.js version 1.7.1 (without multi-page support)
|
||||||
|
* ------------------------------------------------------
|
||||||
|
* Original URL: http://lab.arc90.com/experiments/readability/js/readability.js
|
||||||
|
* Arc90's project URL: http://lab.arc90.com/experiments/readability/
|
||||||
|
* JS Source: http://code.google.com/p/arc90labs-readability
|
||||||
|
* Ported by: Keyvan Minoukadeh, http://www.keyvan.net
|
||||||
|
* Modded by: Dither, https://dithersky.wordpress.com
|
||||||
|
* More information: http://fivefilters.org/content-only/
|
||||||
|
* License: Apache License, Version 2.0
|
||||||
|
* Requires: PHP version 5.2.0+
|
||||||
|
* Date: 2013-08-02.
|
||||||
|
*
|
||||||
|
* Differences between the PHP port and the original
|
||||||
|
* ------------------------------------------------------
|
||||||
|
* Arc90's Readability is designed to run in the browser. It works on the DOM
|
||||||
|
* tree (the parsed HTML) after the page's CSS styles have been applied and
|
||||||
|
* Javascript code executed. This PHP port does not run inside a browser.
|
||||||
|
* We use PHP's ability to parse HTML to build our DOM tree, but we cannot
|
||||||
|
* rely on CSS or Javascript support. As such, the results will not always
|
||||||
|
* match Arc90's Readability. (For example, if a web page contains CSS style
|
||||||
|
* rules or Javascript code which hide certain HTML elements from display,
|
||||||
|
* Arc90's Readability will dismiss those from consideration but our PHP port,
|
||||||
|
* unable to understand CSS or Javascript, will not know any better.)
|
||||||
|
*
|
||||||
|
* Another significant difference is that the aim of Arc90's Readability is
|
||||||
|
* to re-present the main content block of a given web page so users can
|
||||||
|
* read it more easily in their browsers. Correct identification, clean up,
|
||||||
|
* and separation of the content block is only a part of this process.
|
||||||
|
* This PHP port is only concerned with this part, it does not include code
|
||||||
|
* that relates to presentation in the browser - Arc90 already do
|
||||||
|
* that extremely well, and for PDF output there's FiveFilters.org's
|
||||||
|
* PDF Newspaper: http://fivefilters.org/pdf-newspaper/.
|
||||||
|
*
|
||||||
|
* Finally, this class contains methods that might be useful for developers
|
||||||
|
* working on HTML document fragments. So without deviating too much from
|
||||||
|
* the original code (which I don't want to do because it makes debugging
|
||||||
|
* and updating more difficult), I've tried to make it a little more
|
||||||
|
* developer friendly. You should be able to use the methods here on
|
||||||
|
* existing DOMElement objects without passing an entire HTML document to
|
||||||
|
* be parsed.
|
||||||
|
*/
|
||||||
class Readability implements LoggerAwareInterface
|
class Readability implements LoggerAwareInterface
|
||||||
{
|
{
|
||||||
// flags
|
// flags
|
||||||
public const FLAG_STRIP_UNLIKELYS = 1;
|
const FLAG_STRIP_UNLIKELYS = 1;
|
||||||
public const FLAG_WEIGHT_ATTRIBUTES = 2;
|
const FLAG_WEIGHT_ATTRIBUTES = 2;
|
||||||
public const FLAG_CLEAN_CONDITIONALLY = 4;
|
const FLAG_CLEAN_CONDITIONALLY = 4;
|
||||||
public const FLAG_DISABLE_PREFILTER = 8;
|
const FLAG_DISABLE_PREFILTER = 8;
|
||||||
public const FLAG_DISABLE_POSTFILTER = 16;
|
const FLAG_DISABLE_POSTFILTER = 16;
|
||||||
// constants
|
// constants
|
||||||
public const SCORE_CHARS_IN_PARAGRAPH = 100;
|
const SCORE_CHARS_IN_PARAGRAPH = 100;
|
||||||
public const SCORE_WORDS_IN_PARAGRAPH = 20;
|
const SCORE_WORDS_IN_PARAGRAPH = 20;
|
||||||
public const GRANDPARENT_SCORE_DIVISOR = 2;
|
const GRANDPARENT_SCORE_DIVISOR = 2;
|
||||||
public const MIN_PARAGRAPH_LENGTH = 20;
|
const MIN_PARAGRAPH_LENGTH = 20;
|
||||||
public const MIN_COMMAS_IN_PARAGRAPH = 6;
|
const MIN_COMMAS_IN_PARAGRAPH = 6;
|
||||||
public const MIN_ARTICLE_LENGTH = 200;
|
const MIN_ARTICLE_LENGTH = 200;
|
||||||
public const MIN_NODE_LENGTH = 80;
|
const MIN_NODE_LENGTH = 80;
|
||||||
public const MAX_LINK_DENSITY = 0.25;
|
const MAX_LINK_DENSITY = 0.25;
|
||||||
public $convertLinksToFootnotes = false;
|
public $convertLinksToFootnotes = false;
|
||||||
public $revertForcedParagraphElements = false;
|
public $revertForcedParagraphElements = true;
|
||||||
public $articleTitle;
|
public $articleTitle;
|
||||||
public $articleContent;
|
public $articleContent;
|
||||||
public $original_html;
|
public $original_html;
|
||||||
@@ -47,27 +89,14 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Defined up here so we don't instantiate them repeatedly in loops.
|
* Defined up here so we don't instantiate them repeatedly in loops.
|
||||||
*/
|
*/
|
||||||
public $regexps = [
|
public $regexps = [
|
||||||
'unlikelyCandidates' => '/-ad-|ai2html|banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|footer|gdpr|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|ad-break|agegate|pagination|pager|popup|yom-remote/i',
|
'unlikelyCandidates' => '/display\s*:\s*none|ignore|\binfos?\b|annoy|clock|date|time|author|intro|hidd?e|about|archive|\bprint|bookmark|tags|tag-list|share|search|social|robot|published|combx|comment|mast(?:head)|subscri|community|category|disqus|extra|head|head(?:er|note)|floor|foot(?:er|note)|menu|tool\b|function|nav|remark|rss|shoutbox|widget|meta|banner|sponsor|adsense|inner-?ad|ad-|sponsor|\badv\b|\bads\b|agr?egate?|pager|sidebar|popup|tweet|twitter/i',
|
||||||
'okMaybeItsACandidate' => '/article\b|contain|\bcontent|column|general|detail|shadow|lightbox|blog|body|entry|main|page|footnote|element/i',
|
'okMaybeItsACandidate' => '/article\b|contain|\bcontent|column|general|detail|shadow|lightbox|blog|body|entry|main|page|footnote/i',
|
||||||
'positive' => '/read|full|article|body|\bcontent|contain|entry|main|markdown|media|page|attach|pagination|post|text|blog|story/i',
|
'positive' => '/read|full|article|body|\bcontent|contain|entry|main|markdown|media|page|attach|pagination|post|text|blog|story/i',
|
||||||
'negative' => '/bottom|stat|info|discuss|e[\-]?mail|comment|reply|log.{2}(n|ed)|sign|single|combx|com-|contact|_nav|link|media|promo|\bad-|related|scroll|shoutbox|sidebar|sponsor|shopping|teaser|recommend/i',
|
'negative' => '/bottom|stat|info|discuss|e[\-]?mail|comment|reply|log.{2}(n|ed)|sign|single|combx|com-|contact|_nav|link|media|promo|\bad-|related|scroll|shoutbox|sidebar|sponsor|shopping|teaser|recommend/i',
|
||||||
'divToPElements' => '/<(?:blockquote|header|section|code|div|article|footer|aside|img|p|pre|dl|ol|ul)/mi',
|
'divToPElements' => '/<(?:blockquote|header|section|code|div|article|footer|aside|img|p|pre|dl|ol|ul)/mi',
|
||||||
'killBreaks' => '/(<br\s*\/?>([ \r\n\s]| ?)*)+/',
|
'killBreaks' => '/(<br\s*\/?>([ \r\n\s]| ?)*)+/',
|
||||||
'media' => '!//(?:[^\.\?/]+\.)?(?:youtu(?:be)?|giphy|soundcloud|dailymotion|vimeo|pornhub|xvideos|twitvid|rutube|openload\.co|viddler)\.(?:com|be|org|net)/!i',
|
'media' => '!//(?:[^\.\?/]+\.)?(?:youtu(?:be)?|giphy|soundcloud|dailymotion|vimeo|pornhub|xvideos|twitvid|rutube|openload\.co|viddler)\.(?:com|be|org|net)/!i',
|
||||||
'skipFootnoteLink' => '/^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$/i',
|
'skipFootnoteLink' => '/^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$/i',
|
||||||
'hasContent' => '/\S$/',
|
|
||||||
'isNotVisible' => '/display\s*:\s*none/',
|
|
||||||
];
|
|
||||||
public $defaultTagsToScore = ['section', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'td', 'pre'];
|
|
||||||
// The commented out elements qualify as phrasing content but tend to be
|
|
||||||
// removed by readability when put into paragraphs, so we ignore them here.
|
|
||||||
public $phrasingElements = [
|
|
||||||
// "CANVAS", "IFRAME", "SVG", "VIDEO",
|
|
||||||
'ABBR', 'AUDIO', 'B', 'BDO', 'BR', 'BUTTON', 'CITE', 'CODE', 'DATA',
|
|
||||||
'DATALIST', 'DFN', 'EM', 'EMBED', 'I', 'IMG', 'INPUT', 'KBD', 'LABEL',
|
|
||||||
'MARK', 'MATH', 'METER', 'NOSCRIPT', 'OBJECT', 'OUTPUT', 'PROGRESS', 'Q',
|
|
||||||
'RUBY', 'SAMP', 'SCRIPT', 'SELECT', 'SMALL', 'SPAN', 'STRONG', 'SUB',
|
|
||||||
'SUP', 'TEXTAREA', 'TIME', 'VAR', 'WBR',
|
|
||||||
];
|
];
|
||||||
public $tidy_config = [
|
public $tidy_config = [
|
||||||
'tidy-mark' => false,
|
'tidy-mark' => false,
|
||||||
@@ -76,7 +105,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
'numeric-entities' => false,
|
'numeric-entities' => false,
|
||||||
// 'preserve-entities' => true,
|
// 'preserve-entities' => true,
|
||||||
'break-before-br' => false,
|
'break-before-br' => false,
|
||||||
'clean' => false,
|
'clean' => true,
|
||||||
'output-xhtml' => true,
|
'output-xhtml' => true,
|
||||||
'logical-emphasis' => true,
|
'logical-emphasis' => true,
|
||||||
'show-body-only' => false,
|
'show-body-only' => false,
|
||||||
@@ -141,19 +170,23 @@ class Readability implements LoggerAwareInterface
|
|||||||
* @param string $html UTF-8 encoded string
|
* @param string $html UTF-8 encoded string
|
||||||
* @param string $url URL associated with HTML (for footnotes)
|
* @param string $url URL associated with HTML (for footnotes)
|
||||||
* @param string $parser Which parser to use for turning raw HTML into a DOMDocument
|
* @param string $parser Which parser to use for turning raw HTML into a DOMDocument
|
||||||
* @param bool $useTidy Use tidy
|
* @param bool $use_tidy Use tidy
|
||||||
*/
|
*/
|
||||||
public function __construct(string $html, string $url = null, string $parser = 'libxml', bool $useTidy = true)
|
public function __construct($html, $url = null, $parser = 'libxml', $use_tidy = true)
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->html = $html;
|
$this->html = $html;
|
||||||
$this->parser = $parser;
|
$this->parser = $parser;
|
||||||
$this->useTidy = $useTidy && \function_exists('tidy_parse_string');
|
$this->useTidy = $use_tidy && \function_exists('tidy_parse_string');
|
||||||
|
|
||||||
$this->logger = new NullLogger();
|
$this->logger = new NullLogger();
|
||||||
|
$this->loadHtml();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setLogger(LoggerInterface $logger): void
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setLogger(LoggerInterface $logger)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
@@ -161,7 +194,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get article title element.
|
* Get article title element.
|
||||||
*
|
*
|
||||||
* @return DOMElement
|
* @return \DOMElement
|
||||||
*/
|
*/
|
||||||
public function getTitle()
|
public function getTitle()
|
||||||
{
|
{
|
||||||
@@ -171,7 +204,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get article content element.
|
* Get article content element.
|
||||||
*
|
*
|
||||||
* @return DOMElement
|
* @return \DOMElement
|
||||||
*/
|
*/
|
||||||
public function getContent()
|
public function getContent()
|
||||||
{
|
{
|
||||||
@@ -184,7 +217,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* @param string $filter RegExp for replace
|
* @param string $filter RegExp for replace
|
||||||
* @param string $replacer Replacer
|
* @param string $replacer Replacer
|
||||||
*/
|
*/
|
||||||
public function addPreFilter(string $filter, string $replacer = ''): void
|
public function addPreFilter($filter, $replacer = '')
|
||||||
{
|
{
|
||||||
$this->pre_filters[$filter] = $replacer;
|
$this->pre_filters[$filter] = $replacer;
|
||||||
}
|
}
|
||||||
@@ -195,7 +228,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* @param string $filter RegExp for replace
|
* @param string $filter RegExp for replace
|
||||||
* @param string $replacer Replacer
|
* @param string $replacer Replacer
|
||||||
*/
|
*/
|
||||||
public function addPostFilter(string $filter, string $replacer = ''): void
|
public function addPostFilter($filter, $replacer = '')
|
||||||
{
|
{
|
||||||
$this->post_filters[$filter] = $replacer;
|
$this->post_filters[$filter] = $replacer;
|
||||||
}
|
}
|
||||||
@@ -212,10 +245,8 @@ class Readability implements LoggerAwareInterface
|
|||||||
*
|
*
|
||||||
* @return bool true if we found content, false otherwise
|
* @return bool true if we found content, false otherwise
|
||||||
*/
|
*/
|
||||||
public function init(): bool
|
public function init()
|
||||||
{
|
{
|
||||||
$this->loadHtml();
|
|
||||||
|
|
||||||
if (!isset($this->dom->documentElement)) {
|
if (!isset($this->dom->documentElement)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -280,7 +311,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Run any post-process modifications to article content as necessary.
|
* Run any post-process modifications to article content as necessary.
|
||||||
*/
|
*/
|
||||||
public function postProcessContent(DOMElement $articleContent): void
|
public function postProcessContent(\DOMElement $articleContent)
|
||||||
{
|
{
|
||||||
if ($this->convertLinksToFootnotes && !preg_match('/\bwiki/', $this->url)) {
|
if ($this->convertLinksToFootnotes && !preg_match('/\bwiki/', $this->url)) {
|
||||||
$this->addFootnotes($articleContent);
|
$this->addFootnotes($articleContent);
|
||||||
@@ -292,7 +323,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
*
|
*
|
||||||
* @see http://www.roughtype.com/archives/2010/05/experiments_in.php
|
* @see http://www.roughtype.com/archives/2010/05/experiments_in.php
|
||||||
*/
|
*/
|
||||||
public function addFootnotes(DOMElement $articleContent): void
|
public function addFootnotes(\DOMElement $articleContent)
|
||||||
{
|
{
|
||||||
$footnotesWrapper = $this->dom->createElement('footer');
|
$footnotesWrapper = $this->dom->createElement('footer');
|
||||||
$footnotesWrapper->setAttribute('class', 'readability-footnotes');
|
$footnotesWrapper->setAttribute('class', 'readability-footnotes');
|
||||||
@@ -335,7 +366,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
$articleLink->setAttribute('style', 'color: inherit; text-decoration: none;');
|
$articleLink->setAttribute('style', 'color: inherit; text-decoration: none;');
|
||||||
$articleLink->setAttribute('name', 'readabilityLink-' . $linkCount);
|
$articleLink->setAttribute('name', 'readabilityLink-' . $linkCount);
|
||||||
$footnote->setInnerHtml('<small><sup><a href="#readabilityLink-' . $linkCount . '" title="Jump to Link in Article">^</a></sup></small> ');
|
$footnote->setInnerHtml('<small><sup><a href="#readabilityLink-' . $linkCount . '" title="Jump to Link in Article">^</a></sup></small> ');
|
||||||
$footnoteLink->setInnerHtml(('' !== $footnoteLink->getAttribute('title') ? $footnoteLink->getAttribute('title') : $linkText));
|
$footnoteLink->setInnerHtml('' !== $footnoteLink->getAttribute('title') ? $footnoteLink->getAttribute('title') : $linkText);
|
||||||
$footnoteLink->setAttribute('name', 'readabilityFootnoteLink-' . $linkCount);
|
$footnoteLink->setAttribute('name', 'readabilityFootnoteLink-' . $linkCount);
|
||||||
$footnote->appendChild($footnoteLink);
|
$footnote->appendChild($footnoteLink);
|
||||||
|
|
||||||
@@ -354,9 +385,9 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Prepare the article node for display. Clean out any inline styles,
|
* Prepare the article node for display. Clean out any inline styles,
|
||||||
* iframes, forms, strip extraneous <p> tags, etc.
|
* iframes, forms, strip extraneous <p> tags, etc.
|
||||||
*/
|
*/
|
||||||
public function prepArticle(\DOMNode $articleContent): void
|
public function prepArticle(\DOMNode $articleContent)
|
||||||
{
|
{
|
||||||
if (!$articleContent instanceof DOMElement) {
|
if (!$articleContent instanceof \DOMElement) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,11 +487,13 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Get the inner text of a node.
|
* Get the inner text of a node.
|
||||||
* This also strips out any excess whitespace to be found.
|
* This also strips out any excess whitespace to be found.
|
||||||
*
|
*
|
||||||
* @param DOMElement $e
|
* @param \DOMElement $e
|
||||||
* @param bool $normalizeSpaces (default: true)
|
* @param bool $normalizeSpaces (default: true)
|
||||||
* @param bool $flattenLines (default: false)
|
* @param bool $flattenLines (default: false)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getInnerText($e, bool $normalizeSpaces = true, bool $flattenLines = false): string
|
public function getInnerText($e, $normalizeSpaces = true, $flattenLines = false)
|
||||||
{
|
{
|
||||||
if (null === $e || !isset($e->textContent) || '' === $e->textContent) {
|
if (null === $e || !isset($e->textContent) || '' === $e->textContent) {
|
||||||
return '';
|
return '';
|
||||||
@@ -469,11 +502,9 @@ class Readability implements LoggerAwareInterface
|
|||||||
$textContent = trim($e->textContent);
|
$textContent = trim($e->textContent);
|
||||||
|
|
||||||
if ($flattenLines) {
|
if ($flattenLines) {
|
||||||
return (string) mb_ereg_replace('(?:[\r\n](?:\s| )*)+', '', $textContent);
|
$textContent = mb_ereg_replace('(?:[\r\n](?:\s| )*)+', '', $textContent);
|
||||||
}
|
} elseif ($normalizeSpaces) {
|
||||||
|
$textContent = mb_ereg_replace('\s\s+', ' ', $textContent);
|
||||||
if ($normalizeSpaces) {
|
|
||||||
return (string) mb_ereg_replace('\s\s+', ' ', $textContent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $textContent;
|
return $textContent;
|
||||||
@@ -481,31 +512,43 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the style attribute on every $e and under.
|
* Remove the style attribute on every $e and under.
|
||||||
|
*
|
||||||
|
* @param \DOMElement $e
|
||||||
*/
|
*/
|
||||||
public function cleanStyles(DOMElement $e): void
|
public function cleanStyles($e)
|
||||||
{
|
{
|
||||||
if (\is_object($e)) {
|
if (!\is_object($e)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$elems = $e->getElementsByTagName('*');
|
$elems = $e->getElementsByTagName('*');
|
||||||
|
|
||||||
foreach ($elems as $elem) {
|
foreach ($elems as $elem) {
|
||||||
$elem->removeAttribute('style');
|
$elem->removeAttribute('style');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get comma number for a given text.
|
* Get comma number for a given text.
|
||||||
|
*
|
||||||
|
* @param string $text
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getCommaCount(string $text): int
|
public function getCommaCount($text)
|
||||||
{
|
{
|
||||||
return \count(explode(',', $text));
|
return substr_count($text, ',');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get words number for a given text if words separated by a space.
|
* Get words number for a given text if words separated by a space.
|
||||||
* Input string should be normalized.
|
* Input string should be normalized.
|
||||||
|
*
|
||||||
|
* @param string $text
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getWordCount(string $text): int
|
public function getWordCount($text)
|
||||||
{
|
{
|
||||||
return substr_count($text, ' ');
|
return substr_count($text, ' ');
|
||||||
}
|
}
|
||||||
@@ -514,8 +557,12 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Get the density of links as a percentage of the content
|
* Get the density of links as a percentage of the content
|
||||||
* This is the amount of text that is inside a link divided by the total text in the node.
|
* This is the amount of text that is inside a link divided by the total text in the node.
|
||||||
* Can exclude external references to differentiate between simple text and menus/infoblocks.
|
* Can exclude external references to differentiate between simple text and menus/infoblocks.
|
||||||
|
*
|
||||||
|
* @param bool $excludeExternal
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getLinkDensity(DOMElement $e, bool $excludeExternal = false): float
|
public function getLinkDensity(\DOMElement $e, $excludeExternal = false)
|
||||||
{
|
{
|
||||||
$links = $e->getElementsByTagName('a');
|
$links = $e->getElementsByTagName('a');
|
||||||
$textLength = mb_strlen($this->getInnerText($e, true, true));
|
$textLength = mb_strlen($this->getInnerText($e, true, true));
|
||||||
@@ -537,8 +584,10 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an element relative weight.
|
* Get an element relative weight.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getWeight(DOMElement $e): int
|
public function getWeight(\DOMElement $e)
|
||||||
{
|
{
|
||||||
if (!$this->flagIsActive(self::FLAG_WEIGHT_ATTRIBUTES)) {
|
if (!$this->flagIsActive(self::FLAG_WEIGHT_ATTRIBUTES)) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -556,7 +605,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Remove extraneous break tags from a node.
|
* Remove extraneous break tags from a node.
|
||||||
*/
|
*/
|
||||||
public function killBreaks(DOMElement $node): void
|
public function killBreaks(\DOMElement $node)
|
||||||
{
|
{
|
||||||
$html = $node->getInnerHTML();
|
$html = $node->getInnerHTML();
|
||||||
$html = preg_replace($this->regexps['killBreaks'], '<br />', $html);
|
$html = preg_replace($this->regexps['killBreaks'], '<br />', $html);
|
||||||
@@ -568,8 +617,10 @@ class Readability implements LoggerAwareInterface
|
|||||||
* (Unless it's a youtube/vimeo video. People love movies.).
|
* (Unless it's a youtube/vimeo video. People love movies.).
|
||||||
*
|
*
|
||||||
* Updated 2012-09-18 to preserve youtube/vimeo iframes
|
* Updated 2012-09-18 to preserve youtube/vimeo iframes
|
||||||
|
*
|
||||||
|
* @param string $tag
|
||||||
*/
|
*/
|
||||||
public function clean(DOMElement $e, string $tag): void
|
public function clean(\DOMElement $e, $tag)
|
||||||
{
|
{
|
||||||
$targetList = $e->getElementsByTagName($tag);
|
$targetList = $e->getElementsByTagName($tag);
|
||||||
$isEmbed = ('audio' === $tag || 'video' === $tag || 'iframe' === $tag || 'object' === $tag || 'embed' === $tag);
|
$isEmbed = ('audio' === $tag || 'video' === $tag || 'iframe' === $tag || 'object' === $tag || 'embed' === $tag);
|
||||||
@@ -600,8 +651,10 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Clean an element of all tags of type "tag" if they look fishy.
|
* Clean an element of all tags of type "tag" if they look fishy.
|
||||||
* "Fishy" is an algorithm based on content length, classnames,
|
* "Fishy" is an algorithm based on content length, classnames,
|
||||||
* link density, number of images & embeds, etc.
|
* link density, number of images & embeds, etc.
|
||||||
|
*
|
||||||
|
* @param string $tag
|
||||||
*/
|
*/
|
||||||
public function cleanConditionally(DOMElement $e, string $tag): void
|
public function cleanConditionally(\DOMElement $e, $tag)
|
||||||
{
|
{
|
||||||
if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
|
if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
|
||||||
return;
|
return;
|
||||||
@@ -622,9 +675,6 @@ class Readability implements LoggerAwareInterface
|
|||||||
$contentScore = ($node->hasAttribute('readability')) ? (int) $node->getAttribute('readability') : 0;
|
$contentScore = ($node->hasAttribute('readability')) ? (int) $node->getAttribute('readability') : 0;
|
||||||
$this->logger->debug('Start conditional cleaning of ' . $node->getNodePath() . ' (class=' . $node->getAttribute('class') . '; id=' . $node->getAttribute('id') . ')' . (($node->hasAttribute('readability')) ? (' with score ' . $node->getAttribute('readability')) : ''));
|
$this->logger->debug('Start conditional cleaning of ' . $node->getNodePath() . ' (class=' . $node->getAttribute('class') . '; id=' . $node->getAttribute('id') . ')' . (($node->hasAttribute('readability')) ? (' with score ' . $node->getAttribute('readability')) : ''));
|
||||||
|
|
||||||
// XXX Incomplete implementation
|
|
||||||
$isList = \in_array($node->tagName, ['ul', 'ol'], true);
|
|
||||||
|
|
||||||
if ($weight + $contentScore < 0) {
|
if ($weight + $contentScore < 0) {
|
||||||
$this->logger->debug('Removing...');
|
$this->logger->debug('Removing...');
|
||||||
$node->parentNode->removeChild($node);
|
$node->parentNode->removeChild($node);
|
||||||
@@ -659,16 +709,16 @@ class Readability implements LoggerAwareInterface
|
|||||||
$toRemove = false;
|
$toRemove = false;
|
||||||
|
|
||||||
if ($this->lightClean) {
|
if ($this->lightClean) {
|
||||||
if (!$isList && $li > $p) {
|
if ($li > $p && 'ul' !== $tag && 'ol' !== $tag) {
|
||||||
$this->logger->debug(' too many <li> elements, and parent is not <ul> or <ol>');
|
$this->logger->debug(' too many <li> elements, and parent is not <ul> or <ol>');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif ($input > floor($p / 3)) {
|
} elseif ($input > floor($p / 3)) {
|
||||||
$this->logger->debug(' too many <input> elements');
|
$this->logger->debug(' too many <input> elements');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif (!$isList && $contentLength < 6 && (0 === $embedCount && (0 === $img || $img > 2))) {
|
} elseif ($contentLength < 6 && (0 === $embedCount && (0 === $img || $img > 2))) {
|
||||||
$this->logger->debug(' content length less than 6 chars, 0 embeds and either 0 images or more than 2 images');
|
$this->logger->debug(' content length less than 6 chars, 0 embeds and either 0 images or more than 2 images');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif (!$isList && $weight < 25 && $linkDensity > 0.25) {
|
} elseif ($weight < 25 && $linkDensity > 0.25) {
|
||||||
$this->logger->debug(' weight is ' . $weight . ' < 25 and link density is ' . sprintf('%.2f', $linkDensity) . ' > 0.25');
|
$this->logger->debug(' weight is ' . $weight . ' < 25 and link density is ' . sprintf('%.2f', $linkDensity) . ' > 0.25');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif ($a > 2 && ($weight >= 25 && $linkDensity > 0.5)) {
|
} elseif ($a > 2 && ($weight >= 25 && $linkDensity > 0.5)) {
|
||||||
@@ -682,16 +732,16 @@ class Readability implements LoggerAwareInterface
|
|||||||
if ($img > $p) {
|
if ($img > $p) {
|
||||||
$this->logger->debug(' more image elements than paragraph elements');
|
$this->logger->debug(' more image elements than paragraph elements');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif (!$isList && $li > $p) {
|
} elseif ($li > $p && 'ul' !== $tag && 'ol' !== $tag) {
|
||||||
$this->logger->debug(' too many <li> elements, and parent is not <ul> or <ol>');
|
$this->logger->debug(' too many <li> elements, and parent is not <ul> or <ol>');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif ($input > floor($p / 3)) {
|
} elseif ($input > floor($p / 3)) {
|
||||||
$this->logger->debug(' too many <input> elements');
|
$this->logger->debug(' too many <input> elements');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif (!$isList && $contentLength < 10 && (0 === $img || $img > 2)) {
|
} elseif ($contentLength < 10 && (0 === $img || $img > 2)) {
|
||||||
$this->logger->debug(' content length less than 10 chars and 0 images, or more than 2 images');
|
$this->logger->debug(' content length less than 10 chars and 0 images, or more than 2 images');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif (!$isList && $weight < 25 && $linkDensity > 0.2) {
|
} elseif ($weight < 25 && $linkDensity > 0.2) {
|
||||||
$this->logger->debug(' weight is ' . $weight . ' lower than 0 and link density is ' . sprintf('%.2f', $linkDensity) . ' > 0.2');
|
$this->logger->debug(' weight is ' . $weight . ' lower than 0 and link density is ' . sprintf('%.2f', $linkDensity) . ' > 0.2');
|
||||||
$toRemove = true;
|
$toRemove = true;
|
||||||
} elseif ($weight >= 25 && $linkDensity > 0.5) {
|
} elseif ($weight >= 25 && $linkDensity > 0.5) {
|
||||||
@@ -714,7 +764,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Clean out spurious headers from an Element. Checks things like classnames and link density.
|
* Clean out spurious headers from an Element. Checks things like classnames and link density.
|
||||||
*/
|
*/
|
||||||
public function cleanHeaders(DOMElement $e): void
|
public function cleanHeaders(\DOMElement $e)
|
||||||
{
|
{
|
||||||
for ($headerIndex = 1; $headerIndex < 3; ++$headerIndex) {
|
for ($headerIndex = 1; $headerIndex < 3; ++$headerIndex) {
|
||||||
$headers = $e->getElementsByTagName('h' . $headerIndex);
|
$headers = $e->getElementsByTagName('h' . $headerIndex);
|
||||||
@@ -729,32 +779,63 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the given flag is active.
|
* Check if the given flag is active.
|
||||||
|
*
|
||||||
|
* @param int $flag
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function flagIsActive(int $flag): bool
|
public function flagIsActive($flag)
|
||||||
{
|
{
|
||||||
return ($this->flags & $flag) > 0;
|
return ($this->flags & $flag) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a flag.
|
* Add a flag.
|
||||||
|
*
|
||||||
|
* @param int $flag
|
||||||
*/
|
*/
|
||||||
public function addFlag(int $flag): void
|
public function addFlag($flag)
|
||||||
{
|
{
|
||||||
$this->flags = $this->flags | $flag;
|
$this->flags |= $flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a flag.
|
* Remove a flag.
|
||||||
|
*
|
||||||
|
* @param int $flag
|
||||||
*/
|
*/
|
||||||
public function removeFlag(int $flag): void
|
public function removeFlag($flag)
|
||||||
|
{
|
||||||
|
$this->flags &= ~$flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debug.
|
||||||
|
*
|
||||||
|
* @deprecated use $this->logger->debug() instead
|
||||||
|
*
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*/
|
||||||
|
protected function dbg($msg)
|
||||||
|
{
|
||||||
|
$this->logger->debug($msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dump debug info.
|
||||||
|
*
|
||||||
|
* @deprecated since Monolog gather log, we don't need it
|
||||||
|
*
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*/
|
||||||
|
protected function dump_dbg()
|
||||||
{
|
{
|
||||||
$this->flags = $this->flags & ~$flag;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the article title as an H1.
|
* Get the article title as an H1.
|
||||||
*
|
*
|
||||||
* @return DOMElement
|
* @return \DOMElement
|
||||||
*/
|
*/
|
||||||
protected function getArticleTitle()
|
protected function getArticleTitle()
|
||||||
{
|
{
|
||||||
@@ -797,7 +878,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Prepare the HTML document for readability to scrape it.
|
* Prepare the HTML document for readability to scrape it.
|
||||||
* This includes things like stripping javascript, CSS, and handling terrible markup.
|
* This includes things like stripping javascript, CSS, and handling terrible markup.
|
||||||
*/
|
*/
|
||||||
protected function prepDocument(): void
|
protected function prepDocument()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* In some cases a body element can't be found (if the HTML is totally hosed for example)
|
* In some cases a body element can't be found (if the HTML is totally hosed for example)
|
||||||
@@ -826,7 +907,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Initialize a node with the readability object. Also checks the
|
* Initialize a node with the readability object. Also checks the
|
||||||
* className/id for special names to add to its score.
|
* className/id for special names to add to its score.
|
||||||
*/
|
*/
|
||||||
protected function initializeNode(DOMElement $node): void
|
protected function initializeNode(\DOMElement $node)
|
||||||
{
|
{
|
||||||
if (!isset($node->tagName)) {
|
if (!isset($node->tagName)) {
|
||||||
return;
|
return;
|
||||||
@@ -862,7 +943,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
case 'DD':
|
case 'DD':
|
||||||
case 'DT':
|
case 'DT':
|
||||||
case 'LI':
|
case 'LI':
|
||||||
$readability->value -= 3;
|
$readability->value -= 2 * round($this->getLinkDensity($node), 0, \PHP_ROUND_HALF_UP);
|
||||||
break;
|
break;
|
||||||
case 'ASIDE':
|
case 'ASIDE':
|
||||||
case 'FOOTER':
|
case 'FOOTER':
|
||||||
@@ -894,11 +975,11 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Using a variety of metrics (content score, classname, element types), find the content that is
|
* Using a variety of metrics (content score, classname, element types), find the content that is
|
||||||
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
|
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
|
||||||
*
|
*
|
||||||
* @param DOMElement $page
|
* @param ?\DOMElement $page
|
||||||
*
|
*
|
||||||
* @return DOMElement|false
|
* @return \DOMElement|false
|
||||||
*/
|
*/
|
||||||
protected function grabArticle(DOMElement $page = null)
|
protected function grabArticle($page = null)
|
||||||
{
|
{
|
||||||
if (!$page) {
|
if (!$page) {
|
||||||
$page = $this->dom;
|
$page = $this->dom;
|
||||||
@@ -913,8 +994,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
$allElements = $page->getElementsByTagName('*');
|
$allElements = $page->getElementsByTagName('*');
|
||||||
|
|
||||||
for ($nodeIndex = 0; $allElements->item($nodeIndex); ++$nodeIndex) {
|
for ($nodeIndex = 0; $node = $allElements->item($nodeIndex); ++$nodeIndex) {
|
||||||
$node = $allElements->item($nodeIndex);
|
|
||||||
$tagName = $node->tagName;
|
$tagName = $node->tagName;
|
||||||
|
|
||||||
$nodeContent = $node->getInnerHTML();
|
$nodeContent = $node->getInnerHTML();
|
||||||
@@ -923,35 +1003,14 @@ class Readability implements LoggerAwareInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove invisible nodes
|
|
||||||
if (!$this->isNodeVisible($node)) {
|
|
||||||
$this->logger->debug('Removing invisible node ' . $node->getNodePath());
|
|
||||||
$node->parentNode->removeChild($node);
|
|
||||||
--$nodeIndex;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove unlikely candidates
|
|
||||||
$unlikelyMatchString = $node->getAttribute('class') . ' ' . $node->getAttribute('id') . ' ' . $node->getAttribute('style');
|
|
||||||
|
|
||||||
if (mb_strlen($unlikelyMatchString) > 3 && // don't process "empty" strings
|
|
||||||
preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) &&
|
|
||||||
!preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString)
|
|
||||||
) {
|
|
||||||
$this->logger->debug('Removing unlikely candidate (using conf) ' . $node->getNodePath() . ' by "' . $unlikelyMatchString . '"');
|
|
||||||
$node->parentNode->removeChild($node);
|
|
||||||
--$nodeIndex;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some well known site uses sections as paragraphs.
|
// Some well known site uses sections as paragraphs.
|
||||||
if (\in_array($tagName, $this->defaultTagsToScore, true)) {
|
if (0 === strcasecmp($tagName, 'p') || 0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'pre') || 0 === strcasecmp($tagName, 'section')) {
|
||||||
$nodesToScore[] = $node;
|
$nodesToScore[] = $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turn divs into P tags where they have been used inappropriately
|
// Turn divs into P tags where they have been used inappropriately
|
||||||
// (as in, where they contain no other block level elements).
|
// (as in, where they contain no other block level elements).
|
||||||
if ('div' === $tagName) {
|
if (0 === strcasecmp($tagName, 'div') || 0 === strcasecmp($tagName, 'article') || 0 === strcasecmp($tagName, 'section')) {
|
||||||
if (!preg_match($this->regexps['divToPElements'], $nodeContent)) {
|
if (!preg_match($this->regexps['divToPElements'], $nodeContent)) {
|
||||||
$newNode = $this->dom->createElement('p');
|
$newNode = $this->dom->createElement('p');
|
||||||
|
|
||||||
@@ -966,46 +1025,27 @@ class Readability implements LoggerAwareInterface
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Will change these P elements back to text nodes after processing.
|
// Will change these P elements back to text nodes after processing.
|
||||||
$p = null;
|
for ($i = 0, $il = $node->childNodes->length; $i < $il; ++$i) {
|
||||||
// foreach does not handle removeChild very well
|
$childNode = $node->childNodes->item($i);
|
||||||
// See https://www.php.net/manual/en/domnode.removechild.php#90292
|
|
||||||
$childs = iterator_to_array($node->childNodes);
|
// it looks like sometimes the loop is going too far and we are retrieving a non-existant child
|
||||||
foreach ($childs as $childNode) {
|
if (null === $childNode) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// executable tags (<?php or <?xml) warning
|
// executable tags (<?php or <?xml) warning
|
||||||
if ($childNode instanceof \DOMProcessingInstruction) {
|
if (\is_object($childNode) && 'DOMProcessingInstruction' === \get_class($childNode)) {
|
||||||
$childNode->parentNode->removeChild($childNode);
|
$childNode->parentNode->removeChild($childNode);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($childNode instanceof \DOMText && '' === $this->getInnerText($childNode, true, true)) {
|
if (\XML_TEXT_NODE === $childNode->nodeType) {
|
||||||
/* $this->logger->debug('Remove empty text node'); */
|
|
||||||
$childNode->parentNode->removeChild($childNode);
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->isPhrasingContent($childNode)) {
|
|
||||||
if (null !== $p) {
|
|
||||||
$p->appendChild($childNode);
|
|
||||||
} elseif ('' !== $this->getInnerText($childNode, true, true)) {
|
|
||||||
$p = $this->dom->createElement('p');
|
$p = $this->dom->createElement('p');
|
||||||
|
$p->setInnerHtml($childNode->nodeValue);
|
||||||
$p->setAttribute('data-readability-styled', 'true');
|
$p->setAttribute('data-readability-styled', 'true');
|
||||||
$node->replaceChild($p, $childNode);
|
$childNode->parentNode->replaceChild($p, $childNode);
|
||||||
$p->appendChild($childNode);
|
|
||||||
}
|
}
|
||||||
} elseif (null !== $p) {
|
|
||||||
while ($p->lastChild && '' === $this->getInnerText($p->lastChild, true, true)) {
|
|
||||||
$p->removeChild($p->lastChild);
|
|
||||||
}
|
|
||||||
$p = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->hasSingleTagInsideElement($node, 'p') && $this->getLinkDensity($node) < 0.25) {
|
|
||||||
$newNode = $node->childNodes->item(0);
|
|
||||||
$node->parentNode->replaceChild($newNode, $node);
|
|
||||||
$nodesToScore[] = $newNode;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1019,13 +1059,14 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Maybe eventually link density.
|
* Maybe eventually link density.
|
||||||
*/
|
*/
|
||||||
for ($pt = 0, $scored = \count($nodesToScore); $pt < $scored; ++$pt) {
|
for ($pt = 0, $scored = \count($nodesToScore); $pt < $scored; ++$pt) {
|
||||||
$ancestors = $this->getAncestors($nodesToScore[$pt], 5);
|
$parentNode = $nodesToScore[$pt]->parentNode;
|
||||||
|
|
||||||
// No parent node? Move on...
|
// No parent node? Move on...
|
||||||
if (0 === \count($ancestors)) {
|
if (!$parentNode) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$grandParentNode = $parentNode->parentNode instanceof \DOMElement ? $parentNode->parentNode : null;
|
||||||
$innerText = $this->getInnerText($nodesToScore[$pt]);
|
$innerText = $this->getInnerText($nodesToScore[$pt]);
|
||||||
|
|
||||||
// If this paragraph is less than MIN_PARAGRAPH_LENGTH (default:20) characters, don't even count it.
|
// If this paragraph is less than MIN_PARAGRAPH_LENGTH (default:20) characters, don't even count it.
|
||||||
@@ -1033,6 +1074,17 @@ class Readability implements LoggerAwareInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize readability data for the parent.
|
||||||
|
if (!$parentNode->hasAttribute('readability')) {
|
||||||
|
$this->initializeNode($parentNode);
|
||||||
|
$parentNode->setAttribute('data-candidate', 'true');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize readability data for the grandparent.
|
||||||
|
if ($grandParentNode && !$grandParentNode->hasAttribute('readability') && isset($grandParentNode->tagName)) {
|
||||||
|
$this->initializeNode($grandParentNode);
|
||||||
|
$grandParentNode->setAttribute('data-candidate', 'true');
|
||||||
|
}
|
||||||
// Add a point for the paragraph itself as a base.
|
// Add a point for the paragraph itself as a base.
|
||||||
$contentScore = 1;
|
$contentScore = 1;
|
||||||
// Add points for any commas within this paragraph.
|
// Add points for any commas within this paragraph.
|
||||||
@@ -1040,26 +1092,25 @@ class Readability implements LoggerAwareInterface
|
|||||||
// For every SCORE_CHARS_IN_PARAGRAPH (default:100) characters in this paragraph, add another point. Up to 3 points.
|
// For every SCORE_CHARS_IN_PARAGRAPH (default:100) characters in this paragraph, add another point. Up to 3 points.
|
||||||
$contentScore += min(floor(mb_strlen($innerText) / self::SCORE_CHARS_IN_PARAGRAPH), 3);
|
$contentScore += min(floor(mb_strlen($innerText) / self::SCORE_CHARS_IN_PARAGRAPH), 3);
|
||||||
// For every SCORE_WORDS_IN_PARAGRAPH (default:20) words in this paragraph, add another point. Up to 3 points.
|
// For every SCORE_WORDS_IN_PARAGRAPH (default:20) words in this paragraph, add another point. Up to 3 points.
|
||||||
//$contentScore += min(floor($this->getWordCount($innerText) / self::SCORE_WORDS_IN_PARAGRAPH), 3);
|
$contentScore += min(floor($this->getWordCount($innerText) / self::SCORE_WORDS_IN_PARAGRAPH), 3);
|
||||||
|
/* TEST: For every positive/negative parent tag, add/substract half point. Up to 3 points. *\/
|
||||||
foreach ($ancestors as $level => $ancestor) {
|
$up = $nodesToScore[$pt];
|
||||||
if (!$ancestor->nodeName || !$ancestor->parentNode) {
|
$score = 0;
|
||||||
return;
|
while ($up->parentNode instanceof \DOMElement) {
|
||||||
|
$up = $up->parentNode;
|
||||||
|
if (preg_match($this->regexps['positive'], $up->getAttribute('class') . ' ' . $up->getAttribute('id'))) {
|
||||||
|
$score += 0.5;
|
||||||
|
} elseif (preg_match($this->regexps['negative'], $up->getAttribute('class') . ' ' . $up->getAttribute('id'))) {
|
||||||
|
$score -= 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$ancestor->hasAttribute('readability')) {
|
|
||||||
$this->initializeNode($ancestor);
|
|
||||||
$ancestor->setAttribute('data-candidate', 'true');
|
|
||||||
}
|
}
|
||||||
|
$score = floor($score);
|
||||||
|
$contentScore += max(min($score, 3), -3);/**/
|
||||||
|
|
||||||
if (0 === $level) {
|
// Add the score to the parent. The grandparent gets half.
|
||||||
$scoreDivider = 1;
|
$parentNode->getAttributeNode('readability')->value += $contentScore;
|
||||||
} elseif (1 === $level) {
|
if ($grandParentNode) {
|
||||||
$scoreDivider = 2;
|
$grandParentNode->getAttributeNode('readability')->value += round($contentScore / self::GRANDPARENT_SCORE_DIVISOR);
|
||||||
} else {
|
|
||||||
$scoreDivider = $level * 3;
|
|
||||||
}
|
|
||||||
$ancestor->getAttributeNode('readability')->value += $contentScore / $scoreDivider;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1083,6 +1134,18 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
for ($c = $candidates->length - 1; $c >= 0; --$c) {
|
for ($c = $candidates->length - 1; $c >= 0; --$c) {
|
||||||
$node = $candidates->item($c);
|
$node = $candidates->item($c);
|
||||||
|
|
||||||
|
// Remove unlikely candidates
|
||||||
|
$unlikelyMatchString = $node->getAttribute('class') . ' ' . $node->getAttribute('id') . ' ' . $node->getAttribute('style');
|
||||||
|
|
||||||
|
if (mb_strlen($unlikelyMatchString) > 3 // don't process "empty" strings
|
||||||
|
&& preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString)
|
||||||
|
&& !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString)
|
||||||
|
) {
|
||||||
|
$this->logger->debug('Removing unlikely candidate (using conf) ' . $node->getNodePath() . ' by "' . $unlikelyMatchString . '" with readability ' . ($node->hasAttribute('readability') ? (int) $node->getAttributeNode('readability')->value : 0));
|
||||||
|
$node->parentNode->removeChild($node);
|
||||||
|
--$nodeIndex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
unset($candidates);
|
unset($candidates);
|
||||||
}
|
}
|
||||||
@@ -1091,11 +1154,10 @@ class Readability implements LoggerAwareInterface
|
|||||||
* After we've calculated scores, loop through all of the possible candidate nodes we found
|
* After we've calculated scores, loop through all of the possible candidate nodes we found
|
||||||
* and find the one with the highest score.
|
* and find the one with the highest score.
|
||||||
*/
|
*/
|
||||||
$topCandidates = array_fill(0, 5, null);
|
$topCandidate = null;
|
||||||
if ($xpath) {
|
if ($xpath) {
|
||||||
// Using array of DOMElements after deletion is a path to DOOMElement.
|
// Using array of DOMElements after deletion is a path to DOOMElement.
|
||||||
$candidates = $xpath->query('.//*[@data-candidate]', $page->documentElement);
|
$candidates = $xpath->query('.//*[@data-candidate]', $page->documentElement);
|
||||||
$this->logger->debug('Candidates: ' . $candidates->length);
|
|
||||||
|
|
||||||
for ($c = $candidates->length - 1; $c >= 0; --$c) {
|
for ($c = $candidates->length - 1; $c >= 0; --$c) {
|
||||||
$item = $candidates->item($c);
|
$item = $candidates->item($c);
|
||||||
@@ -1106,25 +1168,14 @@ class Readability implements LoggerAwareInterface
|
|||||||
$readability = $item->getAttributeNode('readability');
|
$readability = $item->getAttributeNode('readability');
|
||||||
$readability->value = round($readability->value * (1 - $this->getLinkDensity($item)), 0, \PHP_ROUND_HALF_UP);
|
$readability->value = round($readability->value * (1 - $this->getLinkDensity($item)), 0, \PHP_ROUND_HALF_UP);
|
||||||
|
|
||||||
for ($t = 0; $t < 5; ++$t) {
|
if (!$topCandidate || $readability->value > (int) $topCandidate->getAttribute('readability')) {
|
||||||
$aTopCandidate = $topCandidates[$t];
|
|
||||||
|
|
||||||
if (!$aTopCandidate || $readability->value > (int) $aTopCandidate->getAttribute('readability')) {
|
|
||||||
$this->logger->debug('Candidate: ' . $item->getNodePath() . ' (' . $item->getAttribute('class') . ':' . $item->getAttribute('id') . ') with score ' . $readability->value);
|
$this->logger->debug('Candidate: ' . $item->getNodePath() . ' (' . $item->getAttribute('class') . ':' . $item->getAttribute('id') . ') with score ' . $readability->value);
|
||||||
array_splice($topCandidates, $t, 0, [$item]);
|
$topCandidate = $item;
|
||||||
if (\count($topCandidates) > 5) {
|
|
||||||
array_pop($topCandidates);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$topCandidates = array_filter($topCandidates, function ($v, $idx) {
|
unset($candidates);
|
||||||
return 0 === $idx || null !== $v;
|
}
|
||||||
}, \ARRAY_FILTER_USE_BOTH);
|
|
||||||
$topCandidate = $topCandidates[0];
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we still have no top candidate, just use the body as a last resort.
|
* If we still have no top candidate, just use the body as a last resort.
|
||||||
@@ -1151,59 +1202,6 @@ class Readability implements LoggerAwareInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->initializeNode($topCandidate);
|
$this->initializeNode($topCandidate);
|
||||||
} elseif ($topCandidate) {
|
|
||||||
$alternativeCandidateAncestors = [];
|
|
||||||
foreach ($topCandidates as $candidate) {
|
|
||||||
if ((int) $candidate->getAttribute('readability') / (int) $topCandidate->getAttribute('readability') >= 0.75) {
|
|
||||||
$ancestors = $this->getAncestors($candidate);
|
|
||||||
$this->logger->debug('Adding ' . \count($ancestors) . ' alternative ancestors for ' . $candidate->getNodePath());
|
|
||||||
$alternativeCandidateAncestors[] = $ancestors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (\count($alternativeCandidateAncestors) >= 3) {
|
|
||||||
$parentOfTopCandidate = $topCandidate->parentNode;
|
|
||||||
while ('body' !== $parentOfTopCandidate->nodeName) {
|
|
||||||
$listsContainingThisAncestor = 0;
|
|
||||||
for ($ancestorIndex = 0; $ancestorIndex < \count($alternativeCandidateAncestors) && $listsContainingThisAncestor < 3; ++$ancestorIndex) {
|
|
||||||
$listsContainingThisAncestor += (int) \in_array($parentOfTopCandidate, $alternativeCandidateAncestors[$ancestorIndex], true);
|
|
||||||
}
|
|
||||||
if ($listsContainingThisAncestor >= 3) {
|
|
||||||
$topCandidate = $parentOfTopCandidate;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$parentOfTopCandidate = $parentOfTopCandidate->parentNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!$topCandidate->hasAttribute('readability')) {
|
|
||||||
$this->initializeNode($topCandidate);
|
|
||||||
}
|
|
||||||
$parentOfTopCandidate = $topCandidate->parentNode;
|
|
||||||
$lastScore = (int) $topCandidate->getAttribute('readability');
|
|
||||||
$scoreThreshold = $lastScore / 3;
|
|
||||||
while ('body' !== $parentOfTopCandidate->nodeName) {
|
|
||||||
if (!$parentOfTopCandidate->hasAttribute('readability')) {
|
|
||||||
$parentOfTopCandidate = $parentOfTopCandidate->parentNode;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$parentScore = (int) $parentOfTopCandidate->getAttribute('readability');
|
|
||||||
if ($parentScore < $scoreThreshold) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ($parentScore > $lastScore) {
|
|
||||||
$topCandidate = $parentOfTopCandidate;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$lastScore = (int) $parentOfTopCandidate->getAttribute('readability');
|
|
||||||
$parentOfTopCandidate = $parentOfTopCandidate->parentNode;
|
|
||||||
}
|
|
||||||
$parentOfTopCandidate = $topCandidate->parentNode;
|
|
||||||
while ('body' !== $parentOfTopCandidate->nodeName && 1 === $parentOfTopCandidate->childNodes->length) {
|
|
||||||
$topCandidate = $parentOfTopCandidate;
|
|
||||||
$parentOfTopCandidate = $topCandidate->parentNode;
|
|
||||||
}
|
|
||||||
if (!$topCandidate->hasAttribute('readability')) {
|
|
||||||
$this->initializeNode($topCandidate);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set table as the main node if resulted data is table element.
|
// Set table as the main node if resulted data is table element.
|
||||||
@@ -1211,7 +1209,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
if (0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'tr')) {
|
if (0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'tr')) {
|
||||||
$up = $topCandidate;
|
$up = $topCandidate;
|
||||||
|
|
||||||
if ($up->parentNode instanceof DOMElement) {
|
if ($up->parentNode instanceof \DOMElement) {
|
||||||
$up = $up->parentNode;
|
$up = $up->parentNode;
|
||||||
|
|
||||||
if (0 === strcasecmp($up->tagName, 'table')) {
|
if (0 === strcasecmp($up->tagName, 'table')) {
|
||||||
@@ -1229,10 +1227,9 @@ class Readability implements LoggerAwareInterface
|
|||||||
$articleContent = $this->dom->createElement('div');
|
$articleContent = $this->dom->createElement('div');
|
||||||
$articleContent->setAttribute('class', 'readability-content');
|
$articleContent->setAttribute('class', 'readability-content');
|
||||||
$siblingScoreThreshold = max(10, ((int) $topCandidate->getAttribute('readability')) * 0.2);
|
$siblingScoreThreshold = max(10, ((int) $topCandidate->getAttribute('readability')) * 0.2);
|
||||||
$parentOfTopCandidate = $topCandidate->parentNode;
|
$siblingNodes = $topCandidate->parentNode->childNodes;
|
||||||
$siblingNodes = $parentOfTopCandidate->childNodes;
|
|
||||||
|
|
||||||
if (0 === $siblingNodes->length) {
|
if (null === $siblingNodes) {
|
||||||
$siblingNodes = new \stdClass();
|
$siblingNodes = new \stdClass();
|
||||||
$siblingNodes->length = 0;
|
$siblingNodes->length = 0;
|
||||||
}
|
}
|
||||||
@@ -1245,7 +1242,8 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
if ($siblingNode->isSameNode($topCandidate)) {
|
if ($siblingNode->isSameNode($topCandidate)) {
|
||||||
$append = true;
|
$append = true;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
$contentBonus = 0;
|
$contentBonus = 0;
|
||||||
|
|
||||||
// Give a bonus if sibling nodes and top candidates have the same classname.
|
// Give a bonus if sibling nodes and top candidates have the same classname.
|
||||||
@@ -1255,17 +1253,18 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
|
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
|
||||||
$append = true;
|
$append = true;
|
||||||
} elseif (0 === strcasecmp($siblingNodeName, 'p')) {
|
}
|
||||||
$linkDensity = (int) $this->getLinkDensity($siblingNode);
|
|
||||||
|
if (0 === strcasecmp($siblingNodeName, 'p')) {
|
||||||
|
$linkDensity = $this->getLinkDensity($siblingNode);
|
||||||
$nodeContent = $this->getInnerText($siblingNode, true, true);
|
$nodeContent = $this->getInnerText($siblingNode, true, true);
|
||||||
$nodeLength = mb_strlen($nodeContent);
|
$nodeLength = mb_strlen($nodeContent);
|
||||||
|
|
||||||
if (($nodeLength > self::MIN_NODE_LENGTH && $linkDensity < self::MAX_LINK_DENSITY)
|
if (($nodeLength > self::MIN_NODE_LENGTH && $linkDensity < self::MAX_LINK_DENSITY)
|
||||||
|| ($nodeLength < self::MIN_NODE_LENGTH && 0 === $nodeLength && 0 === $linkDensity && preg_match('/\.( |$)/', $nodeContent))) {
|
|| ($nodeLength < self::MIN_NODE_LENGTH && 0 === $linkDensity && preg_match('/\.( |$)/', $nodeContent))) {
|
||||||
$append = true;
|
$append = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ($append) {
|
if ($append) {
|
||||||
$this->logger->debug('Appending node: ' . $siblingNode->getNodePath());
|
$this->logger->debug('Appending node: ' . $siblingNode->getNodePath());
|
||||||
@@ -1339,8 +1338,12 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get an element weight by attribute.
|
* Get an element weight by attribute.
|
||||||
* Uses regular expressions to tell if this element looks good or bad.
|
* Uses regular expressions to tell if this element looks good or bad.
|
||||||
|
*
|
||||||
|
* @param string $attribute
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function weightAttribute(DOMElement $element, string $attribute): int
|
protected function weightAttribute(\DOMElement $element, $attribute)
|
||||||
{
|
{
|
||||||
if (!$element->hasAttribute($attribute)) {
|
if (!$element->hasAttribute($attribute)) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1371,7 +1374,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Will recreate previously deleted body property.
|
* Will recreate previously deleted body property.
|
||||||
*/
|
*/
|
||||||
protected function reinitBody(): void
|
protected function reinitBody()
|
||||||
{
|
{
|
||||||
if (!isset($this->body->childNodes)) {
|
if (!isset($this->body->childNodes)) {
|
||||||
$this->body = $this->dom->createElement('body');
|
$this->body = $this->dom->createElement('body');
|
||||||
@@ -1383,15 +1386,17 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Load HTML in a DOMDocument.
|
* Load HTML in a DOMDocument.
|
||||||
* Apply Pre filters
|
* Apply Pre filters
|
||||||
* Cleanup HTML using Tidy (or not).
|
* Cleanup HTML using Tidy (or not).
|
||||||
|
*
|
||||||
|
* @todo This should be called in init() instead of from __construct
|
||||||
*/
|
*/
|
||||||
private function loadHtml(): void
|
private function loadHtml()
|
||||||
{
|
{
|
||||||
$this->original_html = $this->html;
|
$this->original_html = $this->html;
|
||||||
|
|
||||||
$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)), ['.' => '\.']) . '/';
|
$this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', parse_url($this->url, \PHP_URL_HOST)), ['.' => '\.']) . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
mb_internal_encoding('UTF-8');
|
mb_internal_encoding('UTF-8');
|
||||||
@@ -1427,7 +1432,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
unset($tidy);
|
unset($tidy);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->html = mb_convert_encoding((string) $this->html, 'HTML-ENTITIES', 'UTF-8');
|
$this->html = '<meta charset="utf-8">' . (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);
|
||||||
@@ -1443,58 +1448,6 @@ class Readability implements LoggerAwareInterface
|
|||||||
libxml_use_internal_errors(false);
|
libxml_use_internal_errors(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->dom->registerNodeClass(DOMElement::class, \Readability\JSLikeHTMLElement::class);
|
$this->dom->registerNodeClass('DOMElement', 'Readability\JSLikeHTMLElement');
|
||||||
}
|
|
||||||
|
|
||||||
private function getAncestors(DOMElement $node, int $maxDepth = 0): array
|
|
||||||
{
|
|
||||||
$ancestors = [];
|
|
||||||
$i = 0;
|
|
||||||
while ($node->parentNode instanceof DOMElement) {
|
|
||||||
$ancestors[] = $node->parentNode;
|
|
||||||
if (++$i === $maxDepth) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$node = $node->parentNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ancestors;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function isPhrasingContent($node): bool
|
|
||||||
{
|
|
||||||
return \XML_TEXT_NODE === $node->nodeType
|
|
||||||
|| \in_array(strtoupper($node->nodeName), $this->phrasingElements, true)
|
|
||||||
|| (\in_array(strtoupper($node->nodeName), ['A', 'DEL', 'INS'], true) && !\in_array(false, array_map(function ($c) {
|
|
||||||
return $this->isPhrasingContent($c);
|
|
||||||
}, iterator_to_array($node->childNodes)), true));
|
|
||||||
}
|
|
||||||
|
|
||||||
private function hasSingleTagInsideElement(DOMElement $node, string $tag): bool
|
|
||||||
{
|
|
||||||
if (1 !== $node->childNodes->length || $node->childNodes->item(0)->nodeName !== $tag) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$a = array_filter(iterator_to_array($node->childNodes), function ($childNode) {
|
|
||||||
return $childNode instanceof \DOMText &&
|
|
||||||
preg_match($this->regexps['hasContent'], $this->getInnerText($childNode));
|
|
||||||
});
|
|
||||||
|
|
||||||
return 0 === \count($a);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return whether a given node is visible or not.
|
|
||||||
*
|
|
||||||
* Tidy must be configured to not clean the input for this function to
|
|
||||||
* work as expected, see $this->tidy_config['clean']
|
|
||||||
*/
|
|
||||||
private function isNodeVisible(DOMElement $node): bool
|
|
||||||
{
|
|
||||||
return !($node->hasAttribute('style')
|
|
||||||
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))
|
|
||||||
)
|
|
||||||
&& !$node->hasAttribute('hidden');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+96
-132
@@ -4,33 +4,27 @@ namespace Tests\Readability;
|
|||||||
|
|
||||||
use Monolog\Handler\TestHandler;
|
use Monolog\Handler\TestHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
use Readability\JSLikeHTMLElement;
|
|
||||||
use Readability\Readability;
|
use Readability\Readability;
|
||||||
|
|
||||||
class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
/** @var TestHandler */
|
|
||||||
public $logHandler;
|
public $logHandler;
|
||||||
/** @var LoggerInterface */
|
|
||||||
public $logger;
|
public $logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @requires extension tidy
|
* @requires extension tidy
|
||||||
*/
|
*/
|
||||||
public function testConstructDefault(): void
|
public function testConstructDefault()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('');
|
$readability = $this->getReadability('');
|
||||||
$readability->init();
|
|
||||||
|
|
||||||
$this->assertNull($readability->url);
|
$this->assertNull($readability->url);
|
||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructHtml5Parser(): void
|
public function testConstructHtml5Parser()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'html5lib');
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'html5lib');
|
||||||
$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', $readability->dom);
|
||||||
@@ -40,10 +34,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
/**
|
/**
|
||||||
* @requires extension tidy
|
* @requires extension tidy
|
||||||
*/
|
*/
|
||||||
public function testConstructSimple(): void
|
public function testConstructSimple()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
||||||
$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', $readability->dom);
|
||||||
@@ -51,10 +44,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertTrue($readability->tidied);
|
$this->assertTrue($readability->tidied);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructDefaultWithoutTidy(): void
|
public function testConstructDefaultWithoutTidy()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('', null, 'libxml', false);
|
$readability = $this->getReadability('', null, 'libxml', false);
|
||||||
$readability->init();
|
|
||||||
|
|
||||||
$this->assertNull($readability->url);
|
$this->assertNull($readability->url);
|
||||||
$this->assertSame('', $readability->original_html);
|
$this->assertSame('', $readability->original_html);
|
||||||
@@ -63,10 +55,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructSimpleWithoutTidy(): void
|
public function testConstructSimpleWithoutTidy()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'libxml', false);
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'libxml', false);
|
||||||
$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', $readability->dom);
|
||||||
@@ -74,59 +65,59 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertFalse($readability->tidied);
|
$this->assertFalse($readability->tidied);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInitNoContent(): void
|
public function testInitNoContent()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertFalse($res);
|
$this->assertFalse($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('Sorry, Readability was unable to parse this page for content.', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('Sorry, Readability was unable to parse this page for content.', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInitP(): void
|
public function testInitP()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability(str_repeat('<p>This is the awesome content :)</p>', 7), 'http://0.0.0.0');
|
$readability = $this->getReadability(str_repeat('<p>This is the awesome content :)</p>', 7), 'http://0.0.0.0');
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInitDivP(): void
|
public function testInitDivP()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div>' . str_repeat('<p>This is the awesome content :)</p>', 7) . '</div>', 'http://0.0.0.0');
|
$readability = $this->getReadability('<div>' . str_repeat('<p>This is the awesome content :)</p>', 7) . '</div>', 'http://0.0.0.0');
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInitDiv(): void
|
public function testInitDiv()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithFootnotes(): void
|
public function testWithFootnotes()
|
||||||
{
|
{
|
||||||
$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->debug = true;
|
||||||
@@ -134,8 +125,8 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||||
@@ -143,7 +134,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertStringContainsString('readabilityLink-3', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('readabilityLink-3', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testStandardClean(): void
|
public function testStandardClean()
|
||||||
{
|
{
|
||||||
$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->debug = true;
|
||||||
@@ -151,8 +142,8 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||||
@@ -160,67 +151,67 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertStringNotContainsString('<h2>', $readability->getContent()->getInnerHtml());
|
$this->assertStringNotContainsString('<h2>', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithIframe(): void
|
public function testWithIframe()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertStringContainsString('nofollow', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('nofollow', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithArticle(): void
|
public function testWithArticle()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertStringContainsString('nofollow', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('nofollow', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithAside(): void
|
public function testWithAside()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertStringNotContainsString('<aside>', $readability->getContent()->getInnerHtml());
|
$this->assertStringNotContainsString('<aside>', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertStringContainsString('<footer readability="9"/>', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<footer readability="5"/>', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithClasses(): void
|
public function testWithClasses()
|
||||||
{
|
{
|
||||||
$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->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->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());
|
$this->assertStringNotContainsString('This text should be removed', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithClassesWithoutLightClean(): void
|
public function testWithClassesWithoutLightClean()
|
||||||
{
|
{
|
||||||
$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->debug = true;
|
||||||
@@ -228,120 +219,124 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->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());
|
$this->assertStringNotContainsString('This text should be removed', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithTd(): void
|
public function testWithTd()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithSameClasses(): void
|
public function testWithSameClasses()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->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());
|
$this->assertStringContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithScript(): void
|
public function testWithScript()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->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());
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTitle(): void
|
public function testTitle()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertSame('this is my title', $readability->getTitle()->getInnerHtml());
|
$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->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());
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTitleWithDash(): void
|
public function testTitleWithDash()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertSame('title2 - title3', $readability->getTitle()->getInnerHtml());
|
$this->assertSame('title2 - title3', $readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->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());
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTitleWithDoubleDot(): void
|
public function testTitleWithDoubleDot()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertSame('title2 : title3', $readability->getTitle()->getInnerHtml());
|
$this->assertSame('title2 : title3', $readability->getTitle()->getInnerHtml());
|
||||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->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());
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTitleTooShortUseH1(): void
|
public function testTitleTooShortUseH1()
|
||||||
{
|
{
|
||||||
$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;
|
$readability->debug = true;
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
$this->assertSame('this is my h1 title !', $readability->getTitle()->getInnerHtml());
|
$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->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());
|
$this->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAutoClosingIframeNotThrowingException(): void
|
|
||||||
{
|
|
||||||
error_reporting(\E_ALL | \E_STRICT);
|
|
||||||
ini_set('display_errors', '1');
|
|
||||||
// dummy function to be used to the next test
|
// dummy function to be used to the next test
|
||||||
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
|
public function error2Exception($code, $string, $file, $line)
|
||||||
throw new \Exception($errstr, $errno);
|
{
|
||||||
}, \E_ALL | \E_STRICT);
|
throw new \Exception($string, $code);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAutoClosingIframeNotThrowingException()
|
||||||
|
{
|
||||||
|
$oldErrorReporting = error_reporting(\E_ALL);
|
||||||
|
$oldDisplayErrors = ini_set('display_errors', true);
|
||||||
|
set_error_handler([$this, 'error2Exception']);
|
||||||
|
|
||||||
|
try {
|
||||||
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru-RU" prefix="og: http://ogp.me/ns#">
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru-RU" prefix="og: http://ogp.me/ns#">
|
||||||
|
|
||||||
@@ -364,6 +359,8 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>';
|
</html>';
|
||||||
|
|
||||||
@@ -373,16 +370,23 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $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('<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());
|
$this->assertStringContainsString('3D Touch', $readability->getTitle()->getInnerHtml());
|
||||||
|
} finally {
|
||||||
|
restore_error_handler();
|
||||||
|
if (false !== $oldDisplayErrors) {
|
||||||
|
ini_set('display_errors', $oldDisplayErrors);
|
||||||
|
}
|
||||||
|
error_reporting($oldErrorReporting);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This should generate an Exception "DOMElement::setAttribute(): ID post-60 already defined".
|
* This should generate an Exception "DOMElement::setAttribute(): ID post-60 already defined".
|
||||||
*/
|
*/
|
||||||
public function testAppendIdAlreadyHere(): void
|
public function testAppendIdAlreadyHere()
|
||||||
{
|
{
|
||||||
$data = '<!DOCTYPE html>
|
$data = '<!DOCTYPE html>
|
||||||
<html lang="fr">
|
<html lang="fr">
|
||||||
@@ -434,11 +438,11 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|
||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getContent());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPostFilters(): void
|
public function testPostFilters()
|
||||||
{
|
{
|
||||||
$readability = $this->getReadability('<div>' . str_repeat('<p>This <strong>is</strong> the awesome content :)</p>', 10) . '</div>', 'http://0.0.0.0');
|
$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', '');
|
$readability->addPostFilter('!<strong[^>]*>(.*?)</strong>!is', '');
|
||||||
@@ -449,8 +453,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertStringContainsString('This the awesome content :)', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPreFilters(): void
|
public function testPreFilters()
|
||||||
{
|
{
|
||||||
|
$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 = $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', '');
|
$readability->addPreFilter('!<b[^>]*>(.*?)</b>!is', '');
|
||||||
|
|
||||||
@@ -460,10 +466,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertStringContainsString('This the awesome and WONDERFUL content :)', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('This the awesome and WONDERFUL content :)', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChildNodeGoneNull(): void
|
public function testChildNodeGoneNull()
|
||||||
{
|
{
|
||||||
// from http://www.ayyaantuu.net/ethiopia-targets-opposition-lawmakers/
|
// from http://www.ayyaantuu.net/ethiopia-targets-opposition-lawmakers/
|
||||||
$html = (string) file_get_contents('tests/fixtures/childNodeGoesNull.html');
|
$html = 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->debug = true;
|
||||||
@@ -473,10 +479,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertTrue($res);
|
$this->assertTrue($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testKeepFootnotes(): void
|
public function testKeepFootnotes()
|
||||||
{
|
{
|
||||||
// from https://www.schreibdichte.de/blog/feed-aggregator-und-spaeter-lesen-dienst-im-team
|
// from https://www.schreibdichte.de/blog/feed-aggregator-und-spaeter-lesen-dienst-im-team
|
||||||
$html = (string) file_get_contents('tests/fixtures/keepFootnotes.html');
|
$html = 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;
|
$readability->debug = true;
|
||||||
@@ -487,10 +493,10 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertStringContainsString('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWithWipedBody(): void
|
public function testWithWipedBody()
|
||||||
{
|
{
|
||||||
// from https://www.cs.cmu.edu/~rgs/alice-table.html
|
// from https://www.cs.cmu.edu/~rgs/alice-table.html
|
||||||
$html = (string) file_get_contents('tests/fixtures/wipedBody.html');
|
$html = 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;
|
$readability->debug = true;
|
||||||
@@ -500,49 +506,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertStringContainsString('<a href="alice-I.html">Down the Rabbit-Hole</a>', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<a href="alice-I.html">Down the Rabbit-Hole</a>', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataForVisibleNode(): array
|
private function getReadability($html, $url = null, $parser = 'libxml', $useTidy = true)
|
||||||
{
|
|
||||||
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');
|
|
||||||
$readability->debug = true;
|
|
||||||
$res = $readability->init();
|
|
||||||
|
|
||||||
if ($shouldBeVisible) {
|
|
||||||
$this->assertStringContainsString('WONDERFUL content', $readability->getContent()->getInnerHtml());
|
|
||||||
} else {
|
|
||||||
$this->assertStringNotContainsString('WONDERFUL content', $readability->getContent()->getInnerHtml());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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