FIX: checklist weren't working if there was an image URL

When clicking a "checklist", we're parsing the raw to change the state of the checklist that was clicked. If there was an image URL with an empty alternative text in the raw "![](https://example.com/image.jpg)" it would consider the "[]" as an empty checklist item.

Internal - t/124499
This commit is contained in:
Régis Hanol 2024-04-06 17:17:19 +02:00
parent 17c92b4b2a
commit d3f7b3b5f9
3 changed files with 21 additions and 2 deletions

View File

@ -124,13 +124,19 @@ export function checklistSyntax(elem, postDecorator) {
// make the first run go to index = 0
let nth = -1;
let found = false;
const newRaw = post.raw.replace(
/\[(\s|\_|\-|\x|\\?\*)?\]/gi,
/\[( |x)?\]/gi,
(match, ignored, off) => {
if (found) {
return match;
}
// skip empty image URLs - "![](https://example.com/image.jpg)"
if (off > 0 && post.raw[off - 1] === "!") {
return match;
}
nth += blocks.every(
(b) => b[0] >= off + match.length || off > b[1]
);

View File

@ -1,4 +1,4 @@
const REGEX = /\[(\s?|x|X)\]/g;
const REGEX = /\[( |x)?\]/gi;
function getClasses(str) {
switch (str) {

View File

@ -38,6 +38,19 @@ acceptance("discourse-checklist | checklist", function (needs) {
]);
});
test("does not check an image URL", async function (assert) {
const [$elem, updated] = await prepare(`
![](upload://zLd8FtsWc2ZSg3cZKIhwvhYxTcn.jpg)
[] first
[] second
`);
$elem.find(".chcklst-box")[0].click();
const output = await updated;
assert.ok(output.includes("[x] first"));
});
test("make checkboxes readonly while updating", async function (assert) {
const [$elem, updated] = await prepare(`
[ ] first