Repository navigation
fix: table cell positions for rows without a leading or trailing pipe (DEV-3395) #27
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,56 @@ function getLine (state, line) { | |
| return state.src.slice(pos, max) | ||
| } | ||
|
|
||
| // Source position of `getLine(state, line).trim()`: besides the spaces and | ||
| // tabs counted by `tShift`, `trim()` removes Unicode whitespace such as U+00A0 | ||
| function trimmedLineStart (state, line) { | ||
| return state.bMarks[line] + state.tShift[line] + trimLeftOffset(getLine(state, line)) | ||
| } | ||
|
|
||
| // Sets the positions of the open, inline and close tokens of a row's cells. | ||
| // | ||
| // - The open token covers the `|` before its cell (size 1), or sits at the | ||
| // start of the cell (size 0) for the first cell of a row without a leading `|`. | ||
| // - The close token sits on the `|` after its cell (size 0); the last cell of | ||
| // the row claims the trailing `|` (size 1), or sits at the end of the cell | ||
| // (size 0) if there is none. | ||
| // - Autocompleted cells (missing from the row): all three at the end of the | ||
| // line, size 0. | ||
| // | ||
| // `cells` lists [open, inline, close] for each cell, `columns` the cells of | ||
| // the row once the empty first and last columns of a leading and trailing `|` | ||
| // are dropped. | ||
| function setCellPositions (state, line, cells, columns, hasLeadingPipe, hasTrailingPipe) { | ||
| let pos = trimmedLineStart(state, line) | ||
| if (hasLeadingPipe) { pos++ } | ||
|
|
||
| for (let i = 0; i < cells.length; i++) { | ||
| const [open, inline, close] = cells[i] | ||
|
|
||
| if (i >= columns.length) { | ||
| const eol = state.eMarks[line] | ||
| open.position = inline.position = close.position = eol | ||
| open.size = close.size = 0 | ||
| continue | ||
| } | ||
|
|
||
| if (i === 0 && !hasLeadingPipe) { | ||
| open.position = pos | ||
| open.size = 0 | ||
| } else { | ||
| open.position = pos - 1 | ||
| open.size = 1 | ||
| } | ||
|
|
||
| inline.position = pos + trimLeftOffset(columns[i]) | ||
| pos += sourceCellLength(columns[i]) | ||
|
|
||
| close.position = pos | ||
| close.size = (i === columns.length - 1 && hasTrailingPipe) ? 1 : 0 | ||
| pos++ | ||
| } | ||
| } | ||
|
|
||
| // Length of a cell in the source: every `|` left in a cell was `\|` | ||
| function sourceCellLength (cell) { | ||
| let length = cell.length | ||
|
|
@@ -128,8 +178,10 @@ export default function table (state, startLine, endLine, silent) { | |
| if (lineText.indexOf('|') === -1) { return false } | ||
| if (state.sCount[startLine] - state.blkIndent >= 4) { return false } | ||
| columns = escapedSplit(lineText) | ||
| if (columns.length && columns[0] === '') columns.shift() | ||
| if (columns.length && columns[columns.length - 1] === '') columns.pop() | ||
| let hasLeadingPipe = columns.length > 0 && columns[0] === '' | ||
| if (hasLeadingPipe) columns.shift() | ||
| let hasTrailingPipe = columns.length > 0 && columns[columns.length - 1] === '' | ||
| if (hasTrailingPipe) columns.pop() | ||
|
|
||
| // header row will define an amount of columns in the entire table, | ||
| // and align row should be exactly the same (the rest of the rows can differ) | ||
|
|
@@ -161,14 +213,11 @@ export default function table (state, startLine, endLine, silent) { | |
| token_htro.size = 0 | ||
| token_htro.position = state.bMarks[startLine] | ||
|
|
||
| let columnVIndex = state.bMarks[startLine] + state.tShift[startLine] | ||
| let cells = [] | ||
|
|
||
| for (let i = 0; i < columns.length; i++) { | ||
| const token_ho = state.push('th_open', 'th', 1) | ||
| token_ho.map = [startLine, startLine + 1] | ||
| token_ho.size = 1 | ||
| token_ho.position = columnVIndex | ||
| columnVIndex += 1 | ||
|
|
||
| if (aligns[i]) { | ||
| token_ho.attrs = [['style', 'text-align:' + aligns[i]]] | ||
|
|
@@ -177,21 +226,15 @@ export default function table (state, startLine, endLine, silent) { | |
| const token_il = state.push('inline', '', 0) | ||
| token_il.content = columns[i].trim() | ||
| token_il.children = [] | ||
| token_il.position = columnVIndex + trimLeftOffset(columns[i]) | ||
| token_il.size = token_il.content.length | ||
| token_il.map = [nextLine, nextLine + 1] | ||
| columnVIndex += sourceCellLength(columns[i]) | ||
| token_il.map = [startLine, startLine + 1] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified: header inlines now get |
||
|
|
||
| const token_hc = state.push('th_close', 'th', -1) | ||
| token_hc.position = columnVIndex | ||
|
|
||
| // Last column? | ||
| if (i === (columns.length - 1)) { | ||
| token_hc.size = 1 | ||
| columnVIndex += 1 | ||
| } | ||
| cells.push([token_ho, token_il, token_hc]) | ||
| } | ||
|
|
||
| setCellPositions(state, startLine, cells, columns, hasLeadingPipe, hasTrailingPipe) | ||
|
|
||
| const token_htrc = state.push('tr_close', 'tr', -1) | ||
| token_htrc.size = 0 | ||
| token_htrc.position = state.eMarks[startLine] | ||
|
|
@@ -219,8 +262,10 @@ export default function table (state, startLine, endLine, silent) { | |
| if (!lineText) { break } | ||
| if (state.sCount[nextLine] - state.blkIndent >= 4) { break } | ||
| columns = escapedSplit(lineText) | ||
| if (columns.length && columns[0] === '') columns.shift() | ||
| if (columns.length && columns[columns.length - 1] === '') columns.pop() | ||
| hasLeadingPipe = columns.length > 0 && columns[0] === '' | ||
| if (hasLeadingPipe) columns.shift() | ||
| hasTrailingPipe = columns.length > 0 && columns[columns.length - 1] === '' | ||
| if (hasTrailingPipe) columns.pop() | ||
|
|
||
| // note: autocomplete count can be negative if user specifies more columns than header, | ||
| // but that does not affect intended use (which is limiting expansion) | ||
|
|
@@ -239,36 +284,27 @@ export default function table (state, startLine, endLine, silent) { | |
| token_tro.size = 0 | ||
| token_tro.position = state.bMarks[nextLine] | ||
|
|
||
| columnVIndex = state.bMarks[nextLine] + state.tShift[nextLine] | ||
| cells = [] | ||
|
|
||
| for (let i = 0; i < columnCount; i++) { | ||
| const token_tdo = state.push('td_open', 'td', 1) | ||
| token_tdo.size = 1 | ||
| token_tdo.position = columnVIndex | ||
| columnVIndex++ | ||
|
|
||
| if (aligns[i]) { | ||
| token_tdo.attrs = [['style', 'text-align:' + aligns[i]]] | ||
| } | ||
|
|
||
| const originalContent = columns[i] || '' | ||
|
|
||
| const token_il = state.push('inline', '', 0) | ||
| token_il.content = columns[i] ? columns[i].trim() : '' | ||
| token_il.children = [] | ||
| token_il.size = token_il.content.length | ||
| token_il.position = columnVIndex + trimLeftOffset(originalContent) | ||
| token_il.map = [nextLine, nextLine + 1] | ||
| columnVIndex += sourceCellLength(originalContent) | ||
|
|
||
| const token_tdc = state.push('td_close', 'td', -1) | ||
| token_tdc.position = columnVIndex | ||
|
|
||
| // Last column? | ||
| if (i === (columns.length - 1)) { | ||
| token_tdc.size = 1 | ||
| } | ||
| cells.push([token_tdo, token_il, token_tdc]) | ||
| } | ||
|
|
||
| setCellPositions(state, nextLine, cells, columns, hasLeadingPipe, hasTrailingPipe) | ||
|
|
||
| const token_trc = state.push('tr_close', 'tr', -1) | ||
| token_trc.size = 0 | ||
| token_trc.position = state.eMarks[nextLine] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: these four lines are repeated at L265–268 for body rows. A small helper (e.g.
splitRow(lineText)→{ columns, hasLeadingPipe, hasTrailingPipe }) would keep the header and body rows from drifting apart. Not blocking.