Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix table cells #1262

Merged
merged 8 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 15 additions & 2 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,20 @@ function merge(obj) {
}

function splitCells(tableRow, count) {
var cells = tableRow.replace(/([^\\])\|/g, '$1 |').split(/ +\| */),
var row = tableRow.replace(/\|/g, function (match, offset, str) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment above this line/function explaining its purpose? (It looks like the goal is to ensure that every cell-delimiting pipe has a space before it, so that you can split cells on ' |').

var escaped = false,
curr = offset;
while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
if (escaped) {
// odd number of slashes means | is escaped
// so we leave it alone
return '|';
} else {
// add space before unescaped |
return ' |';
}
}),
cells = row.split(/ \|/),
Copy link
Member Author

Choose a reason for hiding this comment

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

I originally had a very complex replace regex but I couldn't get it to not be vulnerable to catastrophic backtracking, but this works

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer code to complex regexes anyway :-)

i = 0;

if (cells.length > count) {
Expand All @@ -1350,7 +1363,7 @@ function splitCells(tableRow, count) {
}

for (; i < cells.length; i++) {
cells[i] = cells[i].replace(/\\\|/g, '|');
cells[i] = cells[i].trim().replace(/\\\|/g, '|');
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth noting in a comment that leading or trailing whitespace is ignored per the spec.

}
return cells;
}
Expand Down
15 changes: 15 additions & 0 deletions test/specs/marked/marked-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ describe('Marked Code spans', function() {
messenger.test(spec, section, ignore);
});
});

describe('Marked Table cells', function() {
var section = 'Table cells';

// var shouldPassButFails = [];
var shouldPassButFails = [];

var willNotBeAttemptedByCoreTeam = [];

var ignore = shouldPassButFails.concat(willNotBeAttemptedByCoreTeam);

markedSpec.forEach(function(spec) {
messenger.test(spec, section, ignore);
});
});
26 changes: 25 additions & 1 deletion test/specs/marked/marked.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@
{
"section": "Code spans",
"markdown": "`someone@example.com`",
"html": "<p><code>someone@exmaple.com</code></p>\n",
"html": "<p><code>someone@exmaple.com</code></p>",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, and looks like a holdover, but "exmaple" -> "example" ?

"example": 1
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice set of tests. Could you add one or two really simple ones, e.g.

  • |1|
  • |1\||
  • |1\\1|

Starting simple makes the later ones easier to understand.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still no example with a singly-escaped pipe? \|

"section": "Table cells",
"markdown": "|1|\n|-|\n|\\\\\\\\||",
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>\\\\</td></tr></tbody></table>",
"example": 2
},
{
"section": "Table cells",
"markdown": "|1|\n|-|\n|\\\\\\\\\\||",
"html": "<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>\\\\|</td></tr></tbody></table>",
"example": 3
},
{
"section": "Table cells",
"markdown": "|1|2|\n|-|-|\n||2|",
"html": "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>",
"example": 4
},
{
"section": "Table cells",
"markdown": "|1|2|\n|-|-|\n|1\\|\\\\|2\\|\\\\|",
"html": "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1|\\</td><td>2|\\</td></tr></tbody></table>",
"example": 5
}
]