Skip to content

Commit 168bc81

Browse files
committed
Fix incorrect attribute existence check in DOMElement::setAttributeNodeNS()
Closes GH-11776.
1 parent d439ee1 commit 168bc81

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP NEWS
88

99
- DOM:
1010
. Fix DOMEntity field getter bugs. (nielsdos)
11+
. Fix incorrect attribute existence check in DOMElement::setAttributeNodeNS.
12+
(nielsdos)
1113

1214
- FFI:
1315
. Fix leaking definitions when using FFI::cdef()->new(...). (ilutov)

ext/dom/element.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ PHP_METHOD(DOMElement, setAttributeNodeNS)
862862

863863
nsp = attrp->ns;
864864
if (nsp != NULL) {
865-
existattrp = xmlHasNsProp(nodep, nsp->href, attrp->name);
865+
existattrp = xmlHasNsProp(nodep, attrp->name, nsp->href);
866866
} else {
867867
existattrp = xmlHasProp(nodep, attrp->name);
868868
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
setAttributeNodeNS with same URI but different prefix
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
// Spec doesn't require to remove redundant namespace declarations.
8+
// This means that when appending a new attribute the old namespace declaration is reused, hence the prefix stays "foo".
9+
// Firefox cleans up the old namespace declaration, so there the prefix does change.
10+
// Chrome doesn't clean it up, so there the prefix doesn't change.
11+
// Our behaviour is the same as Chrome's due to implementation difficulties.
12+
$doc = new DOMDocument();
13+
$doc->appendChild($doc->createElement('container'));
14+
$attribute = $doc->createAttributeNS('http://php.net/ns1', 'foo:hello');
15+
$attribute->nodeValue = '1';
16+
var_dump($doc->documentElement->setAttributeNodeNS($attribute)?->nodeValue);
17+
echo $doc->saveXML(), "\n";
18+
$attribute = $doc->createAttributeNS('http://php.net/ns1', 'bar:hello');
19+
$attribute->nodeValue = '2';
20+
var_dump($doc->documentElement->setAttributeNodeNS($attribute)?->nodeValue);
21+
echo $doc->saveXML(), "\n";
22+
$attribute = $doc->createAttributeNS('http://php.net/ns1', 'hello');
23+
$attribute->nodeValue = '3';
24+
var_dump($doc->documentElement->setAttributeNodeNS($attribute)?->nodeValue);
25+
echo $doc->saveXML(), "\n";
26+
?>
27+
--EXPECT--
28+
NULL
29+
<?xml version="1.0"?>
30+
<container xmlns:foo="http://php.net/ns1" foo:hello="1"/>
31+
32+
string(1) "1"
33+
<?xml version="1.0"?>
34+
<container xmlns:foo="http://php.net/ns1" foo:hello="2"/>
35+
36+
string(1) "2"
37+
<?xml version="1.0"?>
38+
<container xmlns:foo="http://php.net/ns1" foo:hello="3"/>

0 commit comments

Comments
 (0)