Skip to content

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

DEV: Preserve `details` state in cooked HTML (#33867)

Sérgio Saquetim3725568e62b1
diff --git a/app/assets/javascripts/discourse/app/components/post/cooked-html.gjs b/app/assets/javascripts/discourse/app/components/post/cooked-html.gjs
index bfadfb1e37e..29ef0ecf6d1 100644
--- a/app/assets/javascripts/discourse/app/components/post/cooked-html.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/cooked-html.gjs
@@ -14,11 +14,13 @@ import decorateMentions from "discourse/lib/post-cooked-html-decorators/mentions
import decorateQuoteControls from "discourse/lib/post-cooked-html-decorators/quote-controls";
import decorateSearchHighlight from "discourse/lib/post-cooked-html-decorators/search-highlight";
import decorateSelectionBarrier from "discourse/lib/post-cooked-html-decorators/selection-barrier";
+import decorateStatefulHtmlElements from "discourse/lib/post-cooked-html-decorators/stateful-html-elements";
import { i18n } from "discourse-i18n";
const detachedDocument = document.implementation.createHTMLDocument("detached");
const POST_COOKED_DECORATORS = [
+ decorateStatefulHtmlElements,
decorateQuoteControls,
decorateLinkCounts,
decorateSearchHighlight,
diff --git a/app/assets/javascripts/discourse/app/lib/post-cooked-html-decorators/stateful-html-elements.js b/app/assets/javascripts/discourse/app/lib/post-cooked-html-decorators/stateful-html-elements.js
new file mode 100644
index 00000000000..41d0e6645a0
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/lib/post-cooked-html-decorators/stateful-html-elements.js
@@ -0,0 +1,45 @@
+export default function (element, context) {
+ restoreOpenedDetails(element, context);
+}
+
+function restoreOpenedDetails(element, context) {
+ const details = element.querySelectorAll("details");
+
+ details.forEach((detailElement, index) => {
+ const id = tagId(detailElement, context, index);
+ const state = context.decoratorState;
+
+ if (state.has(id)) {
+ detailElement.open = true;
+ }
+
+ detailElement.addEventListener("toggle", (event) => {
+ if (event.target.open) {
+ state.set(id, true);
+ } else {
+ state.delete(id);
+ }
+ });
+ });
+}
+
+export function tagId(
+ element,
+ context,
+ index,
+ { topicId, postId, prefix = "" } = {}
+) {
+ if (element.id) {
+ // If the element already has an ID, we don't want to overwrite it.
+ return element.id;
+ }
+
+ prefix ||= element.tagName.toLowerCase();
+ topicId ||= context.post.topic_id || "";
+ postId ||= context.post.post_number;
+
+ const id = `post-cooked-html__${prefix}-${topicId}-${postId}-${index}`;
+ element.id = id;
+
+ return id;
+}
diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/cooked-html-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/cooked-html-test.gjs
index 7d8c0b312bc..f9d909eea6e 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/post/cooked-html-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/post/cooked-html-test.gjs
@@ -1,5 +1,5 @@
import { getOwner } from "@ember/owner";
-import { render } from "@ember/test-helpers";
+import { click, render, settled } from "@ember/test-helpers";
import { module, test } from "qunit";
import PostCookedHtml from "discourse/components/post/cooked-html";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
@@ -45,4 +45,65 @@ module("Integration | Component | Post | PostCookedHtml", function (hooks) {
assert.dom(".quote-controls").doesNotExist();
assert.dom("blockquote").hasText("abcd");
});
+
+ test("it keeps the opened state of the `details` tag between renders", async function (assert) {
+ this.post.cooked = `<details><summary>Quote Summary</summary><p>Quote Content</p></details>`;
+
+ await renderComponent(this.post);
+
+ assert
+ .dom("details")
+ .exists()
+ .doesNotHaveAttribute("open", "the details tag is closed by default");
+
+ await click("details > summary");
+ assert
+ .dom("details")
+ .hasAttribute(
+ "open",
+ "",
+ "the details tag is opened after clicking the summary"
+ );
+
+ this.post.cooked += '<p class="new-content">New Content</p>';
+ await settled();
+ assert
+ .dom("p.new-content")
+ .exists()
+ .hasText("New Content", "ensure the cooked content was updated");
+ assert
+ .dom("details")
+ .exists()
+ .hasAttribute(
+ "open",
+ "",
+ "the details tag remains opened after re-rendering"
+ );
+
+ await click("details > summary");
+ assert
+ .dom("details")
+ .doesNotHaveAttribute(
+ "open",
+ "the details tag is closed after clicking the summary again"
+ );
+
+ this.post.cooked += '<p class="another-content">Another Content</p>';
+ await settled();
+
+ assert
+ .dom("p.another-content")
+ .exists()
+ .hasText(
+ "Another Content",
+ "ensure the cooked content was updated again"
+ );
+ assert
+ .dom("details")
+ .exists()
+ .doesNotHaveAttribute(
+ "open",
+ "the details tag remains closed after re-rendering"
+ );
+ });
});