Skip to content

A platform for community discussion. Free, open, simple.

FIX: handle incomplete tables pasted on rich editor (#36917)

Renato Atilioc35e7366312d
diff --git a/frontend/discourse/app/static/prosemirror/extensions/table.js b/frontend/discourse/app/static/prosemirror/extensions/table.js
index ba0fad43fee..ec574b25bb4 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/table.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/table.js
@@ -178,52 +178,104 @@ const extension = {
table_header_cell() {},
table_cell() {},
},
- plugins({ pmState: { Plugin }, pmModel: { Slice } }) {
+ plugins({ pmState: { Plugin }, pmModel: { Slice, Fragment } }) {
+ function findMaxColumns(tbody) {
+ let maxColumns = 0;
+ tbody.forEach((row) => {
+ maxColumns = Math.max(maxColumns, row.childCount);
+ });
+ return maxColumns;
+ }
+
+ function createHeaderRow(firstRow, maxColumns, schema) {
+ const headerCells = [];
+ for (let i = 0; i < maxColumns; i++) {
+ if (i < firstRow.childCount) {
+ const cell = firstRow.child(i);
+ headerCells.push(
+ schema.nodes.table_header_cell.create(cell.attrs, cell.content)
+ );
+ } else {
+ headerCells.push(schema.nodes.table_header_cell.create());
+ }
+ }
+ return schema.nodes.table_row.create({}, headerCells);
+ }
+
+ function createBodyRows(tbody, maxColumns, schema) {
+ const bodyRows = [];
+ tbody.content.content.slice(1).forEach((row) => {
+ const cells = [];
+ for (let i = 0; i < maxColumns; i++) {
+ if (i < row.childCount) {
+ cells.push(row.child(i));
+ } else {
+ cells.push(schema.nodes.table_cell.create());
+ }
+ }
+ bodyRows.push(schema.nodes.table_row.create({}, cells));
+ });
+ return bodyRows;
+ }
+
+ function normalizeTable(tableNode, schema) {
+ let tbody, thead;
+ tableNode.descendants((node) => {
+ if (node.type.name === "table_body") {
+ tbody = node;
+ return false;
+ }
+ if (node.type.name === "table_head") {
+ thead = node;
+ return false;
+ }
+ });
+
+ if (thead || !tbody) {
+ return tableNode;
+ }
+
+ const maxColumns = findMaxColumns(tbody);
+ const firstRow = tbody.firstChild;
+
+ const header = schema.nodes.table_head.create(
+ {},
+ createHeaderRow(firstRow, maxColumns, schema)
+ );
+
+ const body = schema.nodes.table_body.create(
+ {},
+ createBodyRows(tbody, maxColumns, schema)
+ );
+
+ return schema.nodes.table.create({}, [header, body]);
+ }
+
return new Plugin({
props: {
transformPasted(paste, view) {
- let updatedPaste = paste;
- paste.content.descendants((child, tableIndex) => {
- if (child.type.name === "table") {
- let tbody, thead;
- child.descendants((node) => {
- if (node.type.name === "table_body") {
- tbody = node;
- return false;
- }
-
- if (node.type.name === "table_head") {
- thead = node;
- return false;
- }
- });
-
- if (!thead && tbody) {
- thead = view.state.schema.nodes.table_head.create(
- {},
- tbody.firstChild
- );
-
- tbody = view.state.schema.nodes.table_body.create(
- {},
- tbody.content.content.slice(1)
- );
-
- const newTableContent = view.state.schema.nodes.table.create(
- {},
- [thead, tbody]
- );
-
- updatedPaste = new Slice(
- paste.content.replaceChild(tableIndex, newTableContent),
- paste.openStart,
- paste.openEnd
- );
- }
+ const schema = view.state.schema;
+
+ function transformNode(node) {
+ if (node.type.name === "table") {
+ return normalizeTable(node, schema);
}
- });
- return updatedPaste;
+ if (node.content?.size > 0) {
+ const newChildren = node.content.content.map(transformNode);
+ return node.type.create(node.attrs, newChildren, node.marks);
+ }
+
+ return node;
+ }
+
+ const transformedContent = paste.content.content.map(transformNode);
+
+ return new Slice(
+ Fragment.from(transformedContent),
+ paste.openStart,
+ paste.openEnd
+ );
},
},
});
diff --git a/spec/system/composer/prosemirror_pasting_spec.rb b/spec/system/composer/prosemirror_pasting_spec.rb
index 4fcaf271397..91a96d22a6f 100644
--- a/spec/system/composer/prosemirror_pasting_spec.rb
+++ b/spec/system/composer/prosemirror_pasting_spec.rb
@@ -205,4 +205,120 @@ describe "Composer - ProseMirror - Pasting content", type: :system do
composer.toggle_rich_editor
expect(composer).to have_value("line1\nline2\nline3")
end
+
+ context "when pasting tables" do
+ it "fills incomplete rows" do
+ cdp.allow_clipboard
+ open_composer
+
+ html = <<~HTML
+ <table>
+ <tbody>
+ <tr><td>Title</td></tr>
+ <tr><th>Column A</th><td>Column B</td></tr>
+ <tr><th>Value 1</th><td>Value 2</td></tr>
+ <tr><td colspan="2"></td></tr>
+ <tr><th>Value 3</th><td>Value 4</td></tr>
+ </tbody>
+ </table>
+ HTML
+
+ cdp.copy_paste(html, html: true)
+
+ composer.toggle_rich_editor
+
+ markdown = <<~MARKDOWN
+ | Title | |
+ |----|----|
+ | Column A | Column B |
+ | Value 1 | Value 2 |
+ | | |
+ | Value 3 | Value 4 |
+
+ MARKDOWN
+
+ expect(composer).to have_value(markdown)
+ end
+
+ it "normalizes column counts when header has fewer columns than body rows" do
+ cdp.allow_clipboard
+ open_composer
+
+ html = <<~HTML
+ <table>
+ <tbody>
+ <tr><th>Header1</th><th>Header2</th></tr>
+ <tr><td>Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td></tr>
+ <tr><td>Data1</td><td>Data2</td><td>Data3</td><td>Data4</td></tr>
+ </tbody>
+ </table>
+ HTML
+
+ cdp.copy_paste(html, html: true)
+
+ composer.toggle_rich_editor
+
+ markdown = <<~MARKDOWN
+ | Header1 | Header2 | | |
+ |----|----|----|----|
+ | Cell1 | Cell2 | Cell3 | Cell4 |
+ | Data1 | Data2 | Data3 | Data4 |
+
+ MARKDOWN
+
+ expect(composer).to have_value(markdown)
+ end
+
+ it "normalizes nested table column counts" do
+ cdp.allow_clipboard
+ open_composer
+
+ html = <<~HTML
+ <table>
+ <tbody>
+ <tr>
+ <td>
+ <table>
+ <tbody>
+ <tr><td>CLOSED DOWNSTREAM</td></tr>
+ <tr><td colspan="2"></td></tr>
+ <tr><th>Alias:</th><td>None</td></tr>
+ <tr><th>Product:</th><td>name</td></tr>
+ <tr><th>Component:</th><td>general</td></tr>
+ </tbody>
+ </table>
+ </td>
+ <td></td>
+ <td></td>
+ </tr>
+ </tbody>
+ </table>
+ HTML
+
+ cdp.copy_paste(html, html: true)
+
+ composer.toggle_rich_editor
+
+ # The inner table header should be properly normalized
+ markdown = <<~MARKDOWN
+ | |
+ |----|
+
+
+ | CLOSED DOWNSTREAM | |
+ |----|----|
+ | | |
+ | Alias: | None |
+ | Product: | name |
+ | Component: | general |
+
+
+ | | |
+ |----|----|
+
+ MARKDOWN
+
+ expect(composer).to have_value(markdown)
+ end
+ end
end