Skip to content
Open
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
11 changes: 8 additions & 3 deletions markdown_it/common/html_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]+"
Expand All @@ -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 = "<![A-Za-z][^>]*>"
Expand Down
12 changes: 8 additions & 4 deletions markdown_it/rules_block/html_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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,
),
Expand All @@ -24,12 +27,13 @@
(re.compile(r"^<!\[CDATA\["), re.compile(r"\]\]>"), True),
(
re.compile(
"^</?(?a:(" + "|".join(block_names) + "))(?=(\\s|/?>|$))", re.IGNORECASE
"^</?(?a:(" + "|".join(block_names) + "))(?=(" + whitespace + "|/?>|$))",
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),
]


Expand Down
15 changes: 13 additions & 2 deletions markdown_it/rules_core/replacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
import re

from ..common.html_re import whitespace
from ..token import Token
from .state_core import StateCore

Expand Down Expand Up @@ -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": "™"}
Expand Down
48 changes: 48 additions & 0 deletions tests/test_port/test_whitespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b{char}c="d"> e')
== f"<p>a &lt;b{char}c=&quot;d&quot;&gt; e</p>\n"
)
assert md.render(f"a </b{char}> e") == f"<p>a &lt;/b{char}&gt; e</p>\n"
assert md.render(f"<pre{char}>") == f"<p>&lt;pre{char}&gt;</p>\n"
assert (
md.render(f"<div{char}>\n\n*x*")
== f"<p>&lt;div{char}&gt;</p>\n<p><em>x</em></p>\n"
)
assert md.render(f"</a>{char}") == f"<p></a>{char}</p>\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 <b{char}c="d"> e') == f'<p>a <b{char}c="d"> e</p>\n'
assert md.render(f"<div{char}>\n\n*x*") == f"<div{char}>\n<p><em>x</em></p>\n"
assert md.render(f"</a>{char}") == f"</a>{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"