Skip to content

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

FIX: rich editor inline HTML and table line breaks (#37656)

Sam71eb65a15ee8
diff --git a/frontend/discourse/app/static/prosemirror/core/serializer.js b/frontend/discourse/app/static/prosemirror/core/serializer.js
index 2e604572a9c..8d747e3a433 100644
--- a/frontend/discourse/app/static/prosemirror/core/serializer.js
+++ b/frontend/discourse/app/static/prosemirror/core/serializer.js
@@ -8,7 +8,8 @@ export default class Serializer {
constructor(extensions, pluginParams, includeDefault = true) {
this.nodes = includeDefault ? { ...defaultMarkdownSerializer.nodes } : {};
- this.nodes.hard_break = (state) => state.write("\n");
+ this.nodes.hard_break = (state) =>
+ state.write(state.inTable ? "<br>" : "\n");
this.marks = includeDefault ? { ...defaultMarkdownSerializer.marks } : {};
diff --git a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
index b123cb16dbb..07fe83b1476 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
@@ -17,8 +17,49 @@ const ALLOWED_INLINE = [
"del",
"ins",
"mark",
+ "ruby",
+ "rb",
+ "rt",
+ "rp",
+ "span",
];
+const ALLOWED_TAG_ATTRS = {
+ span: ["lang"],
+ ruby: ["lang"],
+ rb: ["lang"],
+ rt: ["lang"],
+};
+
+function extractHtmlAttrs(element, tagName) {
+ const allowed = ALLOWED_TAG_ATTRS[tagName];
+ if (!allowed) {
+ return null;
+ }
+ const attrs = {};
+ let hasAny = false;
+ for (const attr of allowed) {
+ const value = element.getAttribute(attr);
+ if (value != null) {
+ attrs[attr] = value;
+ hasAny = true;
+ }
+ }
+ return hasAny ? attrs : null;
+}
+
+function serializeHtmlAttrs(htmlAttrs) {
+ if (!htmlAttrs) {
+ return "";
+ }
+ return Object.entries(htmlAttrs)
+ .map(
+ ([k, v]) =>
+ ` ${k}="${v.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;")}"`
+ )
+ .join("");
+}
+
const ALL_ALLOWED_TAGS = [...Object.keys(HTML_INLINE_MARKS), ...ALLOWED_INLINE];
/** @type {RichEditorExtension} */
@@ -29,23 +70,42 @@ const extension = {
inline: true,
defining: true,
content: "inline*",
- attrs: { tag: {} },
+ attrs: { tag: {}, htmlAttrs: { default: null } },
parseDOM: ALLOWED_INLINE.map((tag) => ({
tag,
- getAttrs: () => ({ tag }),
+ getAttrs: (element) => {
+ if (tag === "span") {
+ const htmlAttrs = extractHtmlAttrs(element, tag);
+ if (!htmlAttrs) {
+ return false;
+ }
+ return { tag, htmlAttrs };
+ }
+ return { tag, htmlAttrs: extractHtmlAttrs(element, tag) };
+ },
})),
- toDOM: (node) => [node.attrs.tag, 0],
+ toDOM: (node) => {
+ const domAttrs = node.attrs.htmlAttrs
+ ? { ...node.attrs.htmlAttrs }
+ : {};
+ return [node.attrs.tag, domAttrs, 0];
+ },
},
},
parse: {
html_inline: (state, token) => {
- const openMatch = token.content.match(/^<([a-z]+)(\s[^>]*)?>/i);
+ const openMatch = token.content.match(/^<([a-z]+)(\s[^>]*)?\/?>$/i);
const closeMatch = token.content.match(/^<\/([a-z]+)>$/i);
if (openMatch) {
const tagName = openMatch[1].toLowerCase();
const hasAttributes = openMatch[2];
+ if (tagName === "br") {
+ state.addNode(state.schema.nodes.hard_break);
+ return;
+ }
+
if (hasAttributes) {
const parser = new DOMParser();
const doc = parser.parseFromString(token.content, "text/html");
@@ -74,6 +134,15 @@ const extension = {
state.addNode(state.schema.nodes.image, attrs);
return;
}
+
+ if (ALLOWED_INLINE.includes(tagName)) {
+ const htmlAttrs = extractHtmlAttrs(element, tagName);
+ state.openNode(state.schema.nodes.html_inline, {
+ tag: tagName,
+ htmlAttrs,
+ });
+ return;
+ }
}
}
@@ -114,7 +183,8 @@ const extension = {
},
serializeNode: {
html_inline(state, node) {
- state.write(`<${node.attrs.tag}>`);
+ const attrsStr = serializeHtmlAttrs(node.attrs.htmlAttrs);
+ state.write(`<${node.attrs.tag}${attrsStr}>`);
state.renderInline(node);
state.write(`</${node.attrs.tag}>`);
},
diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
index 3d81f0e7a65..38e5f92706a 100644
--- a/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
@@ -86,6 +86,36 @@ module(
},
"![Alt text](https://example.com/image.png) after",
],
+ "ruby annotation round-trip": [
+ "<ruby>漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp></ruby>",
+ "<p><ruby>漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp></ruby> </p>",
+ "<ruby>漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp></ruby> ",
+ ],
+ "span with lang attribute": [
+ '<span lang="ja">日本語</span>',
+ '<p><span lang="ja">日本語</span> </p>',
+ '<span lang="ja">日本語</span> ',
+ ],
+ "br tag converts to hard break": [
+ "Line1<br>Line2",
+ "<p>Line1<br>Line2</p>",
+ "Line1\nLine2",
+ ],
+ "self-closing br tag converts to hard break": [
+ "Line1<br/>Line2",
+ "<p>Line1<br>Line2</p>",
+ "Line1\nLine2",
+ ],
+ "self-closing br tag with space converts to hard break": [
+ "Line1<br />Line2",
+ "<p>Line1<br>Line2</p>",
+ "Line1\nLine2",
+ ],
+ "br tag with attributes converts to hard break": [
+ 'Line1<br class="x">Line2',
+ "<p>Line1<br>Line2</p>",
+ "Line1\nLine2",
+ ],
}).forEach(([name, [markdown, html, expectedMarkdown]]) => {
test(name, async function (assert) {
this.siteSettings.rich_editor = true;
diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js
index c34adfea66c..77f164daa33 100644
--- a/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js
+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/table-test.js
@@ -23,6 +23,11 @@ module(
`<blockquote><div class="md-table"><table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Cell 1</td><td>Cell 2</td></tr></tbody></table></div></blockquote>`,
`> \n> | Header 1 | Header 2 |\n> |----|----|\n> | Cell 1 | Cell 2 |\n\n`,
],
+ "table with br in cells": [
+ `| Line1<br>Line2 | Cell 2 |\n| --- | --- |\n| Cell 3 | Cell 4 |`,
+ `<div class="md-table"><table><thead><tr><th>Line1<br>Line2</th><th>Cell 2</th></tr></thead><tbody><tr><td>Cell 3</td><td>Cell 4</td></tr></tbody></table></div>`,
+ `| Line1<br>Line2 | Cell 2 |\n|----|----|\n| Cell 3 | Cell 4 |\n\n`,
+ ],
}).forEach(([name, [markdown, html, expectedMarkdown]]) => {
test(name, async function (assert) {
this.siteSettings.rich_editor = true;