Skip to content

table: fix source positions of body rows and of a header row after a paragraph - #401

Open
tsnee wants to merge 3 commits into
github:masterfrom
tsnee:fix-table-row-sourcepos
Open

tsnee wants to merge 3 commits into
github:masterfrom
tsnee:fix-table-row-sourcepos

Conversation

@tsnee

@tsnee tsnee commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #400.

Two fixes to source positions in extensions/table.c: the first commit fixes body rows, and the second and third fix a header row that interrupts a paragraph, so the first can be taken alone. Rendered output doesn't change; only --sourcepos output (data-sourcepos, XML sourcepos) and the node API's positions do.

1. A body row gets its own start column

try_opening_table_row placed every body row at the table's start column, and each cell at the table's start column plus its offset in the row, while the row is parsed from the line's own first non-space byte. A row whose container prefix differs in width from the table's first line was reported at the wrong columns:

> | ID | Req |
> |---|---|
>A | x | y
Node Before After
row 3:3-3:10 3:2-3:10
cell A 3:3-3:4 3:2-3:3
cell x 3:6-3:8 3:5-3:7

The row now starts at cmark_parser_get_first_nonspace(parser) + 1, as the core parser places every block it opens (parser->first_nonspace + 1), and the cells are placed relative to it. Columns stay byte offsets, so a tab in the prefix is counted as in the core parser; the test covers one.

2. A header row that interrupts a paragraph is placed on its own line

When the header row was the last line of a longer paragraph, the table and header kept the paragraph's start, the header cells' columns were counted across the paragraph's whole content, and the paragraph inserted for the lines above had no position:

Reminders are sent daily.
The scheduler shall send reminders.
| ID | Requirement |
|---|---|
Node Before After
paragraph none 1:1-2:35
its second text 1:0-1:34 2:1-2:35
table 1:1-4:9 3:1-4:9
header 1:1-1:82 3:1-3:20
cell ID 1:64-1:67 3:2-3:5

The header row is always the line just before the delimiter row, and the paragraph holds its content. Usually that content starts at the line's first non-space byte; a lazy continuation line is kept with its leading spaces, and with a tab that a block quote marker consumed in part expanded to spaces (the third commit, after Copilot's review). From the first non-space byte on, the content matches the line byte for byte, so the header's start column is cmark_parser_get_last_line_length(parser) minus the length of the content from its first non-space byte, plus one, and it ends at the previous line's length. The cells' columns are counted from that start, and a first cell without a leading pipe starts there, as in a body row. This is exact for any container prefix, including a lazy continuation line.

The paragraph left above the header keeps the original start and ends on the line before the header. Its end column is exact when it is one line. With more lines, the width of its last line's prefix isn't recorded anywhere once the line is in the paragraph, so it assumes the same prefix as the first line, as the positions of inlines on later lines of a paragraph already do. That holds for ordinary block quotes and list items, and is off only when a later line is indented differently or is a lazy continuation.

Tests

Five new cases in api_test/main.c (the spec tests compare HTML without positions), each failing before its fix:

  • table_row_source_pos: body rows under > with no space, and with a tab.
  • table_header_after_paragraph_source_pos: the example above with a body row; a block quote whose header line has a wider prefix than the paragraph; and a header on a lazy continuation line, with leading spaces (> intro / A | B / > ---|---) and with a partly consumed tab (> > intro / >⇥A | B / > > --|--).

ctest passes 13 of 13 and api_test 548 of 548 (543 before, plus the 5 new cases). api_test also passes under -fsanitize=address,undefined.

Related

#222 and #223 cover a wider set of source-position problems; #222's item 1 (a table-like paragraph above the header) is a case of the second fix here, and its output is now 1:1-1:13 for the paragraph and 2:1 for the table. #223 doesn't change try_opening_table_row, and no longer applies to table.c.

tsnee and others added 2 commits November 14, 2026 09:07
try_opening_table_row placed every body row at the table's start column,
and each cell at the table's start column plus the cell's offset within
the row. The row itself is parsed from the line's first non-space byte,
so whenever a row's container prefix is narrower or wider than the
table's first line's, the row, its cells and their inlines were reported
at the wrong columns:

    > | ID | Req |
    > |---|---|
    >A | x | y

gave the row 3:3-3:10 and its cells 3:3-3:4 and 3:6-3:8, where A is at
column 2.

Place the row at the line's first non-space byte plus one, as the core
parser does for every block it opens, and the cells relative to that.
Columns stay byte-based, so a tab in the prefix is handled as in the
core parser.

Only source positions change; rendered output does not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a table's header row is the last line of a longer paragraph,
try_opening_table_header turned the paragraph into the table and kept
its start, so the table, its header row and the header cells were given
the paragraph's first line, with columns counted from the start of the
paragraph's whole content. The paragraph inserted for the lines above
the header had no position at all, so its inlines were counted from
line 0:

    Reminders are sent daily.
    The scheduler shall send reminders.
    | ID | Requirement |
    |---|---|

gave the table 1:1, the header 1:1-1:82 and its first cell 1:64-1:67,
where the header is 3:1-3:20 and that cell 3:2-3:5.

The header row is the line before the delimiter row, and the paragraph
holds it from its first non-space byte, so its start column is the
previous line's length less the length of that content, plus one.
Start the table and the header row there, end the header row at the
previous line's length, and place the header cells relative to that
start. Give the inserted paragraph the original start and end it on the
line above the header. Its end column is exact when it is a single
line; for more lines, it assumes the same container prefix as its first
line, as the positions of inlines on later lines of a paragraph do.

Only source positions change; rendered output does not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI adaptive review requested due to automatic review settings November 14, 2026 14:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Header position calculations still mishandle retained spaces and expanded tabs, reporting the first cell at column 1 instead of column 3 in lazy-continuation cases.

1 open issue
What changed in this PR

This PR addresses #400 by correcting table source positions for body rows with varying container prefixes and headers that interrupt paragraphs, without changing rendered content.

Changes:

  • Position body rows and cells relative to each row’s own start column.
  • Correct table and header positions, and assign positions to the preceding paragraph.
  • Add three XML source-position regression cases.
File Description
extensions/​table.c Adjusts source-position calculations for rows, headers, cells, and preceding paragraphs.
api_test/​main.c Adds a table-rendering helper and source-position regression tests.

Review effort: Balanced (dynamic)

Comment thread extensions/table.c Outdated
Comment on lines +399 to +401
header_start_column = header_end_column + 1 -
length_without_line_ending((unsigned char *)parent_string + header_row->paragraph_offset,
(int)strlen(parent_string) - header_row->paragraph_offset);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, confirmed and fixed in a960969. A lazy continuation line is kept in the paragraph with its leading spaces, and with a partly consumed tab expanded to spaces, so the header origin is now measured from the content's first non-space byte, and the cells are counted from there.

The first input is now a regression case. The second, > > intro\n>\tA | B\n> > - | -\n, doesn't form a table: - | - opens a list item. The test uses > > --|-- as the delimiter row instead, which keeps the partly consumed tab on the header line. Both put A at 2:3 now.

…ace byte

The previous commit derived the header row's start column from the
length of the header line's content in the paragraph, assuming that
content starts at the line's first non-space byte. A lazy continuation
line is kept from where container matching stopped instead, with its
leading spaces, and with a tab that a block quote marker consumed in
part expanded to spaces, so the header and its first cell were placed
too far left:

    > intro
      A | B
    > ---|---

gave the header 2:1-2:7 and the cell A 2:1-2:4, where A is at column 3.

From the first non-space byte on, the content matches the line byte for
byte, so skip the leading spaces and tabs before measuring it, and count
the cells' columns from there. A first cell without a leading pipe now
starts at the row's first non-space byte, as in a body row.

Reported by Copilot's review of commonmark#401.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

table: body rows and a header row after a paragraph get wrong source positions

2 participants