[php-src] Issue #11833: Importing DOM node doesn't set correct default namespace
| From: | RemkoNolten | Date: | Mon, 31 Jul 2023 13:32:20 +0000 |
| Subject: | [php-src] Issue #11833: Importing DOM node doesn't set correct default namespace | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245076@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11833
Author: RemkoNolten
### Description
In PHP 8.1.21, the behavior of importing DOM nodes from other DOMDocument instances changed.
I'm not 100% sure it's a bug, but it did break backwards compatibility in our case and
maybe other people are running into similar issues, so I wanted to report it nonetheless.
To explain it briefly: We have a MathML document which contains a SVG as a fallback. The SVG has a
svg: prefix/namespace to distinguish the MathML part from the SVG part. When we
imported the SVG node to a new DOMDocument, it (correctly?) removed the svg: prefix in
the resulting XML, so we could render it in the browser. Since version 8.1.21, the svg:
prefix isn't removed anymore, so the browser doesn't recognize it and fails to render it.
I was not able to find a workaround using createDocumentFragment or setting/removing
the xmlns attribute(s) so far...
The following code:
```php
<?php
$xml = <<<XML
<?xml version="1.0"?>
<m:math xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:svg="http://www.w3.org/2000/svg">
<m:annotation-xml encoding="image/svg+xml">
<svg:svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1">
</svg:svg>
</m:annotation-xml>
</m:math>
XML;
// Create a DOM Document with the above MathML+SVG:
$dom = new DOMDocument();
$dom->loadXml($xml);
$svg = $dom->getElementsByTagName('svg')->item(0);
// Create a new DOM Document and import the SVG node
$dom2 = new DOMDocument();
$importedNode = $dom2->importNode($svg);
$dom2->appendChild($importedNode);
// Output the results
echo $dom2->saveXml();
```
Resulted in this output:
```
<?xml version="1.0"?>
<svg:svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg"
version="1.1"/>
```
But I expected this output instead:
```
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg"
version="1.1"/>
```
Or even better:
```
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"/>
```
I've created a Online PHP example: https://onlinephp.io/c/90a9b - 3v4l.org doesn't support
8.1.21 (yet).
### PHP Version
PHP 8.1.21
### Operating System
Debian Bullseye