Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mb_convert_encoding PHP 8.2 deprecation #52

Merged
merged 10 commits into from
Nov 28, 2024
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) {

Check warning on line 23 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L23

Added line #L23 was not covered by tests
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);

Check warning on line 27 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L27

Added line #L27 was not covered by tests

$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')

Check warning on line 33 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L32-L33

Added lines #L32 - L33 were not covered by tests
) {
$this->applyAttributes($tags, $attributes);

Check warning on line 35 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L35

Added line #L35 was not covered by tests
}

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();

Check warning on line 38 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L38

Added line #L38 was not covered by tests
}

/**
* @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

Check warning on line 41 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L41

Added line #L41 was not covered by tests
{
foreach ($attributes as $key => $value) {
$key = esc_attr((string) $key);
if ($script->hasAttribute($key)) {
if ((string) $script->get_attribute($key)) {

Check warning on line 45 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L45

Added line #L45 was not covered by tests
continue;
}
if (is_bool($value) && !$value) {
Expand All @@ -113,7 +52,7 @@
? esc_attr($key)
: esc_attr((string) $value);

$script->setAttribute($key, $value);
$script->set_attribute($key, $value);

Check warning on line 55 in src/OutputFilter/AttributesOutputFilter.php

View check run for this annotation

Codecov / codecov/patch

src/OutputFilter/AttributesOutputFilter.php#L55

Added line #L55 was not covered by tests
}
}
}
222 changes: 0 additions & 222 deletions tests/phpunit/Unit/OutputFilter/AttributesOutputFilterTest.php

This file was deleted.

Loading