Skip to content

Commit

Permalink
Merge pull request #52 from inpsyde/fix-php82-deprecation-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrann0us authored Nov 28, 2024
2 parents 8bd2a3c + 1180455 commit 2b2d2ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 302 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"mikey179/vfsstream": "^1.6.8",
"inpsyde/php-coding-standards": "^1",
"vimeo/psalm": "@stable",
"php-stubs/wordpress-stubs": ">=6.0@stable",
"johnpbloch/wordpress-core": ">=6.0"
"php-stubs/wordpress-stubs": ">=6.2@stable",
"johnpbloch/wordpress-core": ">=6.2"
},
"autoload": {
"psr-4": {
Expand All @@ -45,8 +45,7 @@
},
"autoload-dev": {
"psr-4": {
"Inpsyde\\Assets\\Tests\\Unit\\": "tests/phpunit/Unit/",
"Inpsyde\\Assets\\Tests\\Integration\\": "tests/phpunit/Integration/"
"Inpsyde\\Assets\\Tests\\Unit\\": "tests/phpunit/Unit/"
}
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/phpunit/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/phpunit/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" convertDeprecationsToExceptions="true" processIsolation="false" stopOnFailure="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
Expand All @@ -14,7 +14,7 @@
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">tests/phpunit/Unit</directory>
<directory>tests/phpunit/Unit</directory>
</testsuite>
</testsuites>
<logging/>
Expand Down
87 changes: 13 additions & 74 deletions src/OutputFilter/AttributesOutputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,95 +15,34 @@

use Inpsyde\Assets\Asset;

/**
* @psalm-suppress UndefinedMethod
*/
class AttributesOutputFilter implements AssetOutputFilter
{
private const ROOT_ELEMENT_START = '<root>';
private const ROOT_ELEMENT_END = '</root>';

/**
* @param string $html
* @param Asset $asset
*
* @return string
*
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
* @psalm-suppress PossiblyFalseArgument
* @psalm-suppress ArgumentTypeCoercion
*/
public function __invoke(string $html, Asset $asset): string
{
$attributes = $asset->attributes();
if (count($attributes) === 0) {
if (!class_exists(\WP_HTML_Tag_Processor::class) || count($attributes) === 0) {
return $html;
}

$html = $this->wrapHtmlIntoRoot($html);

$doc = new \DOMDocument();
libxml_use_internal_errors(true);
@$doc->loadHTML(
mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"),
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
);
libxml_clear_errors();
$tags = new \WP_HTML_Tag_Processor($html);

$scripts = $doc->getElementsByTagName('script');
foreach ($scripts as $script) {
// Only extend <script> elements with "src" attribute
// and don't extend inline <script></script> before and after.
if (!$script->hasAttribute('src')) {
continue;
}
$this->applyAttributes($script, $attributes);
// Only extend <script> elements with "src" attribute
// and don't extend inline <script></script> before and after.
if (
$tags->next_tag(['tag_name' => 'script'])
&& (string) $tags->get_attribute('src')
) {
$this->applyAttributes($tags, $attributes);
}

return $this->removeRootElement($doc->saveHTML());
}

/**
* Wrapping multiple scripts into a root-element
* to be able to load it via DOMDocument.
*
* @param string $html
*
* @return string
*/
protected function wrapHtmlIntoRoot(string $html): string
{
return self::ROOT_ELEMENT_START . $html . self::ROOT_ELEMENT_END;
}

/**
* Remove root element and return original HTML.
*
* @param string $html
*
* @return string
* @see AttributesOutputFilter::wrapHtmlIntoRoot()
*
*/
protected function removeRootElement(string $html): string
{
$regex = '~' . self::ROOT_ELEMENT_START . '(.+?)' . self::ROOT_ELEMENT_END . '~s';
preg_match($regex, $html, $matches);

return $matches[1];
return $tags->get_updated_html();
}

/**
* @param \DOMElement $script
* @param array $attributes
*
* @return void
*/
protected function applyAttributes(\DOMElement $script, array $attributes)
protected function applyAttributes(\WP_HTML_Tag_Processor $script, array $attributes): void
{
foreach ($attributes as $key => $value) {
$key = esc_attr((string) $key);
if ($script->hasAttribute($key)) {
if ((string) $script->get_attribute($key)) {
continue;
}
if (is_bool($value) && !$value) {
Expand All @@ -113,7 +52,7 @@ protected function applyAttributes(\DOMElement $script, array $attributes)
? esc_attr($key)
: esc_attr((string) $value);

$script->setAttribute($key, $value);
$script->set_attribute($key, $value);
}
}
}
222 changes: 0 additions & 222 deletions tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php

This file was deleted.

0 comments on commit 2b2d2ac

Please sign in to comment.