mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 14:36:23 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ed91105ea | ||
|
|
952588ee1f | ||
|
|
e4e975db96 | ||
|
|
774363e18d | ||
|
|
e6ca6f117f | ||
|
|
0b21d4ab2d | ||
|
|
720f0d5503 | ||
|
|
b9dde0f4cd | ||
|
|
a21742b22a | ||
|
|
40219d4595 | ||
|
|
7f304d03aa | ||
|
|
109a22662d | ||
|
|
f1c6297e3c | ||
|
|
5afefcff34 | ||
|
|
eb6ca1a99b | ||
|
|
235baf965c | ||
|
|
487ce3a517 | ||
|
|
0bae41248f | ||
|
|
ae0c20ab18 | ||
|
|
84220bff99 | ||
|
|
2ae758555a | ||
|
|
f5e25f3c9c | ||
|
|
a209429e8b | ||
|
|
8421ed5962 | ||
|
|
66864279fd | ||
|
|
ae87c8ca8c | ||
|
|
4adb0a96aa | ||
|
|
baa0a78ab6 | ||
|
|
2112dd95d1 | ||
|
|
f4550ccc46 | ||
|
|
6f4404030b | ||
|
|
5638357306 | ||
|
|
651e8a6bb0 | ||
|
|
b1a20a9575 | ||
|
|
97c02e8ad4 | ||
|
|
c506b7ebd7 | ||
|
|
d0af21814a | ||
|
|
5b1eba79bd | ||
|
|
fabf096ce6 | ||
|
|
8ce1663238 | ||
|
|
eb72a315c4 | ||
|
|
d5330a9c28 | ||
|
|
635f8963dc | ||
|
|
19b2a25d96 | ||
|
|
9a490fac07 | ||
|
|
6f6b1f9e2b | ||
|
|
ea1368fac0 | ||
|
|
be81eb2a4f | ||
|
|
9632c4df8c | ||
|
|
bd9ca1b2cd | ||
|
|
6c917794a7 | ||
|
|
7cea79c23a | ||
|
|
c6425cc28b | ||
|
|
e6ad806460 | ||
|
|
3fa88461a0 | ||
|
|
6a8ecf232f | ||
|
|
52b1ddba57 | ||
|
|
90c625bb57 | ||
|
|
4d1c3b1777 | ||
|
|
d649b59414 | ||
|
|
44bebfc3d6 | ||
|
|
b1acc9ed73 | ||
|
|
9306996b47 | ||
|
|
bb75b4f089 | ||
|
|
11d2946904 | ||
|
|
ff78c63e6d | ||
|
|
f808c1b0a2 | ||
|
|
bb65caf864 | ||
|
|
de1b1d9775 | ||
|
|
2e20f76195 |
+3
-2
@@ -4,9 +4,10 @@ root = true
|
||||
; Unix-style newlines
|
||||
[*]
|
||||
end_of_line = LF
|
||||
|
||||
[*.php]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[.github/**.yml]
|
||||
indent_size = 2
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@
|
||||
/.scrutinizer.yml export-ignore
|
||||
/.travis.yml export-ignore
|
||||
/.php_cs export-ignore
|
||||
/README.md export-ignore
|
||||
/phpunit.xml.dist export-ignore
|
||||
/tests export-ignore
|
||||
/phpstan.neon export-ignore
|
||||
/.github export-ignore
|
||||
/tests export-ignore
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
github: j0k3r
|
||||
@@ -0,0 +1,55 @@
|
||||
name: "CS"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- 1.x
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- 1.x
|
||||
|
||||
env:
|
||||
SYMFONY_PHPUNIT_VERSION: 7.5
|
||||
|
||||
jobs:
|
||||
coding-standards:
|
||||
name: "CS Fixer & PHPStan"
|
||||
runs-on: "ubuntu-latest"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
php:
|
||||
- "7.4"
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v4"
|
||||
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
coverage: "none"
|
||||
php-version: "${{ matrix.php }}"
|
||||
tools: cs2pr, composer:v2
|
||||
ini-values: "date.timezone=Europe/Paris"
|
||||
env:
|
||||
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"
|
||||
uses: "ramsey/composer-install@v3"
|
||||
with:
|
||||
composer-options: "--optimize-autoloader --prefer-dist"
|
||||
|
||||
- name: "Run PHP CS Fixer"
|
||||
run: "php vendor/bin/php-cs-fixer fix --verbose --dry-run --format=checkstyle | cs2pr"
|
||||
|
||||
- name: "Install PHPUnit for PHPStan"
|
||||
run: "php vendor/bin/simple-phpunit install"
|
||||
|
||||
- name: "Run PHPStan"
|
||||
run: "php vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"
|
||||
@@ -0,0 +1,153 @@
|
||||
name: "CI"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- "1.x"
|
||||
push:
|
||||
branches:
|
||||
- "1.x"
|
||||
|
||||
env:
|
||||
fail-fast: true
|
||||
|
||||
jobs:
|
||||
phpunit:
|
||||
name: "PHPUnit (PHP ${{ matrix.php }})"
|
||||
runs-on: "ubuntu-latest"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
php:
|
||||
- "7.2"
|
||||
- "7.3"
|
||||
- "7.4"
|
||||
- "8.0"
|
||||
- "8.1"
|
||||
- "8.2"
|
||||
- "8.3"
|
||||
- "8.4"
|
||||
- "8.5"
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v4"
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
php-version: "${{ matrix.php }}"
|
||||
coverage: "none"
|
||||
tools: composer:v2
|
||||
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@v3"
|
||||
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"
|
||||
|
||||
phpunit-coverage:
|
||||
name: "PHPUnit coverage (PHP ${{ matrix.php }})"
|
||||
runs-on: "ubuntu-latest"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
php:
|
||||
- "7.4"
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v4"
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: "Install PHP with Xdebug"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
php-version: "${{ matrix.php }}"
|
||||
coverage: "xdebug"
|
||||
tools: composer:v2
|
||||
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@v3"
|
||||
with:
|
||||
composer-options: "--optimize-autoloader --prefer-dist"
|
||||
|
||||
- name: "Setup logs"
|
||||
run: "mkdir -p build/logs"
|
||||
|
||||
- name: "Run PHPUnit (with coverage)"
|
||||
run: "php vendor/bin/simple-phpunit -v --coverage-clover build/logs/clover.xml"
|
||||
|
||||
- name: "Retrieve Coveralls phar"
|
||||
run: "wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.2/php-coveralls.phar"
|
||||
|
||||
- name: "Enable Coveralls phar"
|
||||
run: "chmod +x php-coveralls.phar"
|
||||
|
||||
- name: "Upload to Coveralls"
|
||||
run: "php php-coveralls.phar -v -x build/logs/clover.xml"
|
||||
env:
|
||||
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
phpunit-lowest:
|
||||
name: "PHPUnit lowest deps (PHP ${{ matrix.php }})"
|
||||
runs-on: "ubuntu-latest"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
php:
|
||||
- "7.4"
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v4"
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: "Install PHP"
|
||||
uses: "shivammathur/setup-php@v2"
|
||||
with:
|
||||
php-version: "${{ matrix.php }}"
|
||||
coverage: "none"
|
||||
tools: composer:v2
|
||||
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@v3"
|
||||
with:
|
||||
composer-options: "--optimize-autoloader --prefer-dist"
|
||||
dependency-versions: "lowest"
|
||||
|
||||
- name: "Setup logs"
|
||||
run: "mkdir -p build/logs"
|
||||
|
||||
- name: "Run PHPUnit"
|
||||
run: "php vendor/bin/simple-phpunit -v"
|
||||
@@ -2,3 +2,5 @@ vendor/
|
||||
coverage/
|
||||
composer.lock
|
||||
.php_cs.cache
|
||||
.phpunit.result.cache
|
||||
phpstan.neon
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
<?php
|
||||
|
||||
return PhpCsFixer\Config::create()
|
||||
$finder = (new PhpCsFixer\Finder())
|
||||
->in(__DIR__)
|
||||
->exclude(['vendor', 'var', 'web'])
|
||||
;
|
||||
|
||||
return (new PhpCsFixer\Config())
|
||||
->setUsingCache(true)
|
||||
->setRiskyAllowed(true)
|
||||
->setRules([
|
||||
@@ -9,7 +14,7 @@ return PhpCsFixer\Config::create()
|
||||
'array_syntax' => ['syntax' => 'short'],
|
||||
'combine_consecutive_unsets' => true,
|
||||
'heredoc_to_nowdoc' => true,
|
||||
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
|
||||
'no_extra_blank_lines' => ['tokens' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block']],
|
||||
'no_unreachable_default_argument_value' => true,
|
||||
'no_useless_else' => true,
|
||||
'no_useless_return' => true,
|
||||
@@ -21,10 +26,10 @@ return PhpCsFixer\Config::create()
|
||||
'strict_comparison' => true,
|
||||
'strict_param' => true,
|
||||
'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(
|
||||
PhpCsFixer\Finder::create()
|
||||
->exclude(['vendor'])
|
||||
->in(__DIR__)
|
||||
)
|
||||
->setFinder($finder)
|
||||
;
|
||||
@@ -1,2 +1,12 @@
|
||||
tools:
|
||||
external_code_coverage: false
|
||||
|
||||
build:
|
||||
nodes:
|
||||
analysis:
|
||||
tests:
|
||||
override:
|
||||
- php-scrutinizer-run
|
||||
environment:
|
||||
php:
|
||||
version: 7.2
|
||||
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
- 7.3
|
||||
- nightly
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- php: 7.2
|
||||
env: CS_FIXER=run
|
||||
fast_finish: true
|
||||
allow_failures:
|
||||
- php: nightly
|
||||
|
||||
# cache vendor dirs
|
||||
cache:
|
||||
directories:
|
||||
- vendor
|
||||
- $HOME/.composer/cache
|
||||
|
||||
before_install:
|
||||
- if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi;
|
||||
|
||||
install:
|
||||
- composer self-update
|
||||
|
||||
before_script:
|
||||
- if [ "$CS_FIXER" = "run" ]; then composer require phpstan/phpstan phpstan/phpstan-phpunit --dev -n ; fi;
|
||||
- composer install -o --prefer-dist --no-interaction
|
||||
|
||||
script:
|
||||
- mkdir -p build/logs
|
||||
- php vendor/bin/simple-phpunit -v --coverage-clover build/logs/clover.xml
|
||||
- if [ "$CS_FIXER" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi;
|
||||
- if [ "$CS_FIXER" = "run" ]; then php vendor/bin/phpstan analyse src tests --no-progress --level 1 ; fi;
|
||||
|
||||
after_script:
|
||||
- php vendor/bin/php-coveralls -v -x build/logs/clover.xml
|
||||
@@ -1,6 +1,6 @@
|
||||
# Readability
|
||||
|
||||
[](https://travis-ci.org/j0k3r/php-readability)
|
||||

|
||||
[](https://coveralls.io/github/j0k3r/php-readability/?branch=master)
|
||||
[](https://packagist.org/packages/j0k3r/php-readability)
|
||||
[](https://packagist.org/packages/j0k3r/php-readability)
|
||||
|
||||
+10
-6
@@ -24,16 +24,15 @@
|
||||
"role": "Developer (original JS version)"
|
||||
}],
|
||||
"require": {
|
||||
"php": ">=5.6.0",
|
||||
"php": ">=7.2",
|
||||
"ext-mbstring": "*",
|
||||
"psr/log": "^1.0",
|
||||
"electrolinux/php-html5lib": "^0.1.0"
|
||||
"psr/log": "^1.0.1 || ^2.0 || ^3.0",
|
||||
"masterminds/html5": "^2.7"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-coveralls/php-coveralls": "^2.1",
|
||||
"friendsofphp/php-cs-fixer": "^2.14",
|
||||
"monolog/monolog": "^1.24",
|
||||
"symfony/phpunit-bridge": "^4.2.3"
|
||||
"monolog/monolog": "^1.24|^2.1|^3.0",
|
||||
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0|^7.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."
|
||||
@@ -43,5 +42,10 @@
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": { "Tests\\Readability\\": "tests/" }
|
||||
},
|
||||
"scripts": {
|
||||
"fix": "php-cs-fixer fix --verbose --diff",
|
||||
"phpstan": "phpstan analyze --memory-limit 512M",
|
||||
"test": "simple-phpunit -v"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
parameters:
|
||||
level: 1
|
||||
paths:
|
||||
- src
|
||||
- tests
|
||||
|
||||
# https://github.com/phpstan/phpstan/issues/694#issuecomment-350724288
|
||||
bootstrapFiles:
|
||||
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
|
||||
|
||||
includes:
|
||||
- vendor/phpstan/phpstan-phpunit/extension.neon
|
||||
- vendor/phpstan/phpstan-phpunit/rules.neon
|
||||
|
||||
parameters:
|
||||
# https://github.com/phpstan/phpstan/issues/694#issuecomment-350724288
|
||||
autoload_files:
|
||||
- vendor/bin/.phpunit/phpunit-6.5/vendor/autoload.php
|
||||
+1
-2
@@ -7,7 +7,6 @@
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
|
||||
@@ -27,6 +26,6 @@
|
||||
</filter>
|
||||
|
||||
<!-- <logging>
|
||||
<log type="coverage-html" target="coverage" title="Readability" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
|
||||
<log type="coverage-html" target="coverage" lowUpperBound="35" highLowerBound="70"/>
|
||||
</logging> -->
|
||||
</phpunit>
|
||||
|
||||
@@ -39,23 +39,25 @@ class JSLikeHTMLElement extends \DOMElement
|
||||
/**
|
||||
* Used for setting innerHTML like it's done in JavaScript:.
|
||||
*
|
||||
* @code
|
||||
* ```php
|
||||
* $div->innerHTML = '<h2>Chapter 2</h2><p>The story begins...</p>';
|
||||
* @endcode
|
||||
* ```
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if ('innerHTML' !== $name) {
|
||||
$trace = debug_backtrace();
|
||||
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
||||
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], \E_USER_NOTICE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// first, empty the element
|
||||
if (isset($this->childNodes)) {
|
||||
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
||||
$this->removeChild($this->childNodes->item($x));
|
||||
}
|
||||
}
|
||||
|
||||
// $value holds our new inner HTML
|
||||
$value = trim($value);
|
||||
@@ -77,14 +79,13 @@ class JSLikeHTMLElement extends \DOMElement
|
||||
} else {
|
||||
// $value is probably ill-formed
|
||||
$f = new \DOMDocument();
|
||||
$value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');
|
||||
|
||||
// Using <htmlfragment> will generate a warning, but so will bad HTML
|
||||
// (and by this point, bad HTML is what we've got).
|
||||
// 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.
|
||||
// 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) {
|
||||
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
||||
@@ -103,24 +104,26 @@ class JSLikeHTMLElement extends \DOMElement
|
||||
/**
|
||||
* Used for getting innerHTML like it's done in JavaScript:.
|
||||
*
|
||||
* @code
|
||||
* ```php
|
||||
* $string = $div->innerHTML;
|
||||
* @endcode
|
||||
* ```
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if ('innerHTML' === $name) {
|
||||
$inner = '';
|
||||
|
||||
if (isset($this->childNodes)) {
|
||||
foreach ($this->childNodes as $child) {
|
||||
$inner .= $this->ownerDocument->saveXML($child);
|
||||
}
|
||||
}
|
||||
|
||||
return $inner;
|
||||
}
|
||||
|
||||
$trace = debug_backtrace();
|
||||
trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
||||
trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], \E_USER_NOTICE);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
|
||||
+68
-59
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Readability;
|
||||
|
||||
use HTML5Lib\Parser;
|
||||
use Masterminds\HTML5;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
@@ -61,7 +61,7 @@ class Readability implements LoggerAwareInterface
|
||||
// constants
|
||||
const SCORE_CHARS_IN_PARAGRAPH = 100;
|
||||
const SCORE_WORDS_IN_PARAGRAPH = 20;
|
||||
const GRANDPARENT_SCORE_DIVISOR = 2.2;
|
||||
const GRANDPARENT_SCORE_DIVISOR = 2;
|
||||
const MIN_PARAGRAPH_LENGTH = 20;
|
||||
const MIN_COMMAS_IN_PARAGRAPH = 6;
|
||||
const MIN_ARTICLE_LENGTH = 200;
|
||||
@@ -92,10 +92,10 @@ class Readability implements LoggerAwareInterface
|
||||
'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/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|\bout|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',
|
||||
'killBreaks' => '/(<br\s*\/?>([ \r\n\s]| ?)*)+/',
|
||||
'media' => '!//(?:[^\.\?/]+\.)?(?:youtu(?:be)?|giphy|soundcloud|dailymotion|vimeo|pornhub|xvideos|twitvid|rutube|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',
|
||||
];
|
||||
public $tidy_config = [
|
||||
@@ -116,10 +116,9 @@ class Readability implements LoggerAwareInterface
|
||||
'drop-empty-paras' => true,
|
||||
'drop-proprietary-attributes' => false,
|
||||
'enclose-text' => true,
|
||||
'enclose-block-text' => true,
|
||||
'merge-divs' => true,
|
||||
// 'merge-spans' => true,
|
||||
'input-encoding' => '????',
|
||||
'input-encoding' => 'utf8',
|
||||
'output-encoding' => 'utf8',
|
||||
'hide-comments' => true,
|
||||
];
|
||||
@@ -138,10 +137,6 @@ class Readability implements LoggerAwareInterface
|
||||
protected $useTidy;
|
||||
// raw HTML filters
|
||||
protected $pre_filters = [
|
||||
// remove obvious scripts
|
||||
'!<script[^>]*>(.*?)</script>!is' => '',
|
||||
// remove obvious styles
|
||||
'!<style[^>]*>(.*?)</style>!is' => '',
|
||||
// remove spans as we redefine styles and they're probably special-styled
|
||||
'!</?span[^>]*>!is' => '',
|
||||
// HACK: firewall-filtered content
|
||||
@@ -158,7 +153,7 @@ class Readability implements LoggerAwareInterface
|
||||
// replace excessive br's
|
||||
'/<br\s*\/?>\s*<p/i' => '<p',
|
||||
// replace empty tags that break layouts
|
||||
'!<(?:a|div|p)[^>]+/>!is' => '',
|
||||
'!<(?:a|div|p|figure)[^>]+/>!is' => '',
|
||||
// remove all attributes on text tags
|
||||
// '!<(\s*/?\s*(?:blockquote|br|hr|code|div|article|span|footer|aside|p|pre|dl|li|ul|ol)) [^>]+>!is' => "<\\1>",
|
||||
// single newlines cleanup
|
||||
@@ -188,7 +183,7 @@ class Readability implements LoggerAwareInterface
|
||||
$this->loadHtml();
|
||||
}
|
||||
|
||||
public function setLogger(LoggerInterface $logger)
|
||||
public function setLogger(LoggerInterface $logger): void
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
@@ -292,6 +287,11 @@ class Readability implements LoggerAwareInterface
|
||||
$innerDiv->appendChild($articleContent);
|
||||
$overlay->appendChild($innerDiv);
|
||||
|
||||
// without tidy the body can (sometimes) be wiped, so re-create it
|
||||
if (false === isset($this->body->childNodes)) {
|
||||
$this->body = $this->dom->createElement('body');
|
||||
}
|
||||
|
||||
// Clear the old HTML, insert the new content.
|
||||
$this->body->setInnerHtml('');
|
||||
$this->body->appendChild($overlay);
|
||||
@@ -307,8 +307,6 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
/**
|
||||
* Run any post-process modifications to article content as necessary.
|
||||
*
|
||||
* @param \DOMElement $articleContent
|
||||
*/
|
||||
public function postProcessContent(\DOMElement $articleContent)
|
||||
{
|
||||
@@ -321,8 +319,6 @@ class Readability implements LoggerAwareInterface
|
||||
* For easier reading, convert this document to have footnotes at the bottom rather than inline links.
|
||||
*
|
||||
* @see http://www.roughtype.com/archives/2010/05/experiments_in.php
|
||||
*
|
||||
* @param \DOMElement $articleContent
|
||||
*/
|
||||
public function addFootnotes(\DOMElement $articleContent)
|
||||
{
|
||||
@@ -340,9 +336,9 @@ class Readability implements LoggerAwareInterface
|
||||
$footnoteLink = $articleLink->cloneNode(true);
|
||||
$refLink = $this->dom->createElement('a');
|
||||
$footnote = $this->dom->createElement('li');
|
||||
$linkDomain = @parse_url($footnoteLink->getAttribute('href'), PHP_URL_HOST);
|
||||
$linkDomain = @parse_url($footnoteLink->getAttribute('href'), \PHP_URL_HOST);
|
||||
if (!$linkDomain && isset($this->url)) {
|
||||
$linkDomain = @parse_url($this->url, PHP_URL_HOST);
|
||||
$linkDomain = @parse_url($this->url, \PHP_URL_HOST);
|
||||
}
|
||||
|
||||
$linkText = $this->getInnerText($articleLink);
|
||||
@@ -367,7 +363,7 @@ class Readability implements LoggerAwareInterface
|
||||
$articleLink->setAttribute('style', 'color: inherit; text-decoration: none;');
|
||||
$articleLink->setAttribute('name', 'readabilityLink-' . $linkCount);
|
||||
$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);
|
||||
$footnote->appendChild($footnoteLink);
|
||||
|
||||
@@ -385,8 +381,6 @@ class Readability implements LoggerAwareInterface
|
||||
/**
|
||||
* Prepare the article node for display. Clean out any inline styles,
|
||||
* iframes, forms, strip extraneous <p> tags, etc.
|
||||
*
|
||||
* @param \DOMNode $articleContent
|
||||
*/
|
||||
public function prepArticle(\DOMNode $articleContent)
|
||||
{
|
||||
@@ -396,6 +390,9 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
$this->logger->debug($this->lightClean ? 'Light clean enabled.' : 'Standard clean enabled.');
|
||||
|
||||
$this->clean($articleContent, 'style');
|
||||
$this->clean($articleContent, 'script');
|
||||
|
||||
$this->cleanStyles($articleContent);
|
||||
$this->killBreaks($articleContent);
|
||||
|
||||
@@ -558,7 +555,6 @@ class Readability implements LoggerAwareInterface
|
||||
* 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.
|
||||
*
|
||||
* @param \DOMElement $e
|
||||
* @param bool $excludeExternal
|
||||
*
|
||||
* @return int
|
||||
@@ -586,8 +582,6 @@ class Readability implements LoggerAwareInterface
|
||||
/**
|
||||
* Get an element relative weight.
|
||||
*
|
||||
* @param \DOMElement $e
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getWeight(\DOMElement $e)
|
||||
@@ -607,8 +601,6 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
/**
|
||||
* Remove extraneous break tags from a node.
|
||||
*
|
||||
* @param \DOMElement $node
|
||||
*/
|
||||
public function killBreaks(\DOMElement $node)
|
||||
{
|
||||
@@ -623,12 +615,10 @@ class Readability implements LoggerAwareInterface
|
||||
*
|
||||
* Updated 2012-09-18 to preserve youtube/vimeo iframes
|
||||
*
|
||||
* @param \DOMElement $e
|
||||
* @param string $tag
|
||||
*/
|
||||
public function clean(\DOMElement $e, $tag)
|
||||
{
|
||||
$currentItem = null;
|
||||
$targetList = $e->getElementsByTagName($tag);
|
||||
$isEmbed = ('audio' === $tag || 'video' === $tag || 'iframe' === $tag || 'object' === $tag || 'embed' === $tag);
|
||||
|
||||
@@ -659,7 +649,6 @@ class Readability implements LoggerAwareInterface
|
||||
* "Fishy" is an algorithm based on content length, classnames,
|
||||
* link density, number of images & embeds, etc.
|
||||
*
|
||||
* @param \DOMElement $e
|
||||
* @param string $tag
|
||||
*/
|
||||
public function cleanConditionally(\DOMElement $e, $tag)
|
||||
@@ -670,7 +659,6 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
$tagsList = $e->getElementsByTagName($tag);
|
||||
$curTagsLength = $tagsList->length;
|
||||
$node = null;
|
||||
|
||||
/*
|
||||
* Gather counts for other typical elements embedded within.
|
||||
@@ -772,8 +760,6 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
/**
|
||||
* Clean out spurious headers from an Element. Checks things like classnames and link density.
|
||||
*
|
||||
* @param \DOMElement $e
|
||||
*/
|
||||
public function cleanHeaders(\DOMElement $e)
|
||||
{
|
||||
@@ -807,7 +793,7 @@ class Readability implements LoggerAwareInterface
|
||||
*/
|
||||
public function addFlag($flag)
|
||||
{
|
||||
$this->flags = $this->flags | $flag;
|
||||
$this->flags |= $flag;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -817,13 +803,14 @@ class Readability implements LoggerAwareInterface
|
||||
*/
|
||||
public function removeFlag($flag)
|
||||
{
|
||||
$this->flags = $this->flags & ~$flag;
|
||||
$this->flags &= ~$flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug.
|
||||
*
|
||||
* @deprecated use $this->logger->debug() instead
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
protected function dbg($msg)
|
||||
@@ -835,6 +822,7 @@ class Readability implements LoggerAwareInterface
|
||||
* Dump debug info.
|
||||
*
|
||||
* @deprecated since Monolog gather log, we don't need it
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
protected function dump_dbg()
|
||||
@@ -915,8 +903,6 @@ class Readability implements LoggerAwareInterface
|
||||
/**
|
||||
* Initialize a node with the readability object. Also checks the
|
||||
* className/id for special names to add to its score.
|
||||
*
|
||||
* @param \DOMElement $node
|
||||
*/
|
||||
protected function initializeNode(\DOMElement $node)
|
||||
{
|
||||
@@ -954,7 +940,7 @@ class Readability implements LoggerAwareInterface
|
||||
case 'DD':
|
||||
case 'DT':
|
||||
case 'LI':
|
||||
$readability->value -= 2 * round($this->getLinkDensity($node), 0, PHP_ROUND_HALF_UP);
|
||||
$readability->value -= 2 * round($this->getLinkDensity($node), 0, \PHP_ROUND_HALF_UP);
|
||||
break;
|
||||
case 'ASIDE':
|
||||
case 'FOOTER':
|
||||
@@ -986,11 +972,11 @@ class Readability implements LoggerAwareInterface
|
||||
* 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.
|
||||
*
|
||||
* @param \DOMElement $page
|
||||
* @param ?\DOMElement $page
|
||||
*
|
||||
* @return \DOMElement|false
|
||||
*/
|
||||
protected function grabArticle(\DOMElement $page = null)
|
||||
protected function grabArticle($page = null)
|
||||
{
|
||||
if (!$page) {
|
||||
$page = $this->dom;
|
||||
@@ -1005,9 +991,15 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
$allElements = $page->getElementsByTagName('*');
|
||||
|
||||
for ($nodeIndex = 0; ($node = $allElements->item($nodeIndex)); ++$nodeIndex) {
|
||||
for ($nodeIndex = 0; $node = $allElements->item($nodeIndex); ++$nodeIndex) {
|
||||
$tagName = $node->tagName;
|
||||
|
||||
$nodeContent = $node->getInnerHTML();
|
||||
if (empty($nodeContent)) {
|
||||
$this->logger->debug('Skipping empty node');
|
||||
continue;
|
||||
}
|
||||
|
||||
// Some well known site uses sections as paragraphs.
|
||||
if (0 === strcasecmp($tagName, 'p') || 0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'pre') || 0 === strcasecmp($tagName, 'section')) {
|
||||
$nodesToScore[] = $node;
|
||||
@@ -1016,11 +1008,11 @@ class Readability implements LoggerAwareInterface
|
||||
// Turn divs into P tags where they have been used inappropriately
|
||||
// (as in, where they contain no other block level elements).
|
||||
if (0 === strcasecmp($tagName, 'div') || 0 === strcasecmp($tagName, 'article') || 0 === strcasecmp($tagName, 'section')) {
|
||||
if (!preg_match($this->regexps['divToPElements'], $node->getInnerHTML())) {
|
||||
if (!preg_match($this->regexps['divToPElements'], $nodeContent)) {
|
||||
$newNode = $this->dom->createElement('p');
|
||||
|
||||
try {
|
||||
$newNode->setInnerHtml($node->getInnerHTML());
|
||||
$newNode->setInnerHtml($nodeContent);
|
||||
|
||||
$node->parentNode->replaceChild($newNode, $node);
|
||||
--$nodeIndex;
|
||||
@@ -1045,7 +1037,7 @@ class Readability implements LoggerAwareInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
if (XML_TEXT_NODE === $childNode->nodeType) {
|
||||
if (\XML_TEXT_NODE === $childNode->nodeType) {
|
||||
$p = $this->dom->createElement('p');
|
||||
$p->setInnerHtml($childNode->nodeValue);
|
||||
$p->setAttribute('data-readability-styled', 'true');
|
||||
@@ -1115,7 +1107,7 @@ class Readability implements LoggerAwareInterface
|
||||
// Add the score to the parent. The grandparent gets half.
|
||||
$parentNode->getAttributeNode('readability')->value += $contentScore;
|
||||
if ($grandParentNode) {
|
||||
$grandParentNode->getAttributeNode('readability')->value += $contentScore / self::GRANDPARENT_SCORE_DIVISOR;
|
||||
$grandParentNode->getAttributeNode('readability')->value += round($contentScore / self::GRANDPARENT_SCORE_DIVISOR);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1125,7 +1117,6 @@ class Readability implements LoggerAwareInterface
|
||||
*/
|
||||
if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS) && $xpath) {
|
||||
$candidates = $xpath->query('.//*[(self::footer and count(//footer)<2) or (self::aside and count(//aside)<2)]', $page->documentElement);
|
||||
$node = null;
|
||||
|
||||
for ($c = $candidates->length - 1; $c >= 0; --$c) {
|
||||
$node = $candidates->item($c);
|
||||
@@ -1137,7 +1128,6 @@ class Readability implements LoggerAwareInterface
|
||||
}
|
||||
|
||||
$candidates = $xpath->query('.//*[not(self::body) and (@class or @id or @style) and ((number(@readability) < 40) or not(@readability))]', $page->documentElement);
|
||||
$node = null;
|
||||
|
||||
for ($c = $candidates->length - 1; $c >= 0; --$c) {
|
||||
$node = $candidates->item($c);
|
||||
@@ -1145,9 +1135,9 @@ class Readability implements LoggerAwareInterface
|
||||
// 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)
|
||||
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);
|
||||
@@ -1173,7 +1163,7 @@ class Readability implements LoggerAwareInterface
|
||||
// relatively small link density (5% or less) and be mostly unaffected by this operation.
|
||||
// If not for this we would have used XPath to find maximum @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);
|
||||
|
||||
if (!$topCandidate || $readability->value > (int) $topCandidate->getAttribute('readability')) {
|
||||
$this->logger->debug('Candidate: ' . $item->getNodePath() . ' (' . $item->getAttribute('class') . ':' . $item->getAttribute('id') . ') with score ' . $readability->value);
|
||||
@@ -1245,7 +1235,7 @@ class Readability implements LoggerAwareInterface
|
||||
$siblingNode = $siblingNodes->item($s);
|
||||
$siblingNodeName = $siblingNode->nodeName;
|
||||
$append = false;
|
||||
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . ((XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
|
||||
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . ((\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
|
||||
|
||||
if ($siblingNode->isSameNode($topCandidate)) {
|
||||
$append = true;
|
||||
@@ -1254,11 +1244,11 @@ class Readability implements LoggerAwareInterface
|
||||
$contentBonus = 0;
|
||||
|
||||
// Give a bonus if sibling nodes and top candidates have the same classname.
|
||||
if (XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
|
||||
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
|
||||
$contentBonus += ((int) $topCandidate->getAttribute('readability')) * 0.2;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1346,7 +1336,6 @@ class Readability implements LoggerAwareInterface
|
||||
* Get an element weight by attribute.
|
||||
* Uses regular expressions to tell if this element looks good or bad.
|
||||
*
|
||||
* @param \DOMElement $element
|
||||
* @param string $attribute
|
||||
*
|
||||
* @return int
|
||||
@@ -1404,7 +1393,10 @@ class Readability implements LoggerAwareInterface
|
||||
$this->logger->debug('Parsing URL: ' . $this->url);
|
||||
|
||||
if ($this->url) {
|
||||
$this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', parse_url($this->url, PHP_URL_HOST)), ['.' => '\.']) . '/';
|
||||
$host = parse_url($this->url, \PHP_URL_HOST);
|
||||
if (null !== $host) {
|
||||
$this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', $host), ['.' => '\.']) . '/';
|
||||
}
|
||||
}
|
||||
|
||||
mb_internal_encoding('UTF-8');
|
||||
@@ -1440,10 +1432,10 @@ class Readability implements LoggerAwareInterface
|
||||
unset($tidy);
|
||||
}
|
||||
|
||||
$this->html = mb_convert_encoding($this->html, 'HTML-ENTITIES', 'UTF-8');
|
||||
$this->html = self::entitizeNonAscii((string) $this->html);
|
||||
|
||||
if ('html5lib' === $this->parser) {
|
||||
$this->dom = Parser::parse($this->html);
|
||||
if ('html5lib' === $this->parser || 'html5' === $this->parser) {
|
||||
$this->dom = (new HTML5())->loadHTML($this->html);
|
||||
}
|
||||
|
||||
if ('libxml' === $this->parser) {
|
||||
@@ -1451,11 +1443,28 @@ class Readability implements LoggerAwareInterface
|
||||
|
||||
$this->dom = new \DOMDocument();
|
||||
$this->dom->preserveWhiteSpace = false;
|
||||
$this->dom->loadHTML($this->html, LIBXML_NOBLANKS | LIBXML_COMPACT | LIBXML_NOERROR);
|
||||
$this->dom->loadHTML($this->html, \LIBXML_NOBLANKS | \LIBXML_COMPACT | \LIBXML_NOERROR);
|
||||
|
||||
libxml_use_internal_errors(false);
|
||||
}
|
||||
|
||||
$this->dom->registerNodeClass('DOMElement', 'Readability\JSLikeHTMLElement');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts non-ASCII UTF-8 characters to numeric HTML entities.
|
||||
*
|
||||
* `DOMDocument::loadHTML` will parse HTML documents as ISO-8859-1 if there is no `meta[charset]` tag.
|
||||
* This means that UTF-8-encoded HTML fragments such as those coming from JSON-LD `articleBody` field would be parsed with incorrect encoding.
|
||||
*
|
||||
* @param string $html UTF-8 encoded document
|
||||
*/
|
||||
private static function entitizeNonAscii($html)
|
||||
{
|
||||
$convmap = [
|
||||
0x80, 0x1FFFFF, 0, 0x10FFFF,
|
||||
];
|
||||
|
||||
return mb_encode_numericentity($html, $convmap, 'utf8', true);
|
||||
}
|
||||
}
|
||||
|
||||
+150
-88
@@ -17,18 +17,19 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
public function testConstructDefault()
|
||||
{
|
||||
$readability = $this->getReadability('');
|
||||
$this->assertSame('utf8', $readability->tidy_config['input-encoding']);
|
||||
|
||||
$this->assertNull($readability->url);
|
||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||
}
|
||||
|
||||
public function testConstructHtml5Parser()
|
||||
{
|
||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'html5lib');
|
||||
|
||||
$this->assertEquals('http://0.0.0.0', $readability->url);
|
||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||
$this->assertEquals('<html/>', $readability->original_html);
|
||||
$this->assertSame('http://0.0.0.0', $readability->url);
|
||||
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||
$this->assertSame('<html/>', $readability->original_html);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,9 +39,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0');
|
||||
|
||||
$this->assertEquals('http://0.0.0.0', $readability->url);
|
||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||
$this->assertEquals('<html/>', $readability->original_html);
|
||||
$this->assertSame('http://0.0.0.0', $readability->url);
|
||||
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||
$this->assertSame('<html/>', $readability->original_html);
|
||||
$this->assertTrue($readability->tidied);
|
||||
}
|
||||
|
||||
@@ -49,19 +50,19 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$readability = $this->getReadability('', null, 'libxml', false);
|
||||
|
||||
$this->assertNull($readability->url);
|
||||
$this->assertEquals('', $readability->original_html);
|
||||
$this->assertSame('', $readability->original_html);
|
||||
$this->assertFalse($readability->tidied);
|
||||
|
||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||
}
|
||||
|
||||
public function testConstructSimpleWithoutTidy()
|
||||
{
|
||||
$readability = $this->getReadability('<html/>', 'http://0.0.0.0', 'libxml', false);
|
||||
|
||||
$this->assertEquals('http://0.0.0.0', $readability->url);
|
||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||
$this->assertEquals('<html/>', $readability->original_html);
|
||||
$this->assertSame('http://0.0.0.0', $readability->url);
|
||||
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||
$this->assertSame('<html/>', $readability->original_html);
|
||||
$this->assertFalse($readability->tidied);
|
||||
}
|
||||
|
||||
@@ -74,7 +75,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('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()
|
||||
@@ -85,9 +86,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
public function testInitDivP()
|
||||
@@ -98,244 +99,230 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
public function testInitDiv()
|
||||
{
|
||||
$readability = $this->getReadability('<div>' . str_repeat('This is the awesome content :)', 7) . '</div>', 'http://0.0.0.0');
|
||||
$readability->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This is the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$readability->convertLinksToFootnotes = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('readabilityFootnoteLink', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('readabilityLink-3', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('readabilityFootnoteLink', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('readabilityLink-3', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$readability->lightClean = false;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('will NOT be removed', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('<h2>', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('will NOT be removed', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringNotContainsString('<h2>', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<div readability=', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('nofollow', $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());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('alt="article"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('nofollow', $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());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('<aside>', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('<footer readability="4"/>', $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->assertStringContainsString('<footer readability="5"/>', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('alt="article"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text should be removed', $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());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$readability->lightClean = false;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('alt="article"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('alt="article"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text should be removed', $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());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('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()
|
||||
{
|
||||
$readability = $this->getReadability('<article class="awesomecontent">' . str_repeat('<p>This is an awesome text with some links, here there are the awesome</p>', 7) . '<div class="awesomecontent">This text is also an awesome text and you should know that !</div></article>', 'http://0.0.0.0');
|
||||
$readability->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('This text is also an awesome text and you should know that', $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());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEmpty($readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text is also an awesome text and you should know that', $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());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEquals('this is my title', $readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text is also an awesome text and you should know that', $readability->getContent()->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->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEquals('title2 - title3', $readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text is also an awesome text and you should know that', $readability->getContent()->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->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEquals('title2 : title3', $readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text is also an awesome text and you should know that', $readability->getContent()->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->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
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->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertEquals('this is my h1 title !', $readability->getTitle()->getInnerHtml());
|
||||
$this->assertContains('This is an awesome text with some links, here there are', $readability->getContent()->getInnerHtml());
|
||||
$this->assertNotContains('This text is also an awesome text and you should know that', $readability->getContent()->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->assertStringNotContainsString('This text is also an awesome text and you should know that', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
// dummy function to be used to the next test
|
||||
public function error2Exception($code, $string, $file, $line, $context)
|
||||
public function error2Exception($code, $string, $file, $line)
|
||||
{
|
||||
throw new \Exception($string, $code);
|
||||
}
|
||||
|
||||
public function testAutoClosingIframeNotThrowingException()
|
||||
{
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
ini_set('display_errors', true);
|
||||
set_error_handler([$this, 'error2Exception'], E_ALL | E_STRICT);
|
||||
$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">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru-RU" prefix="og: http://ogp.me/ns#">
|
||||
|
||||
@@ -358,19 +345,27 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>';
|
||||
|
||||
$readability = $this->getReadability($data, 'http://iosgames.ru/?p=22030');
|
||||
$readability->debug = true;
|
||||
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertContains('<iframe src="https://www.youtube.com/embed/PUep6xNeKjA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"> </iframe>', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('3D Touch', $readability->getTitle()->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());
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
if (false !== $oldDisplayErrors) {
|
||||
ini_set('display_errors', $oldDisplayErrors);
|
||||
}
|
||||
error_reporting($oldErrorReporting);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -423,7 +418,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
</html>';
|
||||
|
||||
$readability = $this->getReadability($data, 'http://0.0.0.0');
|
||||
$readability->debug = true;
|
||||
|
||||
$res = $readability->init();
|
||||
|
||||
@@ -440,7 +434,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertContains('This the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This the awesome content :)', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
public function testPreFilters()
|
||||
@@ -453,7 +447,7 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertContains('This the awesome and WONDERFUL content :)', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('This the awesome and WONDERFUL content :)', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
public function testChildNodeGoneNull()
|
||||
@@ -462,7 +456,6 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$html = file_get_contents('tests/fixtures/childNodeGoesNull.html');
|
||||
|
||||
$readability = $this->getReadability($html, 'http://0.0.0.0');
|
||||
$readability->debug = true;
|
||||
$readability->convertLinksToFootnotes = true;
|
||||
$res = $readability->init();
|
||||
|
||||
@@ -475,12 +468,81 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
||||
$html = file_get_contents('tests/fixtures/keepFootnotes.html');
|
||||
|
||||
$readability = $this->getReadability($html, 'http://0.0.0.0');
|
||||
$readability->debug = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertContains('<sup id="fnref1:fnfeed_2"><a href="#fn:fnfeed_2" class="footnote-ref">2</a></sup>', $readability->getContent()->getInnerHtml());
|
||||
$this->assertContains('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<sup id="fnref1:fnfeed_2"><a href="#fn:fnfeed_2" class="footnote-ref">2</a></sup>', $readability->getContent()->getInnerHtml());
|
||||
$this->assertStringContainsString('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
public function testWithWipedBody()
|
||||
{
|
||||
// from https://www.cs.cmu.edu/~rgs/alice-table.html
|
||||
$html = file_get_contents('tests/fixtures/wipedBody.html');
|
||||
|
||||
$readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', false);
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertStringContainsString('<a href="alice-I.html">Down the Rabbit-Hole</a>', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
// https://github.com/wallabag/wallabag/issues/8158
|
||||
public function testCharsetAfterTitle()
|
||||
{
|
||||
$readability = $this->getReadability('<!DOCTYPE html><html lang="et"><head><title>Tõde ja õigus I</title> <meta charset="utf-8"></head><body><p>See oli läinud aastasaja kolmanda veerandi lõpul. Päike lähenes silmapiirile, seistes sedavõrd madalas, et enam ei ulatunud valgustama ei mäkke ronivat hobust, kes puutelgedega vankrit vedas, ei vankril istuvat noort naist ega ka ligi kolmekümnelist meest, kes kõndis vankri kõrval.</p></body></html>', 'https://et.wikisource.org/wiki/T%C3%B5de_ja_%C3%B5igus_I/I');
|
||||
$readability->convertLinksToFootnotes = true;
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getTitle());
|
||||
$this->assertSame('Tõde ja õigus I', $readability->getTitle()->getInnerHtml());
|
||||
$this->assertStringContainsString('Päike lähenes', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: string, 1: string, 2?: bool}>
|
||||
*/
|
||||
public function dataForHtmlLang()
|
||||
{
|
||||
return [
|
||||
'meta' => [
|
||||
'<html lang="fr"><head><meta charset="utf-8"></head><body><article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article></body></html>',
|
||||
'fr',
|
||||
],
|
||||
'head' => [
|
||||
'<html lang="fr"><head><title>Foo</title></head><body><article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article></body></html>',
|
||||
'fr',
|
||||
],
|
||||
'headless' => [
|
||||
'<html lang="fr"><body><article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article></body></html>',
|
||||
'fr',
|
||||
// tidy would add <head> tag.
|
||||
false,
|
||||
],
|
||||
'fragment' => [
|
||||
'<article>' . str_repeat('<p>Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.</p>', 7) . '</article>',
|
||||
'',
|
||||
// tidy would add <html>.
|
||||
false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataForHtmlLang
|
||||
*/
|
||||
public function testHtmlLang($html, $lang, $useTidy = true)
|
||||
{
|
||||
$readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', $useTidy);
|
||||
$res = $readability->init();
|
||||
|
||||
$this->assertTrue($res);
|
||||
$this->assertInstanceOf(\DOMDocument::class, $readability->dom);
|
||||
$this->assertSame($lang, $readability->dom->documentElement->getAttribute('lang'));
|
||||
$this->assertInstanceOf('Readability\JSLikeHTMLElement', $readability->getContent());
|
||||
$this->assertStringContainsString('êtres', $readability->getContent()->getInnerHtml());
|
||||
}
|
||||
|
||||
private function getReadability($html, $url = null, $parser = 'libxml', $useTidy = true)
|
||||
|
||||
Vendored
+67
@@ -0,0 +1,67 @@
|
||||
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>Alice's Adventures in Wonderland (Project Gutenberg)</TITLE>
|
||||
</HEAD>
|
||||
|
||||
<frameset Rows="50, *">
|
||||
<frame src="alice-finfo.html">
|
||||
<frame src="alice-ftitle.html" name="alice-main">
|
||||
</frameset>
|
||||
|
||||
<noframes>
|
||||
<BODY>
|
||||
<H1>Alice's Adventures in Wonderland</H1>
|
||||
<H1>Lewis Carroll</H1>
|
||||
<H1>The Millennium Fulcrum Edition 3.0</H1>
|
||||
|
||||
NOTE: This is a hypertext formatted version of the Project Gutenberg edition.
|
||||
For more information, check the
|
||||
<A HREF="alice-small.txt">small print</A>
|
||||
or check out the
|
||||
<A HREF="ftp://uiarchive.cso.uiuc.edu/pub/etext/gutenberg/etext91/alice30.txt">
|
||||
full ascii text</A>. The original Tenniel illustrations are also available
|
||||
due to the efforts of Project Gutenberg. You can if you like, grab them as a
|
||||
<A HREF="ftp://uiarchive.cso.uiuc.edu/pub/etext/gutenberg/etext94/algif10.zip">
|
||||
"zip file"</A> or read the <A HREF="algif-small.txt">small print</A>
|
||||
that comes with them.
|
||||
This document is part of a small, but growing collection of html formatted
|
||||
etexts. (Others may be found in either my <A
|
||||
HREF="http://www.cs.cmu.edu/Web/People/rgs/rgs-home.html">home page</A> or
|
||||
John Ockerbloom's indexes by <A
|
||||
HREF="http://www.cs.cmu.edu/Web/bookauthors.html">author</A> and <A
|
||||
HREF="http://www.cs.cmu.edu/Web/booktitles.html">title</A>.)
|
||||
I am still trying to figure out whether anyone else is interested in these
|
||||
on-line readable documents. If you appreciate this document or would like to
|
||||
see more such, send me mail at "rgs@cs.cmu.edu".
|
||||
<P>
|
||||
<A HREF="alice01a.gif"><IMG SRC="alice01th.gif"></A>
|
||||
<P>
|
||||
<H2>CONTENTS</H2>
|
||||
<PRE>
|
||||
CHAPTER I: <A HREF="alice-I.html">Down the Rabbit-Hole</A>
|
||||
CHAPTER II: <A HREF="alice-II.html">The Pool of Tears</A>
|
||||
CHAPTER III: <A HREF="alice-III.html">A Caucus-Race and a Long Tale</A>
|
||||
CHAPTER IV: <A HREF="alice-IV.html">The Rabbit Sends in a Little Bill</A>
|
||||
CHAPTER V: <A HREF="alice-V.html">Advice from a Caterpillar</A>
|
||||
CHAPTER VI: <A HREF="alice-VI.html">Pig and Pepper</A>
|
||||
CHAPTER VII: <A HREF="alice-VII.html">A Mad Tea-Party</A>
|
||||
CHAPTER VIII: <A HREF="alice-VIII.html">The Queen's Croquet-Ground</A>
|
||||
CHAPTER IX: <A HREF="alice-IX.html">The Mock Turtle's Story</A>
|
||||
CHAPTER X: <A HREF="alice-X.html">The Lobster Quadrille</A>
|
||||
CHAPTER XI: <A HREF="alice-XI.html">Who Stole the Tarts?</A>
|
||||
CHAPTER XII: <A HREF="alice-XII.html">Alice's Evidence</A>
|
||||
</PRE>
|
||||
|
||||
<ADDRESS><A HREF="mailto:rgs@cs.cmu.edu">Robert Stockton</A></ADDRESS>
|
||||
<P>
|
||||
<!- Access counter added 5/25 1:49am ->
|
||||
<A href="http://www.dbasics.com/cgi-bin/pages.cgi?143205747"><IMG SRC="http://www.dbasics.com/cgi-bin/counter.cgi?143205747.2&(none)"></A> Access statistics from htmlZine
|
||||
<!- This page has been visited
|
||||
A HREF="http://counter.digits.com/wc?--info=yes&--name=rgsalice"
|
||||
IMG SRC="http://counter.digits.com/wc/-d/4/-r/-z/rgsalice"
|
||||
ALIGN=absmiddle WIDTH=60 HEIGHT=20 BORDER=0 HSPACE=4 ALT="????"/A
|
||||
times since March 2, 1996. ->
|
||||
</BODY>
|
||||
</noframes>
|
||||
</HTML>
|
||||
Reference in New Issue
Block a user