mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38870cdff1 | ||
|
|
9bdd3b6b2e | ||
|
|
f14428e4c0 | ||
|
|
23f824a1ce | ||
|
|
2a57124528 | ||
|
|
0975574bdb | ||
|
|
9ed89bde92 | ||
|
|
2c6c6d5987 | ||
|
|
c5407ec07c | ||
|
|
7cd8476d38 | ||
|
|
82083c872b | ||
|
|
6689f19956 |
@@ -16,11 +16,11 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php:
|
php:
|
||||||
- "7.4"
|
- "7.2"
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v3"
|
||||||
|
|
||||||
- name: "Install PHP"
|
- name: "Install PHP"
|
||||||
uses: "shivammathur/setup-php@v2"
|
uses: "shivammathur/setup-php@v2"
|
||||||
@@ -33,7 +33,7 @@ jobs:
|
|||||||
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: "Install dependencies with Composer"
|
- name: "Install dependencies with Composer"
|
||||||
uses: "ramsey/composer-install@v1"
|
uses: "ramsey/composer-install@v2"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
|
|||||||
@@ -24,10 +24,12 @@ jobs:
|
|||||||
- "7.4"
|
- "7.4"
|
||||||
- "8.0"
|
- "8.0"
|
||||||
- "8.1"
|
- "8.1"
|
||||||
|
- "8.2"
|
||||||
|
- "8.3"
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v3"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -46,7 +48,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@v2"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
@@ -67,7 +69,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v3"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -86,7 +88,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@v2"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
|
|
||||||
@@ -118,7 +120,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: "Checkout"
|
- name: "Checkout"
|
||||||
uses: "actions/checkout@v2"
|
uses: "actions/checkout@v3"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 2
|
fetch-depth: 2
|
||||||
|
|
||||||
@@ -137,7 +139,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@v2"
|
||||||
with:
|
with:
|
||||||
composer-options: "--optimize-autoloader --prefer-dist"
|
composer-options: "--optimize-autoloader --prefer-dist"
|
||||||
dependency-versions: "lowest"
|
dependency-versions: "lowest"
|
||||||
|
|||||||
+7
-6
@@ -26,7 +26,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.2.0",
|
"php": ">=7.2.0",
|
||||||
"ext-mbstring": "*",
|
"ext-mbstring": "*",
|
||||||
"psr/log": "^1.0",
|
"psr/log": "^1.0.1 || ^2.0 || ^3.0",
|
||||||
"masterminds/html5": "^2.7"
|
"masterminds/html5": "^2.7"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0",
|
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0",
|
||||||
"phpstan/phpstan": "^1.3",
|
"phpstan/phpstan": "^1.3",
|
||||||
"phpstan/phpstan-phpunit": "^1.0",
|
"phpstan/phpstan-phpunit": "^1.0",
|
||||||
"rector/rector": "^0.12.15"
|
"rector/rector": "^0.15.0"
|
||||||
},
|
},
|
||||||
"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 +46,10 @@
|
|||||||
"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",
|
||||||
}
|
"rector": "rector process",
|
||||||
|
"test": "simple-phpunit -v"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -6,7 +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
|
checkMissingIterableValueType: false
|
||||||
|
|
||||||
|
|||||||
+10
-11
@@ -2,31 +2,30 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Rector\Core\Configuration\Option;
|
use Rector\Config\RectorConfig;
|
||||||
use Rector\Core\ValueObject\PhpVersion;
|
use Rector\Core\ValueObject\PhpVersion;
|
||||||
use Rector\Set\ValueObject\LevelSetList;
|
use Rector\Set\ValueObject\LevelSetList;
|
||||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
|
||||||
|
|
||||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
|
||||||
$parameters = $containerConfigurator->parameters();
|
|
||||||
|
|
||||||
|
return static function (RectorConfig $rectorConfig): void {
|
||||||
// paths to refactor; solid alternative to CLI arguments
|
// paths to refactor; solid alternative to CLI arguments
|
||||||
$parameters->set(Option::PATHS, [
|
$rectorConfig->paths([
|
||||||
__DIR__ . '/src',
|
__DIR__ . '/src',
|
||||||
__DIR__ . '/tests',
|
__DIR__ . '/tests',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Path to phpstan with extensions, that PHPSTan in Rector uses to determine types
|
// Path to phpstan with extensions, that PHPSTan in Rector uses to determine types
|
||||||
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan.neon');
|
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');
|
||||||
|
|
||||||
$parameters->set(Option::BOOTSTRAP_FILES, [
|
$rectorConfig->bootstrapFiles([
|
||||||
__DIR__ . '/vendor/bin/.phpunit/phpunit-8.5-0/vendor/autoload.php',
|
__DIR__ . '/vendor/bin/.phpunit/phpunit/vendor/autoload.php',
|
||||||
__DIR__ . '/vendor/autoload.php',
|
__DIR__ . '/vendor/autoload.php',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Define what rule sets will be applied
|
// Define what rule sets will be applied
|
||||||
$containerConfigurator->import(LevelSetList::UP_TO_PHP_72);
|
$rectorConfig->sets([
|
||||||
|
LevelSetList::UP_TO_PHP_72,
|
||||||
|
]);
|
||||||
|
|
||||||
// is your PHP version different from the one your refactor to?
|
// is your PHP version different from the one your refactor to?
|
||||||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_72);
|
$rectorConfig->phpVersion(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)
|
||||||
{
|
{
|
||||||
|
|||||||
+39
-41
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
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;
|
||||||
@@ -108,10 +107,6 @@ class Readability implements LoggerAwareInterface
|
|||||||
protected $useTidy;
|
protected $useTidy;
|
||||||
// raw HTML filters
|
// raw HTML filters
|
||||||
protected $pre_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
|
// remove spans as we redefine styles and they're probably special-styled
|
||||||
'!</?span[^>]*>!is' => '',
|
'!</?span[^>]*>!is' => '',
|
||||||
// HACK: firewall-filtered content
|
// HACK: firewall-filtered content
|
||||||
@@ -119,7 +114,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
// HACK: replace linebreaks plus br's with p's
|
// HACK: replace linebreaks plus br's with p's
|
||||||
'!(<br[^>]*>[ \r\n\s]*){2,}!i' => '</p><p>',
|
'!(<br[^>]*>[ \r\n\s]*){2,}!i' => '</p><p>',
|
||||||
// replace noscripts
|
// replace noscripts
|
||||||
//'!</?noscript>!is' => '',
|
// '!</?noscript>!is' => '',
|
||||||
// replace fonts to spans
|
// replace fonts to spans
|
||||||
'!<(/?)font[^>]*>!is' => '<\\1span>',
|
'!<(/?)font[^>]*>!is' => '<\\1span>',
|
||||||
];
|
];
|
||||||
@@ -130,8 +125,8 @@ class Readability implements LoggerAwareInterface
|
|||||||
// replace empty tags that break layouts
|
// replace empty tags that break layouts
|
||||||
'!<(?:a|div|p|figure)[^>]+/>!is' => '',
|
'!<(?:a|div|p|figure)[^>]+/>!is' => '',
|
||||||
// remove all attributes on text tags
|
// 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>",
|
// '!<(\s*/?\s*(?:blockquote|br|hr|code|div|article|span|footer|aside|p|pre|dl|li|ul|ol)) [^>]+>!is' => "<\\1>",
|
||||||
//single newlines cleanup
|
// single newlines cleanup
|
||||||
"/\n+/" => "\n",
|
"/\n+/" => "\n",
|
||||||
// modern web...
|
// modern web...
|
||||||
'!<pre[^>]*>\s*<code!is' => '<pre',
|
'!<pre[^>]*>\s*<code!is' => '<pre',
|
||||||
@@ -165,7 +160,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get article title element.
|
* Get article title element.
|
||||||
*
|
*
|
||||||
* @return DOMElement
|
* @return \DOMElement
|
||||||
*/
|
*/
|
||||||
public function getTitle()
|
public function getTitle()
|
||||||
{
|
{
|
||||||
@@ -175,7 +170,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get article content element.
|
* Get article content element.
|
||||||
*
|
*
|
||||||
* @return DOMElement
|
* @return \DOMElement
|
||||||
*/
|
*/
|
||||||
public function getContent()
|
public function getContent()
|
||||||
{
|
{
|
||||||
@@ -284,7 +279,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): void
|
||||||
{
|
{
|
||||||
if ($this->convertLinksToFootnotes && !preg_match('/\bwiki/', $this->url)) {
|
if ($this->convertLinksToFootnotes && !preg_match('/\bwiki/', $this->url)) {
|
||||||
$this->addFootnotes($articleContent);
|
$this->addFootnotes($articleContent);
|
||||||
@@ -296,7 +291,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): void
|
||||||
{
|
{
|
||||||
$footnotesWrapper = $this->dom->createElement('footer');
|
$footnotesWrapper = $this->dom->createElement('footer');
|
||||||
$footnotesWrapper->setAttribute('class', 'readability-footnotes');
|
$footnotesWrapper->setAttribute('class', 'readability-footnotes');
|
||||||
@@ -339,7 +334,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);
|
||||||
|
|
||||||
@@ -360,12 +355,15 @@ class Readability implements LoggerAwareInterface
|
|||||||
*/
|
*/
|
||||||
public function prepArticle(\DOMNode $articleContent): void
|
public function prepArticle(\DOMNode $articleContent): void
|
||||||
{
|
{
|
||||||
if (!$articleContent instanceof DOMElement) {
|
if (!$articleContent instanceof \DOMElement) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->debug($this->lightClean ? 'Light clean enabled.' : 'Standard clean enabled.');
|
$this->logger->debug($this->lightClean ? 'Light clean enabled.' : 'Standard clean enabled.');
|
||||||
|
|
||||||
|
$this->clean($articleContent, 'style');
|
||||||
|
$this->clean($articleContent, 'script');
|
||||||
|
|
||||||
$this->cleanStyles($articleContent);
|
$this->cleanStyles($articleContent);
|
||||||
$this->killBreaks($articleContent);
|
$this->killBreaks($articleContent);
|
||||||
|
|
||||||
@@ -457,9 +455,9 @@ 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)
|
||||||
*/
|
*/
|
||||||
public function getInnerText($e, bool $normalizeSpaces = true, bool $flattenLines = false): string
|
public function getInnerText($e, bool $normalizeSpaces = true, bool $flattenLines = false): string
|
||||||
{
|
{
|
||||||
@@ -483,7 +481,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Remove the style attribute on every $e and under.
|
* Remove the style attribute on every $e and under.
|
||||||
*/
|
*/
|
||||||
public function cleanStyles(DOMElement $e): void
|
public function cleanStyles(\DOMElement $e): void
|
||||||
{
|
{
|
||||||
if (\is_object($e)) {
|
if (\is_object($e)) {
|
||||||
$elems = $e->getElementsByTagName('*');
|
$elems = $e->getElementsByTagName('*');
|
||||||
@@ -516,7 +514,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
public function getLinkDensity(DOMElement $e, bool $excludeExternal = false): float
|
public function getLinkDensity(\DOMElement $e, bool $excludeExternal = false): float
|
||||||
{
|
{
|
||||||
$links = $e->getElementsByTagName('a');
|
$links = $e->getElementsByTagName('a');
|
||||||
$textLength = mb_strlen($this->getInnerText($e, true, true));
|
$textLength = mb_strlen($this->getInnerText($e, true, true));
|
||||||
@@ -539,7 +537,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get an element relative weight.
|
* Get an element relative weight.
|
||||||
*/
|
*/
|
||||||
public function getWeight(DOMElement $e): int
|
public function getWeight(\DOMElement $e): int
|
||||||
{
|
{
|
||||||
if (!$this->flagIsActive(self::FLAG_WEIGHT_ATTRIBUTES)) {
|
if (!$this->flagIsActive(self::FLAG_WEIGHT_ATTRIBUTES)) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -557,7 +555,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): void
|
||||||
{
|
{
|
||||||
$html = $node->getInnerHTML();
|
$html = $node->getInnerHTML();
|
||||||
$html = preg_replace($this->regexps['killBreaks'], '<br />', $html);
|
$html = preg_replace($this->regexps['killBreaks'], '<br />', $html);
|
||||||
@@ -570,7 +568,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
*
|
*
|
||||||
* Updated 2012-09-18 to preserve youtube/vimeo iframes
|
* Updated 2012-09-18 to preserve youtube/vimeo iframes
|
||||||
*/
|
*/
|
||||||
public function clean(DOMElement $e, string $tag): void
|
public function clean(\DOMElement $e, string $tag): void
|
||||||
{
|
{
|
||||||
$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);
|
||||||
@@ -602,7 +600,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* "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.
|
||||||
*/
|
*/
|
||||||
public function cleanConditionally(DOMElement $e, string $tag): void
|
public function cleanConditionally(\DOMElement $e, string $tag): void
|
||||||
{
|
{
|
||||||
if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
|
if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
|
||||||
return;
|
return;
|
||||||
@@ -715,7 +713,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): void
|
||||||
{
|
{
|
||||||
for ($headerIndex = 1; $headerIndex < 3; ++$headerIndex) {
|
for ($headerIndex = 1; $headerIndex < 3; ++$headerIndex) {
|
||||||
$headers = $e->getElementsByTagName('h' . $headerIndex);
|
$headers = $e->getElementsByTagName('h' . $headerIndex);
|
||||||
@@ -755,7 +753,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Get the article title as an H1.
|
* Get the article title as an H1.
|
||||||
*
|
*
|
||||||
* @return DOMElement
|
* @return \DOMElement
|
||||||
*/
|
*/
|
||||||
protected function getArticleTitle()
|
protected function getArticleTitle()
|
||||||
{
|
{
|
||||||
@@ -827,7 +825,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): void
|
||||||
{
|
{
|
||||||
if (!isset($node->tagName)) {
|
if (!isset($node->tagName)) {
|
||||||
return;
|
return;
|
||||||
@@ -895,11 +893,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(\DOMElement $page = null)
|
||||||
{
|
{
|
||||||
if (!$page) {
|
if (!$page) {
|
||||||
$page = $this->dom;
|
$page = $this->dom;
|
||||||
@@ -1041,7 +1039,7 @@ 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);
|
||||||
|
|
||||||
foreach ($ancestors as $level => $ancestor) {
|
foreach ($ancestors as $level => $ancestor) {
|
||||||
if (!$ancestor->nodeName || !$ancestor->parentNode) {
|
if (!$ancestor->nodeName || !$ancestor->parentNode) {
|
||||||
@@ -1212,7 +1210,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')) {
|
||||||
@@ -1293,8 +1291,8 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
// To ensure a node does not interfere with readability styles, remove its classnames & ids.
|
// To ensure a node does not interfere with readability styles, remove its classnames & ids.
|
||||||
// Now done via RegExp post_filter.
|
// Now done via RegExp post_filter.
|
||||||
//$nodeToAppend->removeAttribute('class');
|
// $nodeToAppend->removeAttribute('class');
|
||||||
//$nodeToAppend->removeAttribute('id');
|
// $nodeToAppend->removeAttribute('id');
|
||||||
// Append sibling and subtract from our list as appending removes a node.
|
// Append sibling and subtract from our list as appending removes a node.
|
||||||
$articleContent->appendChild($nodeToAppend);
|
$articleContent->appendChild($nodeToAppend);
|
||||||
}
|
}
|
||||||
@@ -1341,7 +1339,7 @@ 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.
|
||||||
*/
|
*/
|
||||||
protected function weightAttribute(DOMElement $element, string $attribute): int
|
protected function weightAttribute(\DOMElement $element, string $attribute): int
|
||||||
{
|
{
|
||||||
if (!$element->hasAttribute($attribute)) {
|
if (!$element->hasAttribute($attribute)) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1428,7 +1426,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);
|
||||||
@@ -1444,14 +1442,14 @@ 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::class, \Readability\JSLikeHTMLElement::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getAncestors(DOMElement $node, int $maxDepth = 0): array
|
private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
|
||||||
{
|
{
|
||||||
$ancestors = [];
|
$ancestors = [];
|
||||||
$i = 0;
|
$i = 0;
|
||||||
while ($node->parentNode instanceof DOMElement) {
|
while ($node->parentNode instanceof \DOMElement) {
|
||||||
$ancestors[] = $node->parentNode;
|
$ancestors[] = $node->parentNode;
|
||||||
if (++$i === $maxDepth) {
|
if (++$i === $maxDepth) {
|
||||||
break;
|
break;
|
||||||
@@ -1471,7 +1469,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
}, iterator_to_array($node->childNodes)), true));
|
}, iterator_to_array($node->childNodes)), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function hasSingleTagInsideElement(DOMElement $node, string $tag): bool
|
private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
|
||||||
{
|
{
|
||||||
if (1 !== $node->childNodes->length || $node->childNodes->item(0)->nodeName !== $tag) {
|
if (1 !== $node->childNodes->length || $node->childNodes->item(0)->nodeName !== $tag) {
|
||||||
return false;
|
return false;
|
||||||
@@ -1491,11 +1489,11 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Tidy must be configured to not clean the input for this function to
|
* Tidy must be configured to not clean the input for this function to
|
||||||
* work as expected, see $this->tidy_config['clean']
|
* work as expected, see $this->tidy_config['clean']
|
||||||
*/
|
*/
|
||||||
private function isNodeVisible(DOMElement $node): bool
|
private function isNodeVisible(\DOMElement $node): bool
|
||||||
{
|
{
|
||||||
return !($node->hasAttribute('style')
|
return !($node->hasAttribute('style')
|
||||||
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))
|
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))
|
||||||
)
|
)
|
||||||
&& !$node->hasAttribute('hidden');
|
&& !$node->hasAttribute('hidden');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-30
@@ -335,48 +335,56 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
public function testAutoClosingIframeNotThrowingException(): void
|
public function testAutoClosingIframeNotThrowingException(): void
|
||||||
{
|
{
|
||||||
error_reporting(\E_ALL | \E_STRICT);
|
$oldErrorReporting = error_reporting(\E_ALL | \E_STRICT);
|
||||||
ini_set('display_errors', '1');
|
$oldDisplayErrors = 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) {
|
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
|
||||||
throw new \Exception($errstr, $errno);
|
throw new \Exception($errstr, $errno);
|
||||||
}, \E_ALL | \E_STRICT);
|
}, \E_ALL | \E_STRICT);
|
||||||
|
|
||||||
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
try {
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru-RU" prefix="og: http://ogp.me/ns#">
|
$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#">
|
||||||
|
|
||||||
<head profile="http://gmpg.org/xfn/11">
|
<head profile="http://gmpg.org/xfn/11">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body class="single single-post postid-22030 single-format-standard">
|
<body class="single single-post postid-22030 single-format-standard">
|
||||||
<div id="wrapper">
|
<div id="wrapper">
|
||||||
<div id="content">
|
<div id="content">
|
||||||
<div class="post-22030 post type-post status-publish format-standard has-post-thumbnail hentry category-video category-reviews tag-193" id="post-22030">
|
<div class="post-22030 post type-post status-publish format-standard has-post-thumbnail hentry category-video category-reviews tag-193" id="post-22030">
|
||||||
<h1>3D Touch — будущее мобильных игр</h1>
|
<h1>3D Touch — будущее мобильных игр</h1>
|
||||||
<div class="postdate">Автор: <strong>Сергей Пак</strong> | Просмотров: 1363 | Опубликовано: 14 сентября 2015 </div>
|
<div class="postdate">Автор: <strong>Сергей Пак</strong> | Просмотров: 1363 | Опубликовано: 14 сентября 2015 </div>
|
||||||
<div class="entry">
|
<div class="entry">
|
||||||
<p>Компания Apple представила новую технологию 3D Touch, которая является прямым потомком более ранней версии Force Touch — последняя, напомним, используется сейчас в трекпадах Macbook Pro и Macbook 2015. Теперь управлять устройством стало в разы проще, и Force Touch открывает перед пользователями новые возможности, но при этом 3D Touch — это про другое. Дело в том, что теперь и на мобильных устройствах интерфейс будет постепенно меняться, кардинальные перемены ждут мобильный гейминг, потому что здесь разработчики действительно могут разгуляться.<span id="more-22030"></span></p>
|
<p>Компания Apple представила новую технологию 3D Touch, которая является прямым потомком более ранней версии Force Touch — последняя, напомним, используется сейчас в трекпадах Macbook Pro и Macbook 2015. Теперь управлять устройством стало в разы проще, и Force Touch открывает перед пользователями новые возможности, но при этом 3D Touch — это про другое. Дело в том, что теперь и на мобильных устройствах интерфейс будет постепенно меняться, кардинальные перемены ждут мобильный гейминг, потому что здесь разработчики действительно могут разгуляться.<span id="more-22030"></span></p>
|
||||||
<p><iframe src="https://www.youtube.com/embed/PUep6xNeKjA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>
|
<p><iframe src="https://www.youtube.com/embed/PUep6xNeKjA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>
|
||||||
<p>Итак, просто представьте себе, что iPhone 6S — это, по большому счету, отличная игровая приставка, которую вы носите с собой, а еще она может выдавать невероятной красоты картинку. Но проблема заключается, пожалуй, в том, что управлять персонажем в играх довольно трудно — он неповоротлив, обладает заторможенной реакцией, а игровой клиент зачастую требует перегруза интерфейса для того, чтобы обеспечить максимально большое количество возможностей. Благодаря трехуровневому нажатию можно избавиться от лишних кнопок и обеспечить более качественный обзор местности, и при этом пользователь будет закрывать пальцами минимальное пространство.</p>
|
<p>Итак, просто представьте себе, что iPhone 6S — это, по большому счету, отличная игровая приставка, которую вы носите с собой, а еще она может выдавать невероятной красоты картинку. Но проблема заключается, пожалуй, в том, что управлять персонажем в играх довольно трудно — он неповоротлив, обладает заторможенной реакцией, а игровой клиент зачастую требует перегруза интерфейса для того, чтобы обеспечить максимально большое количество возможностей. Благодаря трехуровневому нажатию можно избавиться от лишних кнопок и обеспечить более качественный обзор местности, и при этом пользователь будет закрывать пальцами минимальное пространство.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</body>
|
||||||
</div>
|
</html>';
|
||||||
</body>
|
|
||||||
</html>';
|
|
||||||
|
|
||||||
$readability = $this->getReadability($data, 'http://iosgames.ru/?p=22030');
|
$readability = $this->getReadability($data, 'http://iosgames.ru/?p=22030');
|
||||||
$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(JSLikeHTMLElement::class, $readability->getContent());
|
||||||
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
$this->assertInstanceOf(JSLikeHTMLElement::class, $readability->getTitle());
|
||||||
$this->assertStringContainsString('<iframe src="https://www.youtube.com/embed/PUep6xNeKjA" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"> </iframe>', $readability->getContent()->getInnerHtml());
|
$this->assertStringContainsString('<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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user