Skip to content

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

FIX: Add local date support in poll options and improve rich editor date formatting (#36718)

Régis Hanol6a7825baf3bb
diff --git a/frontend/discourse/app/components/decorated-html.gjs b/frontend/discourse/app/components/decorated-html.gjs
index 78b0b725cc1..2bdba1762f8 100644
--- a/frontend/discourse/app/components/decorated-html.gjs
+++ b/frontend/discourse/app/components/decorated-html.gjs
@@ -16,6 +16,17 @@ const detachedDocument = document.implementation.createHTMLDocument("detached");
/** @type {Symbol} Default decorator type used when no specific type is provided */
export const NON_STREAM_HTML_DECORATOR = Symbol("non-stream");
+/** Helper object for contexts without a post/model. Use with `applyHtmlDecorators`. */
+export const NULL_HELPER = Object.freeze({
+ getModel: () => null,
+ get model() {
+ return null;
+ },
+ get context() {
+ return null;
+ },
+});
+
/** @type {Object.<Symbol|string, Function[]>} Storage for HTML decorators organized by type */
let htmlDecorators = {};
@@ -57,24 +68,14 @@ export function registerHtmlDecorator(decorator, type) {
* Applies registered HTML decorators to a DOM element and returns their cleanup functions.
*
* @param {HTMLElement} element - The DOM element to apply decorators to
- * @param {Object} options - Options object passed to each decorator function
+ * @param {Object} helper - Helper object passed to each decorator. Use NULL_HELPER for contexts without a model.
* @param {Symbol|string} [type=NON_STREAM_HTML_DECORATOR] - The type of decorators to apply.
- * When not provided, defaults to NON_STREAM_HTML_DECORATOR (non-stream).
* @returns {Function[]} Array of cleanup functions from decorators that returned them.
- * Each function, when called, will undo the decorator's effects.
- * @example
- * const cleanupFunctions = applyHtmlDecorators(
- * document.querySelector('.content'),
- * { highlight: true }
- * );
- *
- * // To clean up all decorations:
- * cleanupFunctions.forEach(cleanup => cleanup());
*/
-export function applyHtmlDecorators(element, options, type) {
+export function applyHtmlDecorators(element, helper, type) {
return getHtmlDecoratorsForType(type)
.map((decorator) => {
- return decorator(element, options);
+ return decorator(element, helper);
})
.filter((fn) => typeof fn === "function");
}
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.js
index 912403c3b0d..e87cd6b15ba 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
@@ -14,6 +14,7 @@ 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";
import richEditorExtension from "../lib/rich-editor-extension";
@@ -176,10 +177,7 @@ function initializeDiscourseLocalDates(api) {
alwaysShowShortcut: true,
shortcutAction: (event) => {
const timezone = api.getCurrentUser().user_option.timezone;
- const time = moment().format("HH:mm:ss");
- const date = moment().format("YYYY-MM-DD");
-
- event.addText(`[date=${date} time=${time} timezone="${timezone}"]`);
+ event.addText(generateCurrentDateMarkup(timezone));
},
});
diff --git a/plugins/discourse-local-dates/assets/javascripts/lib/format-local-date.js b/plugins/discourse-local-dates/assets/javascripts/lib/format-local-date.js
new file mode 100644
index 00000000000..ffd17e336c7
--- /dev/null
+++ b/plugins/discourse-local-dates/assets/javascripts/lib/format-local-date.js
@@ -0,0 +1,17 @@
+import LocalDateBuilder from "./local-date-builder";
+
+/**
+ * @param {string} date - Date in YYYY-MM-DD format
+ * @param {string} [time] - Time in HH:mm:ss format
+ * @param {string} [timezone] - Timezone string (defaults to local timezone)
+ * @returns {{ formatted: string, pastEvent: boolean }}
+ */
+export default function formatLocalDate(date, time, timezone) {
+ const localTimezone = moment.tz.guess();
+ const builder = new LocalDateBuilder(
+ { date, time, timezone: timezone || localTimezone, calendar: true },
+ localTimezone
+ );
+ const { formatted, pastEvent } = builder.build();
+ return { formatted, pastEvent };
+}
diff --git a/plugins/discourse-local-dates/assets/javascripts/lib/generate-current-date-markup.js b/plugins/discourse-local-dates/assets/javascripts/lib/generate-current-date-markup.js
new file mode 100644
index 00000000000..05e7fab5602
--- /dev/null
+++ b/plugins/discourse-local-dates/assets/javascripts/lib/generate-current-date-markup.js
@@ -0,0 +1,11 @@
+/**
+ * Generates BBCode markup for the current date/time.
+ * @param {string} [userTimezone] - User's timezone (defaults to browser guess)
+ * @returns {string} BBCode markup
+ */
+export default function generateCurrentDateMarkup(userTimezone) {
+ const timezone = userTimezone || moment.tz.guess();
+ const date = moment().format("YYYY-MM-DD");
+ const time = moment().format("HH:mm:ss");
+ return `[date=${date} time=${time} timezone="${timezone}"]`;
+}
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.js
index 7892cc60da8..0136dad43a2 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,7 @@
+import formatLocalDate from "./format-local-date";
+
/** @type {RichEditorExtension} */
const extension = {
- // TODO(renato): the rendered date needs to be localized to better match the cooked content
nodeSpec: {
local_date: {
attrs: { date: {}, time: {}, timezone: { default: null } },
@@ -19,7 +20,11 @@ const extension = {
},
],
toDOM: (node) => {
- const optionalTime = node.attrs.time ? ` ${node.attrs.time}` : "";
+ const { formatted } = formatLocalDate(
+ node.attrs.date,
+ node.attrs.time,
+ node.attrs.timezone
+ );
return [
"span",
{
@@ -28,7 +33,7 @@ const extension = {
"data-time": node.attrs.time,
"data-timezone": node.attrs.timezone,
},
- `${node.attrs.date}${optionalTime}`,
+ formatted,
];
},
},
@@ -57,10 +62,16 @@ const extension = {
},
],
toDOM: (node) => {
- const fromTimeStr = node.attrs.fromTime
- ? ` ${node.attrs.fromTime}`
- : "";
- const toTimeStr = node.attrs.toTime ? ` ${node.attrs.toTime}` : "";
+ const { formatted: formattedFrom } = formatLocalDate(
+ node.attrs.fromDate,
+ node.attrs.fromTime,
+ node.attrs.timezone
+ );
+ const { formatted: formattedTo } = formatLocalDate(
+ node.attrs.toDate,
+ node.attrs.toTime,
+ node.attrs.timezone
+ );
return [
"span",
{ class: "discourse-local-date-range" },
@@ -73,7 +84,7 @@ const extension = {
"data-time": node.attrs.fromTime,
"data-timezone": node.attrs.timezone,
},
- `${node.attrs.fromDate}${fromTimeStr}`,
+ formattedFrom,
],
" → ",
[
@@ -85,7 +96,7 @@ const extension = {
"data-time": node.attrs.toTime,
"data-timezone": node.attrs.timezone,
},
- `${node.attrs.toDate}${toTimeStr}`,
+ formattedTo,
],
];
},
diff --git a/plugins/discourse-local-dates/spec/system/rich_editor_extension_spec.rb b/plugins/discourse-local-dates/spec/system/rich_editor_extension_spec.rb
index c5667b70836..c421e250ee6 100644
--- a/plugins/discourse-local-dates/spec/system/rich_editor_extension_spec.rb
+++ b/plugins/discourse-local-dates/spec/system/rich_editor_extension_spec.rb
@@ -28,8 +28,7 @@ describe "Composer - ProseMirror editor - Local Dates extension", type: :system
MARKDOWN
expect(rich).to have_css(
- "span.discourse-local-date[data-timezone='Asia/Singapore']",
- text: "2022-12-15 14:19:00",
+ "span.discourse-local-date[data-date='2022-12-15'][data-time='14:19:00'][data-timezone='Asia/Singapore']",
)
end
@@ -44,12 +43,10 @@ describe "Composer - ProseMirror editor - Local Dates extension", type: :system
expect(rich).to have_css("span.discourse-local-date-range")
expect(rich).to have_css(
- "span.discourse-local-date[data-timezone='Asia/Singapore'][data-range='from']",
- text: "2022-12-15 14:19:00",
+ "span.discourse-local-date[data-date='2022-12-15'][data-time='14:19:00'][data-timezone='Asia/Singapore'][data-range='from']",
)
expect(rich).to have_css(
- "span.discourse-local-date[data-timezone='Asia/Singapore'][data-range='to']",
- text: "2022-12-16 15:20:00",
+ "span.discourse-local-date[data-date='2022-12-16'][data-time='15:20:00'][data-timezone='Asia/Singapore'][data-range='to']",
)
end
end
diff --git a/plugins/discourse-local-dates/test/javascripts/integration/rich-editor-extension-test.js b/plugins/discourse-local-dates/test/javascripts/integration/rich-editor-extension-test.js
index 6b47a56ccf6..fc8758c3835 100644
--- a/plugins/discourse-local-dates/test/javascripts/integration/rich-editor-extension-test.js
+++ b/plugins/discourse-local-dates/test/javascripts/integration/rich-editor-extension-test.js
@@ -7,32 +7,100 @@ module(
function (hooks) {
setupRenderingTest(hooks);
- Object.entries({
- "local date": [
- "[date=2021-01-01 time=12:00:00]",
- '<p><span class="discourse-local-date cooked-date" data-date="2021-01-01" data-time="12:00:00" contenteditable="false">2021-01-01 12:00:00</span></p>',
- "[date=2021-01-01 time=12:00:00]",
- ],
- "local date with timezone": [
- '[date=2021-01-01 time=12:00:00 timezone="America/New_York"]',
- '<p><span class="discourse-local-date cooked-date" data-date="2021-01-01" data-time="12:00:00" data-timezone="America/New_York" contenteditable="false">2021-01-01 12:00:00</span></p>',
- '[date=2021-01-01 time=12:00:00 timezone="America/New_York"]',
- ],
- "local date range": [
- "[date-range from=2021-01-01 to=2021-01-02]",
- '<p><span class="discourse-local-date-range" contenteditable="false"><span class="discourse-local-date cooked-date" data-range="from" data-date="2021-01-01">2021-01-01</span> → <span class="discourse-local-date cooked-date" data-range="to" data-date="2021-01-02">2021-01-02</span></span></p>',
- "[date-range from=2021-01-01 to=2021-01-02]",
- ],
- "local date range with time": [
- '[date-range from=2021-01-01T12:00:00 to=2021-01-02T13:00:00 timezone="America/New_York"]',
- '<p><span class="discourse-local-date-range" contenteditable="false"><span class="discourse-local-date cooked-date" data-range="from" data-date="2021-01-01" data-time="12:00:00" data-timezone="America/New_York">2021-01-01 12:00:00</span> → <span class="discourse-local-date cooked-date" data-range="to" data-date="2021-01-02" data-time="13:00:00" data-timezone="America/New_York">2021-01-02 13:00:00</span></span></p>',
- '[date-range from=2021-01-01T12:00:00 to=2021-01-02T13:00:00 timezone="America/New_York"]',
- ],
- }).forEach(([name, [markdown, html, expectedMarkdown]]) => {
- test(name, async function (assert) {
- this.siteSettings.rich_editor = true;
- await testMarkdown(assert, markdown, html, expectedMarkdown);
- });
+ test("local date", async function (assert) {
+ this.siteSettings.rich_editor = true;
+ const markdown = "[date=2021-01-01 time=12:00:00]";
+
+ await testMarkdown(
+ assert,
+ markdown,
+ (innerAssert) => {
+ const span = document.querySelector(
+ ".ProseMirror .discourse-local-date"
+ );
+ innerAssert.ok(span);
+ innerAssert.strictEqual(span.dataset.date, "2021-01-01");
+ innerAssert.strictEqual(span.dataset.time, "12:00:00");
+ innerAssert.ok(span.textContent.length > 0);
+ },
+ markdown
+ );
+ });
+
+ test("local date with timezone", async function (assert) {
+ this.siteSettings.rich_editor = true;
+ const markdown =
+ '[date=2021-01-01 time=12:00:00 timezone="America/New_York"]';
+
+ await testMarkdown(
+ assert,
+ markdown,
+ (innerAssert) => {
+ const span = document.querySelector(
+ ".ProseMirror .discourse-local-date"
+ );
+ innerAssert.ok(span);
+ innerAssert.strictEqual(span.dataset.date, "2021-01-01");
+ innerAssert.strictEqual(span.dataset.time, "12:00:00");
+ innerAssert.strictEqual(span.dataset.timezone, "America/New_York");
+ },
+ markdown
+ );
+ });
+
+ test("local date range", async function (assert) {
+ this.siteSettings.rich_editor = true;
+ const markdown = "[date-range from=2021-01-01 to=2021-01-02]";
+
+ await testMarkdown(
+ assert,
+ markdown,
+ (innerAssert) => {
+ const rangeSpan = document.querySelector(
+ ".ProseMirror .discourse-local-date-range"
+ );
+ innerAssert.ok(rangeSpan);
+
+ const fromSpan = rangeSpan.querySelector('[data-range="from"]');
+ const toSpan = rangeSpan.querySelector('[data-range="to"]');
+ innerAssert.ok(fromSpan);
+ innerAssert.ok(toSpan);
+ innerAssert.strictEqual(fromSpan.dataset.date, "2021-01-01");
+ innerAssert.strictEqual(toSpan.dataset.date, "2021-01-02");
+ },
+ markdown
+ );
+ });
+
+ test("local date range with time", async function (assert) {
+ this.siteSettings.rich_editor = true;
+ const markdown =
+ '[date-range from=2021-01-01T12:00:00 to=2021-01-02T13:00:00 timezone="America/New_York"]';
+
+ await testMarkdown(
+ assert,
+ markdown,
+ (innerAssert) => {
+ const rangeSpan = document.querySelector(
+ ".ProseMirror .discourse-local-date-range"
+ );
+ innerAssert.ok(rangeSpan);
+
+ const fromSpan = rangeSpan.querySelector('[data-range="from"]');
+ const toSpan = rangeSpan.querySelector('[data-range="to"]');
+ innerAssert.ok(fromSpan);
+ innerAssert.ok(toSpan);
+ innerAssert.strictEqual(fromSpan.dataset.date, "2021-01-01");
+ innerAssert.strictEqual(fromSpan.dataset.time, "12:00:00");
+ innerAssert.strictEqual(toSpan.dataset.date, "2021-01-02");
+ innerAssert.strictEqual(toSpan.dataset.time, "13:00:00");
+ innerAssert.strictEqual(
+ fromSpan.dataset.timezone,
+ "America/New_York"
+ );
+ },
+ markdown
+ );
});
}
);
diff --git a/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.gjs b/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.gjs
index e2146842bb1..bc3a344303b 100644
--- a/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/modal/poll-ui-builder.gjs
@@ -24,6 +24,7 @@ import ComboBox from "discourse/select-kit/components/combo-box";
import GroupChooser from "discourse/select-kit/components/group-chooser";
import { and, not } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
+import generateCurrentDateMarkup from "discourse/plugins/discourse-local-dates/lib/generate-current-date-markup";
export const BAR_CHART_TYPE = "bar";
export const PIE_CHART_TYPE = "pie";
@@ -358,7 +359,7 @@ export default class PollUiBuilderModal extends Component {
}
@action
- onInputKeydown(index, event) {
+ onInputKeydown(option, index, event) {
if (event.key === "Enter") {
event.preventDefault();
event.stopPropagation();
@@ -366,6 +367,30 @@ export default class PollUiBuilderModal extends Component {
if (event.target.value !== "") {
this.addOption(index + 1);
}
+ return;
+ }
+
+ if (
+ event.code === "Period" &&
+ event.shiftKey &&
+ (event.metaKey || event.ctrlKey) &&
+ this.siteSettings.discourse_local_dates_enabled
+ ) {
+ event.preventDefault();
+ const timezone = this.currentUser.user_option?.timezone;
+ const markup = generateCurrentDateMarkup(timezone);
+
+ const input = event.target;
+ const start = input.selectionStart;
+ const end = input.selectionEnd;
+ const value = option.value || "";
+
+ option.value = value.slice(0, start) + markup + value.slice(end);
+ this.enforceMinMaxValues();
+
+ requestAnimationFrame(() => {
+ input.setSelectionRange(start + markup.length, start + markup.length);
+ });
}
}
@@ -493,7 +518,7 @@ export default class PollUiBuilderModal extends Component {
value={{option.value}}
{{autoFocus}}
{{on "input" (fn this.updateValue option)}}
- {{on "keydown" (fn this.onInputKeydown index)}}
+ {{on "keydown" (fn this.onInputKeydown option index)}}
/>
{{#if this.canRemoveOption}}
<DButton
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs
index 7b6072ffc09..5911b09b6ab 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs
@@ -9,6 +9,7 @@ import discourseComputed from "discourse/lib/decorators";
import loadChartJS from "discourse/lib/load-chart-js";
import I18n, { i18n } from "discourse-i18n";
import { getColors } from "discourse/plugins/poll/lib/chart-colors";
+import decoratePollOption from "../modifiers/decorate-poll-option";
@tagName("")
export default class PollBreakdownOption extends Component {
@@ -80,7 +81,10 @@ export default class PollBreakdownOption extends Component {
{{@option.votes}}
{{/if}}
</span>
- <span class="poll-breakdown-option-text">{{htmlSafe @option.html}}</span>
+ <span
+ class="poll-breakdown-option-text"
+ {{decoratePollOption @option.html}}
+ ></span>
</li>
</template>
}
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-option-ranked-choice.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-option-ranked-choice.gjs
index 25fe40a8cf8..93bba492f73 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-option-ranked-choice.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-option-ranked-choice.gjs
@@ -1,9 +1,9 @@
import Component from "@glimmer/component";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import { htmlSafe } from "@ember/template";
import DButton from "discourse/components/d-button";
import routeAction from "discourse/helpers/route-action";
+import decoratePollOption from "../modifiers/decorate-poll-option";
import PollOptionRankedChoiceDropdown from "./poll-option-ranked-choice-dropdown";
export default class PollOptionsComponent extends Component {
@@ -35,7 +35,7 @@ export default class PollOptionsComponent extends Component {
class="btn-default"
/>
{{/if}}
- <span class="option-text">{{htmlSafe @option.html}}</span>
+ <span class="option-text" {{decoratePollOption @option.html}}></span>
</div>
</template>
}
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs
index 3f2b1d88da1..483b70f0f05 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-options.gjs
@@ -3,11 +3,11 @@ import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import { htmlSafe } from "@ember/template";
import concatClass from "discourse/helpers/concat-class";
import icon from "discourse/helpers/d-icon";
import routeAction from "discourse/helpers/route-action";
import { i18n } from "discourse-i18n";
+import decoratePollOption from "../modifiers/decorate-poll-option";
import PollOptionRankedChoice from "./poll-option-ranked-choice";
export default class PollOptionsComponent extends Component {
@@ -18,7 +18,10 @@ export default class PollOptionsComponent extends Component {
};
@action
- sendClick(option) {
+ sendClick(option, event) {
+ if (event.target.closest(".discourse-local-date")) {
+ return;
+ }
this.args.sendOptionSelect(option);
}
@@ -87,7 +90,10 @@ export default class PollOptionsComponent extends Component {
{{icon "far-circle"}}
{{/if}}
{{/if}}
- <span class="option-text">{{htmlSafe option.html}}</span>
+ <span
+ class="option-text"
+ {{decoratePollOption option.html}}
+ ></span>
</button>
{{else}}
<button {{on "click" (routeAction "showLogin")}}>
@@ -104,7 +110,10 @@ export default class PollOptionsComponent extends Component {
{{icon "far-circle"}}
{{/if}}
{{/if}}
- <span class="option-text">{{htmlSafe option.html}}</span>
+ <span
+ class="option-text"
+ {{decoratePollOption option.html}}
+ ></span>
</button>
{{/if}}
</li>
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs
index db4592be8ee..626f6f1d4b8 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs
@@ -3,6 +3,11 @@ import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { htmlSafe } from "@ember/template";
import { modifier } from "ember-modifier";
+import {
+ applyHtmlDecorators,
+ NON_STREAM_HTML_DECORATOR,
+ NULL_HELPER,
+} from "discourse/components/decorated-html";
import loadChartJS from "discourse/lib/load-chart-js";
import { getColors } from "discourse/plugins/poll/lib/chart-colors";
import { PIE_CHART_TYPE } from "../components/modal/poll-ui-builder";
@@ -125,6 +130,11 @@ export default class PollResultsPieComponent extends Component {
const el = this.canvasElement;
this._chart = new Chart(el.getContext("2d"), config);
+ applyHtmlDecorators(
+ this.legendElement,
+ NULL_HELPER,
+ NON_STREAM_HTML_DECORATOR
+ );
}
<template>
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-results-ranked-choice.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-results-ranked-choice.gjs
index e1d6ed455d8..afa082c62cc 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-results-ranked-choice.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-results-ranked-choice.gjs
@@ -1,6 +1,8 @@
import Component from "@glimmer/component";
import { htmlSafe } from "@ember/template";
import { i18n } from "discourse-i18n";
+import decorateCookedContent from "../modifiers/decorate-cooked-content";
+import decoratePollOption from "../modifiers/decorate-poll-option";
export default class PollResultsRankedChoiceComponent extends Component {
get rankedChoiceWinnerText() {
@@ -35,7 +37,7 @@ export default class PollResultsRankedChoiceComponent extends Component {
{{#if round.majority}}
<tr>
<td>{{round.round}}</td>
- <td>{{htmlSafe round.majority.html}}</td>
+ <td {{decoratePollOption round.majority.html}}></td>
<td>{{i18n "poll.ranked_choice.none"}}</td>
</tr>
{{else}}
@@ -44,7 +46,7 @@ export default class PollResultsRankedChoiceComponent extends Component {
<td>{{i18n "poll.ranked_choice.none"}}</td>
<td>
{{#each round.eliminated as |eliminated|}}
- {{htmlSafe eliminated.html}}
+ <span {{decoratePollOption eliminated.html}}></span>
{{/each}}
</td>
</tr>
@@ -62,14 +64,16 @@ export default class PollResultsRankedChoiceComponent extends Component {
>{{this.rankedChoiceTiedText}}</span>
<ul class="poll-results-ranked-choice-tied-candidates">
{{#each @rankedChoiceOutcome.tied_candidates as |tied_candidate|}}
- <li class="poll-results-ranked-choice-tied-candidate">{{htmlSafe
- tied_candidate.html
- }}</li>
+ <li
+ class="poll-results-ranked-choice-tied-candidate"
+ {{decoratePollOption tied_candidate.html}}
+ ></li>
{{/each}}
</ul>
{{else}}
<span
class="poll-results-ranked-choice-info"
+ {{decorateCookedContent}}
>{{this.rankedChoiceWinnerText}}</span>
{{/if}}
{{/if}}
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs
index 50d2ad35ba4..cfcd551e0ac 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-results-standard.gjs
@@ -3,6 +3,7 @@ import { concat } from "@ember/helper";
import { htmlSafe } from "@ember/template";
import { i18n } from "discourse-i18n";
import evenRound from "discourse/plugins/poll/lib/even-round";
+import decoratePollOption from "../modifiers/decorate-poll-option";
import PollVoters from "./poll-voters";
export default class PollResultsStandardComponent extends Component {
@@ -92,7 +93,10 @@ export default class PollResultsStandardComponent extends Component {
}}</span>
{{/if}}
{{/unless}}
- <span class="option-text">{{htmlSafe option.html}}</span>
+ <span
+ class="option-text"
+ {{decoratePollOption option.html}}
+ ></span>
</p>
{{#unless @isRankedChoice}}
<div class="bar-back">
diff --git a/plugins/poll/assets/javascripts/discourse/modifiers/decorate-cooked-content.js b/plugins/poll/assets/javascripts/discourse/modifiers/decorate-cooked-content.js
new file mode 100644
index 00000000000..b882799d304
--- /dev/null
+++ b/plugins/poll/assets/javascripts/discourse/modifiers/decorate-cooked-content.js
@@ -0,0 +1,17 @@
+import { modifier } from "ember-modifier";
+import {
+ applyHtmlDecorators,
+ NON_STREAM_HTML_DECORATOR,
+ NULL_HELPER,
+} from "discourse/components/decorated-html";
+
+/** Modifier that applies cooked content decorators to an element's existing content. */
+export default modifier((element) => {
+ const cleanups = applyHtmlDecorators(
+ element,
+ NULL_HELPER,
+ NON_STREAM_HTML_DECORATOR
+ );
+
+ return () => cleanups.forEach((fn) => fn());
+});
diff --git a/plugins/poll/assets/javascripts/discourse/modifiers/decorate-poll-option.js b/plugins/poll/assets/javascripts/discourse/modifiers/decorate-poll-option.js
new file mode 100644
index 00000000000..7b17170b87d
--- /dev/null
+++ b/plugins/poll/assets/javascripts/discourse/modifiers/decorate-poll-option.js
@@ -0,0 +1,23 @@
+import { modifier } from "ember-modifier";
+import {
+ applyHtmlDecorators,
+ NON_STREAM_HTML_DECORATOR,
+ NULL_HELPER,
+} from "discourse/components/decorated-html";
+
+/** Modifier that sets element innerHTML and applies cooked content decorators. */
+export default modifier((element, [html]) => {
+ if (!html) {
+ return;
+ }
+
+ element.innerHTML = html;
+
+ const cleanups = applyHtmlDecorators(
+ element,
+ NULL_HELPER,
+ NON_STREAM_HTML_DECORATOR
+ );
+
+ return () => cleanups.forEach((fn) => fn());
+});
diff --git a/plugins/poll/assets/stylesheets/common/poll.scss b/plugins/poll/assets/stylesheets/common/poll.scss
index 1a551c59125..102bf487a7b 100644
--- a/plugins/poll/assets/stylesheets/common/poll.scss
+++ b/plugins/poll/assets/stylesheets/common/poll.scss
@@ -37,6 +37,8 @@ div.poll-outer {
text-align: left;
padding-left: 23px;
text-indent: -23px;
+ width: 100%;
+ cursor: pointer;
}
}
diff --git a/plugins/poll/test/javascripts/component/poll-options-test.gjs b/plugins/poll/test/javascripts/component/poll-options-test.gjs
index 247a0296785..835f1669ea4 100644
--- a/plugins/poll/test/javascripts/component/poll-options-test.gjs
+++ b/plugins/poll/test/javascripts/component/poll-options-test.gjs
@@ -185,4 +185,48 @@ module("Poll | Component | poll-options", function (hooks) {
.dom(".dropdown-menu__item:nth-child(4)")
.hasText(`3 ${i18n("poll.options.ranked_choice.lowest_priority")}`);
});
+
+ test("clicking on local date does not select option", async function (assert) {
+ const DATE_OPTIONS = [
+ {
+ id: "1ddc47be0d2315b9711ee8526ca9d83f",
+ html: 'Meeting on <span class="discourse-local-date" data-date="2025-01-15">Jan 15</span>',
+ votes: 0,
+ rank: 0,
+ },
+ ];
+
+ let optionSelected = false;
+ this.setProperties({
+ isCheckbox: false,
+ isRankedChoice: false,
+ options: DATE_OPTIONS,
+ votes: [],
+ sendOptionSelect: () => (optionSelected = true),
+ });
+
+ await render(
+ <template>
+ <PollOptions
+ @isCheckbox={{this.isCheckbox}}
+ @isRankedChoice={{this.isRankedChoice}}
+ @options={{this.options}}
+ @votes={{this.votes}}
+ @sendOptionSelect={{this.sendOptionSelect}}
+ />
+ </template>
+ );
+
+ await click(".discourse-local-date");
+ assert.false(
+ optionSelected,
+ "option should not be selected when clicking on date"
+ );
+
+ await click("li button");
+ assert.true(
+ optionSelected,
+ "option should be selected when clicking on button"
+ );
+ });
});