Skip to content

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

FIX: Strip `lang` from pasted spans (#42163)

Renato Atilioa7d45567bcb4
diff --git a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
index 2e35cddf3b1..34870f44c55 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
@@ -66,6 +66,10 @@ function serializeHtmlAttrs(htmlAttrs) {
const ALL_ALLOWED_TAGS = [...Object.keys(HTML_INLINE_MARKS), ...ALLOWED_INLINE];
+// Publishing tools stamp `lang` on every span; an authored one arrives as a token.
+// A span kept as a node also loses the whitespace sitting at its edges.
+const DOM_PARSED_INLINE = ALLOWED_INLINE.filter((tag) => tag !== "span");
+
const SMALL = "small";
function isSmall(node, schema) {
@@ -237,18 +241,12 @@ const extension = {
defining: true,
content: "inline*",
attrs: { tag: {}, htmlAttrs: { default: null } },
- parseDOM: ALLOWED_INLINE.map((tag) => ({
+ parseDOM: DOM_PARSED_INLINE.map((tag) => ({
tag,
- getAttrs: (element) => {
- if (tag === "span") {
- const htmlAttrs = extractHtmlAttrs(element, tag);
- if (!htmlAttrs) {
- return false;
- }
- return { tag, htmlAttrs };
- }
- return { tag, htmlAttrs: extractHtmlAttrs(element, tag) };
- },
+ getAttrs: (element) => ({
+ tag,
+ htmlAttrs: extractHtmlAttrs(element, tag),
+ }),
})),
toDOM: (node) => {
const domAttrs = node.attrs.htmlAttrs
diff --git a/frontend/discourse/app/static/prosemirror/extensions/word-paste.js b/frontend/discourse/app/static/prosemirror/extensions/word-paste.js
index 564b343e636..c115d61dd3a 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/word-paste.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/word-paste.js
@@ -354,17 +354,6 @@ export function stripWordReviewMarkup(container) {
.forEach((el) => el.remove());
}
-// Word for the web stamps lang on every text span (<span class="TextRun" lang>),
-// which the editor keeps and surfaces as literal <span lang> markup. Strip it
-// from Word's own spans only, so a lang the user added on purpose survives.
-const WORD_LANG_RUN_SELECTOR = "span[class*='TextRun'][lang]";
-
-export function stripWordLangAttributes(container) {
- container
- .querySelectorAll(WORD_LANG_RUN_SELECTOR)
- .forEach((el) => el.removeAttribute("lang"));
-}
-
/**
* Transforms Word-specific markup in an HTML string into standard HTML:
* converts Word lists and quote styles, and strips Word review markup
@@ -399,7 +388,6 @@ export function transformWordHtml(html) {
if (isWord) {
stripWordReviewMarkup(doc.body);
- stripWordLangAttributes(doc.body);
}
return doc.body.innerHTML;
diff --git a/frontend/discourse/tests/unit/lib/to-markdown-test.js b/frontend/discourse/tests/unit/lib/to-markdown-test.js
index aedc186b310..af2a7d601c3 100644
--- a/frontend/discourse/tests/unit/lib/to-markdown-test.js
+++ b/frontend/discourse/tests/unit/lib/to-markdown-test.js
@@ -444,6 +444,46 @@ helloWorld();</code>consectetur.`;
assert.false(markdown.includes("old removed phrase"));
});
+ test("drops the lang spans a publishing tool wraps each sentence in", async function (assert) {
+ const html = `<p><span class="sentence" lang="en">So I have heard.</span> <span class="sentence" lang="en">At one time.</span></p>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "So I have heard. At one time.",
+ "removes generated lang spans"
+ );
+ });
+
+ test("keeps the space a dropped lang span held at its edge", async function (assert) {
+ const html = `<p><span class="sentence" lang="en">So I have heard. </span><span class="sentence" lang="en">At one time.</span></p>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "So I have heard. At one time.",
+ "sentences stay separated"
+ );
+ });
+
+ test("drops an authored lang span too, arriving as HTML (accepted)", async function (assert) {
+ const html = `<p>He said <span lang="ja">日本語</span> loudly.</p>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "He said 日本語 loudly.",
+ "only markdown carries lang into the composer"
+ );
+ });
+
+ test("keeps the markup a dropped lang span wrapped", async function (assert) {
+ const html = `<p><span lang="en">A <b>bold</b> <mark>marked</mark> <ruby lang="ja">漢<rt>かん</rt></ruby> line.</span></p>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ 'A **bold** <mark>marked</mark> <ruby lang="ja">漢<rt>かん</rt></ruby> line.',
+ "preserves nested markup"
+ );
+ });
+
test("drops the document-language spans Word for the web stamps on runs", async function (assert) {
const html = `<div class="OutlineElement"><p class="Paragraph">
<span class="TextRun" lang="EN-GB"><span class="NormalTextRun" lang="EN-GB">Note:</span></span>
diff --git a/frontend/discourse/tests/unit/static/prosemirror/extensions/word-paste-test.js b/frontend/discourse/tests/unit/static/prosemirror/extensions/word-paste-test.js
index 477c2b3100e..50145e39227 100644
--- a/frontend/discourse/tests/unit/static/prosemirror/extensions/word-paste-test.js
+++ b/frontend/discourse/tests/unit/static/prosemirror/extensions/word-paste-test.js
@@ -1,6 +1,5 @@
import { module, test } from "qunit";
import {
- stripWordLangAttributes,
stripWordReviewMarkup,
transformWordHtml,
transformWordLists,
@@ -298,50 +297,10 @@ module("Unit | Static | ProseMirror | Extensions | word-paste", function () {
assert.strictEqual(doc.querySelectorAll("a.msocomanchor").length, 0);
});
- test("strips the document-language lang attribute from Word for the web runs", function (assert) {
- const html = `<div class="OutlineElement"><p class="Paragraph"><span
- class="TextRun" lang="EN-GB"><span class="NormalTextRun"
- lang="EN-GB">Hello</span></span></p></div>`;
- const doc = createDoc(transformWordHtml(html));
-
- assert.strictEqual(doc.querySelectorAll("[lang]").length, 0);
- assert.true(doc.body.textContent.includes("Hello"));
- });
-
- test("does not strip lang from non-Word HTML", function (assert) {
- const html = `<p><span lang="ja">日本語</span></p>`;
- const doc = createDoc(transformWordHtml(html));
-
- assert.strictEqual(doc.querySelectorAll("span[lang='ja']").length, 1);
- });
-
test("does not treat a generic TextRun class as Word (no content loss)", function (assert) {
- const html = `<p class="TextRun">Keep <del>this</del> <span lang="fr">bonjour</span></p>`;
+ const html = `<p class="TextRun">Keep <del>this</del> too</p>`;
const doc = createDoc(transformWordHtml(html));
assert.strictEqual(doc.querySelectorAll("del").length, 1);
- assert.strictEqual(doc.querySelectorAll("span[lang='fr']").length, 1);
- });
-
- test("keeps an intentional lang span inside a Word paste", function (assert) {
- const html = `<div class="OutlineElement"><p class="Paragraph">
- <span class="TextRun" lang="EN-GB">Note </span>
- <span lang="fr">bonjour</span>
- </p></div>`;
- const doc = createDoc(transformWordHtml(html));
-
- assert.strictEqual(doc.querySelectorAll("span.TextRun[lang]").length, 0);
- assert.strictEqual(doc.querySelectorAll("span[lang='fr']").length, 1);
- });
-
- test("stripWordLangAttributes modifies DOM in place", function (assert) {
- const doc = createDoc(
- `<p><span class="TextRun" lang="EN-GB">Text</span></p>`
- );
-
- stripWordLangAttributes(doc.body);
-
- assert.strictEqual(doc.querySelectorAll("[lang]").length, 0);
- assert.true(doc.body.textContent.includes("Text"));
});
});
diff --git a/spec/system/composer/prosemirror_pasting_spec.rb b/spec/system/composer/prosemirror_pasting_spec.rb
index 8aed1427d9e..eff60a5e1a1 100644
--- a/spec/system/composer/prosemirror_pasting_spec.rb
+++ b/spec/system/composer/prosemirror_pasting_spec.rb
@@ -266,6 +266,20 @@ describe "Composer - ProseMirror - Pasting content" do
expect(composer).to have_value("<mark>mark</mark> my <ins>words</ins> <kbd>ctrl</kbd> ")
end
+ it "pastes web page text without leaving literal span markup" do
+ cdp.allow_clipboard
+ open_composer
+ cdp.copy_paste(
+ # the space sits inside the first span, where a surviving span node eats it
+ %(<p><span class="sentence" lang="en">So I have heard. </span><span class="sentence" lang="en">At one time.</span></p>),
+ html: true,
+ )
+ expect(rich).to have_css("p", text: "So I have heard. At one time.")
+ expect(rich).to have_no_css("span[lang]")
+ composer.toggle_rich_editor
+ expect(composer).to have_value("So I have heard. At one time.")
+ end
+
it "converts newlines to hard breaks when parsing `white-space: pre` HTML" do
cdp.allow_clipboard
open_composer