Skip to content

HTML export: a line break inside a cell stays a line break - #4369

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/html-export-cell-line-breaks
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/html-export-cell-line-breaks

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4357

What was wrong

to_html in crates/engine/src/io.rs escaped a cell's text and wrote it as is. A line feed inside a cell (alpha ⏎ beta) reached the page as a raw newline, which HTML (and the cell's white-space:nowrap) shows as a space. So the cell read alpha beta in a browser, and Excel reopened it as one line, the same as a single-line neighbour, as the issue shows.

What changed

  • A line feed in a cell's text is written as <br style="mso-data-placement:same-cell">. That's the form Excel's own Web Page export uses for a break that stays in the cell; a plain <br> can make Excel start a new row when it reads HTML. Browsers ignore the style and break the line.
  • How: a cell-text variant of the escaper (esc_cell_within) does this, inside the same page-size budget.
  • Unchanged: esc_within itself, so image alt text and other attribute values keep a line feed as a character, never markup inside an attribute.

QA report

Main vs branch, end to end (gridcraft-cli convert)

The workbook is the issue's setup, built with cell.set: A1 alpha⏎beta, A2 the single-line alpha beta, B1 neighbor, and A3 a&b⏎<c>, which checks escaping around a break. It was saved to .xlsx and converted with gridcraft-cli convert source.xlsx output.htm. main is a gridcraft-cli built from origin/main (1476d4e7); the branch is this PR (6ba8ea13).

Cell main this PR
A1 alpha⏎beta (raw newline, shown as a space) alpha<br style="mso-data-placement:same-cell">beta
A2 (control) alpha beta alpha beta
A3 a&amp;b⏎&lt;c&gt; a&amp;b<br style="mso-data-placement:same-cell">&lt;c&gt;
B1 (neighbour) neighbor neighbor

Read back. I didn't have Excel. I read each page back with a gridcraft-cli built from #4345, the import-side fix for <br> in cells, and evaluated =LEN(A1)&"|"&CODE(MID(A1,6,1))&"|"&(A1=A2)&"|"&B1&"|"&LEN(A3):

Page from result meaning
main 10|32|TRUE|neighbor|7 A1 came back as alpha beta: the break is a space, and A1 equals A2
this PR 10|10|FALSE|neighbor|7 A1 kept its line feed (code 10), and it no longer equals A2

Test

io::tests::html_export_keeps_line_breaks_inside_cells covers:

  • the issue's two cells;
  • a cell with &, <, > and a double break (a&b⏎<c>⏎⏎d);
  • esc_within keeping a line feed as a character for attributes;
  • the break counting against the page's room (one byte short gives None).

The other HTML export tests still pass.

Negative control

With esc_cell_within writing line feeds as characters again (on top of 6ba8ea13, then restored with git checkout), the test fails at its first assertion.

Gate (macOS arm64)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-engine --all-targets -- -D warnings clean
cargo test -p gridcraft-engine -p gridcraft-xlsx 1005 (+1 ignored) + 120 + 7 + 374 passed, 0 failed

Not covered

  • Excel: I didn't open the output in Excel. The mso-data-placement:same-cell form is the one the issue and Excel's own export use; the read-back above used GridCraft's importer.
  • Wrap text: cells keep white-space:nowrap, so a long line still doesn't wrap. Only an explicit line feed breaks it, as in Excel when wrap text is off.
  • Carriage returns: a lone \r is written as it is; GridCraft stores line breaks as \n.
  • Overlap with HTML export: cell hyperlinks keep their destinations #4366: both PRs change the line in to_html that escapes the cell text (this one swaps esc_within for esc_cell_within). Whichever lands second needs a one-line rebase.
  • cargo xtask ci: I didn't run the full cargo xtask ci (wasm, layers, the other crates).

Summary by CodeRabbit

  • Bug Fixes
    • HTML exports now preserve line breaks within cells, displaying them as breaks rather than running text together. Spaces and escaped HTML characters are also preserved.

A cell holding a line feed was written as plain text, which the page (and the cell's white-space:nowrap) shows as a space, so Excel reopened it as one line. The break is now written as Excel's Web Page export writes it, <br style="mso-data-placement:same-cell">, so it stays in the cell. Image alt text and other attributes are escaped as before. Fixes storytold#4357.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

HTML page export now converts line feeds in cell text to Excel same-cell <br> markup. General-purpose escaping leaves line feeds unchanged. The added test checks line breaks, HTML escaping, and the generated markup’s byte length.

Changes: HTML cell line breaks

Layer / File(s) Summary
Cell escaping and HTML export
crates/engine/src/io.rs, crates/engine/src/io/tests.rs
Cell-specific escaping converts line feeds to same-cell break markup. The HTML page exporter uses this escaping path. A test checks line breaks, ordinary spaces, special-character escaping, and byte-length handling.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue 4329 describes the separate HTML import behavior for <br />. This change updates the writer, not the reader, so it does not address that issue.

Suggested reviewers: dylanmtaylor, echelon

Merge Risk: Minimal · up to 6ba8e

HTML export now keeps a line break inside a cell as a line break. The change is small and has test coverage. No actionable merge-blocking risk remains. Excel rendering itself was not tested.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a line feed inside an HTML-exported cell is now emitted as a same-cell line break instead of being lost. The raw summary, the PR objectives, and the linked issue #4357…
Linked Issues check Passed Issue #4357 requires HTML export to keep a line feed inside a cell as a line break, so A1 (alpha⏎beta) differs from the single-line A2 (alpha beta), and adjacent values survive. The PR routes cell…
Out of Scope Changes check Passed The whole-PR changes (merge base 1476d4e to head 6ba8ea1) touch the HTML cell-text escaping path in crates/engine/src/io.rs and add one test in crates/engine/src/io/tests.rs. The test's added li…
Full details: Title check

Explanation

The title names the main change: a line feed inside an HTML-exported cell is now emitted as a same-cell line break instead of being lost. The raw summary, the PR objectives, and the linked issue #4357 all describe this same change to HTML cell escaping. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

Issue #4357 requires HTML export to keep a line feed inside a cell as a line break, so A1 (alpha⏎beta) differs from the single-line A2 (alpha beta), and adjacent values survive. The PR routes cell text through the new esc_cell_within, which writes each LF as &lt;br style="mso-data-placement:same-cell"&gt;. The added test io::tests::html_export_keeps_line_breaks_inside_cells calls to_html and asserts: &gt;alpha&lt;br…&gt;beta&lt;/td&gt; for A1, &gt;alpha beta&lt;/td&gt; for A2, and escaped a&amp;amp;b&lt;br…&gt;&amp;lt;c&amp;gt;&lt;br…&gt;&lt;br…&gt;d for A3. It also checks that esc_within keeps LF as a character and that the break counts toward the page budget. The PR reports these tests and the wider engine and xlsx test suites pass at head. The issue's requirement is an Excel result. Excel was not run here, and the PR states this. The mso-data-placement:same-cell form matches the form the issue cites from Excel's own Web Page export. The io.rs text diff could not be read in this review. The implementation was checked only through the test's assertions and the PR's summary, not through the changed source lines. The requirement is met as far as this evidence shows. No requirement is unmet in the evidence.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes (merge base 1476d4e to head 6ba8ea1) touch the HTML cell-text escaping path in crates/engine/src/io.rs and add one test in crates/engine/src/io/tests.rs. The test's added lines all exercise the cell-text break behavior, the attribute-escaping boundary (esc_within), or the page-size budget for the same change. The PR says esc_within is unchanged. The summary and test do not show unrelated edits. The io.rs diff could not be read, so line-level scope was verified only through the test and the PR summary. No demonstrated unrelated change was found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between abcfc19 and 6ba8ea1.

Files selected for processing: 2

  • crates/engine/src/io.rs
  • crates/engine/src/io/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

This branch has not been deployed

No deployments
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.

HTML export collapses line breaks inside worksheet cells

1 participant