Skip to content

Commit

Permalink
Change XML-Namespace order from PHP 8.1.21 (Case 161357) (#7)
Browse files Browse the repository at this point in the history
The Update to PHP 8.1.21 apparently changes the search order for defined namespaces during the process  of reconciliation, resulting in finding the namespace having the prefix 'html' prior to the default one  without prefix, as the search seems to start from the last defined prefix now. This results in DOMElements  getting a wrong prefix while being appended to another element using `appendChild()` e.g. in  `BaseParsingHelper::dump()`.

This is definitely a supposition, as we do not get everything completely what happens in the corresponding  commits

- php/php-src@b1d8e24 (https://bugs.php.net/bug.php?id=67440)
- php/php-src@b30be40 (https://bugs.php.net/bug.php?id=55294)

but it perfectly matches our observation that changing the namespace order fixes several bugs and tests  in various private projects of ours.
  • Loading branch information
relthyg authored Jul 12, 2023
1 parent 296711b commit d760f5b
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/Webfactory/Dom/HTMLParsingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

abstract class HTMLParsingHelper extends BaseParsingHelper {

protected $implicitNamespaces = array(
'' => 'http://www.w3.org/1999/xhtml', // default ns
'html' => 'http://www.w3.org/1999/xhtml', // für XPath
'hx' => 'http://purl.org/NET/hinclude' // fuer HInclude http://mnot.github.io/hinclude/; ein Weg um z.B. Controller in Symfony per Ajax zu embedden
);
protected $implicitNamespaces;

public function __construct()
{
$this->implicitNamespaces = $this->defineImplicitNamespaces();

libxml_set_external_entity_loader(function ($public, $system, $context) {
if (isset($public)) {
$catalogDir = __DIR__ . '/../../../xml-catalog/';
Expand All @@ -43,4 +41,37 @@ protected function wrapFragment($fragment, $declaredNamespaces)
{
return "<html {$this->xmlNamespaceDeclaration($declaredNamespaces)}>$fragment</html>";
}

protected function defineImplicitNamespaces(): array
{
/**
* The Update to PHP 8.1.21 apparently changes the search order for defined namespaces during the process
* of reconciliation, resulting in finding the namespace having the prefix 'html' prior to the default one
* without prefix, as the search seems to start from the last defined prefix now. This results in DOMElements
* getting a wrong prefix while being appended to another element using `appendChild()` e.g. in
* `BaseParsingHelper::dump()`.
*
* This is definitely a supposition, as we do not get everything completely what happens in the corresponding
* commits
*
* - https://github.com/php/php-src/commit/b1d8e240e688cae810c83b364772bf140ac45f42 (https://bugs.php.net/bug.php?id=67440)
* - https://github.com/php/php-src/commit/b30be40b86b62fc681c432fd96840d8e57e172a5 (https://bugs.php.net/bug.php?id=55294)
*
* but it perfectly matches our observation that changing the namespace order fixes several bugs and tests
* in various private projects of ours.
*/
if (phpversion('xml') >= '8.1.21') {
return [
'html' => 'http://www.w3.org/1999/xhtml', // für XPath
'' => 'http://www.w3.org/1999/xhtml', // default ns
'hx' => 'http://purl.org/NET/hinclude' // fuer HInclude http://mnot.github.io/hinclude/; ein Weg um z.B. Controller in Symfony per Ajax zu embedden
];
}

return [
'' => 'http://www.w3.org/1999/xhtml', // default ns
'html' => 'http://www.w3.org/1999/xhtml', // für XPath
'hx' => 'http://purl.org/NET/hinclude' // fuer HInclude http://mnot.github.io/hinclude/; ein Weg um z.B. Controller in Symfony per Ajax zu embedden
];
}
}

0 comments on commit d760f5b

Please sign in to comment.