-
Notifications
You must be signed in to change notification settings - Fork 114
SD-2343 - Table borders being converted twice #2667
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
Open
chittolinag
wants to merge
8
commits into
main
Choose a base branch
from
fix/sd-2343
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ed3aa22
fix: table border conversion
chittolina 89cdd8c
fix: ts build issue
chittolina a5a0dc0
fix: tests and final issues
chittolina 29b1d27
fix: convert table borders before creating a new table
chittolina f23908f
fix: convert table styles
chittolina 33bdd02
refactor: simplified code
chittolina 2a6e87b
Merge branch 'main' into fix/sd-2343
chittolinag eda63a0
fix: tests
chittolina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,11 @@ const EIGHTHS_PER_POINT = 8; | |
| const MIN_BORDER_SIZE_PX = 0.5; // Minimum visible border | ||
| const MAX_BORDER_SIZE_PX = 100; // Reasonable maximum | ||
|
|
||
| type BorderConversionUnit = 'px' | 'eighthPoints'; | ||
| type BorderConversionOptions = { | ||
| unit?: BorderConversionUnit; | ||
| }; | ||
|
|
||
| /** | ||
| * Convert an OOXML border size (stored in eighths of a point) to pixels. | ||
| * | ||
|
|
@@ -32,7 +37,7 @@ const MAX_BORDER_SIZE_PX = 100; // Reasonable maximum | |
| * | ||
| * Clamps results to reasonable bounds to prevent edge cases. | ||
| */ | ||
| const borderSizeToPx = (size?: number): number | undefined => { | ||
| export const borderSizeToPx = (size?: number): number | undefined => { | ||
| if (!isFiniteNumber(size)) return undefined; | ||
| if (size <= 0) return 0; | ||
|
|
||
|
|
@@ -43,6 +48,16 @@ const borderSizeToPx = (size?: number): number | undefined => { | |
| return Math.min(MAX_BORDER_SIZE_PX, Math.max(MIN_BORDER_SIZE_PX, pixelValue)); | ||
| }; | ||
|
|
||
| const clampPixelBorderWidth = (width: number): number => | ||
| Math.min(MAX_BORDER_SIZE_PX, Math.max(MIN_BORDER_SIZE_PX, width)); | ||
|
|
||
| const resolveBorderWidth = (size: number, unit: BorderConversionUnit): number | undefined => { | ||
| if (unit === 'eighthPoints') { | ||
| return borderSizeToPx(size); | ||
| } | ||
| return clampPixelBorderWidth(size); | ||
| }; | ||
|
|
||
| /** | ||
| * Normalizes a border/shading color with a default fallback. | ||
| * | ||
|
|
@@ -57,17 +72,18 @@ const normalizeColorWithDefault = (color?: string): string => { | |
| /** | ||
| * Converts an OOXML border specification to layout engine BorderSpec format. | ||
| * | ||
| * Border sizes are assumed to be in eighths of a point (OOXML standard) if >= 8, | ||
| * otherwise treated as already-converted pixel values. Nil/none borders return | ||
| * a special BorderSpec with style 'none' and width 0. | ||
| * Border sizes emitted by the DOCX translator are already expressed in pixels, | ||
| * so the pm-adapter simply clamps them to a safe range instead of converting | ||
| * from eighths-of-a-point. Nil/none borders return a special BorderSpec with | ||
| * style 'none' and width 0. | ||
| * | ||
| * @param ooxmlBorder - Raw OOXML border object with optional val, size, and color properties | ||
| * @returns BorderSpec with style, width (in pixels), and color, or undefined if invalid | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * convertBorderSpec({ val: 'single', size: 16, color: 'FF0000' }); | ||
| * // { style: 'single', width: 2.67, color: '#FF0000' } | ||
| * convertBorderSpec({ val: 'single', size: 4, color: 'FF0000' }); | ||
| * // { style: 'single', width: 4, color: '#FF0000' } | ||
| * | ||
| * convertBorderSpec({ val: 'nil' }); | ||
| * // { style: 'none', width: 0 } | ||
|
|
@@ -76,7 +92,7 @@ const normalizeColorWithDefault = (color?: string): string => { | |
| * // undefined | ||
| * ``` | ||
| */ | ||
| export function convertBorderSpec(ooxmlBorder: unknown): BorderSpec | undefined { | ||
| export function convertBorderSpec(ooxmlBorder: unknown, options?: BorderConversionOptions): BorderSpec | undefined { | ||
| if (!ooxmlBorder || typeof ooxmlBorder !== 'object' || ooxmlBorder === null) { | ||
| return undefined; | ||
| } | ||
|
|
@@ -101,11 +117,17 @@ export function convertBorderSpec(ooxmlBorder: unknown): BorderSpec | undefined | |
| return { style: 'none' as BorderStyle, width: 0 }; | ||
| } | ||
|
|
||
| const width = borderSizeToPx(sizeNumber); | ||
|
Contributor
Author
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. We were converting it again here when it already was in pixels. Adding other verifications just as a fail safe. |
||
| if (width == null) return undefined; | ||
| if (!isFiniteNumber(sizeNumber)) return undefined; | ||
| const numericSize = sizeNumber as number; | ||
| if (numericSize <= 0) { | ||
| return { style: 'none' as BorderStyle, width: 0 }; | ||
| } | ||
|
|
||
| // Ensure color has # prefix | ||
| const normalizedColor = normalizeColorWithDefault(colorString); | ||
| const unit: BorderConversionUnit = options?.unit ?? 'px'; | ||
| const width = resolveBorderWidth(numericSize, unit); | ||
| if (width == null) return undefined; | ||
|
|
||
| return { | ||
| style: (val as BorderStyle) || 'single', | ||
|
|
@@ -125,14 +147,17 @@ export function convertBorderSpec(ooxmlBorder: unknown): BorderSpec | undefined | |
| * | ||
| * @example | ||
| * ```typescript | ||
| * convertTableBorderValue({ val: 'single', size: 16, color: 'FF0000' }); | ||
| * // { style: 'single', width: 2.67, color: '#FF0000' } | ||
| * convertTableBorderValue({ val: 'single', size: 4, color: 'FF0000' }); | ||
| * // { style: 'single', width: 4, color: '#FF0000' } | ||
| * | ||
| * convertTableBorderValue({ val: 'nil' }); | ||
| * // { none: true } | ||
| * ``` | ||
| */ | ||
| export function convertTableBorderValue(ooxmlBorder: unknown): TableBorderValue | undefined { | ||
| export function convertTableBorderValue( | ||
| ooxmlBorder: unknown, | ||
| options?: BorderConversionOptions, | ||
| ): TableBorderValue | undefined { | ||
| if (!ooxmlBorder || typeof ooxmlBorder !== 'object') return undefined; | ||
|
|
||
| const border = ooxmlBorder as OoxmlBorder; | ||
|
|
@@ -145,10 +170,14 @@ export function convertTableBorderValue(ooxmlBorder: unknown): TableBorderValue | |
| return { none: true }; | ||
| } | ||
|
|
||
| const width = borderSizeToPx(size); | ||
| if (width == null) return undefined; | ||
| if (!isFiniteNumber(size)) return undefined; | ||
| const numericSize = size as number; | ||
| if (numericSize <= 0) return { none: true }; | ||
|
|
||
| const normalizedColor = normalizeColorWithDefault(color); | ||
| const unit: BorderConversionUnit = options?.unit ?? 'px'; | ||
| const width = resolveBorderWidth(numericSize, unit); | ||
| if (width == null) return undefined; | ||
|
|
||
| return { | ||
| style: (val as BorderStyle) || 'single', | ||
|
|
@@ -204,7 +233,10 @@ function isTableBorderValue(value: unknown): value is TableBorderValue { | |
| * @param bordersInput - Record of border definitions for sides (top, left, right, etc.) | ||
| * @returns TableBorders | undefined | ||
| */ | ||
| export function extractTableBorders(bordersInput: Record<string, unknown> | undefined): TableBorders | undefined { | ||
| export function extractTableBorders( | ||
| bordersInput: Record<string, unknown> | undefined, | ||
| options?: BorderConversionOptions, | ||
| ): TableBorders | undefined { | ||
| if (!bordersInput || typeof bordersInput !== 'object') { | ||
| return undefined; | ||
| } | ||
|
|
@@ -221,7 +253,7 @@ export function extractTableBorders(bordersInput: Record<string, unknown> | unde | |
| borders[side] = raw; | ||
| } else { | ||
| // Convert from OOXML | ||
| const converted = convertTableBorderValue(raw); | ||
| const converted = convertTableBorderValue(raw, options); | ||
| if (converted !== undefined) { | ||
| borders[side] = converted; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
three different functions now convert eighth-points to pixels: one here (clamps to [0.5, 100]px), one in normalizeNewTableAttrs.js and one in tables-adapter.ts (neither clamps). borders could render differently depending on which path runs. worth consolidating?