diff --git a/ext/dom/VmDom.php b/ext/dom/VmDom.php index f6b55d93551..2396838d9bb 100644 --- a/ext/dom/VmDom.php +++ b/ext/dom/VmDom.php @@ -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) { diff --git a/test/compliance/cases/dom/document_loadhtml_get_element_by_id.phpt b/test/compliance/cases/dom/document_loadhtml_get_element_by_id.phpt new file mode 100644 index 00000000000..8c4bfa41dd5 --- /dev/null +++ b/test/compliance/cases/dom/document_loadhtml_get_element_by_id.phpt @@ -0,0 +1,12 @@ +--TEST-- +dom DOMDocument::getElementById() after loadHTML() HTML id attribute (#15251) +--FILE-- +loadHTML('
hello
'); +$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 diff --git a/test/repro/maintainer_gap_dom_loadhtml_get_element_by_id.php b/test/repro/maintainer_gap_dom_loadhtml_get_element_by_id.php new file mode 100644 index 00000000000..919b9dea6c7 --- /dev/null +++ b/test/repro/maintainer_gap_dom_loadhtml_get_element_by_id.php @@ -0,0 +1,25 @@ +loadHTML('hello
'); +$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";