-
Notifications
You must be signed in to change notification settings - Fork 114
fix: Preserve table cell styles on Google Docs paste #2628
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
Merged
caio-pizzol
merged 21 commits into
superdoc-dev:main
from
Branc0:2150-preserve-table-styles
Apr 8, 2026
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3a6b16c
fix(table-cell): add background color parsing from inline styles
Branc0 10b70b8
fix(table-cell): dd cell margin parsing from inline styles
Branc0 65f7fdc
fix(table-row): persists row height on pasted content from html
Branc0 ba53f9f
fix(table-cell): persists cell verticallAlignment on paste from goog…
Branc0 1ba1a41
fix(test): tests for color and margin were failing
Branc0 ab5e67b
feat(table-cell): add border parsing from inline styles and correspon…
Branc0 4861006
fix(table-row): prevent double interior borders
Branc0 5b77732
refactor(table-cell): optimize border style parsing using a dynamic r…
Branc0 27946cb
refactor(table-cell): returns margins with value only
Branc0 199a112
test(table-cell): increase cooverage and refactor tests
Branc0 27ad316
docs(renderTableRow): clarify assumptions about shared interior cell …
Branc0 36fd587
test(renderTableRow): add test for non-final row border behavior in c…
Branc0 661df8c
Merge branch 'main' into 2150-preserve-table-styles
Branc0 82b9242
Merge branch 'main' into 2150-preserve-table-styles
Branc0 f8a6982
test(table-cell): add integration tests for HTML paste handling of ta…
Branc0 4331cd2
Merge branch 'main' into 2150-preserve-table-styles
Branc0 fa48f6b
feat(table): add parsing for table header and cell
Branc0 6271036
feat(table): move cell border rendering logic to shared
Branc0 54af30d
Merge branch 'main' into 2150-preserve-table-styles
Branc0 6b14e6d
fix(table): strip # prefix from pasted background colors
caio-pizzol 62981a5
Merge branch 'main' into 2150-preserve-table-styles
caio-pizzol 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
139 changes: 139 additions & 0 deletions
139
packages/super-editor/src/editors/v1/extensions/shared/parseCellBorders.js
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 |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // @ts-check | ||
caio-pizzol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { parseSizeUnit } from '@core/utilities/parseSizeUnit.js'; | ||
| import { cssColorToHex } from '@core/utilities/cssColorToHex.js'; | ||
| import { halfPointToPixels } from '@core/super-converter/helpers.js'; | ||
|
|
||
| /** | ||
| * Parsed cell border shape used by table cell / header parseDOM. | ||
| * @typedef {Object} ParsedCellBorder | ||
| * @property {'none' | 'single' | 'dashed' | 'dotted'} val | ||
| * @property {number} size | ||
| * @property {string} color | ||
| * @property {string} style | ||
| */ | ||
|
|
||
| /** | ||
| * Parsed borders object for each side. | ||
| * @typedef {Object} ParsedCellBorders | ||
| * @property {ParsedCellBorder} [top] | ||
| * @property {ParsedCellBorder} [right] | ||
| * @property {ParsedCellBorder} [bottom] | ||
| * @property {ParsedCellBorder} [left] | ||
| */ | ||
|
|
||
| const STYLE_TOKEN_SET = new Set([ | ||
| 'none', | ||
| 'hidden', | ||
| 'dotted', | ||
| 'dashed', | ||
| 'solid', | ||
| 'double', | ||
| 'groove', | ||
| 'ridge', | ||
| 'inset', | ||
| 'outset', | ||
Branc0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ]); | ||
|
|
||
| const STYLE_TOKEN_PATTERN = Array.from(STYLE_TOKEN_SET).join('|'); | ||
|
|
||
| /** | ||
| * Parse border width token into pixel number. | ||
| * | ||
| * @param {string} value | ||
| * @returns {number | null} | ||
| */ | ||
| const parseBorderWidth = (value) => { | ||
| const widthMatch = value.match(/(?:^|\s)(-?\d*\.?\d+(?:px|pt))(?=\s|$)/i); | ||
| if (!widthMatch?.[1]) return null; | ||
|
|
||
| const [widthValue, widthUnit] = parseSizeUnit(widthMatch[1]); | ||
|
|
||
| const numericWidth = Number(widthValue); | ||
| const size = widthUnit === 'pt' ? halfPointToPixels(numericWidth) : numericWidth; | ||
| return size; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse border style token. | ||
| * | ||
| * @param {string} value | ||
| * @returns {string | null} | ||
| */ | ||
| const parseBorderStyle = (value) => { | ||
| const styleMatch = value.match(new RegExp(`(?:^|\\s)(${STYLE_TOKEN_PATTERN})(?=\\s|$)`, 'i')); | ||
| return styleMatch?.[1] ? styleMatch[1].toLowerCase() : null; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse border color token. | ||
| * | ||
| * @param {string} value | ||
| * @returns {string | null} | ||
| */ | ||
| const parseBorderColor = (value) => { | ||
| const directColorMatch = value.match(/(rgba?\([^)]+\)|hsla?\([^)]+\)|#[0-9a-fA-F]{3,8}|var\([^)]+\))/i); | ||
| if (directColorMatch?.[1]) return directColorMatch[1]; | ||
|
|
||
| const tokenColorMatch = value | ||
| .split(/\s+/) | ||
| .find((part) => /^[a-z]+$/i.test(part) && !STYLE_TOKEN_SET.has(part.toLowerCase())); | ||
| return tokenColorMatch || null; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse a single CSS border declaration. | ||
| * | ||
| * @param {string | undefined | null} rawValue | ||
| * @returns {ParsedCellBorder | null} | ||
| */ | ||
| const parseBorderValue = (rawValue) => { | ||
| if (!rawValue || typeof rawValue !== 'string') return null; | ||
| const value = rawValue.trim(); | ||
| if (!value) return null; | ||
|
|
||
| if (value === 'none') { | ||
| return { val: 'none', size: 0, color: 'auto', style: 'none' }; | ||
| } | ||
|
|
||
| const size = parseBorderWidth(value); | ||
| const style = parseBorderStyle(value); | ||
| const color = parseBorderColor(value); | ||
|
|
||
| const hexColor = cssColorToHex(color); | ||
| if (style === 'none') { | ||
| return { val: 'none', size: 0, color: 'auto', style: 'none' }; | ||
| } | ||
|
|
||
| if (size == null && !hexColor && !style) return null; | ||
|
|
||
| return { | ||
| val: style === 'dashed' || style === 'dotted' ? style : 'single', | ||
| size: size ?? 1, | ||
| color: hexColor || 'auto', | ||
| style: style || 'solid', | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse cell borders from inline TD/TH styles. | ||
| * | ||
| * @param {HTMLElement} element | ||
| * @returns {ParsedCellBorders | null} | ||
| */ | ||
| export const parseCellBorders = (element) => { | ||
| const { style } = element; | ||
|
|
||
| const top = parseBorderValue(style?.borderTop || style?.border); | ||
| const right = parseBorderValue(style?.borderRight || style?.border); | ||
| const bottom = parseBorderValue(style?.borderBottom || style?.border); | ||
| const left = parseBorderValue(style?.borderLeft || style?.border); | ||
|
|
||
| if (!top && !right && !bottom && !left) return null; | ||
|
|
||
| return { | ||
| ...(top ? { top } : {}), | ||
| ...(right ? { right } : {}), | ||
| ...(bottom ? { bottom } : {}), | ||
| ...(left ? { left } : {}), | ||
| }; | ||
| }; | ||
52 changes: 52 additions & 0 deletions
52
packages/super-editor/src/editors/v1/extensions/shared/parseCellMargins.js
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // @ts-check | ||
caio-pizzol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { parseSizeUnit } from '@core/utilities/parseSizeUnit.js'; | ||
| import { halfPointToPixels } from '@core/super-converter/helpers.js'; | ||
|
|
||
| /** | ||
| * Cell margins configuration in pixels. | ||
| * @typedef {Object} CellMargins | ||
| * @property {number} [top] - Top margin in pixels | ||
| * @property {number} [right] - Right margin in pixels | ||
| * @property {number} [bottom] - Bottom margin in pixels | ||
| * @property {number} [left] - Left margin in pixels | ||
| */ | ||
|
|
||
| /** | ||
| * Parse one CSS padding side into pixels. | ||
| * | ||
| * @param {string} sideValue | ||
| * @returns {number | undefined} | ||
| */ | ||
| const parseSide = (sideValue) => { | ||
| if (!sideValue) return undefined; | ||
| const [rawValue, unit] = parseSizeUnit(sideValue); | ||
| const numericValue = Number(rawValue); | ||
| const calculatedValue = unit === 'pt' ? halfPointToPixels(numericValue) : numericValue; | ||
| return calculatedValue; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse cell margins from inline TD/TH padding styles. | ||
| * | ||
| * @param {HTMLElement} element | ||
| * @returns {CellMargins | null} | ||
| */ | ||
| export const parseCellMargins = (element) => { | ||
| const { style } = element; | ||
|
|
||
| const top = parseSide(style?.paddingTop); | ||
| const right = parseSide(style?.paddingRight); | ||
| const bottom = parseSide(style?.paddingBottom); | ||
| const left = parseSide(style?.paddingLeft); | ||
|
|
||
| if (top == null && right == null && bottom == null && left == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| ...(top != null ? { top } : {}), | ||
| ...(right != null ? { right } : {}), | ||
| ...(bottom != null ? { bottom } : {}), | ||
| ...(left != null ? { left } : {}), | ||
| }; | ||
| }; | ||
16 changes: 16 additions & 0 deletions
16
packages/super-editor/src/editors/v1/extensions/shared/parseCellVerticalAlign.js
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // @ts-check | ||
|
|
||
| /** | ||
| * Parse `vertical-align` from inline styles on table cells (TD/TH), e.g. pasted HTML. | ||
| * Maps CSS `middle` to the schema value `center`. | ||
| * | ||
| * @param {HTMLElement} element | ||
| * @returns {string | null} | ||
| */ | ||
| export function parseCellVerticalAlignFromStyle(element) { | ||
| const value = element.style?.verticalAlign; | ||
| if (!value || typeof value !== 'string') return null; | ||
| const normalized = value.trim().toLowerCase(); | ||
| if (normalized === 'middle') return 'center'; | ||
| return normalized; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.