diff --git a/packages/pluggableWidgets/rich-text-web/CHANGELOG.md b/packages/pluggableWidgets/rich-text-web/CHANGELOG.md index 9e054338ee..e4b05a8799 100644 --- a/packages/pluggableWidgets/rich-text-web/CHANGELOG.md +++ b/packages/pluggableWidgets/rich-text-web/CHANGELOG.md @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - We fixed an issue where, with multiple Rich Text widgets on a page, toggling fullscreen or pressing Escape in one editor changed the fullscreen mode of a different editor, and the fullscreen button showed the wrong state. +- We fixed an issue where table and cell border settings had no effect when the style data format was set to "Class". + +- We fixed an issue where table, column and cell sizes were saved as inline styles when the style data format was set to "Class". + ## [5.1.0] - 2026-09-29 ### Added diff --git a/packages/pluggableWidgets/rich-text-web/e2e/RichText.spec.js-snapshots/classModeViewCodeDialog-chromium-linux.png b/packages/pluggableWidgets/rich-text-web/e2e/RichText.spec.js-snapshots/classModeViewCodeDialog-chromium-linux.png index d0975a3931..cd006ed4de 100644 Binary files a/packages/pluggableWidgets/rich-text-web/e2e/RichText.spec.js-snapshots/classModeViewCodeDialog-chromium-linux.png and b/packages/pluggableWidgets/rich-text-web/e2e/RichText.spec.js-snapshots/classModeViewCodeDialog-chromium-linux.png differ diff --git a/packages/pluggableWidgets/rich-text-web/src/__tests__/RichTextFormatStyle.spec.ts b/packages/pluggableWidgets/rich-text-web/src/__tests__/RichTextFormatStyle.spec.ts new file mode 100644 index 0000000000..0e27bb5224 --- /dev/null +++ b/packages/pluggableWidgets/rich-text-web/src/__tests__/RichTextFormatStyle.spec.ts @@ -0,0 +1,55 @@ +/** + * @jest-environment node + */ +import { join } from "path"; +import { compile } from "sass"; + +// Class style mode renders table/cell borders purely from CSS, so guard the compiled output: +// Sass treats `or` as a boolean operator and silently drops attr() fallbacks. +describe("RichTextFormatStyle.scss table borders", () => { + const css = compile(join(__dirname, "../ui/RichTextFormatStyle.scss")).css; + + it("scopes the border rule to the table, cell and generic border classes", () => { + expect(css).toContain( + ".widget-rich-text .has-table-border,\n.widget-rich-text .has-cell-border,\n.widget-rich-text .has-border {" + ); + }); + + it("reads each border property with a typed attr() and its own comma fallback", () => { + expect(css).toContain("border-width: attr(data-border-width type(), 1px);"); + expect(css).toContain("border-style: attr(data-border-style type(), solid);"); + expect(css).toContain( + "border-color: attr(data-border-color type(), var(--color-neutral-300, #d9d9d9));" + ); + }); + + it("does not emit the broken unit-typed or fallback-less border declarations", () => { + expect(css).not.toContain("attr(data-border-width px)"); + expect(css).not.toContain("border-style: attr(data-border-style type(*));"); + expect(css).not.toContain("border-color: attr(data-border-color type());"); + expect(css).not.toMatch(/border-(width|style|color): [^;]*\bor\b/); + }); +}); + +// Class style mode saves table, column and cell sizes as data attributes only (no `style` +// attribute), so the sizes must come from these rules. The attribute selectors keep them +// off inline-mode tables. +describe("RichTextFormatStyle.scss table sizes", () => { + const css = compile(join(__dirname, "../ui/RichTextFormatStyle.scss")).css; + + it.each([ + [".widget-rich-text table[data-width] {", "width: attr(data-width type());"], + [".widget-rich-text table[data-min-height] {", "min-height: attr(data-min-height type());"], + [".widget-rich-text col[data-col-width] {", "width: attr(data-col-width type());"], + [ + ".widget-rich-text td[data-cell-width],\n.widget-rich-text th[data-cell-width] {", + "width: attr(data-cell-width type());" + ], + [ + ".widget-rich-text td[data-cell-height],\n.widget-rich-text th[data-cell-height] {", + "height: attr(data-cell-height type());" + ] + ])("renders %s from a typed attr()", (selector, declaration) => { + expect(css).toContain(`${selector}\n ${declaration}\n}`); + }); +}); diff --git a/packages/pluggableWidgets/rich-text-web/src/extensions/TableBackgroundColor.ts b/packages/pluggableWidgets/rich-text-web/src/extensions/TableBackgroundColor.ts index 9576037dec..92fc0a62f8 100644 --- a/packages/pluggableWidgets/rich-text-web/src/extensions/TableBackgroundColor.ts +++ b/packages/pluggableWidgets/rich-text-web/src/extensions/TableBackgroundColor.ts @@ -17,10 +17,13 @@ declare module "@tiptap/core" { } } -// Helper to create colgroup (copied from TipTap source) +// Helper to create colgroup (copied from TipTap source). +// Inline format puts each column width in the `style`; class format stores it in +// `data-col-width` instead and RichTextFormatStyle.scss renders it. function createColGroup( node: any, - cellMinWidth: number + cellMinWidth: number, + styleDataFormat: "inline" | "class" = "inline" ): { colgroup: any[]; tableWidth: string; tableMinWidth: string } { let totalWidth = 0; let fixedWidth = true; @@ -50,7 +53,11 @@ function createColGroup( } const safeCssWidth = safeSize(cssWidth); - cols.push(["col", safeCssWidth ? { style: `width: ${safeCssWidth}` } : {}]); + if (styleDataFormat === "class") { + cols.push(["col", safeCssWidth ? { "data-col-width": safeCssWidth } : {}]); + } else { + cols.push(["col", safeCssWidth ? { style: `width: ${safeCssWidth}` } : {}]); + } } } @@ -241,7 +248,11 @@ export const TableBackgroundColor = Table.extend({ }, renderHTML({ node, HTMLAttributes }): DOMOutputSpec { - const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth || 25); + const { colgroup, tableWidth, tableMinWidth } = createColGroup( + node, + this.options.cellMinWidth || 25, + this.options.styleDataFormat + ); // Get style attributes from node (all validated before entering the style string) const explicitWidth = safeSize(node.attrs.width); @@ -251,22 +262,22 @@ export const TableBackgroundColor = Table.extend({ // Build the style string by merging table width, background, and border properties const segments: string[] = []; - // Explicit table width wins as the footprint; otherwise fall back to the - // colwidth-derived width/min-width. Per-column min-width still comes from colgroup. - if (explicitWidth) { - segments.push(`width: ${explicitWidth}`); - } else if (tableWidth) { - segments.push(`width: ${tableWidth}`); - } else if (tableMinWidth) { - segments.push(`min-width: ${tableMinWidth}`); - } + if (this.options.styleDataFormat === "inline") { + // Explicit table width wins as the footprint; otherwise fall back to the + // colwidth-derived width/min-width. Per-column min-width still comes from colgroup. + if (explicitWidth) { + segments.push(`width: ${explicitWidth}`); + } else if (tableWidth) { + segments.push(`width: ${tableWidth}`); + } else if (tableMinWidth) { + segments.push(`min-width: ${tableMinWidth}`); + } - // Explicit table minimum height (rows still grow with content) - if (minHeight) { - segments.push(`min-height: ${minHeight}`); - } + // Explicit table minimum height (rows still grow with content) + if (minHeight) { + segments.push(`min-height: ${minHeight}`); + } - if (this.options.styleDataFormat === "inline") { // Add background color if present if (backgroundColor) { segments.push(`background-color: ${backgroundColor}`); @@ -279,7 +290,9 @@ export const TableBackgroundColor = Table.extend({ const styleString = segments.join("; "); - // Build class-based attributes for border properties + // Build class-based attributes. Class mode never emits a `style` attribute: explicit + // sizes are stored as data attributes (rendered by RichTextFormatStyle.scss) and the + // colwidth-derived width/min-width is not stored because it is recomputed. const classAttrs: Record = {}; if (this.options.styleDataFormat === "class") { const borderColor = safeColor(node.attrs.borderColor); @@ -580,7 +593,7 @@ class TableBackgroundColorNodeView implements NodeView { } // Create new colgroup - const { colgroup } = createColGroup(this.node, this.cellMinWidth); + const { colgroup } = createColGroup(this.node, this.cellMinWidth, this.styleDataFormat); const colgroupElement = this.createElementFromSpec(colgroup as unknown as DOMOutputSpec); this.table.insertBefore(colgroupElement, this.contentDOM); } @@ -653,18 +666,21 @@ class TableBackgroundColorNodeView implements NodeView { this.table.classList.remove("has-background-color"); } - // Border properties - if (borderColor || borderStyle || borderWidth) { - if (borderColor) this.table.setAttribute("data-border-color", borderColor); - if (borderStyle) this.table.setAttribute("data-border-style", borderStyle); - if (borderWidth) this.table.setAttribute("data-border-width", borderWidth); - this.table.classList.add("has-table-border"); - } else { - this.table.removeAttribute("data-border-color"); - this.table.removeAttribute("data-border-style"); - this.table.removeAttribute("data-border-width"); - this.table.classList.remove("has-table-border"); + // Border properties. Sync each attribute on its own so clearing one property + // (e.g. the color) does not leave a stale value behind for the CSS to pick up. + const borderAttrs: Array<[string, string | null]> = [ + ["data-border-color", borderColor], + ["data-border-style", borderStyle], + ["data-border-width", borderWidth] + ]; + for (const [name, value] of borderAttrs) { + if (value) { + this.table.setAttribute(name, value); + } else { + this.table.removeAttribute(name); + } } + this.table.classList.toggle("has-table-border", Boolean(borderColor || borderStyle || borderWidth)); } } diff --git a/packages/pluggableWidgets/rich-text-web/src/extensions/__tests__/TableClassFormat.spec.ts b/packages/pluggableWidgets/rich-text-web/src/extensions/__tests__/TableClassFormat.spec.ts new file mode 100644 index 0000000000..b46d41c816 --- /dev/null +++ b/packages/pluggableWidgets/rich-text-web/src/extensions/__tests__/TableClassFormat.spec.ts @@ -0,0 +1,228 @@ +import { Editor } from "@tiptap/core"; +import { TableRow } from "@tiptap/extension-table-row"; +import { StarterKit } from "@tiptap/starter-kit"; +import { TableBackgroundColor } from "../TableBackgroundColor"; +import { TableCellBackgroundColor } from "../TableCellBackgroundColor"; +import { TableHeaderBackgroundColor } from "../TableHeaderBackgroundColor"; + +type StyleFormat = "inline" | "class"; + +function makeEditor(styleDataFormat: StyleFormat): Editor { + const element = document.createElement("div"); + document.body.appendChild(element); + return new Editor({ + element, + extensions: [ + StarterKit, + TableBackgroundColor.configure({ resizable: true, styleDataFormat }), + TableRow, + TableHeaderBackgroundColor.configure({ styleDataFormat }), + TableCellBackgroundColor.configure({ styleDataFormat }) + ] + }); +} + +/** Places the cursor inside the paragraph of the first data cell. */ +function placeCursorInFirstCell(editor: Editor): void { + let pos = 1; + editor.state.doc.descendants((node, nodePos) => { + if (pos === 1 && node.type.name === "tableCell") { + pos = nodePos + 2; + } + return pos === 1; + }); + editor.commands.setTextSelection(pos); +} + +/** Merges attrs into the table that contains the cursor. */ +function setTableAttrs(editor: Editor, attrs: Record): void { + const { $from } = editor.state.selection; + for (let depth = $from.depth; depth > 0; depth--) { + const node = $from.node(depth); + if (node.type.name === "table") { + editor.view.dispatch( + editor.state.tr.setNodeMarkup($from.before(depth), undefined, { ...node.attrs, ...attrs }) + ); + return; + } + } +} + +const TABLE_HTML = "
AB
CD
"; + +describe("TableBackgroundColor NodeView — class format borders", () => { + let editor: Editor; + afterEach(() => editor?.destroy()); + + it("syncs each border data attribute independently without inline border styles", () => { + editor = makeEditor("class"); + editor.commands.setContent(TABLE_HTML); + placeCursorInFirstCell(editor); + + setTableAttrs(editor, { borderColor: "#d33115", borderStyle: "dashed", borderWidth: "3px" }); + const table = editor.view.dom.querySelector("table") as HTMLTableElement; + expect(table.getAttribute("data-border-color")).toBe("#d33115"); + expect(table.getAttribute("data-border-style")).toBe("dashed"); + expect(table.getAttribute("data-border-width")).toBe("3px"); + expect(table.classList.contains("has-table-border")).toBe(true); + expect(table.style.borderColor).toBe(""); + expect(table.style.borderStyle).toBe(""); + expect(table.style.borderWidth).toBe(""); + + // Clearing only the color must not leave a stale data-border-color behind. + setTableAttrs(editor, { borderColor: null }); + expect(table.hasAttribute("data-border-color")).toBe(false); + expect(table.getAttribute("data-border-style")).toBe("dashed"); + expect(table.getAttribute("data-border-width")).toBe("3px"); + expect(table.classList.contains("has-table-border")).toBe(true); + + // Clearing only the width keeps the remaining style. + setTableAttrs(editor, { borderWidth: null }); + expect(table.hasAttribute("data-border-width")).toBe(false); + expect(table.getAttribute("data-border-style")).toBe("dashed"); + expect(table.classList.contains("has-table-border")).toBe(true); + + setTableAttrs(editor, { borderStyle: null }); + expect(table.hasAttribute("data-border-style")).toBe(false); + expect(table.classList.contains("has-table-border")).toBe(false); + }); +}); + +const SIZED_CLASS_HTML = + '' + + '' + + '' + + "
HH2
AB
"; + +/** Opening tags of every table, col, td and th element in the serialized HTML. */ +function tableTags(html: string): string[] { + return html.match(/<(table|col|td|th)\b[^>]*>/g) ?? []; +} + +describe("Table sizes — class format", () => { + let editor: Editor; + afterEach(() => editor?.destroy()); + + it("saves table, column and cell sizes as data attributes without any style attribute", () => { + editor = makeEditor("class"); + editor.commands.setContent(SIZED_CLASS_HTML); + const html = editor.getHTML(); + + expect(html).toContain(''); + expect(html).toContain(''); + expect(html).toContain(''); + expect(html).toContain('data-cell-width="80px" data-cell-height="30px"'); + expect(html).not.toContain("style="); + }); + + it("renders and updates sizes without ever setting a style attribute", () => { + const setAttribute = jest.spyOn(Element.prototype, "setAttribute"); + try { + editor = makeEditor("class"); + editor.commands.setContent(SIZED_CLASS_HTML); + placeCursorInFirstCell(editor); + editor.commands.setTableWidth("500px"); + editor.commands.setTableMinHeight("200px"); + editor.commands.setCellAttribute("cellWidth", "220px"); + editor.commands.setCellAttribute("cellHeight", "60px"); + const html = editor.getHTML(); + + expect(html).toContain('
'); + expect(html).toContain(''); + const tags = tableTags(html); + expect(tags.length).toBe(7); + for (const tag of tags) { + expect(tag).not.toContain("style="); + } + }); + + it("does not persist the column-derived table width", () => { + editor = makeEditor("class"); + editor.commands.setContent( + '
AB
' + ); + expect(tableTags(editor.getHTML())[0]).toBe(""); + }); + + it("round-trips sizes through getHTML/setContent", () => { + editor = makeEditor("class"); + editor.commands.setContent(SIZED_CLASS_HTML); + const html = editor.getHTML(); + + editor.commands.setContent(html); + expect(editor.getHTML()).toBe(html); + const table = editor.state.doc.firstChild!; + expect(table.attrs.width).toBe("400px"); + expect(table.attrs.minHeight).toBe("120px"); + expect(table.firstChild!.firstChild!.attrs.cellWidth).toBe("150px"); + expect(table.firstChild!.firstChild!.attrs.cellHeight).toBe("40px"); + }); + + it("still loads sizes from older class-format content that has inline style widths", () => { + editor = makeEditor("class"); + editor.commands.setContent( + '
' + + '' + + '' + + '' + + "
AB
" + ); + const table = editor.state.doc.firstChild!; + const row = table.firstChild!; + expect(table.attrs.width).toBe("400px"); + expect(table.attrs.minHeight).toBe("120px"); + expect(row.child(0).attrs.cellWidth).toBe("150px"); + expect(row.child(0).attrs.cellHeight).toBe("40px"); + // Cells without data attributes fall back to their inline style + expect(row.child(1).attrs.cellWidth).toBe("80px"); + expect(row.child(1).attrs.cellHeight).toBe("30px"); + + // Saving again moves everything to data attributes + const html = editor.getHTML(); + expect(html).toContain('
'); + expect(html).toContain('data-cell-width="220px" data-cell-height="60px"'); + expect(setAttribute.mock.calls.length).toBeGreaterThan(0); + expect(setAttribute.mock.calls.filter(([name]) => name.toLowerCase() === "style")).toEqual([]); + } finally { + setAttribute.mockRestore(); + } + }); +}); + +describe("Table sizes — inline format", () => { + let editor: Editor; + afterEach(() => editor?.destroy()); + + // Expected strings are the inline renderer's output before the class-format sizing change. + it("keeps the explicit-size output unchanged", () => { + editor = makeEditor("inline"); + editor.commands.setContent( + '
' + + '' + + '' + + "
HH2
AB
" + ); + expect(editor.getHTML()).toBe( + '' + + '' + + '' + + "

H

H2

A

B

" + ); + }); + + it("keeps the column-derived width, colors and borders unchanged", () => { + editor = makeEditor("inline"); + editor.commands.setContent( + "" + + '' + + '' + + "
AB
" + ); + expect(editor.getHTML()).toBe( + '' + + '' + + '' + + "

A

B

" + ); + }); +}); diff --git a/packages/pluggableWidgets/rich-text-web/src/extensions/tableCellStyling.ts b/packages/pluggableWidgets/rich-text-web/src/extensions/tableCellStyling.ts index a28e3551b2..a26843276a 100644 --- a/packages/pluggableWidgets/rich-text-web/src/extensions/tableCellStyling.ts +++ b/packages/pluggableWidgets/rich-text-web/src/extensions/tableCellStyling.ts @@ -143,18 +143,18 @@ export function renderCellHTML( const classAttrs: Record = {}; const segments: string[] = []; - // Column width applies in both formats (always an inline width on the cell) - if (cellWidth) { - segments.push(`width: ${cellWidth}`); - } - - // Cell height (minimum) applies in both formats - if (cellHeight) { - segments.push(`height: ${cellHeight}`); - } - // Inline mode: build style string with border defaults if (styleDataFormat === "inline") { + // Column width + if (cellWidth) { + segments.push(`width: ${cellWidth}`); + } + + // Cell height (minimum) + if (cellHeight) { + segments.push(`height: ${cellHeight}`); + } + // Background color if (backgroundColor) { segments.push(`background-color: ${backgroundColor}`); @@ -168,7 +168,8 @@ export function renderCellHTML( const styleString = segments.join("; "); - // Class mode: use data attributes + // Class mode: use data attributes only, never a `style` attribute (sizes are + // rendered by RichTextFormatStyle.scss from data-cell-width/data-cell-height) if (styleDataFormat === "class") { if (cellWidth) { classAttrs["data-cell-width"] = cellWidth; diff --git a/packages/pluggableWidgets/rich-text-web/src/ui/RichTextFormatStyle.scss b/packages/pluggableWidgets/rich-text-web/src/ui/RichTextFormatStyle.scss index ed3788ff1a..0adedb4129 100644 --- a/packages/pluggableWidgets/rich-text-web/src/ui/RichTextFormatStyle.scss +++ b/packages/pluggableWidgets/rich-text-web/src/ui/RichTextFormatStyle.scss @@ -104,11 +104,41 @@ text-align: justify; } + // Table and cell borders, class mode. Each declaration carries its own default through + // the attr() comma fallback, so a border shows even when only one property is set + // (matching the solid/1px defaults of inline mode). Keep the comma syntax: `or` is a + // Sass boolean operator and silently drops the fallback. border-style is stored from + // a keyword allowlist (solid, dashed, none, ...), all valid s. .has-table-border, .has-cell-border, .has-border { - border-width: (attr(data-border-width px) or 1px); - border-style: attr(data-border-style type(*)) or solid; - border-color: (attr(data-border-color type()) or var(--color-neutral-300, #d9d9d9)); + border-width: attr(data-border-width type(), 1px); + border-style: attr(data-border-style type(), solid); + border-color: attr(data-border-color type(), var(--color-neutral-300, #d9d9d9)); + } + + // Table, column and cell sizes, class mode. Class mode saves sizes as data attributes + // (never a `style` attribute); the attribute selectors keep these rules off inline-mode + // tables, which carry their sizes in `style`. + table[data-width] { + width: attr(data-width type()); + } + + table[data-min-height] { + min-height: attr(data-min-height type()); + } + + col[data-col-width] { + width: attr(data-col-width type()); + } + + td[data-cell-width], + th[data-cell-width] { + width: attr(data-cell-width type()); + } + + td[data-cell-height], + th[data-cell-height] { + height: attr(data-cell-height type()); } }