diff --git a/src/polytypo/__init__.py b/src/polytypo/__init__.py index 7e39468..f3700e5 100644 --- a/src/polytypo/__init__.py +++ b/src/polytypo/__init__.py @@ -41,13 +41,20 @@ def transform( mode: str | None = None, dialect: str | None = None, rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, ) -> str: + """`narrow_nbsp="nbsp"` makes the engine emit U+00A0 everywhere it would emit U+202F + (nbsp.md 3.1a). It moves the rule's target rather than post-processing the output, so the + result stays a fixed point. Validation order is mode -> narrow_nbsp -> rules -> locale -> + dialect, and the check runs whether or not `nbsp` is enabled.""" resolved_mode = _resolve_mode(mode) if resolved_mode == "text": - return run_text_pipeline(input, locale=locale, rules=rules) + return run_text_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) if resolved_mode == "html": - return run_html_pipeline(input, locale=locale, rules=rules) - return run_markdown_pipeline(input, locale=locale, dialect=dialect, rules=rules) + return run_html_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) + return run_markdown_pipeline( + input, locale=locale, dialect=dialect, rules=rules, narrow_nbsp=narrow_nbsp + ) def analyze( @@ -57,6 +64,7 @@ def analyze( mode: str | None = None, dialect: str | None = None, rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, ) -> list[Change]: """The same pipeline as `transform`, reporting what it would do instead of doing it (spec/rules/analyze.md). Offsets are code-point offsets into `input` in every mode. @@ -68,7 +76,9 @@ def analyze( for the text (analyze.md sections 4 and 5).""" resolved_mode = _resolve_mode(mode) if resolved_mode == "text": - return analyze_text_pipeline(input, locale=locale, rules=rules) + return analyze_text_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) if resolved_mode == "html": - return analyze_html_pipeline(input, locale=locale, rules=rules) - return analyze_markdown_pipeline(input, locale=locale, dialect=dialect, rules=rules) + return analyze_html_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) + return analyze_markdown_pipeline( + input, locale=locale, dialect=dialect, rules=rules, narrow_nbsp=narrow_nbsp + ) diff --git a/src/polytypo/_engine/mode_pipelines.py b/src/polytypo/_engine/mode_pipelines.py index 777662d..fddb8ce 100644 --- a/src/polytypo/_engine/mode_pipelines.py +++ b/src/polytypo/_engine/mode_pipelines.py @@ -11,6 +11,7 @@ from __future__ import annotations from polytypo._engine.codepoints import from_codepoints, to_codepoints +from polytypo._engine.narrow_target import resolve_narrow_target from polytypo._engine.origin import Change from polytypo._engine.pipeline import prepare, run_rules, run_rules_recording from polytypo._engine.registry import RuleContext @@ -18,68 +19,134 @@ from polytypo._modes.spans import normalize_spans -def run_text_pipeline(input_text: str, *, locale: object, rules: dict[str, bool] | None) -> str: +def run_text_pipeline( + input_text: str, + *, + locale: object, + rules: dict[str, bool] | None, + narrow_nbsp: str | None = None, +) -> str: + narrow_target = resolve_narrow_target(narrow_nbsp) resolved_locale, locale_data, plan = prepare(locale, rules) cp = to_codepoints(input_text) - ctx: RuleContext = {"mode": "text", "dialect": None, "locale": resolved_locale} + ctx: RuleContext = { + "mode": "text", + "dialect": None, + "locale": resolved_locale, + "narrow_target": narrow_target, + } return from_codepoints(run_rules(cp, plan, locale_data, ctx)) -def run_html_pipeline(input_text: str, *, locale: object, rules: dict[str, bool] | None) -> str: +def run_html_pipeline( + input_text: str, + *, + locale: object, + rules: dict[str, bool] | None, + narrow_nbsp: str | None = None, +) -> str: from polytypo._modes.html import html_spans + narrow_target = resolve_narrow_target(narrow_nbsp) resolved_locale, locale_data, plan = prepare(locale, rules) - ctx: RuleContext = {"mode": "html", "dialect": None, "locale": resolved_locale} + ctx: RuleContext = { + "mode": "html", + "dialect": None, + "locale": resolved_locale, + "narrow_target": narrow_target, + } spans = normalize_spans(html_spans(input_text)) return run_over_spans(input_text, spans, plan, locale_data, ctx) def run_markdown_pipeline( - input_text: str, *, locale: object, dialect: str | None, rules: dict[str, bool] | None + input_text: str, + *, + locale: object, + dialect: str | None, + rules: dict[str, bool] | None, + narrow_nbsp: str | None = None, ) -> str: from polytypo._modes.markdown import markdown_spans, resolve_dialect + narrow_target = resolve_narrow_target(narrow_nbsp) resolved_locale, locale_data, plan = prepare(locale, rules) resolve_dialect(dialect) - ctx: RuleContext = {"mode": "markdown", "dialect": dialect, "locale": resolved_locale} + ctx: RuleContext = { + "mode": "markdown", + "dialect": dialect, + "locale": resolved_locale, + "narrow_target": narrow_target, + } spans = normalize_spans(markdown_spans(input_text, dialect)) return run_over_spans(input_text, spans, plan, locale_data, ctx) def analyze_text_pipeline( - input_text: str, *, locale: object, rules: dict[str, bool] | None + input_text: str, + *, + locale: object, + rules: dict[str, bool] | None, + narrow_nbsp: str | None = None, ) -> list[Change]: """analyze.md section 1: the same pipeline as `run_text_pipeline`, reporting instead of applying.""" + narrow_target = resolve_narrow_target(narrow_nbsp) resolved_locale, locale_data, plan = prepare(locale, rules) cp = to_codepoints(input_text) - ctx: RuleContext = {"mode": "text", "dialect": None, "locale": resolved_locale} + ctx: RuleContext = { + "mode": "text", + "dialect": None, + "locale": resolved_locale, + "narrow_target": narrow_target, + } return run_rules_recording(cp, plan, locale_data, ctx, list(range(len(cp))), len(cp)) def analyze_html_pipeline( - input_text: str, *, locale: object, rules: dict[str, bool] | None + input_text: str, + *, + locale: object, + rules: dict[str, bool] | None, + narrow_nbsp: str | None = None, ) -> list[Change]: """analyze.md section 1, `html` mode: offsets are into the document, not into a span (analyze.md section 6).""" from polytypo._modes.html import html_spans + narrow_target = resolve_narrow_target(narrow_nbsp) resolved_locale, locale_data, plan = prepare(locale, rules) - ctx: RuleContext = {"mode": "html", "dialect": None, "locale": resolved_locale} + ctx: RuleContext = { + "mode": "html", + "dialect": None, + "locale": resolved_locale, + "narrow_target": narrow_target, + } spans = normalize_spans(html_spans(input_text)) return analyze_over_spans(input_text, spans, plan, locale_data, ctx) def analyze_markdown_pipeline( - input_text: str, *, locale: object, dialect: str | None, rules: dict[str, bool] | None + input_text: str, + *, + locale: object, + dialect: str | None, + rules: dict[str, bool] | None, + narrow_nbsp: str | None = None, ) -> list[Change]: """analyze.md section 1, `markdown` mode. Dialect validation happens here exactly as it does for `run_markdown_pipeline`, so an absent or unsupported dialect raises POLYTYPO_INVALID_DIALECT from `analyze` too (analyze.md section 4, A1).""" from polytypo._modes.markdown import markdown_spans, resolve_dialect + narrow_target = resolve_narrow_target(narrow_nbsp) resolved_locale, locale_data, plan = prepare(locale, rules) resolve_dialect(dialect) - ctx: RuleContext = {"mode": "markdown", "dialect": dialect, "locale": resolved_locale} + ctx: RuleContext = { + "mode": "markdown", + "dialect": dialect, + "locale": resolved_locale, + "narrow_target": narrow_target, + } spans = normalize_spans(markdown_spans(input_text, dialect)) return analyze_over_spans(input_text, spans, plan, locale_data, ctx) diff --git a/src/polytypo/_engine/narrow_target.py b/src/polytypo/_engine/narrow_target.py new file mode 100644 index 0000000..45d8906 --- /dev/null +++ b/src/polytypo/_engine/narrow_target.py @@ -0,0 +1,26 @@ +"""nbsp.md 3.1a: resolve the ``narrow_nbsp`` 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 raises rather than being silently ignored.""" + +from __future__ import annotations + +from polytypo.errors import POLYTYPO_INVALID_OPTION, PolytypoError + +#: U+202F, the narrow no-break space -- nbsp.md 3.1a's default NARROW-TARGET. +NARROW_NO_BREAK_SPACE = 0x202F +#: U+00A0, what ``narrow_nbsp="nbsp"`` substitutes for it. +NO_BREAK_SPACE = 0x00A0 + + +def resolve_narrow_target(value: str | None) -> int: + if value is None or value == "narrow": + return NARROW_NO_BREAK_SPACE + if value == "nbsp": + return NO_BREAK_SPACE + raise PolytypoError( + POLYTYPO_INVALID_OPTION, + f'Unknown narrow_nbsp {value!r}. Expected "narrow" or "nbsp".', + ) diff --git a/src/polytypo/_engine/registry.py b/src/polytypo/_engine/registry.py index 5be3a11..47bb3b2 100644 --- a/src/polytypo/_engine/registry.py +++ b/src/polytypo/_engine/registry.py @@ -17,6 +17,9 @@ class RuleContext(TypedDict): mode: str dialect: str | None locale: str + # nbsp.md 3.1a NARROW-TARGET, already resolved to a code point: U+202F by default, U+00A0 + # when the caller passed narrow_nbsp="nbsp". A rule reads a code point and never the option. + narrow_target: int RuleFn = Callable[[list[int], dict[str, Any], RuleContext], list[Edit]] diff --git a/src/polytypo/_engine/rules/nbsp.py b/src/polytypo/_engine/rules/nbsp.py index 948340d..9214576 100644 --- a/src/polytypo/_engine/rules/nbsp.py +++ b/src/polytypo/_engine/rules/nbsp.py @@ -84,13 +84,15 @@ def scan(cp: list[int], locale_data: dict[str, Any], ctx: RuleContext) -> list[E sub_rule_edits = [ _n1_before_punctuation(cp, before_punct, narrow_before_punct, openish, closeish), - _n2_narrow_before_punctuation(cp, before_punct, narrow_before_punct, openish, closeish), + _n2_narrow_before_punctuation( + cp, before_punct, narrow_before_punct, openish, closeish, ctx["narrow_target"] + ), _n3_after_short_words(cp, nbsp_data["afterShortWords"], openish), _n4_abbreviations(cp, nbsp_data["abbreviations"]), _n5_before_units(cp, nbsp_data["beforeUnits"]), _n6_after_symbols(cp, nbsp_data["afterSymbols"]), _n7_initial_binding(cp, nbsp_data["initialBinding"], openish), - _n8_quotes_inner_space(cp, locale_data["quotes"]), + _n8_quotes_inner_space(cp, locale_data["quotes"], ctx["narrow_target"]), _n9_before_number(cp, nbsp_data["beforeNumber"], openish), _n10_before_word(cp, nbsp_data["beforeWord"], openish, nbsp_data["initialBinding"]), ] @@ -200,16 +202,22 @@ def _n2_narrow_before_punctuation( narrow_before_punct: set[int], openish: frozenset[int], closeish: frozenset[int], + narrow_target: int, ) -> list[Edit]: + """nbsp.md 3.4 with NARROW-TARGET (3.1a) in place of U+202F: the already-correct state, the + conversion target and the insertion all move together. `other` 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.""" + other = NNBSP if narrow_target == NBSP else NBSP return _before_punctuation_common( cp, narrow_before_punct, before_punct, openish, closeish, - NNBSP, - frozenset({SP, NBSP}), - NNBSP, + narrow_target, + frozenset({SP, other}), + narrow_target, ) @@ -461,7 +469,9 @@ def _chain_confirmed(cp: list[int], p: int, openish: frozenset[int]) -> bool: # ---------------------------------------------------------------- N8 -def _n8_quotes_inner_space(cp: list[int], quotes_data: dict[str, Any]) -> list[Edit]: +def _n8_quotes_inner_space( + cp: list[int], quotes_data: dict[str, Any], narrow_target: int +) -> list[Edit]: edits: list[Edit] = [] n = len(cp) for pair_key in ("primary", "secondary"): @@ -469,7 +479,7 @@ def _n8_quotes_inner_space(cp: list[int], quotes_data: dict[str, Any]) -> list[E inner = pair["innerSpace"] if inner == "none": continue - target = NBSP if inner == "nbsp" else NNBSP + target = NBSP if inner == "nbsp" else narrow_target open_glyph = ord(pair["open"]) close_glyph = ord(pair["close"]) if open_glyph == close_glyph: diff --git a/src/polytypo/errors.py b/src/polytypo/errors.py index 835d5ae..48ef3fc 100644 --- a/src/polytypo/errors.py +++ b/src/polytypo/errors.py @@ -11,10 +11,13 @@ class is enough: the stable machine-readable ``code`` is the contract, not the e POLYTYPO_MALFORMED_LOCALE_DATA = "POLYTYPO_MALFORMED_LOCALE_DATA" POLYTYPO_RULE_CONTRACT = "POLYTYPO_RULE_CONTRACT" POLYTYPO_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. +POLYTYPO_INVALID_OPTION = "POLYTYPO_INVALID_OPTION" class PolytypoError(Exception): - """Carries a stable ``code`` from the seven above. No other exception type from this package + """Carries a stable ``code`` from the eight above. No other exception type from this package or its dependencies is ever allowed to escape :func:`polytypo.transform`.""" def __init__(self, code: str, message: str) -> None: diff --git a/src/polytypo/html.py b/src/polytypo/html.py index d958917..0d6a254 100644 --- a/src/polytypo/html.py +++ b/src/polytypo/html.py @@ -15,12 +15,24 @@ __all__ = ["Change", "PolytypoError", "analyze", "transform"] -def transform(input: str, *, locale: str, rules: dict[str, bool] | None = None) -> str: - return run_html_pipeline(input, locale=locale, rules=rules) - - -def analyze(input: str, *, locale: str, rules: dict[str, bool] | None = None) -> list[Change]: +def transform( + input: str, + *, + locale: str, + rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, +) -> str: + return run_html_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) + + +def analyze( + input: str, + *, + locale: str, + rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, +) -> list[Change]: """The same pipeline as this entry's `transform`, reporting instead of applying (spec/rules/analyze.md). Offsets are code-point offsets into the **document**, not into a span (analyze.md section 2).""" - return analyze_html_pipeline(input, locale=locale, rules=rules) + return analyze_html_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) diff --git a/src/polytypo/markdown.py b/src/polytypo/markdown.py index 7829cb4..80099bb 100644 --- a/src/polytypo/markdown.py +++ b/src/polytypo/markdown.py @@ -19,15 +19,29 @@ def transform( - input: str, *, locale: str, dialect: str, rules: dict[str, bool] | None = None + input: str, + *, + locale: str, + dialect: str, + rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, ) -> str: - return run_markdown_pipeline(input, locale=locale, dialect=dialect, rules=rules) + return run_markdown_pipeline( + input, locale=locale, dialect=dialect, rules=rules, narrow_nbsp=narrow_nbsp + ) def analyze( - input: str, *, locale: str, dialect: str, rules: dict[str, bool] | None = None + input: str, + *, + locale: str, + dialect: str, + rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, ) -> list[Change]: """The same pipeline as this entry's `transform`, reporting instead of applying (spec/rules/analyze.md). Offsets are code-point offsets into the **document**, not into a span (analyze.md section 2).""" - return analyze_markdown_pipeline(input, locale=locale, dialect=dialect, rules=rules) + return analyze_markdown_pipeline( + input, locale=locale, dialect=dialect, rules=rules, narrow_nbsp=narrow_nbsp + ) diff --git a/src/polytypo/text.py b/src/polytypo/text.py index 1df94e2..c923f93 100644 --- a/src/polytypo/text.py +++ b/src/polytypo/text.py @@ -15,11 +15,23 @@ __all__ = ["Change", "PolytypoError", "analyze", "transform"] -def transform(input: str, *, locale: str, rules: dict[str, bool] | None = None) -> str: - return run_text_pipeline(input, locale=locale, rules=rules) - - -def analyze(input: str, *, locale: str, rules: dict[str, bool] | None = None) -> list[Change]: +def transform( + input: str, + *, + locale: str, + rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, +) -> str: + return run_text_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) + + +def analyze( + input: str, + *, + locale: str, + rules: dict[str, bool] | None = None, + narrow_nbsp: str | None = None, +) -> list[Change]: """The same pipeline as this entry's `transform`, reporting instead of applying (spec/rules/analyze.md). Offsets are code-point offsets into `input`.""" - return analyze_text_pipeline(input, locale=locale, rules=rules) + return analyze_text_pipeline(input, locale=locale, rules=rules, narrow_nbsp=narrow_nbsp) diff --git a/tests/conformance/test_fixtures.py b/tests/conformance/test_fixtures.py index 099a473..5c0d41b 100644 --- a/tests/conformance/test_fixtures.py +++ b/tests/conformance/test_fixtures.py @@ -64,6 +64,8 @@ def test_fixture_case(file_name: str, locale: str, case: dict[str, Any]) -> None kwargs["dialect"] = case["dialect"] if case.get("rules") is not None: kwargs["rules"] = case["rules"] + if case.get("narrowNbsp") is not None: + kwargs["narrow_nbsp"] = case["narrowNbsp"] if "throws" in case: with pytest.raises(PolytypoError) as excinfo: @@ -75,7 +77,9 @@ def test_fixture_case(file_name: str, locale: str, case: dict[str, Any]) -> None got = polytypo.transform(case["in"], **kwargs) assert _escape_non_ascii(got) == _escape_non_ascii(expected) - # Free coverage, and the most common port bug (ARCHITECTURE.md 6.1). + # Free coverage, and the most common port bug (ARCHITECTURE.md 6.1). The re-run carries the + # case's OWN options: a case with `narrowNbsp` is a fixed point under that option and not + # under the defaults, so passing `**kwargs` here is contract, not convenience. twice = polytypo.transform(expected, **kwargs) assert _escape_non_ascii(twice) == _escape_non_ascii(expected) diff --git a/tests/test_rules/test_narrow_nbsp.py b/tests/test_rules/test_narrow_nbsp.py new file mode 100644 index 0000000..23d7278 --- /dev/null +++ b/tests/test_rules/test_narrow_nbsp.py @@ -0,0 +1,104 @@ +"""spec/rules/nbsp.md 3.1a -- `narrow_nbsp` moves NARROW-TARGET, it does not post-process.""" + +from __future__ import annotations + +import pytest + +import polytypo +from polytypo.errors import PolytypoError + +NBSP = " " +NNBSP = " " + + +def _fr(text: str, **kwargs: object) -> str: + return polytypo.transform(text, locale="fr", **kwargs) # type: ignore[arg-type] + + +class TestWhatItChanges: + def test_writes_nbsp_where_n2_would_write_nnbsp(self) -> None: + text = "Un délai ? Vraiment ! Et puis ; voilà." + assert _fr(text) == f"Un délai{NNBSP}? Vraiment{NNBSP}! Et puis{NNBSP}; voilà." + assert _fr(text, narrow_nbsp="nbsp") == ( + f"Un délai{NBSP}? Vraiment{NBSP}! Et puis{NBSP}; voilà." + ) + + def test_normalises_an_authored_narrow_space_at_a_claimed_index(self) -> None: + assert _fr(f"Oui{NNBSP}?", narrow_nbsp="nbsp") == f"Oui{NBSP}?" + assert _fr(f"Oui{NNBSP}?") == f"Oui{NNBSP}?" + + def test_leaves_an_authored_narrow_space_alone_elsewhere(self) -> None: + assert _fr(f"mot{NNBSP}mot", narrow_nbsp="nbsp") == f"mot{NNBSP}mot" + + +class TestWhatItDoesNotChange: + def test_claims_the_same_indices_under_the_same_guards(self) -> None: + assert _fr("12:30 et http://x ; oui", narrow_nbsp="nbsp") == f"12:30 et http://x{NBSP}; oui" + + def test_leaves_a_sub_rule_whose_target_was_already_nbsp(self) -> None: + # N1 (the colon) and N8 (fr's primary pair, innerSpace "nbsp") do not move. + assert _fr("Il a dit : « oui » ; puis ?", narrow_nbsp="nbsp") == ( + f"Il a dit{NBSP}: «{NBSP}oui{NBSP}»{NBSP}; puis{NBSP}?" + ) + + @pytest.mark.parametrize("locale", ["en-US", "de-DE", "ru"]) + def test_is_a_no_op_where_nothing_emits_a_narrow_space(self, locale: str) -> None: + text = "She said “hi” — really..." + assert polytypo.transform(text, locale=locale, narrow_nbsp="nbsp") == ( + polytypo.transform(text, locale=locale) + ) + + +class TestIdempotency: + def test_is_a_fixed_point_under_the_option(self) -> None: + for text in ["Un délai ? Vraiment !", f"Oui{NNBSP}?", "Il a dit : « oui » ;"]: + once = _fr(text, narrow_nbsp="nbsp") + assert _fr(once, narrow_nbsp="nbsp") == once + + def test_post_processing_the_default_output_is_not(self) -> None: + # This is why the option moves the target instead: a caller's replace is stable only as + # long as it always runs. Feed it back through the default pipeline and N2 undoes it. + post_processed = _fr("Un délai ?").replace(NNBSP, NBSP) + assert post_processed == f"Un délai{NBSP}?" + assert _fr(post_processed) == f"Un délai{NNBSP}?" + + +class TestValidation: + def test_raises_invalid_option_for_an_unknown_value(self) -> None: + with pytest.raises(PolytypoError) as excinfo: + _fr("x", narrow_nbsp="wide") + assert excinfo.value.code == "POLYTYPO_INVALID_OPTION" + + def test_is_checked_after_mode_and_before_rules_and_locale(self) -> None: + def code_of(**kwargs: object) -> str: + try: + polytypo.transform("x", **kwargs) # type: ignore[arg-type] + except PolytypoError as error: + return error.code + return "NO THROW" + + assert code_of(locale="fr", mode="yaml", narrow_nbsp="wide") == "POLYTYPO_INVALID_MODE" + assert ( + code_of(locale="fr", narrow_nbsp="wide", rules={"nope": True}) + == "POLYTYPO_INVALID_OPTION" + ) + assert code_of(locale="xx", narrow_nbsp="wide") == "POLYTYPO_INVALID_OPTION" + + def test_raises_even_when_nbsp_is_disabled(self) -> None: + # The check belongs to the call, not to the rule. + with pytest.raises(PolytypoError) as excinfo: + _fr("x", narrow_nbsp="wide", rules={"nbsp": False}) + assert excinfo.value.code == "POLYTYPO_INVALID_OPTION" + + def test_accepts_the_explicit_default(self) -> None: + assert _fr("Un délai ?", narrow_nbsp="narrow") == f"Un délai{NNBSP}?" + + +def test_reaches_the_submodule_entries() -> None: + from polytypo.html import transform as transform_html + from polytypo.text import transform as transform_text + + assert transform_text("Un délai ?", locale="fr", narrow_nbsp="nbsp") == f"Un délai{NBSP}?" + assert transform_html("
Un délai ?
", locale="fr", narrow_nbsp="nbsp") == ( + f"Un délai{NBSP}?
" + )