Bug #77686 [PATCH]: Removed elements are still returned by getElementById
| From: | trowski@php.net | Date: | Tue, 04 May 2021 04:09:23 +0000 |
| Subject: | Bug #77686 [PATCH]: Removed elements are still returned by getElementById | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-233670@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77686&edit=1
ID: 77686
Patch added by: trowski@php.net
Reported by: gtisza at gmail dot com
Summary: Removed elements are still returned by
getElementById
Status: Open
Type: Bug
Package: DOM XML related
PHP Version: 7.3.2
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2019-03-04 07:16:44] 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.
------------------------------------------------------------------------
[2019-03-04 06:47:42] 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-01 22:14:54] 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-01 07:11:23] 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">
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77686&edit=1