Skip to content

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

UX: Outline unstyled inline HTML in the rich editor (#44651)

Renato Atiliocfe10b5ada72
diff --git a/app/assets/stylesheets/common/rich-editor/rich-editor.scss b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
index 6f45aeacd13..d8e96fdaa73 100644
--- a/app/assets/stylesheets/common/rich-editor/rich-editor.scss
+++ b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
@@ -310,6 +310,12 @@
}
}
+ .composer-html-inline-node {
+ outline: 1px dashed var(--primary-low-mid);
+ padding-inline: 1px;
+ border-radius: 2px;
+ }
+
.composer-wrap-node {
position: relative;
white-space: normal;
diff --git a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
index 91eee136251..97d364f67e6 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
@@ -30,6 +30,30 @@ const INLINE_TAGS = [
"span",
];
+// Tags with a look of their own on the element (markers and cell borders
+// count, margins and link colour don't). Any other tag is outlined.
+const STYLED_TAGS = [
+ ...INLINE_TAGS.filter((tag) => tag !== "span"),
+ "abbr",
+ "blockquote",
+ "dd",
+ "details",
+ "dl",
+ "dt",
+ "h1",
+ "h2",
+ "h3",
+ "h4",
+ "h5",
+ "h6",
+ "li",
+ "ol",
+ "pre",
+ "summary",
+ "table",
+ "ul",
+];
+
const VOID_TAGS = [
"area",
"base",
@@ -190,9 +214,10 @@ function serializeHtmlAttrs(htmlAttrs) {
return "";
}
return Object.entries(htmlAttrs)
- .map(
- ([k, v]) =>
- ` ${k}="${v.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;")}"`
+ .map(([k, v]) =>
+ v === ""
+ ? ` ${k}`
+ : ` ${k}="${v.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;")}"`
)
.join("");
}
@@ -501,6 +526,40 @@ const extension = {
state.write(`</${node.attrs.tag}>`);
},
},
+ plugins({ pmState: { Plugin }, pmView: { Decoration, DecorationSet } }) {
+ function outlineDecorations(doc) {
+ const decorations = [];
+ doc.descendants((node, pos) => {
+ if (node.type.name !== "html_inline") {
+ return;
+ }
+
+ const { tag, htmlAttrs } = node.attrs;
+ if (STYLED_TAGS.includes(tag)) {
+ return;
+ }
+
+ const attrs = { class: "composer-html-inline-node" };
+ if (!htmlAttrs?.title) {
+ attrs.title = `<${tag}${serializeHtmlAttrs(htmlAttrs)}>`;
+ }
+ decorations.push(Decoration.node(pos, pos + node.nodeSize, attrs));
+ });
+ return DecorationSet.create(doc, decorations);
+ }
+
+ return new Plugin({
+ state: {
+ init: (_, state) => outlineDecorations(state.doc),
+ apply: (tr, set) => (tr.docChanged ? outlineDecorations(tr.doc) : set),
+ },
+ props: {
+ decorations(state) {
+ return this.getState(state);
+ },
+ },
+ });
+ },
inputRules: {
match: new RegExp(`<(${INPUT_RULE_TAGS.join("|")})>$`, "i"),
handler: (state, match, start, end) => {
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 e0115df967b..c12487174fb 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
@@ -11,6 +11,12 @@ module(
function (hooks) {
setupRenderingTest(hooks);
+ const outlined = (selector, title) => (assert) =>
+ assert
+ .dom(`.ProseMirror ${selector}`)
+ .hasClass("composer-html-inline-node")
+ .hasAttribute("title", title);
+
Object.entries({
"basic inline HTML tags": [
"<kbd>Ctrl</kbd>",
@@ -102,9 +108,29 @@ module(
],
"span with lang attribute": [
'<span lang="ja">日本語</span>',
- '<p><span lang="ja">日本語</span> </p>',
+ outlined("span[lang]", '<span lang="ja">'),
'<span lang="ja">日本語</span> ',
],
+ "span with an empty data attribute": [
+ "a <span data-fish>fishy</span> word",
+ outlined("span[data-fish]", "<span data-fish>"),
+ "a <span data-fish>fishy</span> word",
+ ],
+ "span with an explicitly empty attribute is written bare": [
+ 'a <span data-fish="">fishy</span> word',
+ outlined("span[data-fish]", "<span data-fish>"),
+ "a <span data-fish>fishy</span> word",
+ ],
+ "span without attributes": [
+ "<span>plain</span>",
+ outlined("span", "<span>"),
+ "<span>plain</span> ",
+ ],
+ "span with a valued data attribute": [
+ '<span data-mood="happy">text</span>',
+ outlined("span[data-mood]", '<span data-mood="happy">'),
+ '<span data-mood="happy">text</span> ',
+ ],
"br tag converts to hard break": [
"Line1<br>Line2",
"<p>Line1<br>Line2</p>",
@@ -148,10 +174,10 @@ module(
"<p><kbd><sup>x</sup></kbd> y</p>",
"<kbd><sup>x</sup></kbd> y",
],
- "anchor without href keeps its name": [
- 'see <a name="top">here</a>',
- '<p>see <a name="top">here</a> </p>',
- 'see <a name="top">here</a> ',
+ "anchor without href keeps its name and own title": [
+ 'see <a name="top" title="Top">here</a>',
+ outlined("a[name]", "Top"),
+ 'see <a name="top" title="Top">here</a> ',
],
"allow-listed tags and attributes inside a table round-trip": [
'| a |\n|---|\n| <ol start="3"><li>c</li></ol> <div data-x="1" onclick="y()">d</div> <abbr title="T">e</abbr> <h2>f</h2> |\n',
@@ -163,6 +189,17 @@ module(
},
'| a |\n|----|\n| <ol start="3"><li>c</li></ol> <div data-x="1">d</div> <abbr title="T">e</abbr> <h2>f</h2> |\n\n',
],
+ "only tags with no look of their own are outlined": [
+ "| a |\n|---|\n| <p>p</p> <div>d</div> <ul><li>l</li></ul> <kbd>k</kbd> |\n",
+ (assert) => {
+ assert.dom("td p").hasClass("composer-html-inline-node");
+ assert.dom("td div").hasAttribute("title", "<div>");
+ assert.dom("td ul").doesNotHaveClass("composer-html-inline-node");
+ assert.dom("td li").doesNotHaveClass("composer-html-inline-node");
+ assert.dom("td kbd").doesNotHaveClass("composer-html-inline-node");
+ },
+ "| a |\n|----|\n| <p>p</p> <div>d</div> <ul><li>l</li></ul> <kbd>k</kbd> |\n\n",
+ ],
"mark tags keep being marks when they carry attributes": [
'<strong class="x">bold</strong> <code class="lang-js">c</code>',
"<p><strong>bold</strong> <code>c</code></p>",
@@ -192,6 +229,20 @@ module(
assert.true(editor.value.includes("| <ul><li>a</li><li>b</li></ul>"));
});
+ test("inline HTML inserted after load is outlined", async function (assert) {
+ const [editor] = await setupRichEditor(assert, "a");
+ const { state, dispatch } = editor.view;
+ const span = state.schema.nodes.html_inline.create(
+ { tag: "span" },
+ state.schema.text("b")
+ );
+
+ dispatch(state.tr.insert(2, span));
+ await settled();
+
+ assert.dom(".ProseMirror span").hasClass("composer-html-inline-node");
+ });
+
test("pasted phrasing tags keep allow-listed attributes", async function (assert) {
const [editor] = await setupRichEditor(assert, "");