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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

*Mike Dalessio*

* A backslash-escaped pipe (`\|`) in a table row no longer splits the cell,
even inside a code span, following the GFM spec.

Fixes #477.

*Mike Dalessio*

## Version 3.6.1

* Migrate Markdown objects to the `TypedData` API.
Expand Down
29 changes: 22 additions & 7 deletions ext/redcarpet/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,12 @@ parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t
return tag_end;
}

static int
is_table_delimiter(uint8_t *data, size_t i)
{
return data[i] == '|' && (i == 0 || data[i - 1] != '\\');
}

Comment on lines +2279 to +2284

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate this abstraction!

static void
parse_table_row(
struct buf *ob,
Expand All @@ -2298,28 +2304,37 @@ parse_table_row(
i++;

for (col = 0; col < columns && i < size; ++col) {
size_t cell_start, cell_end;
struct buf *cell_work;
size_t cell_start, cell_end, j;
struct buf *cell_work, *cell_text;

cell_work = rndr_newbuf(rndr, BUFFER_SPAN);
cell_text = rndr_newbuf(rndr, BUFFER_SPAN);

while (i < size && _isspace(data[i]))
i++;

cell_start = i;

while (i < size && data[i] != '|')
while (i < size && !is_table_delimiter(data, i))
i++;

cell_end = i - 1;

while (cell_end > cell_start && _isspace(data[cell_end]))
cell_end--;

parse_inline(cell_work, rndr, data + cell_start, 1 + cell_end - cell_start);
/* GFM unescapes pipes before inline parsing, even inside code spans */
for (j = cell_start; j <= cell_end; j++) {
if (data[j] == '\\' && j < cell_end && data[j + 1] == '|')
j++;
bufputc(cell_text, data[j]);
}

parse_inline(cell_work, rndr, cell_text->data, cell_text->size);
rndr->cb.table_cell(row_work, cell_work, col_data[col] | header_flag, rndr->opaque);

rndr_popbuf(rndr, BUFFER_SPAN);
rndr_popbuf(rndr, BUFFER_SPAN); /* cell_text */
rndr_popbuf(rndr, BUFFER_SPAN); /* cell_work */
i++;
}

Expand Down Expand Up @@ -2347,7 +2362,7 @@ parse_table_header(

pipes = 0;
while (i < size && data[i] != '\n')
if (data[i++] == '|')
if (is_table_delimiter(data, i++))
pipes++;

if (i == size || pipes == 0)
Expand All @@ -2361,7 +2376,7 @@ parse_table_header(
if (data[0] == '|')
pipes--;

if (header_end && data[header_end - 1] == '|')
if (header_end && is_table_delimiter(data, header_end - 1))
pipes--;

*columns = pipes + 1;
Expand Down
45 changes: 45 additions & 0 deletions test/markdown_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,49 @@ def test_single_dashes_on_table_headers

assert_match /<table>/, output
end

def test_escaped_pipes_do_not_split_table_cells
markdown = <<-'Markdown'.strip_heredoc
| a | b |
| - | - |
| x\|y | z |
Markdown
output = render(markdown, with: [:tables])

assert_includes output, "<td>x|y</td>\n<td>z</td>"
end

def test_escaped_pipes_do_not_split_code_spans_in_table_cells
markdown = <<-'Markdown'.strip_heredoc
| a | b |
| - | - |
| `x\|[click](javascript:alert(1))` | y |
Markdown
output = render(markdown, with: [:tables])

assert_includes output, "<td><code>x|[click](javascript:alert(1))</code></td>\n<td>y</td>"
end

def test_escaped_pipes_after_a_backslash_in_code_spans_in_table_cells
markdown = <<-'Markdown'.strip_heredoc
| a | b |
| - | - |
| `x\\|y` | z |
Markdown
output = render(markdown, with: [:tables])

assert_includes output, "<td><code>x\\|y</code></td>\n<td>z</td>"
end

def test_escaped_pipes_do_not_add_table_columns
markdown = <<-'Markdown'.strip_heredoc
| a\|b | c |
| - | - |
| d | e |
Markdown
output = render(markdown, with: [:tables])

assert_includes output, "<th>a|b</th>\n<th>c</th>\n</tr>"
assert_includes output, "<td>d</td>\n<td>e</td>\n</tr>"
end
end
Loading