Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Engine/NarrowTarget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Polytypo\Engine;

use Polytypo\PolytypoException;

/**
* nbsp.md 3.1a: resolve the `narrowNbsp` option to the code point the rule writes.
*
* Done once, at the call boundary, so no rule ever sees the option's string. Checked immediately
* after `mode` and before `rules` -- the two checks that read nothing but the call itself come
* first (ARCHITECTURE.md section 7). It runs whether or not `nbsp` is enabled, so a misspelled
* value still throws rather than being silently ignored.
*/
final class NarrowTarget
{
/** U+202F, the narrow no-break space -- the default NARROW-TARGET. */
public const NARROW_NO_BREAK_SPACE = 0x202F;
/** U+00A0, what `narrowNbsp` "nbsp" substitutes for it. */
public const NO_BREAK_SPACE = 0x00A0;

private function __construct()
{
}

public static function resolve(?string $value): int
{
if ($value === null || $value === 'narrow') {
return self::NARROW_NO_BREAK_SPACE;
}
if ($value === 'nbsp') {
return self::NO_BREAK_SPACE;
}

throw new PolytypoException(
PolytypoException::CODE_INVALID_OPTION,
"Unknown narrowNbsp \"{$value}\". Expected \"narrow\" or \"nbsp\".",
);
}
}
6 changes: 6 additions & 0 deletions src/Engine/RuleContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public function __construct(
public readonly string $mode,
public readonly ?string $dialect,
public readonly string $locale,
/**
* nbsp.md 3.1a NARROW-TARGET, already resolved to a code point: U+202F by default,
* U+00A0 when the caller passed narrowNbsp "nbsp". A rule reads a code point and never
* the option, so the string never reaches the pipeline.
*/
public readonly int $narrowTarget = 0x202F,
) {
}
}
14 changes: 9 additions & 5 deletions src/Engine/Rules/NbspRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private static function prepareList(array $entries): array
* @param array<string, mixed> $localeData
* @return array<string, mixed>
*/
private static function prepare(array $localeData): array
private static function prepare(array $localeData, int $narrowTarget = 0x202F): array
{
$data = $localeData['nbsp'];

Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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
Expand Down
57 changes: 41 additions & 16 deletions src/Polytypo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -107,8 +112,13 @@ private static function resolveMode(string $mode): string
}

/** @param array<string, bool>|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,
Expand All @@ -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<string, bool>|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,
Expand All @@ -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);
Expand Down Expand Up @@ -163,8 +178,13 @@ private static function transformMarkdown(string $input, string $locale, ?string
* @param array<string, bool>|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,
Expand All @@ -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));
}
Expand All @@ -182,8 +202,13 @@ private static function analyzeText(string $input, string $locale, ?string $dial
* @param array<string, bool>|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,
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/PolytypoException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 8 additions & 3 deletions tests/ConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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));
}

Expand Down
Loading
Loading