diff --git a/markdown_it/common/html_re.py b/markdown_it/common/html_re.py index 88cddc44..ec0d62c1 100644 --- a/markdown_it/common/html_re.py +++ b/markdown_it/common/html_re.py @@ -2,6 +2,11 @@ import re +#: Whitespace as JavaScript's ``\s`` matches it in the upstream regexps. +#: Python's ``\s`` also matches U+001C-U+001F and U+0085, which are not +#: whitespace in CommonMark or JavaScript (see ``MD_TRIM_CHARS``). +whitespace = "[^\\S\\x1c-\\x1f\\x85]" + attr_name = "[a-zA-Z_:][a-zA-Z0-9:._-]*" unquoted = "[^\"'=<>`\\x00-\\x20]+" @@ -10,11 +15,11 @@ attr_value = "(?:" + unquoted + "|" + single_quoted + "|" + double_quoted + ")" -attribute = "(?:\\s+" + attr_name + "(?:\\s*=\\s*" + attr_value + ")?)" +attribute = f"(?:{whitespace}+{attr_name}(?:{whitespace}*={whitespace}*{attr_value})?)" -open_tag = "<[A-Za-z][A-Za-z0-9\\-]*" + attribute + "*\\s*\\/?>" +open_tag = "<[A-Za-z][A-Za-z0-9\\-]*" + attribute + "*" + whitespace + "*\\/?>" -close_tag = "<\\/[A-Za-z][A-Za-z0-9\\-]*\\s*>" +close_tag = "<\\/[A-Za-z][A-Za-z0-9\\-]*" + whitespace + "*>" comment = "" processing = "<[?][\\s\\S]*?[?]>" declaration = "]*>" diff --git a/markdown_it/rules_block/html_block.py b/markdown_it/rules_block/html_block.py index 1ff23d76..66b54482 100644 --- a/markdown_it/rules_block/html_block.py +++ b/markdown_it/rules_block/html_block.py @@ -5,7 +5,7 @@ import re from ..common.html_blocks import block_names -from ..common.html_re import HTML_OPEN_CLOSE_TAG_STR +from ..common.html_re import HTML_OPEN_CLOSE_TAG_STR, whitespace from .state_block import StateBlock LOGGER = logging.getLogger(__name__) @@ -14,7 +14,10 @@ # last argument defines whether it can terminate a paragraph or not HTML_SEQUENCES: list[tuple[re.Pattern[str], re.Pattern[str], bool]] = [ ( - re.compile(r"^<(?a:(script|pre|style|textarea))(?=(\s|>|$))", re.IGNORECASE), + re.compile( + "^<(?a:(script|pre|style|textarea))(?=(" + whitespace + "|>|$))", + re.IGNORECASE, + ), re.compile(r"<\/(?a:(script|pre|style|textarea))>", re.IGNORECASE), True, ), @@ -24,12 +27,13 @@ (re.compile(r"^"), True), ( re.compile( - "^|$))", re.IGNORECASE + "^|$))", + re.IGNORECASE, ), re.compile(r"^$"), True, ), - (re.compile(HTML_OPEN_CLOSE_TAG_STR + "\\s*$"), re.compile(r"^$"), False), + (re.compile(HTML_OPEN_CLOSE_TAG_STR + whitespace + "*$"), re.compile(r"^$"), False), ] diff --git a/markdown_it/rules_core/replacements.py b/markdown_it/rules_core/replacements.py index bcc99800..19d637fd 100644 --- a/markdown_it/rules_core/replacements.py +++ b/markdown_it/rules_core/replacements.py @@ -19,6 +19,7 @@ import logging import re +from ..common.html_re import whitespace from ..token import Token from .state_core import StateCore @@ -48,9 +49,19 @@ EM_DASH_RE = re.compile(r"(^|[^-])---(?=[^-]|$)", flags=re.MULTILINE) -EN_DASH_RE = re.compile(r"(^|\s)--(?=\s|$)", flags=re.MULTILINE) +# Upstream uses JavaScript's ``\s``; Python's ``\s`` also matches U+001C-U+001F +# and U+0085, which are not whitespace in JavaScript or CommonMark. +_SPACE = whitespace +_NOT_SPACE_OR_DASH = r"(?:[^-\s]|[\x1c-\x1f\x85])" -EN_DASH_INDENT_RE = re.compile(r"(^|[^-\s])--(?=[^-\s]|$)", flags=re.MULTILINE) +EN_DASH_RE = re.compile( + r"(^|" + _SPACE + r")--(?=" + _SPACE + r"|$)", flags=re.MULTILINE +) + +EN_DASH_INDENT_RE = re.compile( + r"(^|" + _NOT_SPACE_OR_DASH + r")--(?=" + _NOT_SPACE_OR_DASH + r"|$)", + flags=re.MULTILINE, +) SCOPED_ABBR = {"c": "©", "r": "®", "tm": "™"} diff --git a/tests/test_port/test_whitespace.py b/tests/test_port/test_whitespace.py index dee1b196..2b79fab4 100644 --- a/tests/test_port/test_whitespace.py +++ b/tests/test_port/test_whitespace.py @@ -79,3 +79,51 @@ def test_fence_info_still_splits_on_whitespace(): def test_table_cell_keeps_the_character(): md = MarkdownIt("js-default").enable("table") assert "c\x85" in md.render("|a|\n|---|\n|c\x85|") + + +@pytest.mark.parametrize("char", EXTRA) +def test_html_tag_is_not_split_on_it(char): + """Python's regex ``\\s`` matches the character, JavaScript's does not. + + So it must not separate a tag name from an attribute or close a tag, in + inline HTML or at the start of an HTML block. + """ + md = MarkdownIt("commonmark") + assert ( + md.render(f'a e') + == f"

a <b{char}c="d"> e

\n" + ) + assert md.render(f"a e") == f"

a </b{char}> e

\n" + assert md.render(f"") == f"

<pre{char}>

\n" + assert ( + md.render(f"\n\n*x*") + == f"

<div{char}>

\n

x

\n" + ) + assert md.render(f"{char}") == f"

{char}

\n" + + +def test_html_tag_still_split_on_whitespace(): + """Control: a real space or tab still works inside a tag.""" + md = MarkdownIt("commonmark") + for char in [" ", "\t"]: + assert md.render(f'a e') == f'

a e

\n' + assert md.render(f"\n\n*x*") == f"\n

x

\n" + assert md.render(f"{char}") == f"{char}" + + +@pytest.mark.parametrize("char", EXTRA) +def test_typographer_dashes_treat_it_as_text(char): + """The en dash rules must not see the character as whitespace.""" + md = MarkdownIt("js-default", {"typographer": True}) + assert md.renderInline(f"a--{char}b") == f"a\u2013{char}b" + assert md.renderInline(f"a{char}-- b") == f"a{char}-- b" + assert md.renderInline(f"a --{char}b") == f"a --{char}b" + assert md.renderInline(f"a{char}--{char}b") == f"a{char}\u2013{char}b" + + +def test_typographer_dashes_still_see_whitespace(): + """Control: a real space or tab is still whitespace around ``--``.""" + md = MarkdownIt("js-default", {"typographer": True}) + for char in [" ", "\t"]: + assert md.renderInline(f"a--{char}b") == f"a--{char}b" + assert md.renderInline(f"a{char}--{char}b") == f"a{char}\u2013{char}b"