Skip to content

Commit 02725d8

Browse files
authored
Merge pull request #4070 from michalsn/fix/assertSee-fix
Fix DOMParser rules to search also outside the body tag
2 parents 440ca29 + d0df601 commit 02725d8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

system/Test/DOMParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,19 @@ protected function doXPath(?string $search, string $element, array $paths = [])
237237
{
238238
$path = empty($selector['tag'])
239239
? "id(\"{$selector['id']}\")"
240-
: "//body//{$selector['tag']}[@id=\"{$selector['id']}\"]";
240+
: "//{$selector['tag']}[@id=\"{$selector['id']}\"]";
241241
}
242242
// By Class
243243
elseif (! empty($selector['class']))
244244
{
245245
$path = empty($selector['tag'])
246246
? "//*[@class=\"{$selector['class']}\"]"
247-
: "//body//{$selector['tag']}[@class=\"{$selector['class']}\"]";
247+
: "//{$selector['tag']}[@class=\"{$selector['class']}\"]";
248248
}
249249
// By tag only
250250
elseif (! empty($selector['tag']))
251251
{
252-
$path = "//body//{$selector['tag']}";
252+
$path = "//{$selector['tag']}";
253253
}
254254

255255
if (! empty($selector['attr']))

tests/system/Test/DOMParserTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public function testSeeHTML()
9797
$this->assertTrue($dom->see('<h1>'));
9898
}
9999

100+
/**
101+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3984
102+
*/
103+
public function testSeeHTMLOutsideBodyTag()
104+
{
105+
$dom = new DOMParser();
106+
107+
$html = '<html><head><title>My Title</title></head><body><h1>Hello World</h1></body></html>';
108+
$dom->withString($html);
109+
110+
$this->assertTrue($dom->see('My Title', 'title'));
111+
}
112+
100113
public function testSeeFail()
101114
{
102115
$dom = new DOMParser();

0 commit comments

Comments
 (0)