HtmlMin icon indicating copy to clipboard operation
HtmlMin copied to clipboard

Uncaught TypeError with PHP 8.3 and `id` attribute

Open lee-peuker opened this issue 7 months ago • 4 comments

What is this feature about (expected vs actual behaviour)?

Running into an error after when minifying <div id="test"></div> after updating to PHP 8.3. I expected to get the minified html.

How can I reproduce it?

Script to reproduce issue (using PHP 8.3.0):

<?php declare(strict_types=1);

use voku\helper\HtmlMin;

include_once(__DIR__ . '/../vendor/autoload.php');

$vokuMinifier = new HtmlMin;

$vokuMinifier->minify('<div id="test"></div>');

Error:

Fatal error: Uncaught TypeError: Cannot assign null to property DOMElement::$id of type string in /vendor/voku/simple_html_dom/src/voku/helper/AbstractSimpleHtmlDom.php:174
Stack trace:
#0 /vendor/voku/html-min/src/voku/helper/HtmlMinDomObserverOptimizeAttributes.php(146): voku\helper\AbstractSimpleHtmlDom->__set('id', NULL)
#1 /vendor/voku/html-min/src/voku/helper/HtmlMin.php(1688): voku\helper\HtmlMinDomObserverOptimizeAttributes->domElementAfterMinification(Object(voku\helper\SimpleHtmlDom), Object(voku\helper\HtmlMin))
#2 /vendor/voku/html-min/src/voku/helper/HtmlMin.php(1667): voku\helper\HtmlMin->notifyObserversAboutDomElementAfterMinification(Object(voku\helper\SimpleHtmlDom))
#3 /vendor/voku/html-min/src/voku/helper/HtmlMin.php(1370): voku\helper\HtmlMin->minifyHtmlDom('<div id="test">...', false)
#4 /scripts/htmlMin.php(9): voku\helper\HtmlMin->minify('<div id="test">...')
#5 {main}
  thrown in /vendor/voku/simple_html_dom/src/voku/helper/AbstractSimpleHtmlDom.php on line 174

Does it take minutes, hours or days to fix?

No idea

Any additional information?

in AbstractSimpleHtmlDom.php:174 there already seems to be some catch in place, if this is extended with $nameOrig === 'id' the error is fixed, but not sure if this is wanted.

                    // INFO: Cannot assign null to property DOMNode::* of type string
                    if ($nameOrig === 'prefix' || $nameOrig === 'textContent') {
                        $value = (string)$value;
                    }

lee-peuker avatar Dec 06 '23 17:12 lee-peuker