php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75779 DOMElement#setAttribute() does not process prefixed namespace declarations
Submitted: 2018-01-08 16:33 UTC Modified: 2024-03-09 16:09 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: daverandom@php.net Assigned: nielsdos (profile)
Status: Closed Package: DOM XML related
PHP Version: 7.1.13 OS: Any
Private report: No CVE-ID: None
 [2018-01-08 16:33 UTC] daverandom@php.net
Description:
------------
DOMElement#setAttribute() correctly handles setting the default (unprefixed) namespace, but does not handle prefixed namespaces, and simply creates a DOMAttr and appends it to the node.

Test script:
---------------
$xml = '<?xml version="1.0" ?><test />';
$doc = new DOMDocument();
$doc->loadXML($xml);

$element = $doc->documentElement;

// Unprefixed works fine

echo "\nElement should not have namespace:\n";
var_dump($element->lookupPrefix('test:unprefixed'), $element->lookupNamespaceURI(null));

echo "\nsetAttribute() call should succeed:\n";
var_dump($element->setAttribute('xmlns', 'test:unprefixed'));

echo "\nElement should have xmlns attribute:\n";
var_dump($element->hasAttribute('xmlns'));

echo "\nElement should have namespace:\n";
var_dump($element->lookupPrefix('test:unprefixed'), $element->lookupNamespaceURI(null));

// Prefixed does not work

echo "\nElement should not have namespace:\n";
var_dump($element->lookupPrefix('test:prefixed'), $element->lookupNamespaceURI('test'));

echo "\nsetAttribute() call should succeed:\n";
var_dump($element->setAttribute('xmlns:test', 'test:prefixed'));

echo "\nElement should have xmlns attribute:\n";
var_dump($element->hasAttribute('xmlns:test'));

echo "\nElement should have namespace:\n";
var_dump($element->lookupPrefix('test:prefixed'), $element->lookupNamespaceURI('test'));


Expected result:
----------------
Namespace is registered with prefix

Actual result:
--------------
DOMAttr is added to node but namespace is not registered

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2024-03-09 16:09 UTC] nielsdos@php.net
This is fixed in the new opt-in spec-compliance mode, which was merged in https://github.com/php/php-src/pull/13031.
For more information about the opt-in mode, please see https://wiki.php.net/rfc/opt_in_dom_spec_compliance.
In short: the behaviour of the old DOM classes are unaffected for backwards-compatibility reasons. There are new DOM classes where your code is correctly handled.
This feature will be available in PHP 8.4.
 [2024-03-09 16:09 UTC] nielsdos@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nielsdos
 [2024-03-09 16:09 UTC] nielsdos@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 09:01:28 2024 UTC