Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/dom/VmDom.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,9 @@ private static function indexElementIdsRecursive(ObjectEntry $document, ObjectEn
if (null === $idAttr && null !== $nodeState->idAttributeName) {
$idAttr = $nodeState->idAttributeName;
}
if (null === $idAttr && $docState->isHtmlDocument && isset($nodeState->attributes['id'])) {
$idAttr = 'id';
}
if (null !== $idAttr) {
$value = $nodeState->attributes[$idAttr] ?? null;
if (null !== $value && '' !== $value) {
Expand Down
12 changes: 12 additions & 0 deletions test/compliance/cases/dom/document_loadhtml_get_element_by_id.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
dom DOMDocument::getElementById() after loadHTML() HTML id attribute (#15251)
--FILE--
<?php
$doc = new DOMDocument();
$doc->loadHTML('<p id="target">hello</p>');
$found = $doc->getElementById('target');
echo null !== $found ? $found->textContent : 'null', "\n";
echo null === $doc->getElementById('missing') ? 'missing_null' : 'missing_found', "\n";
--EXPECT--
hello
missing_null
25 changes: 25 additions & 0 deletions test/repro/maintainer_gap_dom_loadhtml_get_element_by_id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Issue #15251 — DOMDocument::getElementById() after loadHTML() indexes HTML id attributes.
*/

$doc = new DOMDocument();
$doc->loadHTML('<p id="target">hello</p>');
$found = $doc->getElementById('target');
if (null === $found) {
echo "fail: getElementById returned null\n";
exit(1);
}
if ('hello' !== $found->textContent) {
echo 'fail: textContent=', $found->textContent, "\n";
exit(1);
}
if (null !== $doc->getElementById('missing')) {
echo "fail: missing id should be null\n";
exit(1);
}

echo "ok\n";