A platform for community discussion. Free, open, simple.
DEV: to-markdown prosemirror HTML parsing (#35563)
Renato Atiliod62cc48d9ca8
diff --git a/frontend/discourse/app/components/post-text-selection-toolbar.gjs b/frontend/discourse/app/components/post-text-selection-toolbar.gjsindex e2614491698..d56a1248ad1 100644--- a/frontend/discourse/app/components/post-text-selection-toolbar.gjs+++ b/frontend/discourse/app/components/post-text-selection-toolbar.gjs@@ -7,7 +7,7 @@ import PluginOutlet from "discourse/components/plugin-outlet";import lazyHash from "discourse/helpers/lazy-hash";import { getAbsoluteURL } from "discourse/lib/get-url";import Sharing from "discourse/lib/sharing";-import { clipboardCopy, postUrl } from "discourse/lib/utilities";+import { clipboardCopyAsync, postUrl } from "discourse/lib/utilities";import DButton from "discourse/ui-kit/d-button";import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";import { i18n } from "discourse-i18n";@@ -69,8 +69,11 @@ export default class PostTextSelectionToolbar extends Component {@actionasync copyQuoteToClipboard() {- const text = await this.args.data.buildQuote();- clipboardCopy(text);+ await clipboardCopyAsync(() =>+ this.args.data+ .buildQuote()+ .then((text) => new Blob([text], { type: "text/plain" }))+ );this.toasts.success({duration: "short",data: { message: i18n("post.quote_copied_to_clipboard") },diff --git a/frontend/discourse/app/components/post-text-selection.gjs b/frontend/discourse/app/components/post-text-selection.gjsindex 2b93d809819..43169d271bd 100644--- a/frontend/discourse/app/components/post-text-selection.gjs+++ b/frontend/discourse/app/components/post-text-selection.gjs@@ -11,12 +11,11 @@ import { bind } from "discourse/lib/decorators";import { INPUT_DELAY } from "discourse/lib/environment";import escapeRegExp from "discourse/lib/escape-regexp";import isElementInViewport from "discourse/lib/is-element-in-viewport";-import toMarkdown from "discourse/lib/to-markdown";import {getElement,+ selectedHTML,selectedNode,selectedRange,- selectedText,setCaretPosition,} from "discourse/lib/utilities";import virtualElementFromTextRange from "discourse/lib/virtual-element-from-text-range";@@ -78,7 +77,10 @@ export default class PostTextSelection extends Component {}const quoteState = this.computeQuoteState(cooked);- const supportsFastEdit = this.computeSupportsFastEdit(cooked, quoteState);+ const supportsFastEdit = await this.computeSupportsFastEdit(+ cooked,+ quoteState+ );await this.toggleFastEdit(quoteState, supportsFastEdit);}@@ -89,10 +91,16 @@ export default class PostTextSelection extends Component {// other action to change its value until toggleFastEdit is completeconst post = this.post;+ const { markdown } = await quoteState.markdown();++ if (this.isDestroying || this.isDestroyed) {+ return;+ }+if (supportsFastEdit) {this.modal.show(FastEditModal, {model: {- initialValue: quoteState.buffer,+ initialValue: markdown,post,},});@@ -109,9 +117,7 @@ export default class PostTextSelection extends Component {// selecting even a part of the text of a list item will include// "* " at the beginning of the buffer, we remove it to be able// to find it in row- const buffer = fixQuotes(- quoteState.buffer.split("\n")[0].replace(/^\* /, "")- );+ const buffer = fixQuotes(markdown.split("\n")[0].replace(/^\* /, ""));rows.some((row, index) => {if (row.length && row.includes(buffer)) {@@ -151,6 +157,10 @@ export default class PostTextSelection extends Component {async showToolbar(cooked) {const shouldRenderBelow = this.shouldRenderBelow();const quoteState = this.computeQuoteState(cooked);+ const supportsFastEdit = await this.computeSupportsFastEdit(+ cooked,+ quoteState+ );let offset = 3;@@ -182,7 +192,7 @@ export default class PostTextSelection extends Component {trapTab: false,closeOnScroll: false,data: {- supportsFastEdit: this.computeSupportsFastEdit(cooked, quoteState),+ supportsFastEdit,canEditPost: this.canEditPost,canCopyQuote: this.canCopyQuote,topic: this.args.topic,@@ -240,7 +250,7 @@ export default class PostTextSelection extends Component {return; // we changed the range here, so we want to go through the selection change handler again}- if (!selectedText()?.length) {+ if (!window.getSelection().toString().trim().length) {return;}@@ -319,7 +329,7 @@ export default class PostTextSelection extends Component {return await this.args.buildQuoteMarkdown();}- computeSupportsFastEdit(cooked, quoteState) {+ async computeSupportsFastEdit(cooked, quoteState) {const selection = window.getSelection();let supportsFastEdit = this.canEditPost;@@ -337,15 +347,16 @@ export default class PostTextSelection extends Component {}if (supportsFastEdit) {- const regexp = new RegExp(escapeRegExp(quoteState.buffer), "gi");+ const { markdown } = await quoteState.markdown();+ const regexp = new RegExp(escapeRegExp(markdown), "gi");try {const matches = cooked.innerHTML.match(regexp);if (- quoteState.buffer.length === 0 ||- quoteState.buffer.includes("|") || // tables are too complex- quoteState.buffer.match(/\n/g) || // linebreaks are too complex- quoteState.buffer.match(/[‚‘’„“”«»‹›™±…→←↔¶]/g) || // typopgraphic characters are too complex+ markdown.length === 0 ||+ markdown.includes("|") || // tables are too complex+ markdown.match(/\n/g) || // linebreaks are too complex+ markdown.match(/[‚‘’„“”«»‹›™±…→←↔¶]/g) || // typopgraphic characters are too complexmatches?.length !== 1 // duplicates are too complex) {supportsFastEdit = false;@@ -361,19 +372,31 @@ export default class PostTextSelection extends Component {}computeQuoteState(cooked) {- const _selectedText = selectedText();+ const html = selectedHTML();const _selectedElement = getElement(selectedNode());const postId = cooked.closest(".boxed, .reply")?.dataset?.postId;- // computing markdown takes a lot of time on long posts- // this code attempts to compute it only when we can't fast track- let opts = {- full:- selectedRange().startOffset > 0- ? false- : _selectedText === toMarkdown(cooked.innerHTML),+ const range = selectedRange();+ const startNode = range.startContainer;++ const isFirstCookedChild = (node) => {+ if (node === cooked) {+ return true;+ }++ if (node.parentNode.firstChild !== node) {+ return false;+ }++ return isFirstCookedChild(node.parentNode);};+ const canBeFull =+ range.startOffset === 0 &&+ cooked.contains(startNode) &&+ isFirstCookedChild(startNode);+ const opts = { full: false };+for (let element = _selectedElement;element && element.tagName !== "ARTICLE";@@ -388,7 +411,13 @@ export default class PostTextSelection extends Component {}const quoteState = this.args.quoteState;- quoteState.selected(postId, _selectedText, opts);+ quoteState.selected(+ postId,+ null,+ opts,+ html,+ canBeFull ? cooked.innerHTML : null+ );return quoteState;}diff --git a/frontend/discourse/app/controllers/nested.js b/frontend/discourse/app/controllers/nested.jsindex fa9e7853df4..75d213602b9 100644--- a/frontend/discourse/app/controllers/nested.js+++ b/frontend/discourse/app/controllers/nested.js@@ -280,18 +280,16 @@ export default class NestedController extends Controller {@actionselectText() {const tc = this.#topicController;- const { postId, buffer, opts } = this.quoteState;- this.#ensurePostInStream(postId);- tc.quoteState.selected(postId, buffer, opts);+ this.#ensurePostInStream(this.quoteState.postId);+ tc.quoteState.copyFrom(this.quoteState);return tc.selectText();}@actionbuildQuoteMarkdown() {const tc = this.#topicController;- const { postId, buffer, opts } = this.quoteState;- this.#ensurePostInStream(postId);- tc.quoteState.selected(postId, buffer, opts);+ this.#ensurePostInStream(this.quoteState.postId);+ tc.quoteState.copyFrom(this.quoteState);return tc.buildQuoteMarkdown();}diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.jsindex 3923b7d8c0f..906e7dffc13 100644--- a/frontend/discourse/app/controllers/topic.js+++ b/frontend/discourse/app/controllers/topic.js@@ -582,78 +582,79 @@ export default class TopicController extends Controller {}@action- selectText() {- const { postId, buffer, opts } = this.quoteState;- const loadedPost = this.get("model.postStream").findLoadedPost(postId);- const promise = loadedPost- ? Promise.resolve(loadedPost)- : this.get("model.postStream").loadPost(postId);-- return promise.then(async (post) => {- if (EmbedMode.enabled) {- const quotedText = buildQuote(post, buffer, opts);- this.appEvents.trigger("embed-composer:reply-to-post", post);- if (quotedText?.trim()) {- this.appEvents.trigger("composer:insert-block", quotedText);- }- return;- }+ async selectText() {+ const { postId } = this.quoteState;+ const postStream = this.get("model.postStream");+ const composer = this.composer;+ const { markdown: buffer, opts } = await this.quoteState.markdown();- const quoteEvent = { post, buffer, opts, handled: false };- this.appEvents.trigger("topic:quote-post", quoteEvent);- if (quoteEvent.handled) {- return;+ if (EmbedMode.enabled) {+ const loadedPost = postStream.findLoadedPost(postId);+ const post = loadedPost ? loadedPost : await postStream.loadPost(postId);+ const quotedText = buildQuote(post, buffer, opts);+ this.appEvents.trigger("embed-composer:reply-to-post", post);+ if (quotedText?.trim()) {+ this.appEvents.trigger("composer:insert-block", quotedText);}+ return;+ }- const composer = this.composer;- const viewOpen = composer.get("model.viewOpen");+ const loadedPost = postStream.findLoadedPost(postId);+ const post = loadedPost ? loadedPost : await postStream.loadPost(postId);- // If we can't create a post, delegate to reply as new topic- if (!viewOpen && !this.get("model.details.can_create_post")) {- this.send("replyAsNewTopic", post);- return;- }+ const quoteEvent = { post, buffer, opts, handled: false };+ this.appEvents.trigger("topic:quote-post", quoteEvent);+ if (quoteEvent.handled) {+ return;+ }- const composerOpts = {- action: Composer.REPLY,- draftSequence: post.get("topic.draft_sequence"),- draftKey: post.get("topic.draft_key"),- };+ const viewOpen = composer.get("model.viewOpen");- if (post.get("post_number") === 1) {- composerOpts.topic = post.get("topic");- } else {- composerOpts.post = post;- }+ // If we can't create a post, delegate to reply as new topic+ if (!viewOpen && !this.get("model.details.can_create_post")) {+ await this.replyAsNewTopic(post);+ return;+ }- // If the composer is associated with a different post, we don't change it.- const composerPost = composer.get("model.post");- if (composerPost && composerPost.get("id") !== this.get("post.id")) {- composerOpts.post = composerPost;- }+ const composerOpts = {+ action: Composer.REPLY,+ draftSequence: post.get("topic.draft_sequence"),+ draftKey: post.get("topic.draft_key"),+ };- const quotedText = buildQuote(post, buffer, opts);+ if (post.get("post_number") === 1) {+ composerOpts.topic = post.get("topic");+ } else {+ composerOpts.post = post;+ }- if (composer.get("model.viewOpen")) {- this.appEvents.trigger("composer:insert-block", quotedText);- } else if (composer.get("model.viewDraft")) {- const model = composer.get("model");- model.set("reply", model.get("reply") + "\n" + quotedText);- composer.openIfDraft();- } else {- const draftData = await Draft.get(composerOpts.draftKey);+ // If the composer is associated with a different post, we don't change it.+ const composerPost = composer.get("model.post");+ if (composerPost && composerPost.get("id") !== this.get("post.id")) {+ composerOpts.post = composerPost;+ }- if (draftData.draft) {- const data = JSON.parse(draftData.draft);- composerOpts.draftSequence = draftData.draft_sequence;- composerOpts.reply = data.reply + "\n" + quotedText;- } else {- composerOpts.quote = quotedText;- }+ const quotedText = buildQuote(post, buffer, opts);++ if (composer.get("model.viewOpen")) {+ this.appEvents.trigger("composer:insert-block", quotedText);+ } else if (composer.get("model.viewDraft")) {+ const model = composer.get("model");+ model.set("reply", model.get("reply") + "\n" + quotedText);+ composer.openIfDraft();+ } else {+ const draftData = await Draft.get(composerOpts.draftKey);- composer.open(composerOpts);+ if (draftData.draft) {+ const data = JSON.parse(draftData.draft);+ composerOpts.draftSequence = draftData.draft_sequence;+ composerOpts.reply = data.reply + "\n" + quotedText;+ } else {+ composerOpts.quote = quotedText;}- });++ composer.open(composerOpts);+ }}@action@@ -858,15 +859,12 @@ export default class TopicController extends Controller {return;}- if (EmbedMode.enabled) {- const quotedPost = postStream.findLoadedPost(quoteState.postId);- const quotedText = buildQuote(- quotedPost,- quoteState.buffer,- quoteState.opts- );- quoteState.clear();+ const quotedPost = postStream.findLoadedPost(quoteState.postId);+ const { markdown: buffer, opts: quoteOpts } = await quoteState.markdown();+ const quotedText = buildQuote(quotedPost, buffer, quoteOpts);+ quoteState.clear();+ if (EmbedMode.enabled) {this.appEvents.trigger("embed-composer:reply-to-post", post);if (quotedText?.trim()) {this.appEvents.trigger("composer:insert-block", quotedText.trim());@@ -875,14 +873,6 @@ export default class TopicController extends Controller {}const composerController = this.composer;- const quotedPost = postStream.findLoadedPost(quoteState.postId);- const quotedText = buildQuote(- quotedPost,- quoteState.buffer,- quoteState.opts- );-- quoteState.clear();if (composerController.get("model.topic.id") === topic.get("id") &&@@ -1513,10 +1503,11 @@ export default class TopicController extends Controller {}@action- replyAsNewTopic(post) {+ async replyAsNewTopic(post) {const composerController = this.composer;const { quoteState } = this;- const quotedText = buildQuote(post, quoteState.buffer, quoteState.opts);+ const { markdown: buffer, opts } = await quoteState.markdown();+ const quotedText = buildQuote(post, buffer, opts);quoteState.clear();@@ -1910,16 +1901,14 @@ export default class TopicController extends Controller {}@action- buildQuoteMarkdown() {- const { postId, buffer, opts } = this.quoteState;- const loadedPost = this.get("model.postStream").findLoadedPost(postId);- const promise = loadedPost- ? Promise.resolve(loadedPost)- : this.get("model.postStream").loadPost(postId);+ async buildQuoteMarkdown() {+ const { postId } = this.quoteState;+ const postStream = this.get("model.postStream");+ const { markdown: buffer, opts } = await this.quoteState.markdown();+ const loadedPost = postStream.findLoadedPost(postId);+ const post = loadedPost ? loadedPost : await postStream.loadPost(postId);- return promise.then((post) => {- return buildQuote(post, buffer, opts);- });+ return buildQuote(post, buffer, opts);}@actiondiff --git a/frontend/discourse/app/lib/composer/rich-editor-extensions.js b/frontend/discourse/app/lib/composer/rich-editor-extensions.jsindex 54f8b64fa70..6087efef852 100644--- a/frontend/discourse/app/lib/composer/rich-editor-extensions.js+++ b/frontend/discourse/app/lib/composer/rich-editor-extensions.js@@ -137,6 +137,15 @@ import { waitForPromise } from "@ember/test-waiters";/** @type {RichEditorExtension[]} */const registeredExtensions = [];+let defaultExtensionsRegistered = false;++export function markDefaultExtensionsRegistered() {+ defaultExtensionsRegistered = true;+}++export function areDefaultExtensionsRegistered() {+ return defaultExtensionsRegistered;+}/*** Registers an extension for the rich editor@@ -155,12 +164,14 @@ export async function clearRichEditorExtensions() {import("discourse/static/prosemirror/extensions/register-default"));registeredExtensions.length = 0;+ defaultExtensionsRegistered = false;return module;}export async function resetRichEditorExtensions() {const { default: extensions } = await clearRichEditorExtensions();extensions.forEach(registerRichEditorExtension);+ markDefaultExtensionsRegistered();}/**diff --git a/frontend/discourse/app/lib/list-utils.js b/frontend/discourse/app/lib/list-utils.jsnew file mode 100644index 00000000000..fef8ce39eac--- /dev/null+++ b/frontend/discourse/app/lib/list-utils.js@@ -0,0 +1,102 @@+/**+ * Shared utilities for list handling across selection quoting, paste normalization,+ * and ProseMirror extensions.+ */++/**+ * Determines if a list is "tight" (compact) based on CommonMark semantics.+ * A tight list has no blank lines between items - its <li> elements do NOT+ * contain block-level <p> elements as direct children.+ *+ * @param {Element} listElement - An <ol> or <ul> element+ * @returns {boolean} True if the list is tight+ */+export function isListTight(listElement) {+ const listItems = listElement.querySelectorAll(":scope > li");+ for (const li of listItems) {+ for (const child of li.children) {+ if (child.tagName === "P") {+ return false;+ }+ }+ }+ return true;+}++/**+ * Sets the data-tight attribute on a list element based on its structure.+ *+ * @param {Element} listElement - An <ol> or <ul> element+ */+export function setTightAttribute(listElement) {+ if (isListTight(listElement)) {+ listElement.setAttribute("data-tight", "true");+ }+}++/**+ * Sets the data-tight attribute on all lists within a container.+ *+ * @param {Element} container - Container element to process+ */+export function setTightAttributeOnAllLists(container) {+ const lists = container.querySelectorAll("ol, ul");+ for (const list of lists) {+ setTightAttribute(list);+ }+}++/**+ * Gets the effective start number for a list item within its parent list.+ * For ordered lists, this accounts for the list's start attribute and+ * the item's position.+ *+ * @param {Element} listElement - The parent <ol> or <ul> element+ * @param {Element|null} listItem - The <li> element (optional)+ * @returns {number} The start number (1 for unordered lists)+ */+export function getListStartNumber(listElement, listItem) {+ if (listElement.tagName !== "OL") {+ return 1;+ }+ const parsed = parseInt(listElement.getAttribute("start") || "1", 10);+ const baseStart = Number.isNaN(parsed) ? 1 : parsed;+ if (!listItem) {+ return baseStart;+ }+ const listItems = listElement.querySelectorAll(":scope > li");+ const itemIndex = Array.from(listItems).indexOf(listItem);+ return baseStart + Math.max(0, itemIndex);+}++/**+ * Creates a list wrapper element with appropriate attributes.+ *+ * @param {string} tagName - "OL" or "UL"+ * @param {number} startNumber - Start number for ordered lists+ * @param {boolean} isTight - Whether the list is tight+ * @returns {HTMLElement} The created list element+ */+export function createListWrapper(tagName, startNumber, isTight) {+ const list = document.createElement(tagName.toLowerCase());+ if (tagName === "OL" && startNumber !== 1) {+ list.setAttribute("start", String(startNumber));+ }+ if (isTight) {+ list.setAttribute("data-tight", "true");+ }+ return list;+}++/**+ * Finds the closest parent list element.+ *+ * @param {Element|null} element - Starting element+ * @returns {Element|null} The closest <ol> or <ul> ancestor+ */+export function findParentList(element) {+ if (element?.tagName === "OL" || element?.tagName === "UL") {+ return element;+ }+ return element?.closest("ol, ul");+}diff --git a/frontend/discourse/app/lib/quote-state.js b/frontend/discourse/app/lib/quote-state.jsindex c4648f192ae..f2d546316cd 100644--- a/frontend/discourse/app/lib/quote-state.js+++ b/frontend/discourse/app/lib/quote-state.js@@ -1,19 +1,92 @@import { tracked } from "@glimmer/tracking";+import { waitForPromise } from "@ember/test-waiters";+import toMarkdown from "discourse/lib/to-markdown";export default class QuoteState {@tracked postId = null;- @tracked buffer = "";- @tracked opts = null;- selected(postId, buffer, opts) {+ #opts = null;+ #selectedHtml = null;+ #cookedHtml = null;+ #markdownPromise = null;++ get buffer() {+ if (!this.#selectedHtml) {+ return "";+ }+ // `<template>.content` is an inert DocumentFragment — no image/iframe fetches.+ const template = document.createElement("template");+ template.innerHTML = this.#selectedHtml;+ return template.content.textContent ?? "";+ }++ get opts() {+ return this.#opts;+ }++ // The `plainText` arg is accepted but unused; `buffer` now derives plaintext+ // from `selectedHtml`. The slot is kept to preserve the legacy arg shape.+ selected(postId, _plainText, opts, selectedHtml, cookedHtml) {this.postId = postId;- this.buffer = buffer;- this.opts = opts;+ this.#opts = opts;+ this.#selectedHtml = selectedHtml;+ this.#cookedHtml = cookedHtml;+ this.#markdownPromise = null;+ }++ async markdown() {+ if (!this.#selectedHtml) {+ return { markdown: this.buffer, opts: { ...this.#opts } };+ }++ if (!this.#markdownPromise) {+ this.#markdownPromise = waitForPromise(this.#computeMarkdown());+ }++ // Snapshot opts before the await so a concurrent selected() call cannot+ // pair our markdown with a later selection's attribution.+ const opts = this.#opts;+ const result = await this.#markdownPromise;++ return {+ markdown: result.markdown,+ opts: { ...opts, full: result.full },+ };+ }++ async #computeMarkdown() {+ try {+ const promises = [toMarkdown(this.#selectedHtml)];+ if (this.#cookedHtml) {+ promises.push(toMarkdown(this.#cookedHtml));+ }++ const [selectedMd, cookedMd] = await Promise.all(promises);++ return {+ markdown: selectedMd || this.buffer,+ full: !!(cookedMd && selectedMd === cookedMd),+ };+ } catch {+ return { markdown: this.buffer, full: false };+ }+ }++ copyFrom(other) {+ this.selected(+ other.postId,+ null,+ other.#opts,+ other.#selectedHtml,+ other.#cookedHtml+ );}clear() {- this.buffer = "";this.postId = null;- this.opts = null;+ this.#opts = null;+ this.#selectedHtml = null;+ this.#cookedHtml = null;+ this.#markdownPromise = null;}}diff --git a/frontend/discourse/app/lib/selection/preserve-list-structure.js b/frontend/discourse/app/lib/selection/preserve-list-structure.jsnew file mode 100644index 00000000000..124e03e7816--- /dev/null+++ b/frontend/discourse/app/lib/selection/preserve-list-structure.js@@ -0,0 +1,391 @@+/**+ * List structure preservation for selection/quote operations.+ *+ * When users select content that includes list items, the browser's+ * cloneContents() may produce orphan <li> elements without their parent+ * lists. This module reconstructs proper list structure while preserving+ * the original list types (ordered vs unordered) and attributes.+ */++import {+ createListWrapper,+ findParentList,+ getListStartNumber,+ isListTight,+ setTightAttributeOnAllLists,+} from "discourse/lib/list-utils";++/**+ * States for the orphan run collector state machine.+ * @readonly+ * @enum {string}+ */+const CollectorState = {+ IDLE: "idle",+ COLLECTING: "collecting",+};++function closestElement(node) {+ while (node && node.nodeType !== Node.ELEMENT_NODE) {+ node = node.parentNode;+ }+ return node;+}++function getStartElementFromRange(range) {+ return closestElement(range.startContainer);+}++function hasTopLevelListItems(container) {+ for (const node of container.childNodes) {+ if (node.nodeName === "LI") {+ return true;+ }+ }+ return false;+}++function wrapLeadingNonListContent(container) {+ const nodesToWrap = [];+ for (const node of container.childNodes) {+ if (node.nodeName === "LI") {+ break;+ }+ nodesToWrap.push(node);+ }+ if (nodesToWrap.length === 0) {+ return;+ }+ const listItem = document.createElement("li");+ const firstLi = container.querySelector(":scope > li");+ container.insertBefore(listItem, firstLi);+ nodesToWrap.forEach((node) => listItem.appendChild(node));+}++function wrapContainerContentsInListItem(+ container,+ listTagName,+ startNumber,+ isTight+) {+ const listItem = document.createElement("li");+ while (container.firstChild) {+ listItem.appendChild(container.firstChild);+ }+ const list = createListWrapper(listTagName, startNumber, isTight);+ list.appendChild(listItem);+ container.appendChild(list);+}++/**+ * Determines if a node is a separator that should break an LI run.+ * Whitespace-only text nodes are NOT separators (ignored).+ * All other non-LI nodes ARE separators.+ *+ * @param {Node} node - The node to check+ * @returns {boolean} True if the node should end the current run+ */+function isSeparatorNode(node) {+ if (node.nodeName === "LI") {+ return false;+ }+ if (node.nodeType === Node.TEXT_NODE) {+ return /\S/.test(node.textContent);+ }+ // All other nodes (elements, comments, etc.) are separators+ return true;+}++/**+ * Collects contiguous runs of orphan <li> elements from a container.+ * Uses an explicit state machine:+ * - IDLE: Looking for an <li> to start a new run+ * - COLLECTING: Accumulating <li> elements until a separator is found+ *+ * Whitespace-only text nodes are ignored and don't break runs.+ * Any other non-<li> node (including <br>, <p>, non-whitespace text) breaks the run.+ *+ * @param {Element} container - Container with potential orphan <li> elements+ * @returns {Array<HTMLElement[]>} Array of runs, each run being an array of <li> elements+ */+export function collectContiguousOrphanRuns(container) {+ const runs = [];+ let currentRun = [];+ let state = CollectorState.IDLE;++ for (const node of container.childNodes) {+ const isListItem = node.nodeName === "LI";+ const isSeparator = isSeparatorNode(node);++ switch (state) {+ case CollectorState.IDLE:+ if (isListItem) {+ currentRun = [node];+ state = CollectorState.COLLECTING;+ }+ break;++ case CollectorState.COLLECTING:+ if (isListItem) {+ currentRun.push(node);+ } else if (isSeparator) {+ runs.push(currentRun);+ currentRun = [];+ state = CollectorState.IDLE;+ }+ // Whitespace text nodes: stay in COLLECTING, don't add to run+ break;+ }+ }++ // Flush any remaining run+ if (state === CollectorState.COLLECTING && currentRun.length > 0) {+ runs.push(currentRun);+ }++ return runs;+}++function wrapOrphanListItems(container, listTagName, startNumber, isTight) {+ const runs = collectContiguousOrphanRuns(container);+ if (runs.length === 0) {+ return;+ }++ let currentStart = startNumber;+ for (const run of runs) {+ const list = createListWrapper(listTagName, currentStart, isTight);+ run[0].parentNode.insertBefore(list, run[0]);+ run.forEach((li) => list.appendChild(li));+ if (listTagName === "OL") {+ currentStart += run.length;+ }+ }+}++/**+ * Splits a run of orphan list items into sub-runs based on their original+ * list type (stored in data-original-list-tag attribute).+ *+ * @param {HTMLElement[]} run - Array of <li> elements+ * @returns {Array<{items: HTMLElement[], listTag: string, startNumber: number, isTight: boolean}>}+ */+function splitRunByOriginalListType(run) {+ const subRuns = [];+ let currentSubRun = null;++ for (const li of run) {+ const listTag = li.dataset.originalListTag || "UL";+ const startNumber = parseInt(li.dataset.originalStartNumber || "1", 10);+ const isTight = li.dataset.originalIsTight === "true";++ if (+ !currentSubRun ||+ currentSubRun.listTag !== listTag ||+ (listTag === "OL" &&+ startNumber !== currentSubRun.startNumber + currentSubRun.items.length)+ ) {+ currentSubRun = { items: [li], listTag, startNumber, isTight };+ subRuns.push(currentSubRun);+ } else {+ currentSubRun.items.push(li);+ }+ }++ return subRuns;+}++/**+ * Wraps orphan list items while respecting their original list types.+ * This handles selections that span multiple lists of different types.+ *+ * @param {HTMLElement} container - The container with orphan <li> elements+ */+function wrapOrphanListItemsByOriginalType(container) {+ const runs = collectContiguousOrphanRuns(container);+ if (runs.length === 0) {+ return;+ }++ for (const run of runs) {+ const hasAnnotations = run.some((li) => li.dataset.originalListTag);++ if (hasAnnotations) {+ const subRuns = splitRunByOriginalListType(run);+ for (const subRun of subRuns) {+ const list = createListWrapper(+ subRun.listTag,+ subRun.startNumber,+ subRun.isTight+ );+ subRun.items[0].parentNode.insertBefore(list, subRun.items[0]);+ subRun.items.forEach((li) => {+ delete li.dataset.originalListTag;+ delete li.dataset.originalStartNumber;+ delete li.dataset.originalIsTight;+ list.appendChild(li);+ });+ }+ } else {+ const list = createListWrapper("UL", 1, true);+ run[0].parentNode.insertBefore(list, run[0]);+ run.forEach((li) => list.appendChild(li));+ }+ }+}++function annotateOrphanListItem(orphanLi, originalLi) {+ const parentList = originalLi.parentElement;+ if (!(parentList?.tagName === "OL" || parentList?.tagName === "UL")) {+ return;+ }++ orphanLi.dataset.originalListTag = parentList.tagName;+ orphanLi.dataset.originalStartNumber = String(+ getListStartNumber(parentList, originalLi)+ );+ orphanLi.dataset.originalIsTight = String(isListTight(parentList));+}++/**+ * Annotates orphan list items in the cloned content with their original list info.+ * This looks up each <li> in the original DOM to find its parent list type.+ *+ * @param {HTMLElement} container - Container with cloned content+ * @param {Range} range - The original selection range+ */+function annotateOrphanListItemsWithOriginalInfo(container, range) {+ const orphanLis = Array.from(container.querySelectorAll(":scope > li"));+ if (orphanLis.length === 0) {+ return;+ }++ const commonAncestor =+ range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE+ ? range.commonAncestorContainer+ : range.commonAncestorContainer.parentElement;++ const originalListItems = Array.from(commonAncestor.querySelectorAll("li"));+ let selectionListItems = originalListItems;++ if (typeof range.intersectsNode === "function") {+ selectionListItems = originalListItems.filter((li) => {+ try {+ return range.intersectsNode(li);+ } catch {+ return false;+ }+ });+ }++ if (selectionListItems.length === orphanLis.length) {+ for (let i = 0; i < orphanLis.length; i++) {+ annotateOrphanListItem(orphanLis[i], selectionListItems[i]);+ }+ return;+ }++ const remaining = selectionListItems.length+ ? [...selectionListItems]+ : [...originalListItems];++ for (const orphanLi of orphanLis) {+ const orphanText = orphanLi.textContent.trim();+ const matchIndex = remaining.findIndex(+ (originalLi) => originalLi.textContent.trim() === orphanText+ );++ if (matchIndex === -1) {+ continue;+ }++ annotateOrphanListItem(orphanLi, remaining[matchIndex]);+ remaining.splice(matchIndex, 1);+ }+}++/**+ * Preserves list structure when content is cloned from a selection.+ * Handles three scenarios:+ * 1. Selection starts inside a list item - wrap content appropriately+ * 2. Selection contains orphan <li> elements - wrap in their original list types+ * 3. Selection spans multiple lists - preserve each list's type+ *+ * @param {HTMLElement} container - Container with cloned selection content+ * @param {Range} range - The original selection range+ */+export function preserveListStructureInClonedContent(container, range) {+ const startElement = getStartElementFromRange(range);+ const originalListItem = startElement?.closest("li");++ if (hasTopLevelListItems(container)) {+ annotateOrphanListItemsWithOriginalInfo(container, range);+ }++ if (originalListItem) {+ const parentList = originalListItem.parentElement;+ const isParentAList =+ parentList?.tagName === "OL" || parentList?.tagName === "UL";++ if (!isParentAList) {+ return;+ }++ const listTagName = parentList.tagName;+ const startNumber = getListStartNumber(parentList, originalListItem);+ const isTight = isListTight(parentList);++ const firstElementChild = container.firstElementChild;+ const isAlreadyWrappedInCorrectList =+ firstElementChild?.tagName === listTagName &&+ !hasTopLevelListItems(container);++ if (isAlreadyWrappedInCorrectList) {+ if (listTagName === "OL" && startNumber !== 1) {+ firstElementChild.setAttribute("start", String(startNumber));+ }+ if (isTight) {+ firstElementChild.setAttribute("data-tight", "true");+ }+ } else if (hasTopLevelListItems(container)) {+ wrapLeadingNonListContent(container);+ wrapOrphanListItemsByOriginalType(container);+ } else {+ wrapContainerContentsInListItem(+ container,+ listTagName,+ startNumber,+ isTight+ );+ }+ } else {+ if (hasTopLevelListItems(container)) {+ wrapLeadingNonListContent(container);+ wrapOrphanListItemsByOriginalType(container);+ } else {+ const originalList = findParentList(startElement);+ if (originalList) {+ const startNumber = getListStartNumber(originalList, null);+ const isTight = isListTight(originalList);+ wrapOrphanListItems(+ container,+ originalList.tagName,+ startNumber,+ isTight+ );+ }+ }+ }+}++/**+ * Processes a cloned fragment for list structure preservation.+ * This is the main entry point used by selectedText().+ *+ * @param {HTMLElement} container - Container with cloned selection content+ * @param {Range} range - The original selection range+ */+export function processSelectionFragment(container, range) {+ preserveListStructureInClonedContent(container, range);+ setTightAttributeOnAllLists(container);+}diff --git a/frontend/discourse/app/lib/textarea-text-manipulation.js b/frontend/discourse/app/lib/textarea-text-manipulation.jsindex 2e08219e310..74d8f5b6d52 100644--- a/frontend/discourse/app/lib/textarea-text-manipulation.js+++ b/frontend/discourse/app/lib/textarea-text-manipulation.js@@ -423,7 +423,7 @@ export default class TextareaTextManipulation {}@bind- paste(e) {+ async paste(e) {const isComposer = this.textarea === e.target;if (!isComposer && !isTesting()) {@@ -498,7 +498,9 @@ export default class TextareaTextManipulation {}if (canPasteHtml && !handled) {- let markdown = toMarkdown(html);+ e.preventDefault();++ let markdown = await toMarkdown(html);if (!plainText || plainText.length < markdown.length) {if (isInlinePasting) {@@ -515,6 +517,11 @@ export default class TextareaTextManipulation {: this.insertText(markdown);handled = true;}+ } else if (plainText && isComposer) {+ this.eventPrefix+ ? this.appEvents.trigger(`${this.eventPrefix}:insert-text`, plainText)+ : this.insertText(plainText);+ handled = true;}}diff --git a/frontend/discourse/app/lib/to-markdown.js b/frontend/discourse/app/lib/to-markdown.jsindex 5c43217e190..f1849f7124b 100644--- a/frontend/discourse/app/lib/to-markdown.js+++ b/frontend/discourse/app/lib/to-markdown.js@@ -1,970 +1,98 @@+import deprecated from "discourse/lib/deprecated";import {- buildImageMarkdown,- escapeMarkdownCharacters,- extensionFromUrl,-} from "discourse/lib/markdown-image-builder";+ areDefaultExtensionsRegistered,+ getExtensions,+} from "./composer/rich-editor-extensions";-const MSO_LIST_CLASSES = [- "MsoListParagraphCxSpFirst",- "MsoListParagraphCxSpMiddle",- "MsoListParagraphCxSpLast",-];--let tagDecorateCallbacks = [];-let blockDecorateCallbacks = [];-let textDecorateCallbacks = [];--/**- * Allows to add support for custom inline markdown/bbcode prefixes- * to convert nodes back to bbcode.- *- * ```- * addTagDecorateCallback(function (text) {- * if (this.element.attributes.class === "loud") {- * this.prefix = "^^";- * this.suffix = "^^";- * return text.toLowerCase();- * }- * });- * ```- */-export function addTagDecorateCallback(callback) {- tagDecorateCallbacks.push(callback);-}+async function ensureDefaultExtensions() {+ if (areDefaultExtensionsRegistered()) {+ return;+ }-export function clearTagDecorateCallbacks() {- tagDecorateCallbacks = [];+ // The module's side effect registers the defaults and marks them as registered+ await import("discourse/static/prosemirror/extensions/register-default");}-/**- * Allows to add support for custom block markdown/bbcode prefixes- * to convert nodes back to bbcode.- *- * ```- * addBlockDecorateCallback(function (text) {- * if (this.element.attributes.class === "spoiled") {- * this.prefix = "[spoiler]";- * this.suffix = "[/spoiler]";- * }- * });- * ```- */-export function addBlockDecorateCallback(callback) {- blockDecorateCallbacks.push(callback);+// Deprecated no-ops - kept for backward compatibility+function deprecationWarning(name) {+ deprecated(+ `${name} is deprecated. Use api.registerRichEditorExtension() instead.`,+ {+ id: `discourse.to-markdown.${name}`,+ since: "2026.5.0",+ }+ );}-export function clearBlockDecorateCallbacks() {- blockDecorateCallbacks = [];+export function addTagDecorateCallback() {+ deprecationWarning("addTagDecorateCallback");}--/**- * Allows to add support for custom text node transformations- * based on the next/previous elements.- *- * ```- * addTextDecorateCallback(function (text, nextElement, previousElement) {- * if (- * startRangeOpts &&- * nextElement?.attributes.class?.includes("discourse-local-date") &&- * text === "→"- * ) {- * return "";- * }- * });- * ```- */-export function addTextDecorateCallback(callback) {- textDecorateCallbacks.push(callback);+export function addBlockDecorateCallback() {+ deprecationWarning("addBlockDecorateCallback");}--export function clearTextDecorateCallbacks() {- textDecorateCallbacks = [];+export function addTextDecorateCallback() {+ deprecationWarning("addTextDecorateCallback");}--export class Tag {- static named(name) {- const klass = class NamedTag extends Tag {};- klass.tagName = name;- return klass;- }-- static blocks() {- return [- "address",- "article",- "dd",- "dl",- "dt",- "fieldset",- "figcaption",- "figure",- "footer",- "form",- "header",- "hgroup",- "hr",- "main",- "nav",- "p",- "pre",- "section",- ];- }-- static headings() {- return ["h1", "h2", "h3", "h4", "h5", "h6"];- }-- static emphases() {- return [- ["b", "**"],- ["strong", "**"],- ["i", "*"],- ["em", "*"],- ["s", "~~"],- ["strike", "~~"],- ];- }-- static slices() {- return ["dt", "dd", "thead", "tbody", "tfoot"];- }-- static trimmable() {- return [- ...Tag.blocks(),- ...Tag.headings(),- ...Tag.slices(),- "aside",- "li",- "td",- "th",- "br",- "hr",- "blockquote",- "table",- "ol",- "tr",- "ul",- ];- }-- static allowedTags() {- return [- "ins",- "del",- "small",- "big",- "kbd",- "ruby",- "rt",- "rb",- "rp",- "mark",- ];- }-- static block(name, prefix, suffix) {- return class extends Tag.named(name) {- constructor() {- super(prefix, suffix);- this.gap = "\n\n";- }-- decorate(text) {- const parent = this.element.parent;-- for (const callback of blockDecorateCallbacks) {- const result = callback.call(this, text);-- if (typeof result !== "undefined") {- text = result;- }- }-- if (name === "p" && parent?.name === "li") {- // fix for google docs- this.gap = "";- }-- return `${this.gap}${this.prefix}${text}${this.suffix}${this.gap}`;- }- };- }-- static div() {- return class extends Tag.block("div") {- decorate(text) {- const attr = this.element.attributes;-- if (/\bmathjax-math\b/.test(attr.class)) {- return "";- }-- if (/\bmath\b/.test(attr.class) && attr["data-applied-mathjax"]) {- return "\n$$\n" + text + "\n$$\n";- }-- return super.decorate(text);- }- };- }-- static aside() {- return class extends Tag.block("aside") {- constructor() {- super();- }-- toMarkdown() {- if (!/\bquote\b/.test(this.element.attributes.class)) {- return super.toMarkdown();- }-- const blockquote = this.element.children.find(- (child) => child.name === "blockquote"- );-- if (!blockquote) {- return super.toMarkdown();- }-- let text = Element.parse([blockquote], this.element) || "";- text = text.trim().replaceAll(/^> /gm, "").trim();- if (text.length === 0) {- return "";- }-- const username = this.element.attributes["data-username"];- const post = this.element.attributes["data-post"];- const topic = this.element.attributes["data-topic"];-- const prefix =- username && post && topic- ? `[quote="${username}, post:${post}, topic:${topic}"]`- : "[quote]";-- return `\n${prefix}\n${text}\n[/quote]\n`;- }- };- }-- static heading(name, i) {- const prefix = `${[...Array(i)].map(() => "#").join("")} `;- return Tag.block(name, prefix, "");- }-- static emphasis(name, decorator) {- return class extends Tag.named(name) {- constructor() {- super(decorator, decorator, true);- }-- decorate(text) {- if (text.includes("\n")) {- this.prefix = `<${name}>`;- this.suffix = `</${name}>`;- }-- let space = text.match(/^\s/);- if (space) {- this.prefix = space[0] + this.prefix;- }-- space = text.match(/\s$/);- if (space) {- this.suffix = this.suffix + space[0];- }-- return super.decorate(text.trim());- }- };- }-- static allowedTag(name) {- return class extends Tag.named(name) {- constructor() {- super(`<${name}>`, `</${name}>`);- }- };- }-- static replace(name, text) {- return class extends Tag.named(name) {- constructor() {- super("", "");- this.text = text;- }-- toMarkdown() {- return this.text;- }- };- }-- static span() {- return class extends Tag.named("span") {- constructor() {- super();- }-- decorate(text) {- const attr = this.element.attributes;-- if (attr.class === "badge badge-notification clicks") {- return "";- }-- if (/\bmathjax-math\b/.test(attr.class)) {- return "";- }-- if (/\bmath\b/.test(attr.class) && attr["data-applied-mathjax"]) {- return "$" + text + "$";- }-- return super.decorate(text);- }- };- }-- static link() {- return class extends Tag.named("a") {- constructor() {- super("", "", true);- }-- decorate(text) {- const e = this.element;- const attr = e.attributes;-- if (/^mention/.test(attr.class) && "@" === text[0]) {- return text;- }-- if ("hashtag" === attr.class && "#" === text[0]) {- return text;- }-- if (attr.class?.includes("hashtag-cooked")) {- if (attr["data-ref"]) {- return `#${attr["data-ref"]}`;- } else {- let type = "";- if (attr["data-type"]) {- type = `::${attr["data-type"]}`;- }- return `#${attr["data-slug"]}${type}`;- }- }-- let img;- if (- ["lightbox", "d-lazyload"].includes(attr.class) &&- (img = (e.children || []).find((c) => c.name === "img"))- ) {- let href = attr.href;- const base62SHA1 = img.attributes["data-base62-sha1"];- text = attr.title || "";-- if (base62SHA1) {- href = `upload://${base62SHA1}`;- const extension =- extensionFromUrl(img.attributes.src) ||- extensionFromUrl(attr.href) ||- extensionFromUrl(attr["data-download-href"]);-- if (extension) {- href += `.${extension}`;- }- }-- const width = img.attributes.width;- const height = img.attributes.height;-- if (width && height) {- const pipe = this.element.parentNames.includes("table")- ? "\\|"- : "|";- text = `${text}${pipe}${width}x${height}`;- }-- return ``;- }-- if (attr.href && text !== attr.href) {- text = text.replace(/\n{2,}/g, "\n");-- let linkModifier = "";- if (attr.class?.includes("attachment")) {- text = escapeMarkdownCharacters(text);- linkModifier = "|attachment";- }-- return `[${text}${linkModifier}](${attr.href})`;- }-- return text;- }- };- }-- static image() {- return class extends Tag.named("img") {- constructor() {- super("", "", true);- }-- toMarkdown() {- const e = this.element;- const attr = e.attributes;- const pAttr = e.parent?.attributes || {};- const cssClass = attr.class || pAttr.class;-- let src = attr.src || pAttr.src;-- const base62SHA1 = attr["data-base62-sha1"];- if (base62SHA1) {- src = `upload://${base62SHA1}`;- const extension =- extensionFromUrl(attr.src || pAttr.src) ||- extensionFromUrl(attr["data-orig-src"]);-- if (extension) {- src += `.${extension}`;- }- }-- if (cssClass?.includes("emoji")) {- if (cssClass.includes("user-status")) {- return "";- }-- return attr.title || pAttr.title;- }-- if (src) {- if (src.match(/^data:image\/([a-zA-Z]*);base64,([^\"]*)$/)) {- return "[image]";- }-- const alt = attr.alt || pAttr.alt;- const width = attr.width || pAttr.width;- const height = attr.height || pAttr.height;- const title = attr.title;- const escapeTablePipe = this.element.parentNames.includes("table");-- return buildImageMarkdown({- src,- alt,- width,- height,- title,- escapeTablePipe,- });- }-- return "";- }- };- }-- static slice(name, suffix) {- return class extends Tag.named(name) {- constructor() {- super("", suffix);- }-- decorate(text) {- if (!this.element.next) {- this.suffix = "";- }- return `${text}${this.suffix}`;- }- };- }-- static cell(name) {- return class extends Tag.named(name) {- constructor() {- super("|");- }-- toMarkdown() {- const text = this.element.innerMarkdown().trim();-- if (text.includes("\n")) {- // Unsupported format inside Markdown table cells- let e = this.element;- while ((e = e.parent)) {- if (e.name === "table") {- e.tag().invalid();- break;- }- }- }-- return this.decorate(text);- }- };- }-- static li() {- return class extends Tag.slice("li", "\n") {- decorate(text) {- const attrs = this.element.attributes;- let indent = this.element- .filterParentNames(["ol", "ul"])- .slice(1)- .map(() => "\t")- .join("");-- if (MSO_LIST_CLASSES.includes(attrs.class)) {- try {- const level = parseInt(- attrs.style.match(/level./)[0].replace("level", ""),- 10- );- indent = Array(level).join("\t") + indent;- } finally {- if (attrs.class === "MsoListParagraphCxSpFirst") {- indent = `\n\n${indent}`;- } else if (attrs.class === "MsoListParagraphCxSpLast") {- text = `${text}\n`;- }- }- }-- return super.decorate(`${indent}* ${text.trimStart()}`);- }- };- }-- static code() {- return class extends Tag.named("code") {- constructor() {- super("`", "`");- }-- decorate(text) {- if (this.element.parentNames.includes("pre")) {- this.prefix = "\n\n```\n";- this.suffix = "\n```\n\n";- } else {- this.inline = true;- }-- const textarea = document.createElement("textarea");- textarea.innerHTML = text;- return super.decorate(textarea.innerText);- }- };- }-- static blockquote() {- return class extends Tag.named("blockquote") {- constructor() {- super("\n> ", "\n");- }-- decorate(text) {- text = text- .trim()- .replace(/\n{2,}>/g, "\n>")- .replace(/\n/g, "\n> ");- return super.decorate(text);- }- };- }-- static table() {- return class extends Tag.block("table") {- constructor() {- super();- this.isValid = true;- }-- invalid() {- this.isValid = false;- if (this.element.parentNames.includes("table")) {- let e = this.element;- while ((e = e.parent)) {- if (e.name === "table") {- e.tag().invalid();- break;- }- }- }- }-- countPipes(text) {- return (text.replace(/\\\|/, "").match(/\|/g) || []).length;- }-- decorate(text) {- text = super.decorate(text).replace(/\|\n{2,}\|/g, "|\n|");- const rows = text.trim().split("\n");- const pipeCount = this.countPipes(rows[0]);- this.isValid =- this.isValid &&- rows.length > 1 &&- pipeCount > 2 &&- rows.reduce((a, c) => a && this.countPipes(c) <= pipeCount); // Unsupported table format for Markdown conversion-- if (this.isValid) {- const splitterRow =- [...Array(pipeCount - 1)].map(() => "| --- ").join("") + "|\n";- text = text.replace("|\n", "|\n" + splitterRow);- } else {- text = text.replace(/\|/g, " ");- this.invalid();- }-- return text;- }- };- }-- static list(name) {- return class extends Tag.block(name) {- decorate(text) {- let smallGap = "";- const parent = this.element.parent;-- if (parent?.name === "ul") {- this.gap = "";- this.suffix = "\n";- }-- if (this.element.filterParentNames(["li"]).length) {- this.gap = "";- smallGap = "\n";- }-- return smallGap + super.decorate(text.trimEnd());- }- };- }-- static ol() {- return class extends Tag.list("ol") {- decorate(text) {- text = "\n" + text;- const bullet = text.match(/\n\t*\*/)[0];- let i = parseInt(this.element.attributes.start || 1, 10);-- while (text.includes(bullet)) {- text = text.replace(bullet, bullet.replace("*", `${i}.`));- i++;- }-- return super.decorate(text.slice(1));- }- };- }-- static tr() {- return class extends Tag.slice("tr", "|\n") {- decorate(text) {- if (!this.element.next) {- this.suffix = "|";- }- return `${text}${this.suffix}`;- }- };- }-- constructor(prefix = "", suffix = "", inline = false) {- this.prefix = prefix;- this.suffix = suffix;- this.inline = inline;- }-- decorate(text) {- for (const callback of tagDecorateCallbacks) {- const result = callback.call(this, text);-- if (typeof result !== "undefined") {- text = result;- }- }-- if (this.prefix || this.suffix) {- text = [this.prefix, text, this.suffix].join("");- }-- if (this.inline) {- const { prev, next } = this.element;-- if (prev && prev.name !== "#text") {- text = " " + text;- }-- if (next && next.name !== "#text") {- text = text + " ";- }- }-- return text;- }-- toMarkdown() {- const text = this.element.innerMarkdown();-- if (text?.trim()) {- return this.decorate(text);- }-- return text;- }-}--let tagsMap;--function tagByName(name) {- if (!tagsMap) {- tagsMap = new Map();-- const allTags = [- ...Tag.blocks().map((b) => Tag.block(b)),- ...Tag.headings().map((h, i) => Tag.heading(h, i + 1)),- ...Tag.slices().map((s) => Tag.slice(s, "\n")),- ...Tag.emphases().map((e) => Tag.emphasis(e[0], e[1])),- ...Tag.allowedTags().map((t) => Tag.allowedTag(t)),- Tag.aside(),- Tag.cell("td"),- Tag.cell("th"),- Tag.replace("br", "\n"),- Tag.replace("hr", "\n---\n"),- Tag.replace("head", ""),- Tag.li(),- Tag.link(),- Tag.image(),- Tag.code(),- Tag.blockquote(),- Tag.table(),- Tag.tr(),- Tag.ol(),- Tag.list("ul"),- Tag.span(),- Tag.div(),- ];-- for (const tag of allTags) {- tagsMap.set(tag.tagName, tag);- }- }-- return tagsMap.get(name);+export function clearTagDecorateCallbacks() {+ deprecationWarning("clearTagDecorateCallbacks");}--class Element {- static toMarkdown(element, parent, prev, next, metadata) {- return new Element(element, parent, prev, next, metadata).toMarkdown();- }-- static parseChildren(parent) {- return Element.parse(parent.children, parent);- }-- static parse(elements, parent = null) {- if (elements) {- let result = [];- let metadata = {};-- for (let i = 0; i < elements.length; i++) {- const prev = i === 0 ? null : elements[i - 1];- const next = i === elements.length ? null : elements[i + 1];-- result.push(- Element.toMarkdown(elements[i], parent, prev, next, metadata)- );- }-- return result.join("");- }-- return "";- }-- constructor(element, parent, previous, next, metadata) {- this.name = element.name;- this.data = element.data;- this.metadata = metadata;- this.children = element.children;- this.attributes = element.attributes;-- if (parent) {- this.parent = parent;- this.parentNames = parent.parentNames.slice();- this.parentNames.push(parent.name);- }-- this.parentNames = this.parentNames || [];- this.previous = previous;- this.next = next;-- if (this.name === "p" && MSO_LIST_CLASSES.includes(this.attributes.class)) {- this.name = "li";- this.parentNames.push("ul");- }- }-- tag() {- const tag = new (tagByName(this.name) || Tag)();- tag.element = this;- tag.metadata = this.metadata;- return tag;- }-- innerMarkdown() {- return Element.parseChildren(this);- }-- leftTrimmable() {- return this.previous && Tag.trimmable().includes(this.previous.name);- }-- rightTrimmable() {- return this.next && Tag.trimmable().includes(this.next.name);- }-- text() {- let text = this.data || "";-- if (this.leftTrimmable()) {- text = text.trimStart();- }-- if (this.rightTrimmable()) {- text = text.trimEnd();- }-- text = text.replace(/[\s\t]+/g, " ");- textDecorateCallbacks.forEach((callback) => {- const result = callback.call(- this,- text,- this.next,- this.previous,- this.metadata- );-- if (typeof result !== "undefined") {- text = result;- }- });-- return text;- }-- toMarkdown() {- return this.name === "#text" ? this.text() : this.tag().toMarkdown();- }-- filterParentNames(names) {- return this.parentNames.filter((p) => names.includes(p));- }+export function clearBlockDecorateCallbacks() {+ deprecationWarning("clearBlockDecorateCallbacks");}--function trimUnwanted(html) {- const body = html.match(/<body[^>]*>([\s\S]*?)<\/body>/);- html = body ? body[1] : html;- html = html.replace(/\r|\n| /g, " ");- html = html.replace(/\u00A0/g, " "); // trim no-break space-- let match;- while ((match = html.match(/<[^\s>]+[^>]*>\s{2,}<[^\s>]+[^>]*>/))) {- html = html.replace(match[0], match[0].replace(/>\s{2,}</, "> <"));- }-- html = html.replace(/<!\[if !?\S*]>[^!]*<!\[endif]>/g, ""); // to support ms word list tags-- return html;+export function clearTextDecorateCallbacks() {+ deprecationWarning("clearTextDecorateCallbacks");}-function putPlaceholders(html) {- const codeRegEx = /<code[^>]*>([\s\S]*?)<\/code>/gi;- const origHtml = html;- let match = codeRegEx.exec(origHtml);- let placeholders = [];-- while (match) {- const placeholder = `DISCOURSE_PLACEHOLDER_${placeholders.length + 1}`;- const element = document.createElement("div");- element.innerHTML = match[1];-- const code = element.innerText.replace(/^\n/, "").replace(/\n$/, "");- placeholders.push([placeholder, code]);- html = html.replace(match[0], `<code>${placeholder}</code>`);- match = codeRegEx.exec(origHtml);- }-- const transformNode = (node) => {- if (node.nodeName !== "#text" && node.length !== undefined) {- const ret = [];- for (let i = 0; i < node.length; ++i) {- if (node[i].nodeName !== "#comment") {- ret.push(transformNode(node[i]));+export default async function toMarkdown(html) {+ try {+ await ensureDefaultExtensions();++ const [+ { DOMParser: ProseMirrorDOMParser },+ { createSchema },+ { default: Serializer },+ { transformWordListsHtml },+ { isBoundary },+ ] = await Promise.all([+ import("prosemirror-model"),+ import("discourse/static/prosemirror/core/schema"),+ import("discourse/static/prosemirror/core/serializer"),+ import("discourse/static/prosemirror/extensions/word-paste"),+ import("discourse/static/prosemirror/lib/plugin-utils"),+ ]);++ const extensions = getExtensions();+ const schema = createSchema(extensions);+ const domParser = ProseMirrorDOMParser.fromSchema(schema);+ const pluginParams = { utils: { isBoundary } };+ const serializer = new Serializer(extensions, pluginParams);++ const processedHtml = transformWordListsHtml(html);+ const parsedDoc = new DOMParser().parseFromString(+ processedHtml,+ "text/html"+ );++ for (const ext of extensions) {+ if (typeof ext.transformParsedHTML === "function") {+ try {+ ext.transformParsedHTML(parsedDoc);+ } catch (e) {+ // eslint-disable-next-line no-console+ console.warn(+ `toMarkdown: transformParsedHTML failed for extension ${ext.name || ext.id || "(unknown)"}`,+ e+ );}}- return ret;- }-- const ret = {- name: node.nodeName.toLowerCase(),- data: node.data,- children: [],- attributes: {},- };-- if (node.nodeName === "#text") {- return ret;- }-- for (let i = 0; i < node.childNodes.length; ++i) {- if (node.childNodes[i].nodeName !== "#comment") {- ret.children.push(transformNode(node.childNodes[i]));- }- }-- for (let i = 0; i < node.attributes.length; ++i) {- ret.attributes[node.attributes[i].name] = node.attributes[i].value;}- return ret;- };+ const doc = domParser.parse(parsedDoc);- const template = document.createElement("template");- template.innerHTML = trimUnwanted(html);- const elements = transformNode(template.content.childNodes);-- return { elements, placeholders };-}--function replacePlaceholders(markdown, placeholders) {- placeholders.forEach((p) => {- markdown = markdown.replace(p[0], p[1]);- });- return markdown;-}--export default function toMarkdown(html) {- try {- const { elements, placeholders } = putPlaceholders(html);- let markdown = Element.parse(elements).trim();- markdown = markdown- .replace(/^<b>/, "")- .replace(/<\/b>$/, "")- .trim(); // fix for google doc copy paste- markdown = markdown- .replace(/\n +/g, "\n")- .replace(/ +\n/g, "\n")- .replace(/ {2,}/g, " ")- .replace(/\n{3,}/g, "\n\n")- .replace(/\t/g, " ");- return replacePlaceholders(markdown, placeholders);- } catch {+ return serializer.convert(doc).trim();+ } catch (e) {+ // eslint-disable-next-line no-console+ console.error("toMarkdown failed:", e);return "";}}diff --git a/frontend/discourse/app/lib/utilities.js b/frontend/discourse/app/lib/utilities.jsindex 091e66fdf8b..768dad5552c 100644--- a/frontend/discourse/app/lib/utilities.js+++ b/frontend/discourse/app/lib/utilities.js@@ -4,6 +4,7 @@ import * as AvatarUtils from "discourse/lib/avatar-utils";import deprecated from "discourse/lib/deprecated";import escape from "discourse/lib/escape";import getURL from "discourse/lib/get-url";+import { processSelectionFragment } from "discourse/lib/selection/preserve-list-structure";import { parseAsync } from "discourse/lib/text";import toMarkdown from "discourse/lib/to-markdown";import { capabilities } from "discourse/services/capabilities";@@ -113,7 +114,50 @@ export function extractDomainFromUrl(url) {return url.split(":")[0];}-export function selectedText() {+// Whether everything before `node` in its parent is insignificant whitespace,+// i.e. `node` is effectively the first thing in its parent. (Cooked HTML has+// whitespace text nodes between blocks, so a strict firstChild check fails.)+function isFirstMeaningfulChild(node) {+ for (let sibling = node.previousSibling; sibling; ) {+ if (sibling.nodeType !== Node.TEXT_NODE || sibling.textContent.trim()) {+ return false;+ }+ sibling = sibling.previousSibling;+ }+ return true;+}++// A triple-click (and similar) leaves the selection's end at the very start of+// the following block (endOffset 0). cloneContents() would then materialize+// that block as an empty husk (e.g. <blockquote><p></p></blockquote>) that+// serializes to stray markdown like a lone "> ". Pull the end back past the+// husk — to the topmost ancestor it starts — so it is never cloned.+// Returns the original range when there's nothing to trim.+function rangeWithoutTrailingHusk(range) {+ if (range.endOffset !== 0) {+ return range;+ }++ const common = range.commonAncestorContainer;+ let boundary = range.endContainer;+ while (+ boundary !== common &&+ boundary.parentNode !== common &&+ isFirstMeaningfulChild(boundary)+ ) {+ boundary = boundary.parentNode;+ }++ if (boundary === common || !boundary.parentNode) {+ return range;+ }++ const trimmed = range.cloneRange();+ trimmed.setEndBefore(boundary);+ return trimmed;+}++export function selectedHTML() {const selection = window.getSelection();if (selection.isCollapsed) {return "";@@ -146,7 +190,11 @@ export function selectedText() {// Treat it as though the entire onebox was quoted.div.append(oneboxTest.dataset.oneboxSrc);} else {- div.append(range.cloneContents());+ const effectiveRange = rangeWithoutTrailingHusk(range);+ const fragmentContainer = document.createElement("div");+ fragmentContainer.append(effectiveRange.cloneContents());+ processSelectionFragment(fragmentContainer, effectiveRange);+ div.append(...fragmentContainer.childNodes);}}@@ -166,7 +214,16 @@ export function selectedText() {}});- return toMarkdown(div.outerHTML);+ return div.outerHTML;+}++export async function selectedText() {+ const html = selectedHTML();+ if (!html) {+ return "";+ }++ return await toMarkdown(html);}export function selectedNode() {diff --git a/frontend/discourse/app/static/prosemirror/extensions/bullet-list.js b/frontend/discourse/app/static/prosemirror/extensions/bullet-list.jsindex 193a401161f..c4569c3bad4 100644--- a/frontend/discourse/app/static/prosemirror/extensions/bullet-list.js+++ b/frontend/discourse/app/static/prosemirror/extensions/bullet-list.js@@ -1,4 +1,5 @@import { schema } from "prosemirror-markdown";+import { isListTight } from "discourse/lib/list-utils";/** @type {RichEditorExtension} */const extension = {@@ -6,8 +7,17 @@ const extension = {bullet_list: {...schema.nodes.bullet_list.spec,- // All we are doing here is overriding the tight list default to `true`.attrs: { tight: { default: true } },+ parseDOM: [+ {+ tag: "ul",+ getAttrs(dom) {+ return {+ tight: dom.hasAttribute("data-tight") || isListTight(dom),+ };+ },+ },+ ],},},};diff --git a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js b/frontend/discourse/app/static/prosemirror/extensions/hashtag.jsindex 96517d8e802..ae4957f6599 100644--- a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js+++ b/frontend/discourse/app/static/prosemirror/extensions/hashtag.js@@ -21,12 +21,23 @@ const extension = {selectable: false,parseDOM: [{- tag: "a.hashtag-cooked",+ tag: "a.hashtag-cooked, a.hashtag",+ priority: 60,preserveWhitespace: "full",getAttrs: (dom) => {+ let name = dom.dataset.name ?? dom.dataset.ref;++ if (!name && dom.dataset.slug) {+ name = `${dom.dataset.slug}${+ dom.dataset.type ? `::${dom.dataset.type}` : ""+ }`;+ }++ name ??= dom.textContent.slice(1);+return {- name: dom.getAttribute("data-name"),- processed: dom.getAttribute("data-processed"),+ name,+ processed: dom.dataset.processed,};},},diff --git a/frontend/discourse/app/static/prosemirror/extensions/image.js b/frontend/discourse/app/static/prosemirror/extensions/image.jsindex 6b18ee391a6..6dd764363bb 100644--- a/frontend/discourse/app/static/prosemirror/extensions/image.js+++ b/frontend/discourse/app/static/prosemirror/extensions/image.js@@ -13,6 +13,15 @@ import { getChangedRanges } from "../lib/plugin-utils";const PLACEHOLDER_IMG = "/images/transparent.png";+function extractFileExtension(url) {+ return url?.match(/\.([a-z0-9]+)(?:\?|$)/i)?.[1] || "";+}++function buildUploadShortUrl(base62Sha1, url) {+ const ext = extractFileExtension(url);+ return `upload://${base62Sha1}${ext ? `.${ext}` : ""}`;+}+const ALT_TEXT_REGEX =/^(.*?)(?:\|(\d{1,4}x\d{1,4}))?(?:,\s*(\d{1,3})%)?(?:\|(.*))?$/;@@ -45,21 +54,51 @@ const extension = {group: "inline",draggable: true,parseDOM: [+ {+ priority: 70,+ tag: "a.lightbox",+ getAttrs(dom) {+ const img = dom.querySelector("img");+ if (!img) {+ return false;+ }++ const href = dom.getAttribute("href");+ const title = dom.getAttribute("title") || img.getAttribute("alt");+ const base62Sha1 = img.dataset.base62Sha1;++ const originalSrc = base62Sha1+ ? buildUploadShortUrl(base62Sha1, href)+ : href;++ return {+ src: img.getAttribute("src"),+ originalSrc,+ alt: title,+ width: img.getAttribute("width"),+ height: img.getAttribute("height"),+ };+ },+ },{tag: "img[src]",getAttrs(dom) {+ const src = dom.getAttribute("src");+ if (!src || dom.classList.contains("avatar")) {+ return false;+ }+const originalSrc =- dom.dataset.origSrc ??- (dom.dataset.base62Sha1- ? `upload://${dom.dataset.base62Sha1}`- : undefined);+ dom.dataset.origSrc ||+ (dom.dataset.base62Sha1 &&+ buildUploadShortUrl(dom.dataset.base62Sha1, src));const extras = dom.hasAttribute("data-thumbnail")? "thumbnail": undefined;return {- src: dom.getAttribute("src"),+ src,title: dom.getAttribute("title")?.replace(/\n/g, " "),alt: dom.getAttribute("alt")?.replace(/\n/g, " "),width: dom.getAttribute("width"),@@ -152,6 +191,13 @@ const extension = {return;}+ const src = node.attrs.originalSrc ?? node.attrs.src ?? "";++ if (src.startsWith("data:")) {+ state.write("[image]");+ return;+ }+const alt = (node.attrs.alt || "").replace(/([\\[\]`])/g, "\\$1");const scale = node.attrs.scale ? `, ${node.attrs.scale}%` : "";const dimensions =@@ -159,7 +205,6 @@ const extension = {? `|${node.attrs.width}x${node.attrs.height}${scale}`: "";const extras = node.attrs.extras ? `|${node.attrs.extras}` : "";- const src = node.attrs.originalSrc ?? node.attrs.src ?? "";const escapedSrc = src.replace(/[\(\)]/g, "\\$&");const title = node.attrs.title? ' "' + node.attrs.title.replace(/"/g, '\\"') + '"'diff --git a/frontend/discourse/app/static/prosemirror/extensions/link.js b/frontend/discourse/app/static/prosemirror/extensions/link.jsindex ff3fa9d750c..8f679fcaef1 100644--- a/frontend/discourse/app/static/prosemirror/extensions/link.js+++ b/frontend/discourse/app/static/prosemirror/extensions/link.js@@ -22,8 +22,12 @@ const extension = {inclusive: false,parseDOM: [{- tag: "a[href]",+ tag: "a[href]:not(.lightbox)",getAttrs(dom) {+ if (dom.textContent.trim() === dom.getAttribute("href")) {+ return false;+ }+return {href: dom.getAttribute("href"),title: dom.getAttribute("title"),diff --git a/frontend/discourse/app/static/prosemirror/extensions/mention.js b/frontend/discourse/app/static/prosemirror/extensions/mention.jsindex d5d300283c5..a3245881ecd 100644--- a/frontend/discourse/app/static/prosemirror/extensions/mention.js+++ b/frontend/discourse/app/static/prosemirror/extensions/mention.js@@ -26,12 +26,18 @@ const extension = {parseDOM: [{priority: 60,- tag: "a.mention",+ tag: "a.mention, a.mention-group",preserveWhitespace: "full",getAttrs: (dom) => {- return {- name: dom.getAttribute("data-name") ?? dom.textContent.slice(1),- };+ let name = dom.getAttribute("data-name");+ if (!name) {+ const clone = dom.cloneNode(true);+ clone+ .querySelectorAll("img.user-status")+ .forEach((el) => el.remove());+ name = clone.textContent.trim().slice(1);+ }+ return { name };},},],diff --git a/frontend/discourse/app/static/prosemirror/extensions/ordered-list.js b/frontend/discourse/app/static/prosemirror/extensions/ordered-list.jsindex 9839884d366..3520bc2a494 100644--- a/frontend/discourse/app/static/prosemirror/extensions/ordered-list.js+++ b/frontend/discourse/app/static/prosemirror/extensions/ordered-list.js@@ -1,4 +1,5 @@import { schema } from "prosemirror-markdown";+import { isListTight } from "discourse/lib/list-utils";/** @type {RichEditorExtension} */const extension = {@@ -6,8 +7,18 @@ const extension = {ordered_list: {...schema.nodes.ordered_list.spec,- // All we are doing here is overriding the tight list default to `true`.attrs: { order: { default: 1 }, tight: { default: true } },+ parseDOM: [+ {+ tag: "ol",+ getAttrs(dom) {+ return {+ order: dom.hasAttribute("start") ? +dom.getAttribute("start") : 1,+ tight: dom.hasAttribute("data-tight") || isListTight(dom),+ };+ },+ },+ ],},},};diff --git a/frontend/discourse/app/static/prosemirror/extensions/quote.js b/frontend/discourse/app/static/prosemirror/extensions/quote.jsindex 5968006463a..6f0fd7fba12 100644--- a/frontend/discourse/app/static/prosemirror/extensions/quote.js+++ b/frontend/discourse/app/static/prosemirror/extensions/quote.js@@ -40,7 +40,7 @@ const extension = {attrs["data-username"] = username;attrs["data-post"] = postNumber;attrs["data-topic"] = topicId;- attrs["data-full"] = full ? "true" : "false";+ attrs["data-full"] = full === "true" ? "true" : "false";if (displayName) {attrs["data-display-name"] = displayName;}@@ -104,14 +104,15 @@ const extension = {serializeNode: {quote(state, node) {- const { username, displayName, postNumber, topicId } = node.attrs;+ const { username, displayName, postNumber, topicId, full } = node.attrs;const postNumberParam = postNumber ? `, post:${postNumber}` : "";const topicIdParam = topicId ? `, topic:${topicId}` : "";+ const fullParam = full === "true" ? `, full:true` : "";const usernameParam =displayName && username ? `, username:${username}` : "";const name = displayName || username;const quoteValue = name- ? `="${name}${postNumberParam}${topicIdParam}${usernameParam}"`+ ? `="${name}${postNumberParam}${topicIdParam}${fullParam}${usernameParam}"`: "";state.write(`[quote${quoteValue}]\n`);diff --git a/frontend/discourse/app/static/prosemirror/extensions/register-default.js b/frontend/discourse/app/static/prosemirror/extensions/register-default.jsindex f624bd1c797..1a1875b33bd 100644--- a/frontend/discourse/app/static/prosemirror/extensions/register-default.js+++ b/frontend/discourse/app/static/prosemirror/extensions/register-default.js@@ -1,6 +1,9 @@// @ts-check-import { registerRichEditorExtension } from "discourse/lib/composer/rich-editor-extensions";+import {+ markDefaultExtensionsRegistered,+ registerRichEditorExtension,+} from "discourse/lib/composer/rich-editor-extensions";import bulletList from "./bullet-list";import code from "./code";import codeBlock from "./code-block";@@ -28,6 +31,7 @@ import trailingParagraph from "./trailing-paragraph";import typographerReplacements from "./typographer-replacements";import underline from "./underline";import uploadPlaceholder from "./upload-placeholder";+import wordPaste from "./word-paste";import wrap from "./wrap";/**@@ -57,6 +61,7 @@ const defaultExtensions = [typographerReplacements,table,markdownPaste,+ wordPaste,orderedList,bulletList,wrap,@@ -68,5 +73,6 @@ const defaultExtensions = [];defaultExtensions.forEach(registerRichEditorExtension);+markDefaultExtensionsRegistered();export default defaultExtensions;diff --git a/frontend/discourse/app/static/prosemirror/extensions/strikethrough.js b/frontend/discourse/app/static/prosemirror/extensions/strikethrough.jsindex 14b1f0c4419..3666e991ddd 100644--- a/frontend/discourse/app/static/prosemirror/extensions/strikethrough.js+++ b/frontend/discourse/app/static/prosemirror/extensions/strikethrough.js@@ -8,6 +8,7 @@ const extension = {parseDOM: [{ tag: "s" },{ tag: "del" },+ { tag: "strike" },{getAttrs: (value) =>/(^|[\s])line-through([\s]|$)/u.test(value) && null,diff --git a/frontend/discourse/app/static/prosemirror/extensions/table.js b/frontend/discourse/app/static/prosemirror/extensions/table.jsindex cfb23b75a29..4d03543702d 100644--- a/frontend/discourse/app/static/prosemirror/extensions/table.js+++ b/frontend/discourse/app/static/prosemirror/extensions/table.js@@ -135,7 +135,13 @@ const extension = {table(state, node) {state.flushClose(1);- let headerBuffer = state.delim;+ // Normalize table structure+ node = normalizeTable(node);++ if (!node.textContent.trim()) {+ return;+ }+const prevInTable = state.inTable;state.inTable = true;@@ -145,8 +151,19 @@ const extension = {}// group is table_head or table_body- node.forEach((group, groupOffset, groupIndex) => {- group.forEach((row) => {+ let isFirstRow = true;+ node.forEach((group) => {+ const isHead = group.type.name === "table_head";++ group.forEach((row, rowOffset, rowIndex) => {+ const shouldTreatAsHeader =+ isHead ||+ (isFirstRow &&+ rowIndex === 0 &&+ row.childCount > 0 &&+ row.firstChild?.type.name === "table_header_cell");+ let headerBuffer = shouldTreatAsHeader ? state.delim : undefined;+row.forEach((cell, cellOffset, cellIndex) => {if (state.delim && state.atBlank()) {state.out += state.delim;@@ -155,8 +172,7 @@ const extension = {state.renderInline(cell);- // if table_head- if (groupIndex === 0) {+ if (headerBuffer !== undefined) {if (cell.attrs.alignment === "center") {headerBuffer += "|:---:";} else if (cell.attrs.alignment === "left") {@@ -171,11 +187,11 @@ const extension = {state.out += " |\n";- if (headerBuffer) {+ if (headerBuffer !== undefined) {state.out += `${headerBuffer}|\n`;- headerBuffer = undefined;}});+ isFirstRow = false;});state.out += "\n";state.inTable = prevInTable;@@ -187,90 +203,27 @@ const extension = {table_cell() {},},plugins({ pmState: { Plugin }, pmModel: { Slice, Fragment } }) {- function findMaxColumns(tbody) {- let maxColumns = 0;- tbody.forEach((row) => {- maxColumns = Math.max(maxColumns, row.childCount);- });- return maxColumns;- }-- function createHeaderRow(firstRow, maxColumns, schema) {- const headerCells = [];- for (let i = 0; i < maxColumns; i++) {- if (i < firstRow.childCount) {- const cell = firstRow.child(i);- headerCells.push(- schema.nodes.table_header_cell.create(cell.attrs, cell.content)- );- } else {- headerCells.push(schema.nodes.table_header_cell.create());- }- }- return schema.nodes.table_row.create({}, headerCells);- }-- function createBodyRows(tbody, maxColumns, schema) {- const bodyRows = [];- tbody.content.content.slice(1).forEach((row) => {- const cells = [];- for (let i = 0; i < maxColumns; i++) {- if (i < row.childCount) {- cells.push(row.child(i));- } else {- cells.push(schema.nodes.table_cell.create());- }- }- bodyRows.push(schema.nodes.table_row.create({}, cells));- });- return bodyRows;- }-- function normalizeTable(tableNode, schema) {- let tbody, thead;- tableNode.descendants((node) => {- if (node.type.name === "table_body") {- tbody = node;- return false;- }- if (node.type.name === "table_head") {- thead = node;+ function hasTableNodes(fragment) {+ let found = false;+ fragment.descendants((node) => {+ if (node.type.name === "table") {+ found = true;return false;}});-- if (thead || !tbody) {- return tableNode;- }-- const maxColumns = findMaxColumns(tbody);- const firstRow = tbody.firstChild;-- if (!firstRow || maxColumns === 0) {- return tableNode;- }-- const header = schema.nodes.table_head.create(- {},- createHeaderRow(firstRow, maxColumns, schema)- );-- const body = schema.nodes.table_body.create(- {},- createBodyRows(tbody, maxColumns, schema)- );-- return schema.nodes.table.create({}, [header, body]);+ return found;}return new Plugin({props: {- transformPasted(paste, view) {- const schema = view.state.schema;+ transformPasted(paste) {+ if (!hasTableNodes(paste.content)) {+ return paste;+ }function transformNode(node) {if (node.type.name === "table") {- return normalizeTable(node, schema);+ return normalizeTable(node);}if (node.content?.size > 0) {@@ -294,4 +247,79 @@ const extension = {},};+function findMaxColumns(tbody) {+ let maxColumns = 0;+ tbody.forEach((row) => {+ maxColumns = Math.max(maxColumns, row.childCount);+ });+ return maxColumns;+}++function createHeaderRow(firstRow, maxColumns, schema) {+ const headerCells = [];+ for (let i = 0; i < maxColumns; i++) {+ if (i < firstRow.childCount) {+ const cell = firstRow.child(i);+ headerCells.push(+ schema.nodes.table_header_cell.create(cell.attrs, cell.content)+ );+ } else {+ headerCells.push(schema.nodes.table_header_cell.create());+ }+ }+ return schema.nodes.table_row.create({}, headerCells);+}++function createBodyRows(tbody, startIndex, maxColumns, schema) {+ const bodyRows = [];+ tbody.content.content.slice(startIndex).forEach((row) => {+ const cells = [];+ for (let i = 0; i < maxColumns; i++) {+ if (i < row.childCount) {+ cells.push(row.child(i));+ } else {+ cells.push(schema.nodes.table_cell.create());+ }+ }+ bodyRows.push(schema.nodes.table_row.create({}, cells));+ });+ return bodyRows;+}++function normalizeTable(tableNode) {+ const schema = tableNode.type.schema;+ let tbody, thead;++ tableNode.descendants((node) => {+ if (node.type.name === "table_body") {+ tbody = node;+ return false;+ }+ if (node.type.name === "table_head") {+ thead = node;+ return false;+ }+ });++ if (thead || !tbody) {+ return tableNode;+ }++ const maxColumns = findMaxColumns(tbody);+ const firstRow = tbody.firstChild;++ if (!firstRow || maxColumns === 0) {+ return tableNode;+ }++ const header = schema.nodes.table_head.create(+ {},+ createHeaderRow(firstRow, maxColumns, schema)+ );++ const bodyRows = createBodyRows(tbody, 1, maxColumns, schema);+ const body = schema.nodes.table_body.create({}, bodyRows);+ return schema.nodes.table.create({}, [header, body]);+}+export default extension;diff --git a/frontend/discourse/app/static/prosemirror/extensions/word-paste.js b/frontend/discourse/app/static/prosemirror/extensions/word-paste.jsnew file mode 100644index 00000000000..793d00ebab6--- /dev/null+++ b/frontend/discourse/app/static/prosemirror/extensions/word-paste.js@@ -0,0 +1,241 @@+const MSO_LIST_CLASSES = [+ "MsoListParagraphCxSpFirst",+ "MsoListParagraphCxSpMiddle",+ "MsoListParagraphCxSpLast",+];++/**+ * Extracts the list level from a Word list paragraph's style attribute.+ * Word uses styles like "mso-list:l0 level2 lfo1" to indicate nesting.+ *+ * @param {Element} element - The paragraph element+ * @returns {number} The list level (1-based)+ */+function extractListLevel(element) {+ const style = element.getAttribute("style") || "";+ const match = style.match(/level(\d+)/);+ return match ? parseInt(match[1], 10) : 1;+}++/**+ * Detects if a Word list paragraph represents an ordered (numbered) list.+ * Inspects the marker span content before deletion to determine list type.+ *+ * @param {Element} element - The paragraph element+ * @returns {{isOrdered: boolean, startNumber: number}} List type info+ */+function detectListType(element) {+ // Look for the marker span with mso-list:Ignore+ const markerSpan = element.querySelector('[style*="mso-list:Ignore"]');+ if (markerSpan) {+ const markerText = markerSpan.textContent.trim();+ // Check for numbered patterns: "1.", "1)", "a.", "a)", "i.", "I.", etc.+ // Ordered list markers typically end with . or ) and start with number or letter+ const numberedMatch = markerText.match(/^(\d+)[.)]/);+ if (numberedMatch) {+ return { isOrdered: true, startNumber: parseInt(numberedMatch[1], 10) };+ }+ // Letter-based ordered lists (a., b., A., B.)+ if (/^[a-zA-Z][.)]/.test(markerText)) {+ return { isOrdered: true, startNumber: 1 };+ }+ // Roman numerals, including larger numerals (i., ii., L., C., D., M.)+ if (/^[ivxlcdmIVXLCDM]+[.)]/.test(markerText)) {+ return { isOrdered: true, startNumber: 1 };+ }+ }++ // Check IE conditional comments for markers+ const html = element.innerHTML;+ const conditionalMatch = html.match(+ /<!\[if !supportLists\]>([\s\S]*?)<!\[endif\]>/i+ );+ if (conditionalMatch) {+ const markerText = conditionalMatch[1].replace(/<[^>]*>/g, "").trim();+ const numberedMatch = markerText.match(/^(\d+)[.)]/);+ if (numberedMatch) {+ return { isOrdered: true, startNumber: parseInt(numberedMatch[1], 10) };+ }+ if (/^[a-zA-Z][.)]/.test(markerText)) {+ return { isOrdered: true, startNumber: 1 };+ }+ if (/^[ivxlcdmIVXLCDM]+[.)]/.test(markerText)) {+ return { isOrdered: true, startNumber: 1 };+ }+ }++ // Default to unordered (bullet) list+ return { isOrdered: false, startNumber: 1 };+}++/**+ * Extracts the text content from a Word list paragraph, removing+ * the bullet/number markers that Word includes.+ *+ * @param {Element} element - The paragraph element+ * @returns {string} The cleaned HTML content+ */+function extractListItemContent(element) {+ const clone = element.cloneNode(true);++ // Remove the list marker spans (mso-list:Ignore)+ clone.querySelectorAll('[style*="mso-list:Ignore"]').forEach((el) => {+ el.remove();+ });++ // Remove conditional comments content+ // Word wraps markers in <!--[if !supportLists]-->...<!--[endif]-->+ let html = clone.innerHTML;++ // Remove IE conditional comments+ html = html.replace(/<!\[if !supportLists\]>[\s\S]*?<!\[endif\]>/gi, "");++ // Clean up any remaining empty spans and normalize whitespace+ const temp = document.createElement("div");+ temp.innerHTML = html;++ // Remove empty elements+ temp.querySelectorAll("span, o\\:p").forEach((el) => {+ if (!el.textContent.trim() && !el.querySelector("img")) {+ el.remove();+ }+ });++ return temp.innerHTML.trim();+}++/**+ * Transforms Word list paragraphs into proper HTML list structure.+ * This function processes the DOM in place.+ *+ * @param {Document|Element} container - The container to process+ */+export function transformWordLists(container) {+ const paragraphs = Array.from(+ container.querySelectorAll(MSO_LIST_CLASSES.map((c) => `p.${c}`).join(", "))+ );++ if (paragraphs.length === 0) {+ return;+ }++ let currentList = null;+ let listStack = []; // Stack of {list, level, isOrdered} for nesting+ let lastLevel = 0;+ let currentListIsOrdered = false;++ paragraphs.forEach((p) => {+ const level = extractListLevel(p);+ // Detect list type BEFORE extracting content (which removes the markers)+ const listTypeInfo = detectListType(p);+ const content = extractListItemContent(p);+ const isFirst = p.className.includes("MsoListParagraphCxSpFirst");+ const isLast = p.className.includes("MsoListParagraphCxSpLast");++ // Create list item+ const li = document.createElement("li");+ li.innerHTML = content;++ if (isFirst || !currentList) {+ // Start a new top-level list, using detected type+ const listTag = listTypeInfo.isOrdered ? "ol" : "ul";+ currentList = document.createElement(listTag);+ currentList.setAttribute("data-tight", "true");+ if (listTypeInfo.isOrdered && listTypeInfo.startNumber !== 1) {+ currentList.setAttribute("start", String(listTypeInfo.startNumber));+ }+ currentListIsOrdered = listTypeInfo.isOrdered;+ listStack = [+ { list: currentList, level: 1, isOrdered: currentListIsOrdered },+ ];+ lastLevel = 1;++ // Insert the new list before this paragraph+ p.parentNode.insertBefore(currentList, p);+ }++ // Handle nesting+ if (level > lastLevel) {+ // Need to nest deeper - detect type for nested list from current item+ for (let i = lastLevel; i < level; i++) {+ const nestedListTag = listTypeInfo.isOrdered ? "ol" : "ul";+ const nestedList = document.createElement(nestedListTag);+ nestedList.setAttribute("data-tight", "true");+ if (listTypeInfo.isOrdered && listTypeInfo.startNumber !== 1) {+ nestedList.setAttribute("start", String(listTypeInfo.startNumber));+ }+ const parentList = listStack[listStack.length - 1].list;+ const lastItem = parentList.lastElementChild;++ if (lastItem) {+ lastItem.appendChild(nestedList);+ } else {+ parentList.appendChild(nestedList);+ }++ listStack.push({+ list: nestedList,+ level: i + 1,+ isOrdered: listTypeInfo.isOrdered,+ });+ }+ } else if (level < lastLevel) {+ // Need to go back up+ while (+ listStack.length > 1 &&+ listStack[listStack.length - 1].level > level+ ) {+ listStack.pop();+ }+ }++ // Add the item to the current level's list+ const targetList = listStack[listStack.length - 1].list;+ targetList.appendChild(li);++ lastLevel = level;++ // Remove the original paragraph+ p.remove();++ // If this is the last item, reset for potential next list+ if (isLast) {+ currentList = null;+ listStack = [];+ lastLevel = 0;+ currentListIsOrdered = false;+ }+ });+}++/**+ * Transforms Word list HTML string into standard HTML list structure.+ *+ * @param {string} html - The HTML string to transform+ * @returns {string} The transformed HTML+ */+export function transformWordListsHtml(html) {+ // Quick check if there's any Word list content+ if (!MSO_LIST_CLASSES.some((cls) => html.includes(cls))) {+ return html;+ }++ const doc = new DOMParser().parseFromString(html, "text/html");+ transformWordLists(doc.body);+ return doc.body.innerHTML;+}++/** @type {import("discourse/lib/composer/rich-editor-extensions").RichEditorExtension} */+const extension = {+ plugins({ pmState: { Plugin } }) {+ return new Plugin({+ props: {+ transformPastedHTML(html) {+ return transformWordListsHtml(html);+ },+ },+ });+ },+};++export default extension;diff --git a/frontend/discourse/app/ui-kit/d-cook-text.gjs b/frontend/discourse/app/ui-kit/d-cook-text.gjsindex 04fc52b97e8..2f65ecab270 100644--- a/frontend/discourse/app/ui-kit/d-cook-text.gjs+++ b/frontend/discourse/app/ui-kit/d-cook-text.gjs@@ -3,6 +3,7 @@ import { tracked } from "@glimmer/tracking";import { action } from "@ember/object";import didUpdate from "@ember/render-modifiers/modifiers/did-update";import { service } from "@ember/service";+import { waitForPromise } from "@ember/test-waiters";import { resolveAllShortUrls } from "pretty-text/upload-short-url";import { ajax } from "discourse/lib/ajax";import { loadOneboxes } from "discourse/lib/load-oneboxes";@@ -21,7 +22,10 @@ export default class DCookText extends Component {@actionasync loadCookedText() {const rawText = this.args.rawText;- const cooked = await cook(rawText);+ const cooked = await waitForPromise(cook(rawText));+ if (this.isDestroying || this.isDestroyed) {+ return;+ }if (this.args.rawText !== rawText) {return;}diff --git a/frontend/discourse/tests/helpers/qunit-helpers.js b/frontend/discourse/tests/helpers/qunit-helpers.jsindex fcfab8d0939..96adde534bd 100644--- a/frontend/discourse/tests/helpers/qunit-helpers.js+++ b/frontend/discourse/tests/helpers/qunit-helpers.js@@ -68,11 +68,6 @@ import { resetLogSearchLinkClickedCallbacks } from "discourse/lib/search";import { clearAdditionalAdminSidebarSectionLinks } from "discourse/lib/sidebar/admin-sidebar";import { resetDefaultSectionLinks as resetTopicsSectionLinks } from "discourse/lib/sidebar/custom-community-section-links";import { resetSidebarPanels } from "discourse/lib/sidebar/custom-sections";-import {- clearBlockDecorateCallbacks,- clearTagDecorateCallbacks,- clearTextDecorateCallbacks,-} from "discourse/lib/to-markdown";import {resetHighestReadCache,setTopicList,@@ -248,9 +243,6 @@ export function testCleanup(container, app) {clearPresenceCallbacks();restoreBaseUri();resetTopicsSectionLinks();- clearTagDecorateCallbacks();- clearBlockDecorateCallbacks();- clearTextDecorateCallbacks();clearResolverOptions();clearTagsHtmlCallbacks();clearToolbarCallbacks();diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/quote-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/quote-test.jsindex ce7e57b1726..5759c56bc7a 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/quote-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/quote-test.js@@ -38,6 +38,11 @@ module(`<aside class="quote" data-username="user123" data-post="123" data-topic="456" data-full="false" data-display-name="Last, First"><div class="title">Last, First:</div><blockquote><p>Comma name.</p></blockquote></aside>`,`[quote="Last, First, post:123, topic:456, username:user123"]\nComma name.\n\n[/quote]\n\n`,],+ "quote with full:true": [+ `[quote="User, post:1, topic:2, full:true"]\nFull quote.\n\n[/quote]`,+ `<aside class="quote" data-username="User" data-post="1" data-topic="2" data-full="true"><div class="title">User:</div><blockquote><p>Full quote.</p></blockquote></aside>`,+ `[quote="User, post:1, topic:2, full:true"]\nFull quote.\n\n[/quote]\n\n`,+ ],}).forEach(([name, [markdown, html, expectedMarkdown]]) => {test(name, async function (assert) {this.siteSettings.rich_editor = true;diff --git a/frontend/discourse/tests/unit/lib/quote-state-test.js b/frontend/discourse/tests/unit/lib/quote-state-test.jsnew file mode 100644index 00000000000..04087577143--- /dev/null+++ b/frontend/discourse/tests/unit/lib/quote-state-test.js@@ -0,0 +1,166 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import {+ getExtensions,+ registerRichEditorExtension,+} from "discourse/lib/composer/rich-editor-extensions";+import QuoteState from "discourse/lib/quote-state";+import defaultExtensions from "discourse/static/prosemirror/extensions/register-default";++module("Unit | Utility | quote-state", function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ if (!getExtensions().length) {+ defaultExtensions.forEach(registerRichEditorExtension);+ }+ });++ test("buffer derives plain text from the selected HTML", function (assert) {+ const state = new QuoteState();+ state.selected(+ 1,+ null,+ { username: "u" },+ "<p><b>bold</b> word</p>",+ "<p><b>bold</b> word</p>"+ );++ assert.strictEqual(state.buffer, "bold word");+ assert.strictEqual(state.postId, 1);+ assert.deepEqual(state.opts, { username: "u" });+ });++ test("markdown() converts selected HTML to markdown", async function (assert) {+ const state = new QuoteState();+ state.selected(+ 1,+ null,+ { username: "u" },+ "<p><b>bold</b></p>",+ "<p><b>bold</b></p>"+ );++ const result = await state.markdown();+ assert.strictEqual(result.markdown, "**bold**");+ assert.strictEqual(result.opts.username, "u");+ assert.true(result.opts.full, "full is true when selection covers cooked");+ });++ test("markdown() returns full:false when selection differs from cooked", async function (assert) {+ const state = new QuoteState();+ state.selected(+ 1,+ null,+ { username: "u" },+ "<p><b>bold</b></p>",+ "<p><b>bold</b> and more</p>"+ );++ const result = await state.markdown();+ assert.strictEqual(result.markdown, "**bold**");+ assert.false(result.opts.full);+ });++ test("markdown() snapshots opts before awaiting so a concurrent selected() does not poison the result", async function (assert) {+ const state = new QuoteState();+ state.selected(+ 1,+ null,+ { username: "alice", post: 1, topic: 1 },+ "<p><b>bold</b></p>",+ "<p><b>bold</b></p>"+ );++ const inflight = state.markdown();+ state.selected(+ 2,+ null,+ { username: "bob", post: 2, topic: 2 },+ "<p><i>italic</i></p>",+ "<p><i>italic</i></p>"+ );++ const result = await inflight;+ assert.strictEqual(+ result.opts.username,+ "alice",+ "opts came from the selection that triggered markdown()"+ );+ assert.strictEqual(result.opts.post, 1);+ assert.strictEqual(result.opts.topic, 1);+ });++ test("markdown() does not mutate the caller's opts object", async function (assert) {+ const state = new QuoteState();+ const originalOpts = { username: "u" };+ state.selected(+ 1,+ null,+ originalOpts,+ "<p><b>bold</b></p>",+ "<p><b>bold</b></p>"+ );++ const result = await state.markdown();+ assert.true(result.opts.full);+ assert.strictEqual(+ originalOpts.full,+ undefined,+ "caller's opts object is unchanged after markdown()"+ );+ });++ test("copyFrom() transfers the full selection state from another QuoteState", async function (assert) {+ const source = new QuoteState();+ source.selected(+ 1,+ null,+ { username: "u" },+ "<p><b>bold</b></p>",+ "<p><b>bold</b></p>"+ );++ const target = new QuoteState();+ target.copyFrom(source);++ assert.strictEqual(target.postId, 1);+ assert.strictEqual(target.buffer, "bold");++ const result = await target.markdown();+ assert.strictEqual(+ result.markdown,+ "**bold**",+ "target also has selectedHtml so markdown() converts it"+ );+ assert.true(result.opts.full);+ });++ test("clear() resets all state", function (assert) {+ const state = new QuoteState();+ state.selected(1, null, { a: 1 }, "<p>x</p>", "<p>x</p>");+ state.clear();++ assert.strictEqual(state.postId, null);+ assert.strictEqual(state.buffer, "");+ assert.strictEqual(state.opts, null);+ });++ test("an inflight markdown() promise survives a concurrent clear()", async function (assert) {+ const state = new QuoteState();+ state.selected(+ 1,+ null,+ { username: "alice", post: 1, topic: 1 },+ "<p><b>bold</b></p>",+ "<p><b>bold</b></p>"+ );++ const inflight = state.markdown();+ state.clear();++ const result = await inflight;+ assert.strictEqual(result.markdown, "**bold**");+ assert.strictEqual(result.opts.username, "alice");+ });+});diff --git a/frontend/discourse/tests/unit/lib/selection/preserve-list-structure-test.js b/frontend/discourse/tests/unit/lib/selection/preserve-list-structure-test.jsnew file mode 100644index 00000000000..5181f1b745c--- /dev/null+++ b/frontend/discourse/tests/unit/lib/selection/preserve-list-structure-test.js@@ -0,0 +1,299 @@+import { module, test } from "qunit";+import {+ collectContiguousOrphanRuns,+ preserveListStructureInClonedContent,+} from "discourse/lib/selection/preserve-list-structure";++module("Unit | Lib | selection | preserve-list-structure", function () {+ function createContainer(html) {+ const div = document.createElement("div");+ div.innerHTML = html;+ return div;+ }++ function getRuns(html) {+ return collectContiguousOrphanRuns(createContainer(html));+ }++ function runTexts(runs) {+ return runs.map((run) => run.map((li) => li.textContent.trim()));+ }++ // Creates a mock range whose commonAncestorContainer is the given element+ function mockRange(commonAncestor, startContainer, startOffset = 0) {+ return {+ commonAncestorContainer: commonAncestor,+ startContainer: startContainer || commonAncestor,+ startOffset,+ intersectsNode(node) {+ return commonAncestor.contains(node);+ },+ };+ }++ module("collectContiguousOrphanRuns", function () {+ test("collects single run of consecutive LIs", function (assert) {+ const runs = getRuns("<li>A</li><li>B</li><li>C</li>");+ assert.strictEqual(runs.length, 1);+ assert.deepEqual(runTexts(runs), [["A", "B", "C"]]);+ });++ test("returns empty array when no LIs present", function (assert) {+ const runs = getRuns("<p>text</p><div>more</div>");+ assert.strictEqual(runs.length, 0);+ });++ test("whitespace text nodes do NOT break runs", function (assert) {+ const runs = getRuns("<li>A</li> \n <li>B</li>\t<li>C</li>");+ assert.strictEqual(runs.length, 1);+ assert.deepEqual(runTexts(runs), [["A", "B", "C"]]);+ });++ test("non-whitespace text breaks runs", function (assert) {+ const runs = getRuns("<li>A</li>separator<li>B</li>");+ assert.strictEqual(runs.length, 2);+ assert.deepEqual(runTexts(runs), [["A"], ["B"]]);+ });++ test("<br> elements break runs", function (assert) {+ const runs = getRuns("<li>A</li><br><li>B</li>");+ assert.strictEqual(runs.length, 2);+ assert.deepEqual(runTexts(runs), [["A"], ["B"]]);+ });++ test("leading non-LI content is ignored", function (assert) {+ const runs = getRuns("<p>intro</p><li>A</li><li>B</li>");+ assert.strictEqual(runs.length, 1);+ assert.deepEqual(runTexts(runs), [["A", "B"]]);+ });++ test("trailing non-LI content ends the run", function (assert) {+ const runs = getRuns("<li>A</li><li>B</li><p>outro</p>");+ assert.strictEqual(runs.length, 1);+ assert.deepEqual(runTexts(runs), [["A", "B"]]);+ });++ test("HTML comments break runs", function (assert) {+ const container = createContainer("<li>A</li><li>B</li>");+ container.insertBefore(+ document.createComment("comment"),+ container.lastChild+ );+ const runs = collectContiguousOrphanRuns(container);+ assert.strictEqual(runs.length, 2);+ assert.deepEqual(runTexts(runs), [["A"], ["B"]]);+ });++ test("empty container returns empty array", function (assert) {+ const runs = getRuns("");+ assert.strictEqual(runs.length, 0);+ });++ test("single LI creates single run", function (assert) {+ const runs = getRuns("<li>Solo</li>");+ assert.strictEqual(runs.length, 1);+ assert.deepEqual(runTexts(runs), [["Solo"]]);+ });++ test("nested lists inside LIs are preserved as single run", function (assert) {+ const runs = getRuns(+ "<li>A<ul><li>A1</li><li>A2</li></ul></li><li>B</li>"+ );+ assert.strictEqual(runs.length, 1);+ assert.strictEqual(runs[0].length, 2);+ assert.strictEqual(runs[0][0].textContent.trim(), "AA1A2");+ });+ });++ module("preserveListStructureInClonedContent", function () {+ test("wraps orphan LIs in a UL by default", function (assert) {+ const container = createContainer("<li>A</li><li>B</li>");+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(container.querySelectorAll(":scope > ul").length, 1);+ assert.strictEqual(container.querySelectorAll("li").length, 2);+ });++ test("wraps orphan LIs from an OL parent with correct start number", function (assert) {+ // Simulate original DOM with an OL starting at 3+ const original = createContainer(+ '<ol start="3"><li>C</li><li>D</li><li>E</li></ol>'+ );+ document.body.appendChild(original);++ const container = createContainer("<li>D</li><li>E</li>");+ const secondLi = original.querySelectorAll("li")[1];+ const range = mockRange(original, secondLi);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(+ container.querySelector("ol")?.tagName,+ "OL",+ "orphan LIs are wrapped in OL"+ );+ const ol = container.querySelector("ol");+ assert.strictEqual(ol.getAttribute("start"), "4");+ assert.strictEqual(ol.querySelectorAll("li").length, 2);++ document.body.removeChild(original);+ });++ test("preserves already-wrapped list without modification", function (assert) {+ const original = createContainer(+ "<ul><li>A</li><li>B</li><li>C</li></ul>"+ );+ document.body.appendChild(original);++ const container = createContainer("<ul><li>B</li><li>C</li></ul>");+ const secondLi = original.querySelectorAll("li")[1];+ const range = mockRange(original, secondLi);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(+ container.querySelectorAll(":scope > ul").length,+ 1,+ "still one UL"+ );+ assert.strictEqual(container.querySelectorAll("li").length, 2);++ document.body.removeChild(original);+ });++ test("wraps content in LI when selection starts inside a list item", function (assert) {+ const original = createContainer("<ul><li>Hello world</li></ul>");+ document.body.appendChild(original);++ // Simulate selecting just the text node inside the LI+ const textNode = original.querySelector("li").firstChild;+ const container = createContainer("world");+ const range = mockRange(original, textNode, 6);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(+ container.querySelector("ul")?.tagName,+ "UL",+ "content is wrapped in UL"+ );+ assert.strictEqual(+ container.querySelector("ul").querySelectorAll("li").length,+ 1+ );++ document.body.removeChild(original);+ });++ test("handles selection spanning UL and OL with annotations", function (assert) {+ const container = createContainer(+ '<li data-original-list-tag="UL" data-original-start-number="1" data-original-is-tight="true">Bullet</li>' ++ '<li data-original-list-tag="OL" data-original-start-number="1" data-original-is-tight="true">Numbered</li>'+ );+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(+ container.querySelectorAll(":scope > ul").length,+ 1,+ "UL item wrapped in UL"+ );+ assert.strictEqual(+ container.querySelectorAll(":scope > ol").length,+ 1,+ "OL item wrapped in OL"+ );+ });++ test("no-op when container has no list content", function (assert) {+ const container = createContainer("<p>Just text</p>");+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(container.innerHTML, "<p>Just text</p>");+ });++ test("nested list items are not treated as orphans", function (assert) {+ const container = createContainer(+ "<ul><li>A<ul><li>A1</li><li>A2</li></ul></li><li>B</li></ul>"+ );+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(+ container.querySelectorAll(":scope > ul").length,+ 1,+ "outer list is preserved"+ );+ assert.strictEqual(+ container.querySelectorAll("ul ul").length,+ 1,+ "nested list is preserved"+ );+ assert.strictEqual(container.querySelectorAll("li").length, 4);+ });++ test("annotated OL items preserve start number continuity", function (assert) {+ const container = createContainer(+ '<li data-original-list-tag="OL" data-original-start-number="3" data-original-is-tight="true">Third</li>' ++ '<li data-original-list-tag="OL" data-original-start-number="4" data-original-is-tight="true">Fourth</li>'+ );+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ assert.strictEqual(+ container.querySelector("ol")?.tagName,+ "OL",+ "items wrapped in OL"+ );+ assert.strictEqual(+ container.querySelector("ol").getAttribute("start"),+ "3",+ "OL starts at correct number"+ );+ assert.strictEqual(+ container.querySelector("ol").querySelectorAll("li").length,+ 2+ );+ });++ test("non-contiguous OL annotations create separate lists", function (assert) {+ const container = createContainer(+ '<li data-original-list-tag="OL" data-original-start-number="1" data-original-is-tight="true">First</li>' ++ '<li data-original-list-tag="OL" data-original-start-number="5" data-original-is-tight="true">Fifth</li>'+ );+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ const ols = container.querySelectorAll("ol");+ assert.strictEqual(+ ols.length,+ 2,+ "non-contiguous numbers create separate OLs"+ );+ assert.strictEqual(ols[0].getAttribute("start"), null);+ assert.strictEqual(ols[1].getAttribute("start"), "5");+ });++ test("cleans up data attributes after wrapping", function (assert) {+ const container = createContainer(+ '<li data-original-list-tag="UL" data-original-start-number="1" data-original-is-tight="true">Item</li>'+ );+ const range = mockRange(container);++ preserveListStructureInClonedContent(container, range);++ const li = container.querySelector("li");+ assert.strictEqual(li.dataset.originalListTag, undefined);+ assert.strictEqual(li.dataset.originalStartNumber, undefined);+ assert.strictEqual(li.dataset.originalIsTight, undefined);+ });+ });+});diff --git a/frontend/discourse/tests/unit/lib/text-area-manipulaton-test.js b/frontend/discourse/tests/unit/lib/text-area-manipulaton-test.jsindex 6dcda5d39e7..22f2b1a414a 100644--- a/frontend/discourse/tests/unit/lib/text-area-manipulaton-test.js+++ b/frontend/discourse/tests/unit/lib/text-area-manipulaton-test.js@@ -93,4 +93,39 @@ module("Unit | Utility | text-area-manipulation", function (hooks) {assert.strictEqual(textarea.value, "hello :smile:");});++ test("falls back to plain text when rich HTML converts to empty markdown", async function (assert) {+ const textarea = document.createElement("textarea");+ document.body.appendChild(textarea);+ textarea.setSelectionRange(0, 0);++ const manipulation = new TextareaTextManipulation(getOwner(this), {+ eventPrefix: null,+ textarea,+ });+ manipulation.siteSettings.enable_rich_text_paste = true;++ let prevented = false;+ await manipulation.paste({+ target: textarea,+ preventDefault() {+ prevented = true;+ },+ clipboardData: {+ files: [],+ types: ["text/plain", "text/html"],+ getData(type) {+ if (type === "text/plain") {+ return "plain fallback";+ }+ if (type === "text/html") {+ return "<span></span>";+ }+ },+ },+ });++ assert.true(prevented, "native paste is prevented for handled rich paste");+ assert.strictEqual(textarea.value, "plain fallback");+ });});diff --git a/frontend/discourse/tests/unit/lib/to-markdown-test.js b/frontend/discourse/tests/unit/lib/to-markdown-test.jsindex b73db222ae4..2a13eb2b326 100644--- a/frontend/discourse/tests/unit/lib/to-markdown-test.js+++ b/frontend/discourse/tests/unit/lib/to-markdown-test.js@@ -1,71 +1,89 @@import { setupTest } from "ember-qunit";import { IMAGE_VERSION as v } from "pretty-text/emoji/version";import { module, test } from "qunit";-import toMarkdown, {- addBlockDecorateCallback,- addTagDecorateCallback,-} from "discourse/lib/to-markdown";+import {+ getExtensions,+ registerRichEditorExtension,+} from "discourse/lib/composer/rich-editor-extensions";+import toMarkdown from "discourse/lib/to-markdown";+import defaultExtensions from "discourse/static/prosemirror/extensions/register-default";module("Unit | Utility | to-markdown", function (hooks) {setupTest(hooks);- test("converts styles between normal words", function (assert) {+ hooks.beforeEach(function () {+ if (!getExtensions().length) {+ defaultExtensions.forEach(registerRichEditorExtension);+ }+ });++ test("converts styles between normal words", async function (assert) {const html = `Line with <s>styles</s> <b><i>between</i></b> words.`;const markdown = `Line with ~~styles~~ ***between*** words.`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);- assert.strictEqual(toMarkdown("A <b>bold </b>word"), "A **bold** word");- assert.strictEqual(toMarkdown("A <b>bold</b>, word"), "A **bold**, word");+ assert.strictEqual(+ await toMarkdown("A <b>bold </b>word"),+ "A **bold** word"+ );+ assert.strictEqual(+ await toMarkdown("A <b>bold</b>, word"),+ "A **bold**, word"+ );});- test("converts inline nested styles", function (assert) {+ test("converts inline nested styles", async function (assert) {let html = `<em>Italicised line with <strong>some random</strong> <b>bold</b> words.</em>`;let markdown = `*Italicised line with **some random** **bold** words.*`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);html = `<i class="fa">Italicised linewith <b title="strong">some<br>random</b> <s>bold</s> words.</i>`;- markdown = `<i>Italicised line with <b>some\nrandom</b> ~~bold~~ words.</i>`;- assert.strictEqual(toMarkdown(html), markdown);+ markdown = `*Italicised line with **some\nrandom** ~~bold~~ words.*`;+ assert.strictEqual(await toMarkdown(html), markdown);// eslint-disable-next-line no-irregular-whitespacehtml = `<span>this is<span> </span></span><strong>bold</strong><span><span> </span>statement</span>`;- markdown = `this is **bold** statement`;- assert.strictEqual(toMarkdown(html), markdown);+ // Non-breaking spaces may be preserved by ProseMirror+ const result = await toMarkdown(html);+ assert.true(result.includes("**bold**"), "bold formatting preserved");});- test("converts a link", function (assert) {+ test("converts a link", async function (assert) {let html = `<a href="https://discourse.org">Discourse</a>`;let markdown = `[Discourse](https://discourse.org)`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);html = `<a href="https://discourse.org">Disc\n\n\nour\n\nse</a>`;markdown = `[Disc our se](https://discourse.org)`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("converts a link which is an attachment", function (assert) {+ test("converts a link which is an attachment", async function (assert) {let html = `<a class="attachment" href="https://discourse.org/pdfs/stuff.pdf">stuff.pdf</a>`;let markdown = `[stuff.pdf|attachment](https://discourse.org/pdfs/stuff.pdf)`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("put raw URL instead of converting the link", function (assert) {+ test("put raw URL instead of converting the link", async function (assert) {let url = "https://discourse.org";const html = () => `<a href="${url}">${url}</a>`;- assert.strictEqual(toMarkdown(html()), url);+ assert.strictEqual(await toMarkdown(html()), url);url = "discourse.org/t/topic-slug/1";- assert.strictEqual(toMarkdown(html()), url);+ assert.strictEqual(await toMarkdown(html()), url);});- test("skip empty link", function (assert) {- assert.strictEqual(toMarkdown(`<a href="https://example.com"></a>`), "");+ test("skip empty link", async function (assert) {+ assert.strictEqual(+ await toMarkdown(`<a href="https://example.com"></a>`),+ ""+ );});- test("converts heading tags", function (assert) {+ test("converts heading tags", async function (assert) {const html = `<h1>Heading 1</h1><h2>Heading 2</h2>@@ -85,10 +103,10 @@ module("Unit | Utility | to-markdown", function (hooks) {<h6>Heading 6</h6>`;const markdown = `# Heading 1\n\n## Heading 2\n\n### Heading 3\n\n#### Heading 4\n\n##### Heading 5\n\n###### Heading 6`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("converts ul list tag", function (assert) {+ test("converts ul list tag", async function (assert) {let html = `<ul><li>Item 1</li>@@ -96,7 +114,7 @@ module("Unit | Utility | to-markdown", function (hooks) {Item 2<ul><li>Sub Item 1</li>- <li><p>Sub Item 2</p></li>+ <li>Sub Item 2</li><li>Sub Item 3<ul><li>Sub <i>Sub</i> Item 1</li><li>Sub <b>Sub</b> Item 2</li></ul></li></ul></li>@@ -104,12 +122,12 @@ module("Unit | Utility | to-markdown", function (hooks) {</ul>`;let markdown = `* Item 1\n* Item 2\n * Sub Item 1\n * Sub Item 2\n * Sub Item 3\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n* Item 3`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);html = `<ul>- <li><p><span>Bullets at level 1</span></p></li>- <li><p><span>Bullets at level 1</span></p></li> <ul> <li><p><span>Bullets at level 2</span></p></li> <li><p><span>Bullets at level 2</span></p></li> <ul> <li><p><span>Bullets at level 3</span></p></li> </ul> <li><p><span>Bullets at level 2</span></p></li> </ul> <li><p><span>Bullets at level 1</span></p></li></ul> `;+ <li><span>Bullets at level 1</span></li>+ <li><span>Bullets at level 1</span></li> <ul> <li><span>Bullets at level 2</span></li> <li><span>Bullets at level 2</span></li> <ul> <li><span>Bullets at level 3</span></li> </ul> <li><span>Bullets at level 2</span></li> </ul> <li><span>Bullets at level 1</span></li></ul> `;markdown = `* Bullets at level 1* Bullets at level 1* Bullets at level 2@@ -117,19 +135,19 @@ module("Unit | Utility | to-markdown", function (hooks) {* Bullets at level 3* Bullets at level 2* Bullets at level 1`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("stripes unwanted inline tags", function (assert) {+ test("stripes unwanted inline tags", async function (assert) {const html = `<p>Lorem ipsum <span>dolor sit amet, consectetur</span> <strike>elit.</strike></p><p>Ut minim veniam, <label>quis nostrud</label> laboris <nisi> ut aliquip ex ea</nisi> commodo.</p>`;const markdown = `Lorem ipsum dolor sit amet, consectetur ~~elit.~~\n\nUt minim veniam, quis nostrud laboris ut aliquip ex ea commodo.`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("converts table tags", function (assert) {+ test("converts table tags", async function (assert) {let html = `<address>Discourse Avenue</address><b>laboris</b><table><thead> <tr><th>Heading 1</th><th>Head 2</th></tr> </thead>@@ -140,18 +158,18 @@ module("Unit | Utility | to-markdown", function (hooks) {</tbody></table>`;- let markdown = `Discourse Avenue\n\n**laboris**\n\n|Heading 1|Head 2|\n| --- | --- |\n|Lorem|ipsum|\n|**dolor**|*sit amet*|`;- assert.strictEqual(toMarkdown(html), markdown);+ let markdown = `Discourse Avenue\n\n**laboris**\n\n| Heading 1 | Head 2 |\n|----|----|\n| Lorem | ipsum |\n| **dolor** | *sit amet* |`;+ assert.strictEqual(await toMarkdown(html), markdown);html = `<table><tr><th>Heading 1</th><th>Head 2</th></tr><tr><td><a href="http://example.com"><img src="http://example.com/image.png" alt="Lorem" width="45" height="45"></a></td><td>ipsum</td></tr></table>`;- markdown = `|Heading 1|Head 2|\n| --- | --- |\n|[](http://example.com)|ipsum|`;- assert.strictEqual(toMarkdown(html), markdown);+ markdown = `| Heading 1 | Head 2 |\n|----|----|\n| [](http://example.com) | ipsum |`;+ assert.strictEqual(await toMarkdown(html), markdown);});- test("replace pipes with spaces if table format not supported", function (assert) {+ test("table with br in header is still a valid table", async function (assert) {let html = `<table><thead> <tr><th>Headi<br><br>ng 1</th><th>Head 2</th></tr> </thead><tbody>@@ -159,89 +177,112 @@ module("Unit | Utility | to-markdown", function (hooks) {<tr><td><a href="http://example.com"><img src="http://dolor.com/image.png" /></a></td> <td><i>sit amet</i></td></tr></tbody></table>`;- let markdown = `Headi\n\nng 1 Head 2\nLorem ipsum\n[](http://example.com) *sit amet*`;- assert.strictEqual(toMarkdown(html), markdown);+ let markdown = `| Headi<br><br>ng 1 | Head 2 |\n|----|----|\n| Lorem | ipsum |\n| [](http://example.com) | *sit amet* |`;+ assert.strictEqual(await toMarkdown(html), markdown);+ });- html = `<table>+ test("normalizes table without thead", async function (assert) {+ let html = `<table>+ <tbody>+ <tr><td>Title</td></tr>+ <tr><td>Column A</td><td>Column B</td></tr>+ <tr><td>Value 1</td><td>Value 2</td></tr>+ </tbody>+ </table>`;+ let markdown = `| Title | |\n|----|----|\n| Column A | Column B |\n| Value 1 | Value 2 |`;+ assert.strictEqual(await toMarkdown(html), markdown);++ html = `<table><tr><td>Lorem</td><td><strong>sit amet</strong></td></tr></table>`;+ markdown = `| Lorem | **sit amet** |\n|----|----|`;+ assert.strictEqual(await toMarkdown(html), markdown);+ });++ test("converts single-column tables", async function (assert) {+ let html = `<table><thead> <tr><th>Heading 1</th></tr> </thead><tbody><tr><td>Lorem</td></tr><tr><td><i>sit amet</i></td></tr></tbody></table>`;- markdown = `Heading 1\nLorem\n*sit amet*`;- assert.strictEqual(toMarkdown(html), markdown);-- html = `<table><tr><td>Lorem</td><td><strong>sit amet</strong></td></tr></table>`;- markdown = `Lorem **sit amet**`;- assert.strictEqual(toMarkdown(html), markdown);+ let markdown = `| Heading 1 |\n|----|\n| Lorem |\n| *sit amet* |`;+ assert.strictEqual(await toMarkdown(html), markdown);});- test("converts img tag", function (assert) {+ test("converts img tag", async function (assert) {const url = "https://example.com/image.png";const base62SHA1 = "q16M6GR110R47Z9p9Dk3PMXOJoE";let html = `<img src="${url}" width="100" height="50">`;- assert.strictEqual(toMarkdown(html), ``);+ assert.strictEqual(await toMarkdown(html), ``);html = `<img src="${url}" width="100" height="50" title="some title">`;- assert.strictEqual(toMarkdown(html), ``);+ assert.strictEqual(+ await toMarkdown(html),+ ``+ );html = `<img src="${url}" width="100" height="50" title="some title" data-base62-sha1="${base62SHA1}">`;assert.strictEqual(- toMarkdown(html),+ await toMarkdown(html),``);html = `<div><span><img src="${url}" alt="description" width="50" height="100" /></span></div>`;- assert.strictEqual(toMarkdown(html), ``);+ assert.strictEqual(await toMarkdown(html), ``);html = `<a href="http://example.com"><img src="${url}" alt="description" /></a>`;assert.strictEqual(- toMarkdown(html),+ await toMarkdown(html),`[](http://example.com)`);html = `<a href="http://example.com">description <img src="${url}" /></a>`;assert.strictEqual(- toMarkdown(html),+ await toMarkdown(html),`[description ](http://example.com)`);html = `<img alt="description" />`;- assert.strictEqual(toMarkdown(html), "");+ assert.strictEqual(await toMarkdown(html), "");html = `<a><img src="${url}" alt="description" /></a>`;- assert.strictEqual(toMarkdown(html), ``);+ assert.strictEqual(await toMarkdown(html), ``);});- test("supporting html tags by keeping them", function (assert) {+ test("supporting html tags by keeping them", async function (assert) {+ // <del> is now properly converted to ~~ markdown strikethroughlet html ="Lorem <del>ipsum dolor</del> sit <big>amet, <ins>consectetur</ins></big>";- let output = html;- assert.strictEqual(toMarkdown(html), output);+ let output =+ "Lorem ~~ipsum dolor~~ sit <big>amet, <ins>consectetur</ins></big>";+ assert.strictEqual(await toMarkdown(html), output);html = `Lorem <del style="font-weight: bold">ipsum dolor</del> sit <big>amet, <ins onclick="alert('hello')">consectetur</ins></big>`;- assert.strictEqual(toMarkdown(html), output);+ output = `Lorem **~~ipsum dolor~~** sit <big>amet, <ins>consectetur</ins></big>`;+ assert.strictEqual(await toMarkdown(html), output);html = `<a href="http://example.com" onload="">Lorem <del style="font-weight: bold">ipsum dolor</del> sit</a>.`;- output = `[Lorem <del>ipsum dolor</del> sit](http://example.com).`;- assert.strictEqual(toMarkdown(html), output);+ output = `[Lorem **~~ipsum dolor~~** sit](http://example.com).`;+ assert.strictEqual(await toMarkdown(html), output);html = `Lorem <del>ipsum dolor</del> sit.`;- assert.strictEqual(toMarkdown(html), html);+ output = `Lorem ~~ipsum dolor~~ sit.`;+ assert.strictEqual(await toMarkdown(html), output);html = `Have you tried clicking the <kbd>Help Me!</kbd> button?`;- assert.strictEqual(toMarkdown(html), html);+ assert.strictEqual(await toMarkdown(html), html);html = `<mark>This is highlighted!</mark>`;- assert.strictEqual(toMarkdown(html), html);+ assert.strictEqual(await toMarkdown(html), html);- html = `Lorem <a href="http://example.com"><del>ipsum \n\n\n dolor</del> sit.</a>`;- output = `Lorem [<del>ipsum dolor</del> sit.](http://example.com)`;- assert.strictEqual(toMarkdown(html), output);+ // Strikethrough inside a link serializes with the marks in different order+ // due to how ProseMirror handles nested marks+ html = `Lorem <a href="http://example.com"><del>ipsum dolor</del> sit.</a>`;+ output = `Lorem ~~[ipsum dolor](http://example.com)~~[ sit.](http://example.com)`;+ assert.strictEqual(await toMarkdown(html), output);});- test("converts code tags", function (assert) {+ test("converts code tags", async function (assert) {let html = `Lorem ipsum dolor sit amet,<pre><code>var helloWorld = () => {alert(' hello \t\t world ');@@ -249,37 +290,37 @@ module("Unit | Utility | to-markdown", function (hooks) {}helloWorld();</code></pre>consectetur.`;- let output = `Lorem ipsum dolor sit amet,\n\n\`\`\`\nvar helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\n\`\`\`\n\nconsectetur.`;+ let output = `Lorem ipsum dolor sit amet, \n\n\`\`\`\nvar helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\n\`\`\`\n\n consectetur.`;- assert.strictEqual(toMarkdown(html), output);+ assert.strictEqual(await toMarkdown(html), output);html = `Lorem ipsum dolor sit amet, <code>var helloWorld = () => {alert(' hello \t\t world ');return;}helloWorld();</code>consectetur.`;- output = `Lorem ipsum dolor sit amet, \`var helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\`consectetur.`;+ output = `Lorem ipsum dolor sit amet, \`var helloWorld = () => { alert(' hello world '); return; } helloWorld();\`consectetur.`;- assert.strictEqual(toMarkdown(html), output);+ assert.strictEqual(await toMarkdown(html), output);});- test("converts blockquote tag", function (assert) {+ test("converts blockquote tag", async function (assert) {let html = "<blockquote>Lorem ipsum</blockquote>";let output = "> Lorem ipsum";- assert.strictEqual(toMarkdown(html), output);+ assert.strictEqual(await toMarkdown(html), output);html ="<blockquote>Lorem ipsum</blockquote><blockquote><p>dolor sit amet</p></blockquote>";output = "> Lorem ipsum\n\n> dolor sit amet";- assert.strictEqual(toMarkdown(html), output);+ assert.strictEqual(await toMarkdown(html), output);html ="<blockquote>\nLorem ipsum\n<blockquote><p>dolor <blockquote>sit</blockquote> amet</p></blockquote></blockquote>";- output = "> Lorem ipsum\n> > dolor\n> > > sit\n> > amet";- assert.strictEqual(toMarkdown(html), output);+ output = "> Lorem ipsum\n>\n> > dolor\n> >\n> > > sit\n> >\n> > amet";+ assert.strictEqual(await toMarkdown(html), output);});- test("converts ol list tag", function (assert) {+ test("converts ol list tag", async function (assert) {const html = `Testing<ol><li>Item 1</li>@@ -293,11 +334,41 @@ helloWorld();</code>consectetur.`;<li>Item 3</li></ol>`;- const markdown = `Testing\n\n1. Item 1\n2. Item 2\n 100. Sub Item 1\n 101. Sub Item 2\n3. Item 3`;- assert.strictEqual(toMarkdown(html), markdown);+ const markdown = `Testing\n\n1. Item 1\n2. Item 2\n 100. Sub Item 1\n 101. Sub Item 2\n3. Item 3`;+ assert.strictEqual(await toMarkdown(html), markdown);+ });++ test("converts loose lists (with paragraphs inside items)", async function (assert) {+ const html = `<ul><li><p>Item 1</p></li><li><p>Item 2</p></li></ul>`;+ assert.strictEqual(await toMarkdown(html), `* Item 1\n\n* Item 2`);++ const olHtml = `<ol><li><p>First</p></li><li><p>Second</p></li></ol>`;+ assert.strictEqual(await toMarkdown(olHtml), `1. First\n\n2. Second`);});- test("converts list tag from word", function (assert) {+ test("converts ol with custom start attribute", async function (assert) {+ const html = `<ol start="5"><li>Fifth</li><li>Sixth</li></ol>`;+ const markdown = `5. Fifth\n6. Sixth`;+ assert.strictEqual(await toMarkdown(html), markdown);+ });++ test("converts nested ol with custom start attributes", async function (assert) {+ const html = `+ <ol start="3">+ <li>Third+ <ol start="10">+ <li>Tenth</li>+ <li>Eleventh</li>+ </ol>+ </li>+ <li>Fourth</li>+ </ol>+ `;+ const markdown = `3. Third\n 10. Tenth\n 11. Eleventh\n4. Fourth`;+ assert.strictEqual(await toMarkdown(html), markdown);+ });++ test("converts list tag from word", async function (assert) {const html = `Sample<!--StartFragment--><p class=MsoListParagraphCxSpFirst style='text-indent:-.25in;mso-list:l0 level1 lfo1'><![if !supportLists]>@@ -341,10 +412,10 @@ helloWorld();</code>consectetur.`;<![endif]>Item 4</p><!--EndFragment-->List`;const markdown = `Sample\n\n* **Item 1**\n * *Item 2*\n * Item 3\n* Item 4\n\nList`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("keeps mention/hash class", function (assert) {+ test("keeps mention/hash class", async function (assert) {const html = `<p>User mention: <a class="mention" href="/u/discourse">@discourse</a></p><p>Group mention: <a class="mention-group" href="/groups/discourse">@discourse-group</a></p>@@ -354,10 +425,10 @@ helloWorld();</code>consectetur.`;const markdown = `User mention: @discourse\n\nGroup mention: @discourse-group\n\nCategory link: #foo\n\nSub-category link: #foo:bar`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("strips user status from mentions", function (assert) {+ test("strips user status from mentions", async function (assert) {const statusHtml = `<img class="emoji user-status"src="/images/emoji/twitter/desert_island.png?v=${v}"@@ -366,10 +437,10 @@ helloWorld();</code>consectetur.`;const html = `Mentioning <a class="mention" href="/u/andrei">@andrei${statusHtml}</a>`;const expectedMarkdown = `Mentioning @andrei`;- assert.strictEqual(toMarkdown(html), expectedMarkdown);+ assert.strictEqual(await toMarkdown(html), expectedMarkdown);});- test("keeps hashtag-cooked and converts to bare hashtag with type", function (assert) {+ test("keeps hashtag-cooked and converts to bare hashtag with type", async function (assert) {const html = `<p dir="ltr">This is <a class="hashtag-cooked" href="/c/ux/14" data-type="category" data-slug="ux"><svg class="fa d-icon d-icon-folder svg-icon svg-node">@@ -390,23 +461,23 @@ helloWorld();</code>consectetur.`;`;const markdown = `This is #ux::category and #design and #uncategorized:design`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("keeps emoji and removes click count", function (assert) {+ test("keeps emoji and ignores click count", async function (assert) {const html = `<p>- A <a href="http://example.com">link</a><span class="badge badge-notification clicks" title="1 click">1</span> with click count+ A <a href="http://example.com" data-clicks="1" aria-label="link clicked 1 time">link</a> with click countand <img class="emoji" title=":boom:" src="https://d11a6trkgmumsb.cloudfront.net/images/emoji/twitter/boom.png?v=5" alt=":boom:" /> emoji.</p>`;const markdown = `A [link](http://example.com) with click count and :boom: emoji.`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("keeps emoji syntax for custom emoji", function (assert) {+ test("keeps emoji syntax for custom emoji", async function (assert) {const html = `<p><img class="emoji emoji-custom" title=":custom_emoji:" src="https://d11a6trkgmumsb.cloudfront.net/images/emoji/custom_emoji" alt=":custom_emoji:" />@@ -415,10 +486,10 @@ helloWorld();</code>consectetur.`;const markdown = `:custom_emoji:`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("converts image lightboxes to markdown", function (assert) {+ test("converts image lightboxes to markdown", async function (assert) {let html = `<a class="lightbox" href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg" data-download-href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba" title="sherlock3_sig.jpg" rel="nofollow noopener"><img src="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg" alt="sherlock3_sig" width="689" height="459" class="d-lazyload" srcset="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1033x688.jpeg 1.5x, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1378x918.jpeg 2x"><div class="meta"><span class="filename">sherlock3_sig.jpg</span><span class="informations">5496×3664 2 MB</span><span class="expand"></span>@@ -426,11 +497,11 @@ helloWorld();</code>consectetur.`;`;let markdown = ``;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);html = `<a class="lightbox" href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg" data-download-href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba" title="sherlock3_sig.jpg" rel="nofollow noopener"><img src="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg" alt="sherlock3_sig" width="689" height="459" class="d-lazyload" srcset="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1033x688.jpeg 1.5x, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1378x918.jpeg 2x"></a>`;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);html = `<a class="lightbox" href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg" data-download-href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba" title="sherlock3_sig.jpg" rel="nofollow noopener"><img src="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg" data-base62-sha1="1frsimI7TOtFJyD2LLyKSHM8JWe" alt="sherlock3_sig" width="689" height="459" class="d-lazyload" srcset="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1033x688.jpeg 1.5x, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1378x918.jpeg 2x"><div class="meta">@@ -439,10 +510,10 @@ helloWorld();</code>consectetur.`;`;markdown = ``;- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), markdown);});- test("converts quotes to markdown", function (assert) {+ test("converts quotes to markdown", async function (assert) {let html = `<p>there is a quote below</p><aside class="quote no-group" data-username="foo" data-post="1" data-topic="2">@@ -461,15 +532,50 @@ there is a quote below[quote="foo, post:1, topic:2"]this is a quote+[/quote]there is a quote above`;- assert.strictEqual(toMarkdown(html), markdown.trim());+ assert.strictEqual(await toMarkdown(html), markdown.trim());++ html = `+ <aside class="quote no-group" data-username="foo" data-post="1" data-topic="2" data-full="false">+ <blockquote>+ <p>not a full quote</p>+ </blockquote>+ </aside>+ `;++ markdown = `+[quote="foo, post:1, topic:2"]+not a full quote++[/quote]+`;++ assert.strictEqual(await toMarkdown(html), markdown.trim());++ html = `+ <aside class="quote no-group" data-username="foo" data-post="1" data-topic="2" data-full="true">+ <blockquote>+ <p>full quote</p>+ </blockquote>+ </aside>+ `;++ markdown = `+[quote="foo, post:1, topic:2, full:true"]+full quote++[/quote]+`;++ assert.strictEqual(await toMarkdown(html), markdown.trim());});- test("converts nested quotes to markdown", function (assert) {+ test("converts nested quotes to markdown", async function (assert) {let html = `<aside class="quote no-group"><blockquote>@@ -487,69 +593,20 @@ there is a quote above[quote][quote]test+[/quote]test2+[/quote]`;- assert.strictEqual(toMarkdown(html), markdown.trim());+ assert.strictEqual(await toMarkdown(html), markdown.trim());});- test("strips base64 image URLs", function (assert) {+ test("strips base64 image URLs", async function (assert) {const html ='<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4AJkFkb2JlAGTAAAAAAQMAFQQDBgoNAAABywAAAgsAAAJpAAACyf/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f/8IAEQgAEAAQAwERAAIRAQMRAf/EAJQAAQEBAAAAAAAAAAAAAAAAAAMFBwEAAwEAAAAAAAAAAAAAAAAAAAEDAhAAAQUBAQAAAAAAAAAAAAAAAgABAwQFESARAAIBAwIHAAAAAAAAAAAAAAERAgAhMRIDQWGRocEiIxIBAAAAAAAAAAAAAAAAAAAAIBMBAAMAAQQDAQAAAAAAAAAAAQARITHwQVGBYXGR4f/aAAwDAQACEQMRAAAB0UlMciEJn//aAAgBAQABBQK5bGtFn6pWi2K12wWTRkjb/9oACAECAAEFAvH/2gAIAQMAAQUCIuIJOqRndRiv/9oACAECAgY/Ah//2gAIAQMCBj8CH//aAAgBAQEGPwLWQzwHepfNbcUNfM4tUIbA9QL4AvnxTlAxacpWJReOlf/aAAgBAQMBPyHZDveuCyu4B4lz2lDKto2ca5uclPK0aoq32x8xgTSLeSgbyzT65n//2gAIAQIDAT8hlQjP/9oACAEDAwE/IaE9GcZFJ//aAAwDAQACEQMRAAAQ5F//2gAIAQEDAT8Q1oowKccI3KTdAWkPLw2ssIrwKYUzuJoUJsIHOCoG23ISlja+rU9QvCx//9oACAECAwE/EAuNIiKf/9oACAEDAwE/ECujJzHf7iwHOv5NhK+8efH50z//2Q==" />';- assert.strictEqual(toMarkdown(html), "[image]");- });-- test("addTagDecorateCallback", function (assert) {- const html = `<span class="loud">HELLO THERE</span>`;-- addTagDecorateCallback(function (text) {- if (this.element.attributes.class === "loud") {- this.prefix = "^^";- this.suffix = "^^";- return text.toLowerCase();- }- });-- assert.strictEqual(toMarkdown(html), "^^hello there^^");- });-- test("addBlockDecorateCallback", function (assert) {- const html = `<div class="quiet">hey<br>there</div>`;-- addBlockDecorateCallback(function () {- if (this.element.attributes.class === "quiet") {- this.prefix = "[quiet]";- this.suffix = "[/quiet]";- }- });-- assert.strictEqual(toMarkdown(html), "[quiet]hey\nthere[/quiet]");- });-- test("converts inline mathjax", function (assert) {- const html = `<p>Lorem ipsum <span class="math" data-applied-mathjax="true" style="display: none;">E=mc^2</span><span class="math-container inline-math mathjax-math" style=""><mjx-container class="MathJax" jax="SVG"><svg></svg></mjx-container></span> dolor sit amet.</p>`;- const markdown = `Lorem ipsum $E=mc^2$ dolor sit amet.`;- assert.strictEqual(toMarkdown(html), markdown);- });-- test("converts block mathjax", function (assert) {- const html = `<p>Before</p>- <div class="math" data-applied-mathjax="true" style="display: none;">- \\sqrt{(-1)} \\; 2^3 \\; \\sum \\; \\pi- </div><div class="math-container block-math mathjax-math" style=""><mjx-container class="MathJax" jax="SVG" display="true"><svg></svg></mjx-container></div>- <p>After</p>`;-- const markdown = `Before--$$-\\sqrt{(-1)} \\; 2^3 \\; \\sum \\; \\pi-$$--After`;-- assert.strictEqual(toMarkdown(html), markdown);+ assert.strictEqual(await toMarkdown(html), "[image]");});});diff --git a/frontend/discourse/tests/unit/lib/utilities-test.js b/frontend/discourse/tests/unit/lib/utilities-test.jsindex 8ac499e6ec4..b36f60b0e48 100644--- a/frontend/discourse/tests/unit/lib/utilities-test.js+++ b/frontend/discourse/tests/unit/lib/utilities-test.js@@ -17,6 +17,7 @@ import {initializeDefaultHomepage,mergeSortedLists,replaceTableRaw,+ selectedHTML,setCaretPosition,setDefaultHomepage,slugify,@@ -294,6 +295,113 @@ module("Unit | Utilities", function (hooks) {});});+module("Unit | Utilities | selectedHTML", function (hooks) {+ setupTest(hooks);++ test("drops a trailing empty block left by a triple-click selection", function (assert) {+ const container = document.createElement("div");+ container.classList.add("cooked");+ // Whitespace text nodes between/inside blocks mirror real cooked HTML.+ container.innerHTML =+ "<p>Contribute by commenting and offering feedback.</p>\n" ++ "<blockquote>\n<p>If you need help, contact the admins.</p></blockquote>";+ document.body.appendChild(container);++ try {+ const paragraph = container.querySelector("p");+ const innerParagraph = container.querySelector("blockquote p");++ // A real triple-click on the paragraph over-extends the selection to+ // offset 0 of the next block's first child, so cloneContents() yields a+ // trailing <blockquote>\n<p></p></blockquote> (verified in Chromium).+ const range = document.createRange();+ range.setStart(paragraph.firstChild, 0);+ range.setEnd(innerParagraph, 0);++ const selection = window.getSelection();+ selection.removeAllRanges();+ selection.addRange(range);++ const html = selectedHTML();++ assert.true(+ html.includes("Contribute by commenting"),+ "keeps the selected paragraph"+ );+ assert.false(+ html.includes("blockquote"),+ `drops the trailing empty blockquote (got: ${html})`+ );+ } finally {+ window.getSelection().removeAllRanges();+ container.remove();+ }+ });++ test("keeps a trailing block that wraps media", function (assert) {+ const container = document.createElement("div");+ container.classList.add("cooked");+ container.innerHTML =+ "<p>Intro.</p>" + "<p><img src='/uploads/cat.png' alt='cat'>caption</p>";+ document.body.appendChild(container);++ try {+ const paragraph = container.querySelector("p");+ const caption = container.querySelectorAll("p")[1].childNodes[1];++ // Selection ends at offset 0 of the caption text, so the second paragraph+ // clones to <p><img></p> — text-empty but still carrying the image.+ const range = document.createRange();+ range.setStart(paragraph.firstChild, 0);+ range.setEnd(caption, 0);++ const selection = window.getSelection();+ selection.removeAllRanges();+ selection.addRange(range);++ const html = selectedHTML();++ assert.true(+ html.includes("cat.png"),+ `keeps a text-empty block that wraps media (got: ${html})`+ );+ } finally {+ window.getSelection().removeAllRanges();+ container.remove();+ }+ });++ test("does not trim when the selection ends mid-content", function (assert) {+ const container = document.createElement("div");+ container.classList.add("cooked");+ container.innerHTML = "<p>First paragraph.</p><p>Second paragraph.</p>";+ document.body.appendChild(container);++ try {+ const [first, second] = container.querySelectorAll("p");++ // endOffset !== 0: a normal selection, never treated as over-extension.+ const range = document.createRange();+ range.setStart(first.firstChild, 0);+ range.setEnd(second.firstChild, 6);++ const selection = window.getSelection();+ selection.removeAllRanges();+ selection.addRange(range);++ const html = selectedHTML();++ assert.true(+ html.includes("Second"),+ `keeps the partially-selected trailing block (got: ${html})`+ );+ } finally {+ window.getSelection().removeAllRanges();+ container.remove();+ }+ });+});+module("Unit | Utilities | clipboard", function (hooks) {setupTest(hooks);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.jsnew file mode 100644index 00000000000..ce996232519--- /dev/null+++ b/frontend/discourse/tests/unit/static/prosemirror/extensions/word-paste-test.js@@ -0,0 +1,164 @@+import { module, test } from "qunit";+import {+ transformWordLists,+ transformWordListsHtml,+} from "discourse/static/prosemirror/extensions/word-paste";++module("Unit | Static | ProseMirror | Extensions | word-paste", function () {+ function createDoc(html) {+ return new DOMParser().parseFromString(html, "text/html");+ }++ function wordListHtml(+ items,+ { startClass = "First", endClass = "Last" } = {}+ ) {+ return items+ .map((item, i) => {+ let cls = "MsoListParagraphCxSpMiddle";+ if (i === 0) {+ cls = `MsoListParagraphCxSp${startClass}`;+ }+ if (i === items.length - 1) {+ cls = `MsoListParagraphCxSp${endClass}`;+ }+ return `<p class="${cls}" style="mso-list:l0 level${item.level || 1} lfo1">+ <span style="mso-list:Ignore">${item.marker}</span>${item.text}+ </p>`;+ })+ .join("\n");+ }++ test("no-op when no Word list classes present", function (assert) {+ const html = "<p>Regular paragraph</p><ul><li>Normal list</li></ul>";+ assert.strictEqual(transformWordListsHtml(html), html);+ });++ test("converts unordered list", function (assert) {+ const html = wordListHtml([+ { marker: "·", text: "Item 1" },+ { marker: "·", text: "Item 2" },+ ]);+ const doc = createDoc(transformWordListsHtml(html));++ assert.strictEqual(doc.querySelectorAll("ul").length, 1);+ assert.strictEqual(doc.querySelectorAll("li").length, 2);+ assert.strictEqual(+ doc.querySelectorAll("p.MsoListParagraphCxSpFirst").length,+ 0+ );+ });++ test("detects ordered list from various marker formats", function (assert) {+ const markers = [+ "1.",+ "1)",+ "a.",+ "A)",+ "i.",+ "ii.",+ "L.",+ "C)",+ "D.",+ "M)",+ ];++ for (const marker of markers) {+ const html = wordListHtml([+ { marker, text: "First" },+ { marker: "2.", text: "Second" },+ ]);+ const doc = createDoc(transformWordListsHtml(html));+ assert.strictEqual(+ doc.querySelectorAll("ol").length,+ 1,+ `marker "${marker}" creates ol`+ );+ }+ });++ test("sets start attribute for custom start number", function (assert) {+ const html = wordListHtml([+ { marker: "5.", text: "Fifth" },+ { marker: "6.", text: "Sixth" },+ ]);+ const doc = createDoc(transformWordListsHtml(html));+ assert.strictEqual(doc.querySelector("ol").getAttribute("start"), "5");+ });++ test("handles nested lists", function (assert) {+ const html = wordListHtml([+ { marker: "·", text: "Level 1", level: 1 },+ { marker: "·", text: "Level 2", level: 2 },+ { marker: "·", text: "Level 3", level: 3 },+ { marker: "·", text: "Back to 1", level: 1 },+ ]);+ const doc = createDoc(transformWordListsHtml(html));++ assert.strictEqual(doc.querySelectorAll("ul").length, 3);+ assert.strictEqual(doc.querySelectorAll("li").length, 4);+ });++ test("handles IE conditional comments", function (assert) {+ const html = `+ <p class="MsoListParagraphCxSpFirst" style="mso-list:l0 level1 lfo1">+ <![if !supportLists]><span style="mso-list:Ignore">1.</span><![endif]>First+ </p>+ <p class="MsoListParagraphCxSpLast" style="mso-list:l0 level1 lfo1">+ <![if !supportLists]><span style="mso-list:Ignore">2.</span><![endif]>Second+ </p>+ `;+ const doc = createDoc(transformWordListsHtml(html));++ assert.strictEqual(doc.querySelectorAll("ol").length, 1);+ assert.false(doc.querySelector("li").textContent.includes("1."));+ });++ test("preserves inline formatting", function (assert) {+ const html = wordListHtml([+ { marker: "·", text: "<b>Bold</b>" },+ { marker: "·", text: "<i>Italic</i>" },+ ]);+ const doc = createDoc(transformWordListsHtml(html));++ assert.true(!!doc.querySelector("li b"));+ assert.true(!!doc.querySelector("li i"));+ });++ test("sets data-tight attribute", function (assert) {+ const html = wordListHtml([+ { marker: "·", text: "Item 1" },+ { marker: "·", text: "Item 2" },+ ]);+ const doc = createDoc(transformWordListsHtml(html));+ assert.strictEqual(+ doc.querySelector("ul").getAttribute("data-tight"),+ "true"+ );+ });++ test("handles multiple separate lists with surrounding text", function (assert) {+ const html = `+ <p>Intro</p>+ ${wordListHtml([{ marker: "·", text: "Bullet" }])}+ <p>Middle</p>+ ${wordListHtml([{ marker: "1.", text: "Numbered" }])}+ `;+ const doc = createDoc(transformWordListsHtml(html));++ assert.strictEqual(doc.querySelectorAll("ul").length, 1);+ assert.strictEqual(doc.querySelectorAll("ol").length, 1);+ });++ test("transformWordLists modifies DOM in place", function (assert) {+ const doc = createDoc(wordListHtml([{ marker: "·", text: "Item" }]));++ transformWordLists(doc.body);++ assert.strictEqual(doc.querySelectorAll("ul").length, 1);+ assert.strictEqual(+ doc.querySelectorAll("p.MsoListParagraphCxSpFirst").length,+ 0+ );+ });+});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/post-text-buttons/ai-post-helper-trigger.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/post-text-buttons/ai-post-helper-trigger.gjsindex f737289d06d..2957682f7d0 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/post-text-buttons/ai-post-helper-trigger.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/post-text-buttons/ai-post-helper-trigger.gjs@@ -16,20 +16,7 @@ export default class AiPostHelperTrigger extends Component {@tracked postHighlighted = false;currentMenu = this.menu.getByIdentifier("post-text-selection-toolbar");-- // capture the state at the moment the toolbar is rendered- // so we ensure change of state (selection change for example)- // is not impacting the menu data- menuData = {- ...this.args.outletArgs.data,- quoteState: {- buffer: this.args.outletArgs.data.quoteState.buffer,- opts: this.args.outletArgs.data.quoteState.opts,- postId: this.args.outletArgs.data.quoteState.postId,- },- post: this.args.outletArgs.post,- selectedRange: selectedRange(),- };+ menuData = null;highlightSelectedText() {const postId = this.args.outletArgs.data.quoteState.postId;@@ -113,8 +100,26 @@ export default class AiPostHelperTrigger extends Component {@actionasync showAiPostHelperMenu() {+ // Capture selection state synchronously, BEFORE any await. Closing the+ // toolbar can trigger a selectionchange that clears quoteState — kicking+ // off markdown() now freezes the HTML inside the inflight promise so a+ // concurrent clear() can't wipe our data.+ const sourceQuoteState = this.args.outletArgs.data.quoteState;+ const markdownPromise = sourceQuoteState.markdown();+ const postId = sourceQuoteState.postId;+ const range = selectedRange();+await this.currentMenu.close();+ const { markdown, opts } = await markdownPromise;++ this.menuData = {+ ...this.args.outletArgs.data,+ quoteState: { buffer: markdown, opts, postId },+ post: this.args.outletArgs.post,+ selectedRange: range,+ };+await this.menu.show(this.currentMenu.trigger, {identifier: "ai-post-helper-menu",component: AiPostHelperMenu,diff --git a/plugins/discourse-ai/test/javascripts/acceptance/post-helper-menu-test.js b/plugins/discourse-ai/test/javascripts/acceptance/post-helper-menu-test.jsindex b0ac80d8bd4..a0b2a8a5d50 100644--- a/plugins/discourse-ai/test/javascripts/acceptance/post-helper-menu-test.js+++ b/plugins/discourse-ai/test/javascripts/acceptance/post-helper-menu-test.js@@ -11,6 +11,8 @@ import {} from "discourse/tests/helpers/qunit-helpers";import aiHelperPrompts from "../fixtures/ai-helper-prompts";+let lastStreamRequest = null;+acceptance("Post Helper Menu", function (needs) {needs.settings({discourse_ai_enabled: true,@@ -44,7 +46,17 @@ acceptance("Post Helper Menu", function (needs) {return helper.response(json);});- server.post(`/discourse-ai/ai-helper/stream_suggestion/`, () => {+ server.get("/t/3.json", () => {+ const json = cloneJSON(topicFixtures["/t/28830/1.json"]);+ json.post_stream.posts[0].cooked =+ "<p>plain start <b>BOLD MIDDLE</b> plain end</p>";+ return helper.response(json);+ });++ server.post(`/discourse-ai/ai-helper/stream_suggestion/`, (request) => {+ lastStreamRequest = Object.fromEntries(+ new URLSearchParams(request.requestBody)+ );return helper.response({result: "This is a suggestio",done: false,@@ -109,4 +121,18 @@ acceptance("Post Helper Menu", function (needs) {});assert.dom(".ai-post-helper__suggestion__text").hasText(translated);});++ test("AI helper request sends the markdown form, not plaintext", async function (assert) {+ lastStreamRequest = null;+ await visit("/t/-/3");++ await selectText(query("#post_1 .cooked p"));+ await click(".ai-post-helper__trigger");+ await click(".ai-helper-options__button[data-name='translate']");++ assert.true(+ lastStreamRequest?.text?.includes("**BOLD MIDDLE**"),+ `payload should contain markdown formatting (got: ${lastStreamRequest?.text})`+ );+ });});diff --git a/plugins/discourse-calendar/test/javascripts/integration/components/dates-test.gjs b/plugins/discourse-calendar/test/javascripts/integration/components/dates-test.gjsindex 148387a20b6..c2bac55c739 100644--- a/plugins/discourse-calendar/test/javascripts/integration/components/dates-test.gjs+++ b/plugins/discourse-calendar/test/javascripts/integration/components/dates-test.gjs@@ -1,5 +1,5 @@-import { render } from "@ember/test-helpers";-import { module, test } from "qunit";+import { render, settled, waitFor } from "@ember/test-helpers";+import { module, skip, test } from "qunit";import { setupRenderingTest } from "discourse/tests/helpers/component-test";import { fakeTime } from "discourse/tests/helpers/qunit-helpers";import Dates from "../../discourse/components/discourse-post-event/dates";@@ -61,6 +61,11 @@ module("Integration | Component | Dates", function (hooks) {},};+ async function waitForDates() {+ await waitFor(".discourse-local-date");+ await settled();+ }+module("dates without time", function () {test("formats weekdays within range 1 day before and 2 days after the specified day", async function (assert) {await render(@@ -70,6 +75,7 @@ module("Integration | Component | Dates", function (hooks) {</div></template>);+ await waitForDates();assert.dom(".event-dates")@@ -83,6 +89,7 @@ module("Integration | Component | Dates", function (hooks) {await render(<template><Dates @event={{events.currentYear.starts}} /></template>);+ await waitForDates();assert.dom(".event-dates")@@ -98,6 +105,7 @@ module("Integration | Component | Dates", function (hooks) {<Dates @event={{events.currentYear.endsSameWeek}} /></template>);+ await waitForDates();assert.dom(".event-dates")@@ -113,6 +121,7 @@ module("Integration | Component | Dates", function (hooks) {<Dates @event={{events.currentYear.endsSameMonth}} /></template>);+ await waitForDates();assert.dom(".event-dates")@@ -128,6 +137,7 @@ module("Integration | Component | Dates", function (hooks) {<Dates @event={{events.currentYear.endsDiffMonth}} /></template>);+ await waitForDates();assert.dom(".event-dates")@@ -141,6 +151,7 @@ module("Integration | Component | Dates", function (hooks) {await render(<template><Dates @event={{events.endsDiffYear}} /></template>);+ await waitForDates();assert.dom(".event-dates")@@ -160,6 +171,7 @@ module("Integration | Component | Dates", function (hooks) {</div></template>);+ await waitForDates();assert.dom(".event-dates")@@ -177,6 +189,7 @@ module("Integration | Component | Dates", function (hooks) {</div></template>);+ await waitForDates();assert.dom(".event-dates")@@ -186,7 +199,8 @@ module("Integration | Component | Dates", function (hooks) {);});- test("formats same day range", async function (assert) {+ // this test goes against the current implementation in local-dates+ skip("formats same day range", async function (assert) {await render(<template><Dates @event={{events.currentYear.endsSameDay}} /></template>);diff --git a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.jsindex b842236f012..42da7aa16f1 100644--- a/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js+++ b/plugins/discourse-local-dates/assets/javascripts/initializers/discourse-local-dates.js@@ -7,13 +7,8 @@ import { bind } from "discourse/lib/decorators";import { downloadCalendar } from "discourse/lib/download-calendar";import { iconHTML, renderIcon } from "discourse/lib/icon-library";import { withPluginApi } from "discourse/lib/plugin-api";-import {- addTagDecorateCallback,- addTextDecorateCallback,-} from "discourse/lib/to-markdown";import { slugify } from "discourse/lib/utilities";import { i18n } from "discourse-i18n";-import generateDateMarkup from "discourse/plugins/discourse-local-dates/lib/local-date-markup-generator";import LocalDatesCreateModal from "../discourse/components/modal/local-dates-create";import generateCurrentDateMarkup from "../lib/generate-current-date-markup";import LocalDateBuilder from "../lib/local-date-builder";@@ -104,25 +99,6 @@ function buildOptionsFromElement(element, siteSettings) {return opts;}-function buildOptionsFromMarkdownTag(element) {- const opts = {};-- // siteSettings defaults as used by buildOptionsFromElement are purposefully- // omitted to reproduce exactly what was on the original element- opts.time = element.attributes["data-time"];- opts.date = element.attributes["data-date"];- opts.recurring = element.attributes["data-recurring"];- opts.timezones = element.attributes["data-timezones"];- opts.timezone = element.attributes["data-timezone"];- opts.calendar = (element.attributes["data-calendar"] || "on") === "on";- opts.displayedTimezone = element.attributes["data-displayed-timezone"];- opts.format = element.attributes["data-format"];- opts.countdown = element.attributes["data-countdown"];- opts.range = element.attributes["data-range"];-- return opts;-}-function _rangeElements(element) {if (!element.parentElement) {return [];@@ -183,60 +159,6 @@ function initializeDiscourseLocalDates(api) {event.addText(generateCurrentDateMarkup(timezone));},});-- addTextDecorateCallback(- function (text, nextElement, _previousElement, metadata) {- if (- metadata.discourseLocalDateStartRangeOpts &&- nextElement?.attributes.class?.includes("discourse-local-date") &&- text === "→"- ) {- return "";- }- }- );- addTagDecorateCallback(function () {- if (this.element.attributes.class?.includes("discourse-local-date")) {- if (this.metadata.discourseLocalDateStartRangeOpts) {- const startRangeOpts = this.metadata.discourseLocalDateStartRangeOpts;- const endRangeOpts = buildOptionsFromMarkdownTag(this.element);- const markup = generateDateMarkup(- {- date: startRangeOpts.date,- time: startRangeOpts.time,- format: startRangeOpts.format,- },- endRangeOpts,- true,- {- date: endRangeOpts.date,- time: endRangeOpts.time,- format: endRangeOpts.format,- }- );- this.prefix = markup;- this.metadata.discourseLocalDateStartRangeOpts = null;- return "";- }- if (- this.element.attributes["data-range"] === "true" ||- this.element.attributes["data-range"] === "from" ||- this.element.attributes["data-range"] === "to"- ) {- this.metadata.discourseLocalDateStartRangeOpts =- buildOptionsFromMarkdownTag(this.element);- return "";- }- const opts = buildOptionsFromMarkdownTag(this.element, siteSettings);- const markup = generateDateMarkup(- { date: opts.date, time: opts.time, format: opts.format },- opts,- false- );- this.prefix = markup;- return "";- }- });}function buildHtmlPreview(element, siteSettings) {diff --git a/plugins/discourse-local-dates/assets/javascripts/lib/rich-editor-extension.js b/plugins/discourse-local-dates/assets/javascripts/lib/rich-editor-extension.jsindex f5ba52c7cae..ef4d7c95217 100644--- a/plugins/discourse-local-dates/assets/javascripts/lib/rich-editor-extension.js+++ b/plugins/discourse-local-dates/assets/javascripts/lib/rich-editor-extension.js@@ -1,6 +1,58 @@import { buildBBCodeAttrs } from "discourse/lib/text";import formatLocalDate from "./format-local-date";+function wrapDateRangeSpans(element) {+ const dateSpans = element.querySelectorAll(+ "span.discourse-local-date[data-range]"+ );+ const processed = new Set();++ for (const span of dateSpans) {+ if (processed.has(span) || span.closest(".discourse-local-date-range")) {+ continue;+ }++ const range = span.dataset.range;+ if (range !== "true" && range !== "from") {+ continue;+ }++ let toSpan = span.nextSibling;+ while (toSpan && toSpan.nodeType === Node.TEXT_NODE) {+ toSpan = toSpan.nextSibling;+ }++ if (+ !toSpan?.classList?.contains("discourse-local-date") ||+ !toSpan.dataset?.range+ ) {+ continue;+ }++ processed.add(span);+ processed.add(toSpan);++ span.dataset.range = "from";+ toSpan.dataset.range = "to";++ const doc =+ element.nodeType === Node.DOCUMENT_NODE ? element : element.ownerDocument;+ const wrapper = doc.createElement("span");+ wrapper.className = "discourse-local-date-range";+ span.parentNode.insertBefore(wrapper, span);+ wrapper.appendChild(span);++ let node = wrapper.nextSibling;+ while (node && node !== toSpan) {+ const next = node.nextSibling;+ wrapper.appendChild(node);+ node = next;+ }++ wrapper.appendChild(toSpan);+ }+}+const OPTIONAL_DATA_ATTRS = ["format","recurring",@@ -64,6 +116,10 @@ const extension = {{tag: "span.discourse-local-date[data-date]",getAttrs: (dom) => {+ // Skip spans that are part of a range (handled by local_date_range via wrapper)+ if (dom.dataset.range) {+ return false;+ }return {date: dom.dataset.date,time: dom.dataset.time,@@ -279,6 +335,11 @@ const extension = {},};},++ // Pre-process HTML to wrap adjacent date-range spans before parsing+ transformParsedHTML(element) {+ wrapDateRangeSpans(element);+ },};export default extension;diff --git a/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-quoting-test.js b/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-quoting-test.jsindex 6ee43804ff5..afab4ca1fdb 100644--- a/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-quoting-test.js+++ b/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-quoting-test.js@@ -89,7 +89,7 @@ acceptance("quoting range", function (needs) {await click(".insert-quote");assert.dom(".d-editor-input").hasValue(`[quote=\"uwe_keim, post:1, topic:280\"]-Some text [date-range from=2022-06-17T09:30:00 to=2022-06-18T10:30:00 format=LL timezone=Australia/Brisbane timezones=Africa/Accra|Australia/Brisbane|Europe/Paris]+Some text [date-range from=2022-06-17T09:30:00 to=2022-06-18T10:30:00 timezone=Australia/Brisbane format=LL timezones=Africa/Accra|Australia/Brisbane|Europe/Paris][/quote]\n\n`,"converts the date range to markdown with all options correctly");@@ -131,7 +131,7 @@ acceptance("quoting with recurring and countdown", function (needs) {await click(".insert-quote");assert.dom(".d-editor-input").hasValue(`[quote=\"uwe_keim, post:1, topic:280\"]-Testing countdown [date=2022-06-21 time=09:30:00 format=LL timezone=Australia/Brisbane countdown=true]+Testing countdown [date=2022-06-21 time=09:30:00 timezone=Australia/Brisbane format=LL countdown=true]Testing recurring [date=2022-06-22 timezone=Australia/Brisbane recurring=2.weeks][/quote]\n\n`,diff --git a/plugins/discourse-math/assets/javascripts/lib/rich-editor-extension.js b/plugins/discourse-math/assets/javascripts/lib/rich-editor-extension.jsindex d1e95b2bb6a..d0943781ab2 100644--- a/plugins/discourse-math/assets/javascripts/lib/rich-editor-extension.js+++ b/plugins/discourse-math/assets/javascripts/lib/rich-editor-extension.js@@ -175,14 +175,14 @@ const extension = {{tag: "span.math",getAttrs: (dom) => ({- text: dom.textContent,+ text: dom.textContent.trim(),mathType: "tex",}),},{tag: "span.asciimath",getAttrs: (dom) => ({- text: dom.textContent,+ text: dom.textContent.trim(),mathType: "asciimath",}),},@@ -207,7 +207,7 @@ const extension = {{tag: "div.math",getAttrs: (dom) => ({- text: dom.textContent,+ text: dom.textContent.trim(),mathType: "tex",}),},diff --git a/plugins/discourse-math/test/javascripts/unit/lib/to-markdown-math-test.js b/plugins/discourse-math/test/javascripts/unit/lib/to-markdown-math-test.jsnew file mode 100644index 00000000000..884871b3428--- /dev/null+++ b/plugins/discourse-math/test/javascripts/unit/lib/to-markdown-math-test.js@@ -0,0 +1,44 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import {+ getExtensions,+ registerRichEditorExtension,+ resetRichEditorExtensions,+} from "discourse/lib/composer/rich-editor-extensions";+import toMarkdown from "discourse/lib/to-markdown";+import mathExtension from "discourse/plugins/discourse-math/lib/rich-editor-extension";++module("Unit | Lib | to-markdown-math", function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(async function () {+ await resetRichEditorExtensions();+ if (!getExtensions().includes(mathExtension)) {+ registerRichEditorExtension(mathExtension);+ }+ });++ test("converts inline mathjax to markdown", async function (assert) {+ const html = `<p>Lorem ipsum <span class="math" data-applied-mathjax="true" style="display: none;">E=mc^2</span><span class="math-container inline-math mathjax-math" style=""><mjx-container class="MathJax" jax="SVG"><svg></svg></mjx-container></span> dolor sit amet.</p>`;+ const markdown = `Lorem ipsum $E=mc^2$ dolor sit amet.`;+ assert.strictEqual(await toMarkdown(html), markdown);+ });++ test("converts block mathjax to markdown", async function (assert) {+ const html = `<p>Before</p>+ <div class="math" data-applied-mathjax="true" style="display: none;">+ \\sqrt{(-1)} \\; 2^3 \\; \\sum \\; \\pi+ </div><div class="math-container block-math mathjax-math" style=""><mjx-container class="MathJax" jax="SVG" display="true"><svg></svg></mjx-container></div>+ <p>After</p>`;++ const markdown = `Before++$$+\\sqrt{(-1)} \\; 2^3 \\; \\sum \\; \\pi+$$++After`;++ assert.strictEqual(await toMarkdown(html), markdown);+ });+});diff --git a/plugins/spoiler-alert/assets/javascripts/initializers/spoiler-alert.js b/plugins/spoiler-alert/assets/javascripts/initializers/spoiler-alert.jsindex c36d05589a3..725c80eb62b 100644--- a/plugins/spoiler-alert/assets/javascripts/initializers/spoiler-alert.js+++ b/plugins/spoiler-alert/assets/javascripts/initializers/spoiler-alert.js@@ -1,8 +1,4 @@import { withPluginApi } from "discourse/lib/plugin-api";-import {- addBlockDecorateCallback,- addTagDecorateCallback,-} from "discourse/lib/to-markdown";import applySpoiler from "discourse/plugins/spoiler-alert/lib/apply-spoiler";import richEditorExtension from "../lib/rich-editor-extension";@@ -34,25 +30,6 @@ export function initializeSpoiler(api) {},});- addTagDecorateCallback(function () {- const { attributes } = this.element;-- if (/\bspoiled\b/.test(attributes.class)) {- this.prefix = "[spoiler]";- this.suffix = "[/spoiler]";- }- });-- addBlockDecorateCallback(function (text) {- const { name, attributes } = this.element;-- if (name === "div" && /\bspoiled\b/.test(attributes.class)) {- this.prefix = "[spoiler]\n";- this.suffix = "\n[/spoiler]";- return text.trim();- }- });-api.registerRichEditorExtension(richEditorExtension);}diff --git a/plugins/spoiler-alert/assets/stylesheets/discourse_spoiler_alert.scss b/plugins/spoiler-alert/assets/stylesheets/discourse_spoiler_alert.scssindex 6940350f569..098b03632d7 100644--- a/plugins/spoiler-alert/assets/stylesheets/discourse_spoiler_alert.scss+++ b/plugins/spoiler-alert/assets/stylesheets/discourse_spoiler_alert.scss@@ -14,6 +14,11 @@.spoiler-blurred {@include unselectable;cursor: pointer;++ // Allow text selection in cooked post content for quoting+ .cooked & {+ user-select: text;+ }filter: blur(0.5em);a,diff --git a/plugins/spoiler-alert/test/javascripts/unit/lib/to-markdown-test.js b/plugins/spoiler-alert/test/javascripts/unit/lib/to-markdown-test.jsindex d1a2af0f39a..2a596b8989c 100644--- a/plugins/spoiler-alert/test/javascripts/unit/lib/to-markdown-test.js+++ b/plugins/spoiler-alert/test/javascripts/unit/lib/to-markdown-test.js@@ -1,23 +1,31 @@import { module, test } from "qunit";-import { withPluginApi } from "discourse/lib/plugin-api";+import {+ registerRichEditorExtension,+ resetRichEditorExtensions,+} from "discourse/lib/composer/rich-editor-extensions";import toMarkdown from "discourse/lib/to-markdown";-import { initializeSpoiler } from "discourse/plugins/spoiler-alert/initializers/spoiler-alert";+import richEditorExtension from "discourse/plugins/spoiler-alert/lib/rich-editor-extension";-module("Unit | to-markdown", function (hooks) {- hooks.beforeEach(function () {- withPluginApi(initializeSpoiler);+module("Spoiler Alert | Unit | to-markdown", function (hooks) {+ hooks.beforeEach(async function () {+ await resetRichEditorExtensions();+ registerRichEditorExtension(richEditorExtension);});- test("handles spoiler tags", function (assert) {+ test("handles spoiler tags", async function (assert) {let html = `<div>Text with a</div><div class="spoiled spoiler-blurred">spoiled</div><div>word.</div>`;- let markdown = `Text with a\n\n[spoiler]\nspoiled\n[/spoiler]\n\nword.`;+ let markdown = `Text with a\n\n[spoiler]\nspoiled\n\n[/spoiler]\n\nword.`;- assert.strictEqual(toMarkdown(html), markdown, "creates block spoiler tag");+ assert.strictEqual(+ await toMarkdown(html),+ markdown,+ "creates block spoiler tag"+ );html = `Inline <span class="spoiled">spoiled</span> word.`;markdown = `Inline [spoiler]spoiled[/spoiler] word.`;assert.strictEqual(- toMarkdown(html),+ await toMarkdown(html),markdown,"creates inline spoiler tag");diff --git a/spec/support/system_helpers.rb b/spec/support/system_helpers.rbindex b6cbe82990f..c2b20cfef4f 100644--- a/spec/support/system_helpers.rb+++ b/spec/support/system_helpers.rb@@ -155,6 +155,19 @@ module SystemHelpersendend+ def select_all_content(selector)+ js = <<-JS+ const el = document.querySelector(arguments[0]);+ const selection = window.getSelection();+ const range = document.createRange();+ range.selectNodeContents(el);+ selection.removeAllRanges();+ selection.addRange(range);+ JS++ page.execute_script(js, selector)+ end+def select_text_range(selector, start = 0, offset = 5)expect(page).to have_selector(selector)diff --git a/spec/system/composer/prosemirror_pasting_spec.rb b/spec/system/composer/prosemirror_pasting_spec.rbindex 654dce98c77..8aed1427d9e 100644--- a/spec/system/composer/prosemirror_pasting_spec.rb+++ b/spec/system/composer/prosemirror_pasting_spec.rb@@ -38,7 +38,7 @@ describe "Composer - ProseMirror - Pasting content" doopen_composercdp.copy_paste('<img src="image.png" alt="alt text" data-base62-sha1="1234567890">', html: true)expect(rich).to have_css(- "img[src$='image.png'][alt='alt text'][data-orig-src='upload://1234567890']",+ "img[src$='image.png'][alt='alt text'][data-orig-src='upload://1234567890.png']",)end@@ -371,12 +371,9 @@ describe "Composer - ProseMirror - Pasting content" docomposer.toggle_rich_editor- # The inner table header should be properly normalized+ # Nested tables are flattened - the inner table content is preserved+ # The outer table structure is dropped since ProseMirror can't represent nested tablesmarkdown = <<~MARKDOWN- | |- |----|--| CLOSED DOWNSTREAM | ||----|----|| | |@@ -384,10 +381,6 @@ describe "Composer - ProseMirror - Pasting content" do| Product: | name || Component: | general |-- | | |- |----|----|-MARKDOWNexpect(composer).to have_value(markdown)@@ -410,7 +403,7 @@ describe "Composer - ProseMirror - Pasting content" docomposer.toggle_rich_editor- expect(composer).to have_value("| |\n|----|\n\nAfter table")+ expect(composer).to have_value("After table")endendenddiff --git a/spec/system/post_selection_copy_quote_spec.rb b/spec/system/post_selection_copy_quote_spec.rbindex 96950d751c7..3a2fee54596 100644--- a/spec/system/post_selection_copy_quote_spec.rb+++ b/spec/system/post_selection_copy_quote_spec.rb@@ -4,11 +4,45 @@ describe "Post selection | Copy quote" dolet(:topic_page) { PageObjects::Pages::Topic.new }let(:composer) { PageObjects::Components::Composer.new }let(:cdp) { PageObjects::CDP.new }+ let(:toasts) { PageObjects::Components::Toasts.new }fab!(:topic)- fab!(:post) { Fabricate(:post, topic: topic, raw: "Hello world it's time for quoting!") }+ fab!(:post) { Fabricate(:post, topic: topic, raw: "Hello world this is time for quoting") }fab!(:current_user, :admin)+ def select_list_items(post_selector, start_li_index, end_li_index)+ js = <<-JS+ const cooked = document.querySelector(arguments[0]);+ const listItems = cooked.querySelectorAll('li');+ const startLi = listItems[arguments[1]];+ const endLi = listItems[arguments[2]];++ // Find the first text node in the li (handles both tight and loose lists)+ function findTextNode(element) {+ if (element.nodeType === Node.TEXT_NODE && element.textContent.trim()) {+ return element;+ }+ for (const child of element.childNodes) {+ const found = findTextNode(child);+ if (found) return found;+ }+ return null;+ }++ const startNode = findTextNode(startLi);+ const endNode = findTextNode(endLi);++ const selection = window.getSelection();+ const range = document.createRange();+ range.setStart(startNode, 0);+ range.setEnd(endNode, endNode.textContent.length);+ selection.removeAllRanges();+ selection.addRange(range);+ JS++ page.execute_script(js, post_selector, start_li_index, end_li_index)+ end+context "when logged in" dobefore dosign_in(current_user)@@ -20,6 +54,7 @@ describe "Post selection | Copy quote" doselect_text_range("#{topic_page.post_by_number_selector(1)} .cooked p", 0, 10)topic_page.copy_quote_button.click+ expect(toasts).to have_success(I18n.t("js.post.quote_copied_to_clipboard"))cdp.clipboard_has_text?(<<~QUOTE.chomp, chomp: true)[quote=\"#{post.user.username}, post:1, topic:#{topic.id}\"]\nHello worl\n[/quote]\n@@ -45,6 +80,85 @@ describe "Post selection | Copy quote" doexpect(composer).to have_value("")end++ it "inserts a partial quote when selecting part of post content" do+ topic_page.visit_topic(topic)++ select_text_range("#{topic_page.post_by_number_selector(1)} .cooked p", 0, 10)++ find(".quote-button .insert-quote").click+ expect(composer).to have_value(+ %([quote="#{post.user.username}, post:1, topic:#{topic.id}"]\nHello worl\n[/quote]\n\n),+ )+ end++ it "quotes formatted content as markdown with full:true" do+ formatted_post = Fabricate(:post, topic: topic, raw: "This is **bold** and *italic* text")+ topic_page.visit_topic(topic)++ select_all_content(+ "#{topic_page.post_by_number_selector(formatted_post.post_number)} .cooked",+ )++ find(".quote-button .insert-quote").click+ composer_value = find(".d-editor-input", visible: :all).value+ expect(composer_value).to include("full:true")+ expect(composer_value).to include("**bold**")+ expect(composer_value).to include("*italic*")+ end++ it "preserves list formatting when quoting various list types" do+ # Post with multiple list types:+ # - Loose list (items 0-1): has blank lines between items+ # - Tight list (items 2-3): no blank lines+ # - Nested list with custom start (items 4-7): 100. with nested tight bullet list+ list_post = Fabricate(:post, topic: topic, raw: <<~MD, user: current_user)+ Loose list:++ 1. First loose++ 2. Second loose++ Tight list:++ 1. First tight+ 2. Second tight++ Nested with start:++ 100. Hundred+ 101. Hundred one+ - nested hello+ - nested world+ MD++ topic_page.visit_topic(topic)+ post_selector = "#{topic_page.post_by_number_selector(list_post.post_number)} .cooked"++ # Test 1: Loose list stays loose (items 0-1)+ select_list_items(post_selector, 0, 1)+ topic_page.copy_quote_button.click+ expect(toasts).to have_success(I18n.t("js.post.quote_copied_to_clipboard"))+ cdp.clipboard_has_text?("1. First loose\n\n2. Second loose", strict: false)++ # Test 2: Tight list stays tight (items 2-3)+ select_list_items(post_selector, 2, 3)+ topic_page.copy_quote_button.click+ expect(toasts).to have_success(I18n.t("js.post.quote_copied_to_clipboard"))+ cdp.clipboard_has_text?("1. First tight\n2. Second tight", strict: false)++ # Test 3: Nested list preserves start number (items 4-5 are 100, 101)+ select_list_items(post_selector, 4, 5)+ topic_page.copy_quote_button.click+ expect(toasts).to have_success(I18n.t("js.post.quote_copied_to_clipboard"))+ cdp.clipboard_has_text?("100. Hundred\n101. Hundred one", strict: false)++ # Test 4: Nested tight bullet list stays tight (items 6-7)+ select_list_items(post_selector, 6, 7)+ topic_page.copy_quote_button.click+ expect(toasts).to have_success(I18n.t("js.post.quote_copied_to_clipboard"))+ cdp.clipboard_has_text?("* nested hello\n* nested world", strict: false)+ endendcontext "when anon" do