From e93e16457033c73952801e39d8c8957e84050dc9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Jul 2020 06:49:18 +0700 Subject: [PATCH 1/6] Fixes #3319 add multibyte support on Domparser::see() --- system/Test/DOMParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 343ef752028e..726f7fb3e858 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -143,7 +143,7 @@ public function see(string $search = null, string $element = null): bool if (is_null($element)) { $content = $this->dom->saveHTML(); - return strpos($content, $search) !== false; + return mb_strpos($content, $search) !== false; } $result = $this->doXPath($search, $element); From 7f842989526e9110e60570cf04b535151799b1fe Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Jul 2020 07:42:44 +0700 Subject: [PATCH 2/6] ensure pass $this->dom->documentElement on dom->saveHTML() --- system/Test/DOMParser.php | 6 +++--- tests/system/Test/DOMParserTest.php | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 726f7fb3e858..95b10279b6f8 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -76,7 +76,7 @@ public function __construct() */ public function getBody(): string { - return $this->dom->saveHTML(); + return $this->dom->saveHTML($this->dom->documentElement); } /** @@ -142,8 +142,8 @@ public function see(string $search = null, string $element = null): bool // If Element is null, we're just scanning for text if (is_null($element)) { - $content = $this->dom->saveHTML(); - return mb_strpos($content, $search) !== false; + $content = $this->dom->saveHTML($this->dom->documentElement); + return strpos($content, $search) !== false; } $result = $this->doXPath($search, $element); diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index 94597caacc18..cff19d70084e 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -66,14 +66,25 @@ public function testParseSelectorWithAttribute() $this->assertEquals(['href' => 'http://example.com'], $selector['attr']); } - public function testSeeText() + public function provideText() + { + return [ + ['Hello World'], + ['Hellö Wörld'], + ]; + } + + /** + * @dataProvider provideText + */ + public function testSeeText($text) { $dom = new DOMParser(); - $html = '

Hello World

'; + $html = '

' . $text . '

'; $dom->withString($html); - $this->assertTrue($dom->see('Hello World')); + $this->assertTrue($dom->see($text)); } public function testSeeHTML() From 8c3e677d36367fb935365ab446a55ed4b255935e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Jul 2020 07:46:55 +0700 Subject: [PATCH 3/6] use pass $this->dom->documentElement only on search --- system/Test/DOMParser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 95b10279b6f8..95b889693757 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -76,7 +76,7 @@ public function __construct() */ public function getBody(): string { - return $this->dom->saveHTML($this->dom->documentElement); + return $this->dom->saveHTML(); } /** @@ -143,7 +143,7 @@ public function see(string $search = null, string $element = null): bool if (is_null($element)) { $content = $this->dom->saveHTML($this->dom->documentElement); - return strpos($content, $search) !== false; + return mb_strpos($content, $search) !== false; } $result = $this->doXPath($search, $element); From 7a2179a74a309e1b42664ae08cd9ad32a76b10e7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Jul 2020 07:51:01 +0700 Subject: [PATCH 4/6] by pass $this->dom->documentElement to saveHTML(), the mb_strpos is not needed, use strpos instead --- system/Test/DOMParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 95b889693757..a6164e54325f 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -143,7 +143,7 @@ public function see(string $search = null, string $element = null): bool if (is_null($element)) { $content = $this->dom->saveHTML($this->dom->documentElement); - return mb_strpos($content, $search) !== false; + return strpos($content, $search) !== false; } $result = $this->doXPath($search, $element); From 5bfa4c7ae53307c33ab7d57cd024483885e5eebc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Jul 2020 08:05:53 +0700 Subject: [PATCH 5/6] ensure multibyte search work on dom->see(text, element) as well --- tests/system/Test/DOMParserTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index cff19d70084e..1efe81e8c2b9 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -107,14 +107,17 @@ public function testSeeFail() $this->assertFalse($dom->see('Hello Worlds')); } - public function testSeeElement() + /** + * @dataProvider provideText + */ + public function testSeeElement($text) { $dom = new DOMParser(); - $html = '

Hello World

'; + $html = '

' . $text . '

'; $dom->withString($html); - $this->assertTrue($dom->see('Hello World', 'h1')); + $this->assertTrue($dom->see($text, 'h1')); } public function testSeeElementPartialText() From d8e6ea364629871aa37242256bd1a2444b9cedae Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 16 Jul 2020 08:26:20 +0700 Subject: [PATCH 6/6] ensure use mb_strpos() for scan text to always get correct result --- system/Test/DOMParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index a6164e54325f..95b889693757 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -143,7 +143,7 @@ public function see(string $search = null, string $element = null): bool if (is_null($element)) { $content = $this->dom->saveHTML($this->dom->documentElement); - return strpos($content, $search) !== false; + return mb_strpos($content, $search) !== false; } $result = $this->doXPath($search, $element);