Skip to content

Fix incorrect attribute existence check in DOMElement::setAttributeNodeNS() #11776

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ PHP_METHOD(DOMElement, setAttributeNodeNS)

nsp = attrp->ns;
if (nsp != NULL) {
existattrp = xmlHasNsProp(nodep, nsp->href, attrp->name);
existattrp = xmlHasNsProp(nodep, attrp->name, nsp->href);
} else {
existattrp = xmlHasProp(nodep, attrp->name);
}
Expand Down
38 changes: 38 additions & 0 deletions ext/dom/tests/setAttributeNodeNS_same_uri_different_prefix.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
setAttributeNodeNS with same URI but different prefix
--EXTENSIONS--
dom
--FILE--
<?php
// Spec doesn't require to remove redundant namespace declarations.
// This means that when appending a new attribute the old namespace declaration is reused, hence the prefix stays "foo".
// Firefox cleans up the old namespace declaration, so there the prefix does change.
// Chrome doesn't clean it up, so there the prefix doesn't change.
// Our behaviour is the same as Chrome's due to implementation difficulties.
$doc = new DOMDocument();
$doc->appendChild($doc->createElement('container'));
$attribute = $doc->createAttributeNS('http://php.net/ns1', 'foo:hello');
$attribute->nodeValue = '1';
var_dump($doc->documentElement->setAttributeNodeNS($attribute)?->nodeValue);
echo $doc->saveXML(), "\n";
$attribute = $doc->createAttributeNS('http://php.net/ns1', 'bar:hello');
$attribute->nodeValue = '2';
var_dump($doc->documentElement->setAttributeNodeNS($attribute)?->nodeValue);
echo $doc->saveXML(), "\n";
$attribute = $doc->createAttributeNS('http://php.net/ns1', 'hello');
$attribute->nodeValue = '3';
var_dump($doc->documentElement->setAttributeNodeNS($attribute)?->nodeValue);
echo $doc->saveXML(), "\n";
?>
--EXPECT--
NULL
<?xml version="1.0"?>
<container xmlns:foo="http://php.net/ns1" foo:hello="1"/>

string(1) "1"
<?xml version="1.0"?>
<container xmlns:foo="http://php.net/ns1" foo:hello="2"/>

string(1) "2"
<?xml version="1.0"?>
<container xmlns:foo="http://php.net/ns1" foo:hello="3"/>