From d0df6010ab537be917219c1d2f3a91822243a137 Mon Sep 17 00:00:00 2001 From: michalsn Date: Wed, 6 Jan 2021 10:03:44 +0100 Subject: [PATCH] Fix DOMParser rules to search also outside the body tag --- system/Test/DOMParser.php | 6 +++--- tests/system/Test/DOMParserTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index a987d9acff32..ab8616756d34 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -237,19 +237,19 @@ protected function doXPath(?string $search, string $element, array $paths = []) { $path = empty($selector['tag']) ? "id(\"{$selector['id']}\")" - : "//body//{$selector['tag']}[@id=\"{$selector['id']}\"]"; + : "//{$selector['tag']}[@id=\"{$selector['id']}\"]"; } // By Class elseif (! empty($selector['class'])) { $path = empty($selector['tag']) ? "//*[@class=\"{$selector['class']}\"]" - : "//body//{$selector['tag']}[@class=\"{$selector['class']}\"]"; + : "//{$selector['tag']}[@class=\"{$selector['class']}\"]"; } // By tag only elseif (! empty($selector['tag'])) { - $path = "//body//{$selector['tag']}"; + $path = "//{$selector['tag']}"; } if (! empty($selector['attr'])) diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index 1efe81e8c2b9..49e88c625f59 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -97,6 +97,19 @@ public function testSeeHTML() $this->assertTrue($dom->see('

')); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/3984 + */ + public function testSeeHTMLOutsideBodyTag() + { + $dom = new DOMParser(); + + $html = 'My Title

Hello World

'; + $dom->withString($html); + + $this->assertTrue($dom->see('My Title', 'title')); + } + public function testSeeFail() { $dom = new DOMParser();