Bug #79701 [Opn->Ver]: getElementById does not correctly work with duplicate definitions

From: Date: Mon, 15 Jun 2020 08:23:33 +0000
Subject: Bug #79701 [Opn->Ver]: getElementById does not correctly work with duplicate definitions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227487@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79701&edit=1

 ID:                 79701
 Updated by:         cmb@php.net
 Reported by:        beberlei@php.net
 Summary:            getElementById does not correctly work with
                     duplicate definitions
-Status:             Open
+Status:             Verified
 Type:               Bug
 Package:            DOM XML related
 PHP Version:        7.2.31
 Block user comment: N
 Private report:     N

 New Comment:

Confirmed: <https://3v4l.org/Ols6m>


Previous Comments:
------------------------------------------------------------------------
[2020-06-15 02:17:41] beberlei@php.net

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 this bug report at https://bugs.php.net/bug.php?id=79701&edit=1


Thread (2 messages)

« previous php.bugs (#227487) next »