mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-10 17:52:47 +00:00
New fix to table regex. Avoids ReDOS issues.
This commit is contained in:
@@ -32,7 +32,7 @@ module.exports = {
|
|||||||
skipBlankLines : true,
|
skipBlankLines : true,
|
||||||
}],
|
}],
|
||||||
'max-depth' : ['warn', { max: 4 }],
|
'max-depth' : ['warn', { max: 4 }],
|
||||||
'max-params' : ['warn', { max: 4 }],
|
'max-params' : ['warn', { max: 5 }],
|
||||||
'no-restricted-syntax' : ['warn', 'ClassDeclaration', 'SwitchStatement'],
|
'no-restricted-syntax' : ['warn', 'ClassDeclaration', 'SwitchStatement'],
|
||||||
'no-unused-vars' : ['warn', {
|
'no-unused-vars' : ['warn', {
|
||||||
vars : 'all',
|
vars : 'all',
|
||||||
|
|||||||
@@ -371,30 +371,36 @@ const getTableCell = (text, cell, type, align)=>{
|
|||||||
const splitCells = (tableRow, count, prevRow = [])=>{
|
const splitCells = (tableRow, count, prevRow = [])=>{
|
||||||
// trim any excessive pipes at start of row
|
// trim any excessive pipes at start of row
|
||||||
tableRow = tableRow.replace(/^\|+(?=\|)/, '')
|
tableRow = tableRow.replace(/^\|+(?=\|)/, '')
|
||||||
.replace(/(\|+)/g, (match, p1, offset, str)=>{
|
.replace(/(\|)(\|*)/g, (match, p1, p2, offset, str)=>{
|
||||||
let escaped = false,
|
let escaped = false,
|
||||||
curr = offset;
|
curr = offset;
|
||||||
while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
|
while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
|
||||||
if(escaped) {
|
if(escaped) {
|
||||||
// odd number of slashes means | is escaped
|
// odd number of slashes means | is escaped
|
||||||
// so we leave it and the slashes alone
|
// so apply a space after to separate from unescaped pipes
|
||||||
return p1;
|
return `${p1} ${p2}`;
|
||||||
} else {
|
} else {
|
||||||
// add space before unescaped | to distinguish it from an escaped pipe
|
// add space before unescaped | to distinguish it from an escaped pipe
|
||||||
return ` ${p1}`;
|
return ` ${p1}${p2}`;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
tableRow = ` ${tableRow}`;
|
tableRow = ` ${tableRow}`;
|
||||||
|
|
||||||
// Split into cells. Contents are: no pipes unless escaped. Add any ending pipes; if not don't include ending space
|
// Split into cells by matching anything that ends with space followed by pipes
|
||||||
const cells = [...tableRow.matchAll(/(?:[^\|]+(?:\\\|)*)+(?:\|+(?=\|)|(?= \|))/g)].map((x)=>x[0]);
|
const cells = [...tableRow.matchAll(/[^\|].*?(?: \|+|$)/g)].map((x)=>{
|
||||||
let i = 0;
|
if(x[0].slice(-2) == ' |') //Cut off the added space too if only one pipe
|
||||||
|
return x[0].slice(0, -2);
|
||||||
|
else {
|
||||||
|
return x[0].slice(0, -1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// First/last cell in a row cannot be empty if it has no leading/trailing pipe
|
// First/last cell in a row cannot be empty if it has no leading/trailing pipe
|
||||||
if(!cells[0].trim()) { cells.shift(); }
|
if(!cells[0].trim()) { cells.shift(); }
|
||||||
if(!cells[cells.length - 1].trim()) { cells.pop(); }
|
if(!cells[cells.length - 1].trim()) { cells.pop(); }
|
||||||
|
|
||||||
let numCols = 0;
|
let numCols = 0;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
for (; i < cells.length; i++) {
|
for (; i < cells.length; i++) {
|
||||||
const trimmedCell = cells[i].split(/ \|+$/)[0];
|
const trimmedCell = cells[i].split(/ \|+$/)[0];
|
||||||
|
|||||||
Reference in New Issue
Block a user