php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77686 Removed elements are still returned by getElementById
Submitted: 2019-03-01 07:11 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: gtisza at gmail dot com Assigned:
Status: Closed Package: DOM XML related
PHP Version: 7.3.2 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gtisza at gmail dot com
New email:
PHP Version: OS:

 

 [2019-03-01 07:11 UTC] gtisza at gmail dot com
Description:
------------
Removing a DOMElement from a HTML document does not refresh the id index; getElementById() will still return the removed element. (getElementsByTagName(), in contrast, works as expected.)
Live test: https://3v4l.org/UQYTG

Calling setIdAttribute() on the removed element apparently updates the index and fixes getElementById() behavior. It does not fix it for any children of the removed node, however.

Test script:
---------------
$doc = new DOMDocument;
$doc->loadHTML('<html><body id="x"><div id="y"></div></body></html>');
$body = $doc->getElementById('x');
$div = $doc->getElementById('y');
$doc->getElementById('y'); // <div id="y">


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-01 22:14 UTC] bd808@php.net
Corrected test script including the DOM removal:
---------------
$doc = new DOMDocument;
$doc->loadHTML('<html><body id="x"><div id="y"></div></body></html>');
$body = $doc->getElementById('x');
$div = $doc->getElementById('y');
$body->removeChild($div);
$doc->getElementById('y'); // <div id="y">
 [2019-03-04 06:47 UTC] gtisza at gmail dot com
Apparently the id is only unindexed when the element is destroyed, not when it is detached from the document: https://3v4l.org/61Hbv (thanks to Ori Livneh for tracking that down). So not that easy to run into this bug as typically no reference is retained to a detached element, once the variable used for detaching goes out of scope.
 [2019-03-04 07:16 UTC] gtisza at gmail dot com
Also it seems certain method calls refresh the index (setIdAttribute, specifically): https://3v4l.org/kk8iv
Only for a single element, though, not the subtree of the element.
 [2021-05-04 04:09 UTC] trowski@php.net
The following pull request has been associated:

Patch Name: Fix #77686: Immediately remove ID from DOMDocument when removing child element
On GitHub:  https://github.com/php/php-src/pull/6936
Patch:      https://github.com/php/php-src/pull/6936.patch
 [2023-06-04 14:35 UTC] git@php.net
Automatic comment on behalf of nielsdos
Revision: https://github.com/php/php-src/commit/0e34ac864a20bd03a35741db09f0bdf72ae56874
Log: Fix bug #77686: Removed elements are still returned by getElementById
 [2023-06-04 14:35 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 06 21:01:29 2024 UTC