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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

# Matches a pipe together with the (possibly empty) run of backslashes in front
# of it, so that run can be doubled before the pipe is escaped.
_PIPE_ESCAPE_RE = re.compile(r"(\\*)\|")
# The lookbehind avoids retrying from each position inside a backslash run.
_PIPE_ESCAPE_RE = re.compile(r"(?<!\\)(\\*)\|")


def _escape_table_cell(value: str) -> str:
Expand Down
14 changes: 14 additions & 0 deletions packages/markitdown/tests/test_module_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,20 @@ def test_csv_backslash_without_a_pipe_is_left_alone() -> None:
assert r"| Widget | C:\temp\file.txt |" in result


@pytest.mark.parametrize(
"suffix,escaped_suffix",
[("", ""), ("|", r"\|"), ("x|", r"x\|")],
)
def test_csv_long_backslash_runs(suffix: str, escaped_suffix: str) -> None:
backslashes = "\\" * 65_536
value = backslashes + suffix
expected = backslashes * (2 if suffix == "|" else 1) + escaped_suffix

result = _convert_csv(f"{value}\n{value}\n".encode("utf-8"), charset="utf-8")

assert result == f"| {expected} |\n| --- |\n| {expected} |"


# ---------------------------------------------------------------------------
# Regression test for issue #1960:
# exiftool_path pointing to a nonexistent binary used to leak a raw
Expand Down