fix(table): emit numeric pct widths in fiftieths of a percent - #3476
fix(table): emit numeric pct widths in fiftieths of a percent#3476omartuhintvs wants to merge 1 commit into
Conversation
createTableWidthElement wrote a numeric PERCENTAGE size as a "50%" string. Both that string and the fiftieths-of-a-percent integer (50% = 2500) are valid under ST_MeasurementOrPercent, but Word writes the integer, and stricter consumers — Google Docs and Apple Pages/QuickLook/Mail — misread the string form and collapse table columns to one character. Emit the integer (Math.round(size * 50)) for numeric pct sizes to match Word's output. Callers who want the literal percent string can still pass a Percentage (e.g. "50%"), which is left untouched. Adds table-width.spec.ts and updates the two specs that asserted the old string output.
📝 WalkthroughWalkthroughNumeric percentage widths now use WordprocessingML’s fiftieths-of-a-percent integer representation. Table width and cell margin tests were updated, with new coverage for rounding, literal percentage strings, and DXA passthrough. ChangesPercentage width encoding
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/file/table/table-width.spec.ts (1)
5-5: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse the configured
@file/alias.This new TypeScript spec imports
table-widthrelatively instead of using the repository’s required path aliases.Suggested change
-import { WidthType, createTableWidthElement } from "./table-width"; +import { WidthType, createTableWidthElement } from "`@file/table/table-width`";As per coding guidelines, TypeScript imports should use the
@file/,@export/, and@util/path aliases.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/file/table/table-width.spec.ts` at line 5, Update the import in the table-width spec to use the configured `@file/` alias for table-width instead of a relative path, while preserving the existing WidthType and createTableWidthElement imports.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/file/table/table-width.spec.ts`:
- Around line 16-20: Update the rounding test in the percentage-width case to
use a fractional value such as 33.31 whose multiplication by 50 requires
rounding, and change the expected width to 1666. Keep the existing formatter and
percentage configuration unchanged so the assertion specifically verifies
Math.round behavior.
---
Nitpick comments:
In `@src/file/table/table-width.spec.ts`:
- Line 5: Update the import in the table-width spec to use the configured `@file/`
alias for table-width instead of a relative path, while preserving the existing
WidthType and createTableWidthElement imports.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: e5dab2f7-e1bc-472c-aa8b-5e8dbe388b9b
📒 Files selected for processing (4)
src/file/table/table-properties/table-cell-margin.spec.tssrc/file/table/table-width.spec.tssrc/file/table/table-width.tssrc/file/table/table.spec.ts
| it("rounds fractional percentages to the nearest fiftieth", () => { | ||
| const tree = new Formatter().format(createTableWidthElement("w:tblW", { size: 33.3, type: WidthType.PERCENTAGE })); | ||
|
|
||
| expect(tree).to.deep.equal({ "w:tblW": { _attr: { "w:type": "pct", "w:w": 1665 } } }); | ||
| }); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Make the rounding test exercise rounding.
33.3 * 50 equals 1665, so this test would also pass with truncation or without meaningful rounding. Use a value such as 33.31 and expect 1666 to verify the Math.round behavior.
As per coding guidelines, test files should cover edge cases and option combinations.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/file/table/table-width.spec.ts` around lines 16 - 20, Update the rounding
test in the percentage-width case to use a fractional value such as 33.31 whose
multiplication by 50 requires rounding, and change the expected width to 1666.
Keep the existing formatter and percentage configuration unchanged so the
assertion specifically verifies Math.round behavior.
Source: Coding guidelines
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3476 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 311 311
Lines 3237 3237
Branches 732 732
=========================================
Hits 3237 3237 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What
createTableWidthElementserialises a numericWidthType.PERCENTAGEsize as a percent string (w:w="50%"). This changes it to emit the value in fiftieths of a percent (50% → 2500), which is the form Word itself writes.Fixes #1457 (also the root cause behind #349, #216, #3015).
Why
For
w:tblW/w:tcW,@w:wisST_MeasurementOrPercent, whose union permits both anST_Percentagestring ("50%") and anST_UnqualifiedPercentageinteger in fiftieths. So the current"50%"output is schema-valid — this is an interoperability issue, not a spec violation:pctwidths as the fiftieths integer..docximporter and Apple's shared OOXML parser (Pages, QuickLook, Mail preview, iCloud) — misread the"50%"string, fall back totblGrid, and collapse every column to ~1 character wide (text stacked vertically). This is the long-standing "tables broken in Google Docs" report.Emitting the integer matches Word and renders correctly everywhere, with no visual change in Word/LibreOffice.
Compatibility
{ size: 50, type: PERCENTAGE }still means 50%).Percentage(e.g.{ size: "50%", type: PERCENTAGE }) — that path is untouched."100%"→5000). Two existing specs asserted the old string output and are updated. I recognise%was introduced deliberately in Fix: table width in percentage should include '%' #282; happy to gate this behind an option instead if you'd prefer to preserve the exact previous bytes by default.Tests
src/file/table/table-width.spec.tscovering: numeric pct → fiftieths, fractional rounding, thePercentage-string path still emitting the string, anddxauntouched.table.spec.ts,table-cell-margin.spec.ts).Summary by CodeRabbit