diff --git a/src/Engine/NarrowTarget.php b/src/Engine/NarrowTarget.php new file mode 100644 index 0000000..bb22434 --- /dev/null +++ b/src/Engine/NarrowTarget.php @@ -0,0 +1,42 @@ + $localeData * @return array */ - private static function prepare(array $localeData): array + private static function prepare(array $localeData, int $narrowTarget = 0x202F): array { $data = $localeData['nbsp']; @@ -248,7 +248,7 @@ private static function prepare(array $localeData): array continue; } - $target = $pair['innerSpace'] === 'nbsp' ? self::NBSP : self::NNBSP; + $target = $pair['innerSpace'] === 'nbsp' ? self::NBSP : $narrowTarget; $quotePairs[] = ['open' => $openCp, 'close' => $closeCp, 'target' => $target]; } @@ -929,11 +929,15 @@ private static function forwardBindingSubRule( */ public static function scan(array $cp, array $localeData, RuleContext $ctx): array { - $prep = self::prepare($localeData); + $prep = self::prepare($localeData, $ctx->narrowTarget); $claims = array_fill(0, count($cp) + 1, null); - self::punctuationSubRule($cp, $prep, $claims, $prep['beforePunctuation'], self::NBSP, self::NNBSP); // N1 - self::punctuationSubRule($cp, $prep, $claims, $prep['narrowBeforePunctuation'], self::NNBSP, self::NBSP); // N2 + self::punctuationSubRule($cp, $prep, $claims, $prep['beforePunctuation'], self::NBSP, self::NNBSP); // N1 + // N2's target is NARROW-TARGET (nbsp.md 3.1a); the last argument is the NOBREAK member + // that is not the target, which is what the sub-rule converts. With the substitution on, + // N2 and N1 want the same character -- never different ones. + $narrowOther = $ctx->narrowTarget === self::NBSP ? self::NNBSP : self::NBSP; + self::punctuationSubRule($cp, $prep, $claims, $prep['narrowBeforePunctuation'], $ctx->narrowTarget, $narrowOther); // N2 self::shortWordsSubRule($cp, $prep, $claims); // N3 self::abbreviationsSubRule($cp, $prep, $claims); // N4 self::unitsSubRule($cp, $prep, $claims); // N5 diff --git a/src/Polytypo.php b/src/Polytypo.php index ed42cb6..579dfda 100644 --- a/src/Polytypo.php +++ b/src/Polytypo.php @@ -5,6 +5,7 @@ namespace Polytypo; use Polytypo\Engine\Codepoints; +use Polytypo\Engine\NarrowTarget; use Polytypo\Engine\Pipeline; use Polytypo\Engine\RuleContext; use Polytypo\Modes\Html; @@ -44,12 +45,14 @@ public static function transform( string $mode = 'text', ?string $dialect = null, ?array $rules = null, + ?string $narrowNbsp = null, ): string { $resolvedMode = self::resolveMode($mode); + $narrowTarget = NarrowTarget::resolve($narrowNbsp); return match ($resolvedMode) { - 'text' => self::transformText($input, $locale, $dialect, $rules), - 'html' => self::transformHtml($input, $locale, $dialect, $rules), + 'text' => self::transformText($input, $locale, $dialect, $rules, $narrowTarget), + 'html' => self::transformHtml($input, $locale, $dialect, $rules, $narrowTarget), // 'markdown': no dialect is implemented by this runtime (verified this session: // league/commonmark gives no position data at all on inline text nodes, and no other // maintained PHP CommonMark/GFM library with the needed raw-extent span property @@ -82,12 +85,14 @@ public static function analyze( string $mode = 'text', ?string $dialect = null, ?array $rules = null, + ?string $narrowNbsp = null, ): array { $resolvedMode = self::resolveMode($mode); + $narrowTarget = NarrowTarget::resolve($narrowNbsp); return match ($resolvedMode) { - 'text' => self::analyzeText($input, $locale, $dialect, $rules), - 'html' => self::analyzeHtml($input, $locale, $dialect, $rules), + 'text' => self::analyzeText($input, $locale, $dialect, $rules, $narrowTarget), + 'html' => self::analyzeHtml($input, $locale, $dialect, $rules, $narrowTarget), // 'markdown': not implemented by this runtime, exactly as for transform() -- A1 // requires analyze() to reject what transform() rejects, with the same code. default => self::transformMarkdown($input, $locale, $dialect, $rules), @@ -107,8 +112,13 @@ private static function resolveMode(string $mode): string } /** @param array|null $rules */ - private static function transformText(string $input, string $locale, ?string $dialect, ?array $rules): string - { + private static function transformText( + string $input, + string $locale, + ?string $dialect, + ?array $rules, + int $narrowTarget, + ): string { if ($dialect !== null) { throw new PolytypoException( PolytypoException::CODE_INVALID_DIALECT, @@ -117,15 +127,20 @@ private static function transformText(string $input, string $locale, ?string $di } [, $localeData, $plan] = Pipeline::prepare($locale, $rules); $cp = Codepoints::toCodepoints($input); - $ctx = new RuleContext(mode: 'text', dialect: null, locale: $locale); + $ctx = new RuleContext(mode: 'text', dialect: null, locale: $locale, narrowTarget: $narrowTarget); $result = Pipeline::runRules($cp, $plan, $localeData, $ctx); return Codepoints::fromCodepoints($result); } /** @param array|null $rules */ - private static function transformHtml(string $input, string $locale, ?string $dialect, ?array $rules): string - { + private static function transformHtml( + string $input, + string $locale, + ?string $dialect, + ?array $rules, + int $narrowTarget, + ): string { if ($dialect !== null) { throw new PolytypoException( PolytypoException::CODE_INVALID_DIALECT, @@ -134,7 +149,7 @@ private static function transformHtml(string $input, string $locale, ?string $di } [$resolvedLocale, $localeData, $plan] = Pipeline::prepare($locale, $rules); $spans = Html::htmlSpans($input); - $ctx = new RuleContext(mode: 'html', dialect: null, locale: $resolvedLocale); + $ctx = new RuleContext(mode: 'html', dialect: null, locale: $resolvedLocale, narrowTarget: $narrowTarget); $cp = Codepoints::toCodepoints($input); return Runner::runOverSpans($cp, $spans, $plan, $localeData, $ctx); @@ -163,8 +178,13 @@ private static function transformMarkdown(string $input, string $locale, ?string * @param array|null $rules * @return Change[] */ - private static function analyzeText(string $input, string $locale, ?string $dialect, ?array $rules): array - { + private static function analyzeText( + string $input, + string $locale, + ?string $dialect, + ?array $rules, + int $narrowTarget, + ): array { if ($dialect !== null) { throw new PolytypoException( PolytypoException::CODE_INVALID_DIALECT, @@ -173,7 +193,7 @@ private static function analyzeText(string $input, string $locale, ?string $dial } [, $localeData, $plan] = Pipeline::prepare($locale, $rules); $cp = Codepoints::toCodepoints($input); - $ctx = new RuleContext(mode: 'text', dialect: null, locale: $locale); + $ctx = new RuleContext(mode: 'text', dialect: null, locale: $locale, narrowTarget: $narrowTarget); return Pipeline::runRulesRecording($cp, $plan, $localeData, $ctx, array_keys($cp), count($cp)); } @@ -182,8 +202,13 @@ private static function analyzeText(string $input, string $locale, ?string $dial * @param array|null $rules * @return Change[] */ - private static function analyzeHtml(string $input, string $locale, ?string $dialect, ?array $rules): array - { + private static function analyzeHtml( + string $input, + string $locale, + ?string $dialect, + ?array $rules, + int $narrowTarget, + ): array { if ($dialect !== null) { throw new PolytypoException( PolytypoException::CODE_INVALID_DIALECT, @@ -192,7 +217,7 @@ private static function analyzeHtml(string $input, string $locale, ?string $dial } [$resolvedLocale, $localeData, $plan] = Pipeline::prepare($locale, $rules); $spans = Html::htmlSpans($input); - $ctx = new RuleContext(mode: 'html', dialect: null, locale: $resolvedLocale); + $ctx = new RuleContext(mode: 'html', dialect: null, locale: $resolvedLocale, narrowTarget: $narrowTarget); return Runner::analyzeOverSpans(Codepoints::toCodepoints($input), $spans, $plan, $localeData, $ctx); } diff --git a/src/PolytypoException.php b/src/PolytypoException.php index e81a896..4e2c75a 100644 --- a/src/PolytypoException.php +++ b/src/PolytypoException.php @@ -21,6 +21,9 @@ final class PolytypoException extends \RuntimeException public const CODE_MALFORMED_LOCALE_DATA = 'POLYTYPO_MALFORMED_LOCALE_DATA'; public const CODE_RULE_CONTRACT = 'POLYTYPO_RULE_CONTRACT'; public const CODE_MALFORMED_INPUT = 'POLYTYPO_MALFORMED_INPUT'; + // Spec 1.3.0. Deliberately general: every option added from 1.3.0 on shares this code, while + // `mode` and `dialect` keep their own because callers branch on them. + public const CODE_INVALID_OPTION = 'POLYTYPO_INVALID_OPTION'; private readonly string $errorCode; diff --git a/tests/ConformanceTest.php b/tests/ConformanceTest.php index 627e739..68ff72d 100644 --- a/tests/ConformanceTest.php +++ b/tests/ConformanceTest.php @@ -57,10 +57,15 @@ public function testFixture(string $file, array $case): void $locale = $case['locale']; $mode = $case['mode'] ?? 'text'; $rules = $case['rules'] ?? null; + // Spec 1.3.0's case-level option (nbsp.md 3.1a). It is passed on BOTH calls below: a + // case carrying it is a fixed point under its own options and not under the defaults, + // so carrying it into the idempotency re-run is contract (ARCHITECTURE.md 6.1), not + // convenience. + $narrowNbsp = $case['narrowNbsp'] ?? null; if (isset($case['throws'])) { try { - Polytypo::transform($case['in'], $locale, mode: $mode, rules: $rules); + Polytypo::transform($case['in'], $locale, mode: $mode, rules: $rules, narrowNbsp: $narrowNbsp); self::fail('expected ' . $case['throws'] . ' to be thrown'); } catch (PolytypoException $e) { self::assertSame($case['throws'], $e->getErrorCode()); @@ -70,11 +75,11 @@ public function testFixture(string $file, array $case): void } $expected = $case['out']; - $got = Polytypo::transform($case['in'], $locale, mode: $mode, rules: $rules); + $got = Polytypo::transform($case['in'], $locale, mode: $mode, rules: $rules, narrowNbsp: $narrowNbsp); self::assertSame($expected, $got, "out = " . self::escape($got) . ', want ' . self::escape($expected)); // Free coverage, and the most common port bug (ARCHITECTURE.md 6.1). - $twice = Polytypo::transform($expected, $locale, mode: $mode, rules: $rules); + $twice = Polytypo::transform($expected, $locale, mode: $mode, rules: $rules, narrowNbsp: $narrowNbsp); self::assertSame($expected, $twice, 'not idempotent: transform(out) = ' . self::escape($twice)); } diff --git a/tests/NarrowNbspTest.php b/tests/NarrowNbspTest.php new file mode 100644 index 0000000..5f7668e --- /dev/null +++ b/tests/NarrowNbspTest.php @@ -0,0 +1,134 @@ +assertSame("Un délai{$nn}? Vraiment{$nn}! Et puis{$nn}; voilà.", self::fr($input)); + $this->assertSame( + "Un délai{$nb}? Vraiment{$nb}! Et puis{$nb}; voilà.", + self::fr($input, 'nbsp'), + ); + } + + public function testNormalisesAnAuthoredNarrowSpaceAtAClaimedIndex(): void + { + $nb = self::NBSP; + $nn = self::NNBSP; + $this->assertSame("Oui{$nb}?", self::fr("Oui{$nn}?", 'nbsp')); + $this->assertSame("Oui{$nn}?", self::fr("Oui{$nn}?")); + // Elsewhere it is left alone: the option changes what is written, not which indices are + // claimed. + $this->assertSame("mot{$nn}mot", self::fr("mot{$nn}mot", 'nbsp')); + } + + public function testClaimsTheSameIndicesUnderTheSameGuards(): void + { + $nb = self::NBSP; + $this->assertSame("12:30 et http://x{$nb}; oui", self::fr('12:30 et http://x ; oui', 'nbsp')); + // N1 (the colon) and N8 (fr's primary pair, innerSpace "nbsp") already wrote U+00A0. + $this->assertSame( + "Il a dit{$nb}: «{$nb}oui{$nb}»{$nb}; puis{$nb}?", + self::fr('Il a dit : « oui » ; puis ?', 'nbsp'), + ); + } + + /** @return array */ + public static function localesWithoutNarrowSpaces(): array + { + return ['en-US' => ['en-US'], 'de-DE' => ['de-DE'], 'ru' => ['ru']]; + } + + #[DataProvider('localesWithoutNarrowSpaces')] + public function testIsANoOpWhereNothingEmitsANarrowSpace(string $locale): void + { + $input = 'She said “hi” — really...'; + $this->assertSame( + Polytypo::transform($input, $locale), + Polytypo::transform($input, $locale, narrowNbsp: 'nbsp'), + ); + } + + public function testIsAFixedPointUnderTheOption(): void + { + $nn = self::NNBSP; + foreach (['Un délai ? Vraiment !', "Oui{$nn}?", 'Il a dit : « oui » ;'] as $input) { + $once = self::fr($input, 'nbsp'); + $this->assertSame($once, self::fr($once, 'nbsp'), $input); + } + } + + public function testPostProcessingTheDefaultOutputIsNotAFixedPoint(): void + { + // Which is why the option moves the target instead: a caller's str_replace is stable + // only as long as it always runs. Feed it back through the default pipeline and N2 + // converts it straight back. + $nb = self::NBSP; + $nn = self::NNBSP; + $postProcessed = str_replace($nn, $nb, self::fr('Un délai ?')); + $this->assertSame("Un délai{$nb}?", $postProcessed); + $this->assertSame("Un délai{$nn}?", self::fr($postProcessed)); + } + + public function testValidationAndItsOrder(): void + { + $codeOf = static function (callable $run): string { + try { + $run(); + } catch (PolytypoException $e) { + return $e->getErrorCode(); + } + + return 'NO THROW'; + }; + + $this->assertSame( + PolytypoException::CODE_INVALID_OPTION, + $codeOf(static fn () => Polytypo::transform('x', 'fr', narrowNbsp: 'wide')), + ); + $this->assertSame( + PolytypoException::CODE_INVALID_MODE, + $codeOf(static fn () => Polytypo::transform('x', 'fr', 'yaml', narrowNbsp: 'wide')), + ); + $this->assertSame( + PolytypoException::CODE_INVALID_OPTION, + $codeOf(static fn () => Polytypo::transform('x', 'fr', rules: ['nope' => true], narrowNbsp: 'wide')), + ); + $this->assertSame( + PolytypoException::CODE_INVALID_OPTION, + $codeOf(static fn () => Polytypo::transform('x', 'xx', narrowNbsp: 'wide')), + ); + // The check belongs to the call, not to the rule. + $this->assertSame( + PolytypoException::CODE_INVALID_OPTION, + $codeOf(static fn () => Polytypo::transform('x', 'fr', rules: ['nbsp' => false], narrowNbsp: 'wide')), + ); + } + + public function testAcceptsTheExplicitDefault(): void + { + $nn = self::NNBSP; + $this->assertSame("Un délai{$nn}?", self::fr('Un délai ?', 'narrow')); + } +}