Skip to content

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

FIX: Support escaping checkbox syntax with backslash (#36763)

Gerhard Schlager6e4dc50b7a68
diff --git a/plugins/checklist/assets/javascripts/discourse/initializers/checklist.js b/plugins/checklist/assets/javascripts/discourse/initializers/checklist.js
index 78c249e4d3e..d65ee040d1e 100644
--- a/plugins/checklist/assets/javascripts/discourse/initializers/checklist.js
+++ b/plugins/checklist/assets/javascripts/discourse/initializers/checklist.js
@@ -131,6 +131,11 @@ export function checklistSyntax(elem, postDecorator) {
return match;
}
+ // skip escaped opening bracket - "\[x]"
+ if (off > 0 && post.raw[off - 1] === "\\") {
+ return match;
+ }
+
nth += blocks.every(
(b) => b[0] >= off + match.length || off > b[1]
);
diff --git a/plugins/checklist/assets/javascripts/lib/discourse-markdown/checklist.js b/plugins/checklist/assets/javascripts/lib/discourse-markdown/checklist.js
index 2488ec72fc6..8bf89771f2c 100644
--- a/plugins/checklist/assets/javascripts/lib/discourse-markdown/checklist.js
+++ b/plugins/checklist/assets/javascripts/lib/discourse-markdown/checklist.js
@@ -99,6 +99,6 @@ export function setup(helper) {
]);
helper.registerPlugin((md) =>
- md.core.ruler.push("checklist", processChecklist)
+ md.core.ruler.before("text_join", "checklist", processChecklist)
);
}
diff --git a/plugins/checklist/spec/pretty_text_spec.rb b/plugins/checklist/spec/pretty_text_spec.rb
index 4f041cba227..c1f4e3a577f 100644
--- a/plugins/checklist/spec/pretty_text_spec.rb
+++ b/plugins/checklist/spec/pretty_text_spec.rb
@@ -9,11 +9,46 @@ describe PrettyText do
MD
html = <<~HTML
- <p><span class="chcklst-box fa fa-square-o fa-fw"></span>,<span class="chcklst-box fa fa-square-o fa-fw"></span>,<span class="chcklst-box checked fa fa-square-check-o fa-fw"></span>,<span class="chcklst-box checked permanent fa fa-square-check fa-fw"></span> are all checkboxes<br>
- <code>[ ]</code> <a>x</a> <em>[ ]</em> <strong>[ ]</strong> <em>[ ]</em> <strong>[ ]</strong> <s>[ ]</s> are not checkboxes</p>
+ <p><span class="chcklst-box fa fa-square-o fa-fw"></span>,<span class="chcklst-box fa fa-square-o fa-fw"></span>,<span class="chcklst-box checked fa fa-square-check-o fa-fw"></span>,<span class="chcklst-box checked permanent fa fa-square-check fa-fw"></span> are all checkboxes<br>
+ <code>[ ]</code> <a>x</a> <em>[ ]</em> <strong>[ ]</strong> <em>[ ]</em> <strong>[ ]</strong> <s>[ ]</s> are not checkboxes</p>
HTML
cooked = PrettyText.cook(md)
expect(cooked).to eq(html.strip)
end
+
+ it "does not treat escaped brackets as checkboxes" do
+ md = <<~MD
+ \\[x] escaped opening bracket
+ [x\\] escaped closing bracket
+ \\[x\\] both brackets escaped
+ \\[ ] escaped empty checkbox
+ [x] real checkbox
+ MD
+
+ cooked = PrettyText.cook(md)
+
+ expect(cooked.scan("chcklst-box").count).to eq(1)
+ expect(cooked).to include("[x] escaped opening bracket")
+ expect(cooked).to include("[x] escaped closing bracket")
+ expect(cooked).to include("[x] both brackets escaped")
+ expect(cooked).to include("[ ] escaped empty checkbox")
+ expect(cooked).to include(
+ '<span class="chcklst-box checked fa fa-square-check-o fa-fw"></span> real checkbox',
+ )
+ end
+
+ it "handles escaped checkbox followed by real checkbox" do
+ md = <<~MD
+ \\[x] hello [x] world
+ MD
+
+ cooked = PrettyText.cook(md)
+
+ expect(cooked.scan("chcklst-box").count).to eq(1)
+ expect(cooked).to include("[x] hello")
+ expect(cooked).to include(
+ '<span class="chcklst-box checked fa fa-square-check-o fa-fw"></span> world',
+ )
+ end
end
end
diff --git a/plugins/checklist/test/javascripts/integration/rich-editor-extension-test.js b/plugins/checklist/test/javascripts/integration/rich-editor-extension-test.js
index b7e93ba76f0..76b09cef9bf 100644
--- a/plugins/checklist/test/javascripts/integration/rich-editor-extension-test.js
+++ b/plugins/checklist/test/javascripts/integration/rich-editor-extension-test.js
@@ -51,6 +51,21 @@ module(
`<p>${unchecked} <em>italics</em> and ${checked} <strong>bold</strong></p>`,
"[ ] *italics* and [x] **bold**",
],
+ "does not render escaped opening bracket as checkbox": [
+ "\\[x] not a checkbox",
+ "<p>[x] not a checkbox</p>",
+ "\\[x\\] not a checkbox",
+ ],
+ "does not render escaped closing bracket as checkbox": [
+ "[x\\] not a checkbox",
+ "<p>[x] not a checkbox</p>",
+ "\\[x\\] not a checkbox",
+ ],
+ "handles escaped checkbox followed by real checkbox": [
+ "\\[x] escaped [x] real",
+ `<p>[x] escaped ${checked} real</p>`,
+ "\\[x\\] escaped [x] real",
+ ],
}).forEach(([name, [markdown, html, expectedMarkdown]]) => {
test(name, async function (assert) {
await testMarkdown(assert, markdown, html, expectedMarkdown);
diff --git a/plugins/checklist/test/javascripts/lib/checklist-test.js b/plugins/checklist/test/javascripts/lib/checklist-test.js
index 5f51535b1cf..7d0ac260b60 100644
--- a/plugins/checklist/test/javascripts/lib/checklist-test.js
+++ b/plugins/checklist/test/javascripts/lib/checklist-test.js
@@ -110,6 +110,22 @@ acceptance("discourse-checklist | checklist", function (needs) {
assert.true(output.includes("[x] nope"));
});
+ test("escaped checkbox syntax is ignored", async function (assert) {
+ const { updated } = await prepare(`
+\\[x] first escaped
+\\[ ] second escaped
+[ ] actual
+ `);
+
+ assert.dom(".chcklst-box").exists({ count: 1 });
+ await click(".chcklst-box");
+
+ const output = await updated;
+ assert.true(output.includes("\\[x] first escaped"));
+ assert.true(output.includes("\\[ ] second escaped"));
+ assert.true(output.includes("[x] actual"));
+ });
+
test("permanently checked checkbox", async function (assert) {
const { updated } = await prepare(`
[X] permanent
@@ -263,4 +279,38 @@ Actual checkboxes:
assert.dom("ol > li").doesNotHaveClass("has-checkbox");
assert.dom("ol .chcklst-box").doesNotHaveClass("list-item-checkbox");
});
+
+ test("does not treat escaped brackets as checkboxes", async function (assert) {
+ const { updated } = await prepare(`
+\\[x] escaped opening
+[x\\] escaped closing
+\\[x\\] both escaped
+[ ] real checkbox
+[x] another real one
+ `);
+
+ assert.dom(".chcklst-box").exists({ count: 2 });
+
+ await click(".chcklst-box");
+
+ const output = await updated;
+
+ assert.true(output.includes("\\[x] escaped opening"));
+ assert.true(output.includes("[x\\] escaped closing"));
+ assert.true(output.includes("\\[x\\] both escaped"));
+
+ assert.true(output.includes("[x] real checkbox"));
+ });
+
+ test("handles escaped checkbox followed by real checkbox", async function (assert) {
+ const { updated } = await prepare(`\\[x] hello [x] world`);
+
+ assert.dom(".chcklst-box").exists({ count: 1 });
+
+ await click(".chcklst-box");
+
+ const output = await updated;
+ assert.true(output.includes("\\[x] hello"));
+ assert.true(output.includes("[ ] world"));
+ });
});