Bug #79701 [NEW]: getElementById does not correctly work with duplicate definitions
| From: | beberlei@php.net | Date: | Mon, 15 Jun 2020 02:17:41 +0000 |
| Subject: | Bug #79701 [NEW]: getElementById does not correctly work with duplicate definitions | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-227484@lists.php.net to get a copy of this message | ||
From: beberlei
Operating system:
PHP version: 7.2.31
Package: DOM XML related
Bug Type: Bug
Bug description:getElementById does not correctly work with duplicate definitions
Description:
------------
A DOM id element is supposed to be unique, but sometimes the world is
messy. The DOM specification says:
> The getElementById(elementId) method, when invoked, must return the
first element,
> in tree order, within thisâs descendants, whose ID is elementId, and
null if there
> is no such element otherwise.
PHP uses libxmls ID Map functionality, which does not allow duplicates.
The return value of "xmlAddID" is not checked for the error, so elements
with duplicate ID don't cause a problem.
However if you remove the first element with an ID, or re-order the
elements, then the specifications assumption of returning the first
element in tree order does not work anymore.
Test script:
---------------
<?php
$dom = new DOMDocument();
$root = $dom->createElement('html');
$dom->appendChild($root);
$el1 = $dom->createElement('p1');
$el1->setAttribute('id', 'foo');
$el1->setIdAttribute('id', true);
$root->appendChild($el1);
$el2 = $dom->createElement('p2');
$el2->setAttribute('id', 'foo');
$el2->setIdAttribute('id', true);
$root->appendChild($el2);
unset($el1, $el2);
$root->removeChild($dom->getElementById('foo'));
var_dump($dom->getElementById('foo'));
Expected result:
----------------
Returns Element <p2 id="foo" />
Actual result:
--------------
NULL
--
Edit bug report at https://bugs.php.net/bug.php?id=79701&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79701&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79701&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79701&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79701&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79701&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79701&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79701&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79701&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79701&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79701&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79701&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79701&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79701&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79701&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79701&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79701&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79701&r=mysqlcfg