|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79701 getElementById does not correctly work with duplicate definitions
Submitted: 2020-06-15 02:17 UTC Modified: 2020-06-15 08:23 UTC
From: Assigned:
Status: Verified Package: DOM XML related
PHP Version: 7.2.31 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
29 - 9 = ?
Subscribe to this entry?

 [2020-06-15 02:17 UTC]
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:

$dom = new DOMDocument();
$root = $dom->createElement('html');

$el1 = $dom->createElement('p1');
$el1->setAttribute('id', 'foo');
$el1->setIdAttribute('id', true);


$el2 = $dom->createElement('p2');
$el2->setAttribute('id', 'foo');
$el2->setIdAttribute('id', true);

unset($el1, $el2);



Expected result:
Returns Element <p2 id="foo" />

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-15 08:23 UTC]
-Status: Open +Status: Verified
 [2020-06-15 08:23 UTC]
Confirmed: <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Mar 03 16:01:24 2021 UTC