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
31 changes: 31 additions & 0 deletions src/polytypo/_engine/rules/nbsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
SP = 0x20
NBSP = 0xA0
NNBSP = 0x202F
SEMICOLON = 0x3B
AMPERSAND = 0x26
HASH = 0x23
# The longest HTML named reference is 31 code points ("CounterClockwiseContourIntegral").
MAX_CHARACTER_REFERENCE_NAME = 32
NOBREAK = frozenset({NBSP, NNBSP})
OTHER_SPACE = frozenset(
{
Expand Down Expand Up @@ -105,6 +110,27 @@ def scan(cp: list[int], locale_data: dict[str, Any], ctx: RuleContext) -> list[E
# ---------------------------------------------------------------- N1 / N2


def _ends_character_reference(cp: list[int], i: int) -> bool:
"""nbsp.md 3.3 step 4: do the code points left of this ";" have the shape of a character
reference? A bounded left walk over ASCII alphanumerics, optionally one "#", then "&". Shape,
not the HTML named-reference table -- declining on "&notaname;" costs nothing, and no runtime
carries thousands of entries for it. The bound is the longest named reference (31) plus one.
"""
j = i - 1
while j >= 0 and _is_ascii_alphanumeric(cp[j]):
j -= 1
length = i - 1 - j
if length < 1 or length > MAX_CHARACTER_REFERENCE_NAME:
return False
if j >= 0 and cp[j] == HASH:
j -= 1
return j >= 0 and cp[j] == AMPERSAND


def _is_ascii_alphanumeric(c: int) -> bool:
return 0x30 <= c <= 0x39 or 0x41 <= c <= 0x5A or 0x61 <= c <= 0x7A


def _before_punctuation_common(
cp: list[int],
target_set: set[int],
Expand Down Expand Up @@ -139,6 +165,11 @@ def _before_punctuation_common(
# Quote-glyph guard.
if (prev in SPACELIKE and _at(cp, i - 2) in openish) or prev in openish:
continue
# Character-reference guard (nbsp.md 3.3 step 4, spec 1.3.0). text mode has no markup
# concept, so a locale listing ";" used to insert before the ";" that *ends* a reference
# and `Bonjour&#160;: oui` stopped being what it was.
if cp[i] == SEMICOLON and _ends_character_reference(cp, i):
continue
left = prev
if left == already_correct:
continue # already correct
Expand Down
22 changes: 22 additions & 0 deletions tests/test_rules/test_nbsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@ def test_n1_right_context_accepts_span_boundary_marker() -> None:
# nbsp.md 3.3 step 2 (spec 1.2.0): the inline marker is in nbsp's CLOSEISH.
out = polytypo.html.transform("<strong>gel :</strong> il", locale="fr")
assert out == f"<strong>gel{NBSP}:</strong> il"


def test_n2_character_reference_guard_numeric() -> None:
# nbsp.md 3.3 step 4 (spec 1.3.0): the ";" closes "&#160;", so N2 emits nothing. The colon
# takes nothing either -- step 1's run guard sees a mark to its left.
assert polytypo.transform("Bonjour&#160;: oui", locale="fr") == "Bonjour&#160;: oui"


def test_n2_character_reference_guard_named() -> None:
assert polytypo.transform("Tom &amp; Jerry", locale="fr") == "Tom &amp; Jerry"


def test_n2_character_reference_guard_tests_shape_not_the_entity_table() -> None:
assert polytypo.transform("a &notaname; b", locale="fr") == "a &notaname; b"


def test_n2_ordinary_semicolon_still_binds() -> None:
assert polytypo.transform("Oui ; non", locale="fr") == f"Oui{NNBSP}; non"


def test_n2_semicolon_after_digit_run_without_ampersand_binds() -> None:
assert polytypo.transform("Section 4; suite", locale="fr") == f"Section 4{NNBSP}; suite"
Loading