| 1357 | } |
| 1358 | |
| 1359 | function splitCells(tableRow, count) { |
| 1360 | // ensure that every cell-delimiting pipe has a space |
| 1361 | // before it to distinguish it from an escaped pipe |
| 1362 | var row = tableRow.replace(/\|/g, function (match, offset, str) { |
| 1363 | var escaped = false, |
| 1364 | curr = offset; |
| 1365 | while (--curr >= 0 && str[curr] === '\\') escaped = !escaped; |
| 1366 | if (escaped) { |
| 1367 | // odd number of slashes means | is escaped |
| 1368 | // so we leave it alone |
| 1369 | return '|'; |
| 1370 | } else { |
| 1371 | // add space before unescaped | |
| 1372 | return ' |'; |
| 1373 | } |
| 1374 | }), |
| 1375 | cells = row.split(/ \|/), |
| 1376 | i = 0; |
| 1377 | |
| 1378 | if (cells.length > count) { |
| 1379 | cells.splice(count); |
| 1380 | } else { |
| 1381 | while (cells.length < count) cells.push(''); |
| 1382 | } |
| 1383 | |
| 1384 | for (; i < cells.length; i++) { |
| 1385 | // leading or trailing whitespace is ignored per the gfm spec |
| 1386 | cells[i] = cells[i].trim().replace(/\\\|/g, '|'); |
| 1387 | } |
| 1388 | return cells; |
| 1389 | } |
| 1390 | |
| 1391 | // Remove trailing 'c's. Equivalent to str.replace(/c*$/, ''). |
| 1392 | // /c*$/ is vulnerable to REDOS. |