Skip to content

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

DEV: support img and a when parsing html_inline token (#35389)

Renato Atilio41436bc56233
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/html-inline.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/html-inline.js
index 84f9f5f79cd..b123cb16dbb 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/html-inline.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/html-inline.js
@@ -38,13 +38,45 @@ const extension = {
},
},
parse: {
- // TODO(renato): it breaks if it's missing an end tag
html_inline: (state, token) => {
- const openMatch = token.content.match(/^<([a-z]+)>$/);
- const closeMatch = token.content.match(/^<\/([a-z]+)>$/);
+ const openMatch = token.content.match(/^<([a-z]+)(\s[^>]*)?>/i);
+ const closeMatch = token.content.match(/^<\/([a-z]+)>$/i);
if (openMatch) {
- const tagName = openMatch[1];
+ const tagName = openMatch[1].toLowerCase();
+ const hasAttributes = openMatch[2];
+
+ if (hasAttributes) {
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(token.content, "text/html");
+ const element = doc.body.firstElementChild;
+
+ if (element) {
+ // Handle links by delegating to the link mark
+ if (tagName === "a" && element.href) {
+ const attrs = {
+ href: element.getAttribute("href"),
+ title: element.title || null,
+ };
+ state.openMark(state.schema.marks.link.create(attrs));
+ return;
+ }
+
+ // Handle images by delegating to the image node (self-closing)
+ if (tagName === "img" && element.src) {
+ const attrs = {
+ src: element.src,
+ alt: element.alt || null,
+ title: element.title || null,
+ width: element.width || null,
+ height: element.height || null,
+ };
+ state.addNode(state.schema.nodes.image, attrs);
+ return;
+ }
+ }
+ }
+
const markName = HTML_INLINE_MARKS[tagName];
if (markName) {
state.openMark(state.schema.marks[markName].create());
@@ -61,7 +93,13 @@ const extension = {
}
if (closeMatch) {
- const tagName = closeMatch[1];
+ const tagName = closeMatch[1].toLowerCase();
+
+ if (tagName === "a") {
+ state.closeMark(state.schema.marks.link);
+ return;
+ }
+
const markName = HTML_INLINE_MARKS[tagName];
if (markName) {
state.closeMark(state.schema.marks[markName].create());
diff --git a/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs b/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs
index 2c155a00c6a..7296439e36a 100644
--- a/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs
+++ b/app/assets/javascripts/discourse/tests/helpers/rich-editor-helper.gjs
@@ -84,7 +84,16 @@ export async function testMarkdown(
multiToggle
);
- assert.strictEqual(html, expectedHtml, `HTML should match for "${markdown}"`);
+ if (typeof expectedHtml === "function") {
+ expectedHtml(assert);
+ } else {
+ assert.strictEqual(
+ html,
+ expectedHtml,
+ `HTML should match for "${markdown}"`
+ );
+ }
+
assert.strictEqual(
editorClass.value,
expectedMarkdown,
diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
index e9d12e33bcd..3d81f0e7a65 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
@@ -43,6 +43,49 @@ module(
"<p><ins>inserted</ins> and <del>deleted</del> </p>",
"<ins>inserted</ins> and <del>deleted</del> ",
],
+ "link HTML with href attribute": [
+ '<a href="https://example.com">text</a>',
+ '<p><a href="https://example.com">text</a></p>',
+ "[text](https://example.com)",
+ ],
+ "link HTML with href and title": [
+ '<a href="https://example.com" title="Title">x</a>',
+ '<p><a href="https://example.com" title="Title">x</a></p>',
+ '[x](https://example.com "Title")',
+ ],
+ "unmatched opening inline tag auto-closes": [
+ "<kbd>x",
+ "<p><kbd>x</kbd> </p>",
+ "<kbd>x</kbd> ",
+ ],
+ "unmatched opening link tag auto-closes": [
+ '<a href="https://example.com">x',
+ '<p><a href="https://example.com">x</a></p>',
+ "[x](https://example.com)",
+ ],
+ "image HTML with src, alt, width and height": [
+ '<img src="https://example.com/image.png" alt="Alt text" width="100" height="200">',
+ (assert) => {
+ assert
+ .dom("p img")
+ .hasAttribute("src", "https://example.com/image.png");
+ assert.dom("p img").hasAttribute("alt", "Alt text");
+ assert.dom("p img").hasAttribute("width", "100");
+ assert.dom("p img").hasAttribute("height", "200");
+ },
+ "![Alt text|100x200](https://example.com/image.png)",
+ ],
+ "image HTMl with text after": [
+ '<img src="https://example.com/image.png" alt="Alt text"> after',
+ (assert) => {
+ assert
+ .dom("p img")
+ .hasAttribute("src", "https://example.com/image.png");
+ assert.dom("p img").hasAttribute("alt", "Alt text");
+ assert.dom("p").hasText("after");
+ },
+ "![Alt text](https://example.com/image.png) after",
+ ],
}).forEach(([name, [markdown, html, expectedMarkdown]]) => {
test(name, async function (assert) {
this.siteSettings.rich_editor = true;