Skip to content

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

FIX: Prevent stray list marker when quoting a quote containing a list (#41532)

Renato Atilio76e92eace8b5
diff --git a/frontend/discourse/app/lib/selection/preserve-list-structure.js b/frontend/discourse/app/lib/selection/preserve-list-structure.js
index 124e03e7816..208b35e3bb4 100644
--- a/frontend/discourse/app/lib/selection/preserve-list-structure.js
+++ b/frontend/discourse/app/lib/selection/preserve-list-structure.js
@@ -322,8 +322,15 @@ export function preserveListStructureInClonedContent(container, range) {
annotateOrphanListItemsWithOriginalInfo(container, range);
}
- if (originalListItem) {
- const parentList = originalListItem.parentElement;
+ // When the list's wrapper is itself inside the clone (e.g. a list nested in a
+ // fully-selected quote), the structure is intact and must not be re-wrapped.
+ const parentList = originalListItem?.parentElement;
+ const listWrapperInClone =
+ parentList &&
+ parentList !== range.commonAncestorContainer &&
+ range.commonAncestorContainer.contains(parentList);
+
+ if (originalListItem && !listWrapperInClone) {
const isParentAList =
parentList?.tagName === "OL" || parentList?.tagName === "UL";
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.js
index 5181f1b745c..c27cfc32814 100644
--- a/frontend/discourse/tests/unit/lib/selection/preserve-list-structure-test.js
+++ b/frontend/discourse/tests/unit/lib/selection/preserve-list-structure-test.js
@@ -167,10 +167,11 @@ module("Unit | Lib | selection | preserve-list-structure", function () {
const original = createContainer("<ul><li>Hello world</li></ul>");
document.body.appendChild(original);
- // Simulate selecting just the text node inside the LI
+ // A partial selection within a single text node has that text node as its
+ // common ancestor, so the <li>/<ul> wrapper is not part of the clone.
const textNode = original.querySelector("li").firstChild;
const container = createContainer("world");
- const range = mockRange(original, textNode, 6);
+ const range = mockRange(textNode, textNode, 6);
preserveListStructureInClonedContent(container, range);
@@ -187,6 +188,43 @@ module("Unit | Lib | selection | preserve-list-structure", function () {
document.body.removeChild(original);
});
+ test("does not re-wrap when the start list is nested in a fully-selected quote", function (assert) {
+ // Selection starts inside the nested <li>, but the whole quote (and its
+ // list) is selected, so the cloned fragment already has the list wrapper.
+ const original = createContainer(
+ '<aside class="quote"><blockquote><ul><li>Mark wish</li></ul></blockquote></aside>' +
+ "<p>And signup abuse</p>"
+ );
+ document.body.appendChild(original);
+
+ const startTextNode = original.querySelector("li").firstChild;
+ const container = createContainer(
+ '<aside class="quote"><blockquote><ul><li>Mark wish</li></ul></blockquote></aside>' +
+ "<p>And signup abuse</p>"
+ );
+ const range = mockRange(original, startTextNode, 0);
+
+ preserveListStructureInClonedContent(container, range);
+
+ assert.strictEqual(
+ container.querySelectorAll(":scope > ul, :scope > ol").length,
+ 0,
+ "no stray top-level list is introduced around the quote"
+ );
+ assert.strictEqual(
+ container.firstElementChild?.tagName,
+ "ASIDE",
+ "the quote remains the first top-level node"
+ );
+ assert.strictEqual(
+ container.querySelector("aside ul li")?.textContent.trim(),
+ "Mark wish",
+ "the list stays nested inside the quote"
+ );
+
+ 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>' +