From 517e0fe98e2048b8d99233b4f564a62f8f022b47 Mon Sep 17 00:00:00 2001 From: Iurii Rogulia Date: Fri, 18 Sep 2026 19:39:35 +0300 Subject: [PATCH] analyze: prove document offsets past a non-ASCII character MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every html offset assertion added with analyze() was ASCII-only, so none of them discriminated a code-point document offset from a byte one through the span adapter. That is the mistake available to PHP specifically: strings here are bytes, and on an ASCII document mb_strpos and strpos return the same number. `café` in the first span puts the byte offset one ahead of the code-point one, and the test asserts that premise before asserting the offset, so a byte offset leaking out of the span adapter fails loudly rather than passing quietly. Test-only. 17 tests, 52 assertions; PHPStan level 8 and phpcs clean. Co-Authored-By: Claude Opus 5 --- tests/AnalyzeTest.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/AnalyzeTest.php b/tests/AnalyzeTest.php index 123656c..44a19c7 100644 --- a/tests/AnalyzeTest.php +++ b/tests/AnalyzeTest.php @@ -178,14 +178,23 @@ public function testHtmlModeReportsDocumentOffsets(): void public function testHtmlModeReportsDocumentOffsetsInALaterSpan(): void { - // Three spans, and the change is in the third: the two markers before it are the only - // code points in the joined array with no origin, so a doubled or dropped one shifts this - // offset and nothing in a one- or two-span document would notice. - $input = '

one

two

Wait... three

'; + // Three spans, a non-ASCII character before the change, and the change in the third + // span: the two markers are the only code points in the joined array with no origin, so + // a doubled or dropped one shifts this offset and nothing in a one- or two-span document + // would notice. `café` then puts the byte offset one ahead of the code-point offset, so + // a byte offset leaking out of the span adapter cannot pass this either -- which is the + // mistake available to PHP specifically. + $input = '

café

two

Wait... three

'; $changes = Polytypo::analyze($input, 'en-US', 'html'); $this->assertNotSame([], $changes); $this->assertSame('ellipsis', $changes[0]->ruleId); - $this->assertSame(mb_strpos($input, '...', 0, 'UTF-8'), $changes[0]->start); + $want = mb_strpos($input, '...', 0, 'UTF-8'); + $this->assertSame( + $want, + strpos($input, '...') - 1, + 'test premise: the byte offset differs from the code-point one', + ); + $this->assertSame($want, $changes[0]->start); } public function testReportsRulesInPipelineOrder(): void