A platform for community discussion. Free, open, simple.
FIX: Keep rich editor tables that hold only images or emoji (#44684)
Renato Atilio99ed0e335644
diff --git a/frontend/discourse/app/static/prosemirror/extensions/table.js b/frontend/discourse/app/static/prosemirror/extensions/table.jsindex add1d9867ff..7baf4896fb9 100644--- a/frontend/discourse/app/static/prosemirror/extensions/table.js+++ b/frontend/discourse/app/static/prosemirror/extensions/table.js@@ -132,15 +132,16 @@ const extension = {},serializeNode: {table(state, node) {+ // Checked before flushing, so the surrounding blocks stay separated.+ if (!hasContent(node)) {+ return;+ }+state.flushClose(1);// Normalize table structurenode = normalizeTable(node);- if (!node.textContent.trim()) {- return;- }-const prevInTable = state.inTable;state.inTable = true;@@ -296,6 +297,25 @@ function createBodyRows(tbody, startIndex, maxColumns, schema) {return bodyRows;}+/**+ * Pasted and quoted HTML often carries empty tables. Whitespace and line+ * breaks don't count as content.+ */+function hasContent(table) {+ let found = false;+ table.descendants((node) => {+ if (found) {+ return false;+ }+ if (node.isText) {+ found = node.text.trim() !== "";+ } else if (node.isInline && node.isLeaf) {+ found = node.type.name !== "hard_break";+ }+ });+ return found;+}+function normalizeTable(tableNode) {const schema = tableNode.type.schema;let tbody, thead;diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.jsindex ce19eb11277..b5cee10f89d 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js@@ -245,6 +245,32 @@ module(});});+ test("tables without text keep their emoji and images", async function (assert) {+ const [editor] = await setupRichEditor(+ assert,+ `before\n\n| :smile: | |\n| --- | --- |\n| |  |\n\nafter`+ );++ assert.strictEqual(+ editor.value,+ `before\n\n| :smile: | |\n|----|----|\n| |  |\n\nafter`,+ "the table is serialized"+ );+ });++ test("tables with no content are dropped", async function (assert) {+ const [editor] = await setupRichEditor(+ assert,+ `before\n\n| | |\n| --- | --- |\n| <br> | |\n\nafter`+ );++ assert.strictEqual(+ editor.value,+ `before\n\nafter`,+ "a table holding only blank cells and line breaks is omitted"+ );+ });+test("table within quotes", async function (assert) {await testMarkdown(assert,diff --git a/frontend/discourse/tests/unit/lib/to-markdown-test.js b/frontend/discourse/tests/unit/lib/to-markdown-test.jsindex 6cdb93d6fce..2b90a7cb9ee 100644--- a/frontend/discourse/tests/unit/lib/to-markdown-test.js+++ b/frontend/discourse/tests/unit/lib/to-markdown-test.js@@ -238,6 +238,19 @@ module("Unit | Utility | to-markdown", function (hooks) {assert.strictEqual(await toMarkdown(html), markdown);});+ test("keeps tables whose cells hold only images", async function (assert) {+ const html = `<table><tr><th><img src="http://example.com/a.png" alt="a"></th><th></th></tr><tr><td></td><td><img src="http://example.com/b.png" alt="b"></td></tr></table>`;+ assert.strictEqual(+ await toMarkdown(html),+ `|  | |\n|----|----|\n| |  |`+ );+ });++ test("skips tables without content", async function (assert) {+ const html = `<p>before</p><table><tr><td><br></td><td> </td></tr></table><p>after</p>`;+ assert.strictEqual(await toMarkdown(html), `before\n\nafter`);+ });+test("converts single-column tables", async function (assert) {let html = `<table><thead> <tr><th>Heading 1</th></tr> </thead>