fix: Correct logic errors in shared utilities (#14904)

* fix: Correct logic errors in shared utilities

* fix: Restore per-column cells in getCellsInRow and convert every attachment url

The g flag made the attachment regex skip every second match, and removing
duplicate cells broke callers that index getCellsInRow by column.

* test: Cover uppercase attachment URLs

* revert: Remove uncertain fixes
This commit is contained in:
Tom Moor authored and GitHub committed 2026-12-05 12:48:21 -05:00
1 parent 9e719dbf1e
commit ce845b89e9
14 files changed
+142 -17

No files matched your search

+1 -1
View File
@@ -94,7 +94,7 @@ export default function toggleList(
);
dispatch?.(tr);
return false;
return true;
}
}
@@ -0,0 +1,32 @@
import type { Command } from "prosemirror-state";
import { createEditorStateWithSelection, doc, p } from "@shared/test/editor";
import { chainTransactions } from "./chainTransactions";
describe("chainTransactions", () => {
it("can be invoked more than once without losing commands", () => {
const cmd1: Command = (state, dispatch) => {
dispatch?.(state.tr.insertText("a", state.selection.from));
return true;
};
const cmd2: Command = (state, dispatch) => {
dispatch?.(state.tr.insertText("b", state.selection.from));
return true;
};
const chained = chainTransactions(cmd1, cmd2);
const runOnce = () => {
const state = createEditorStateWithSelection(doc([p("x")]), 1);
let next = state;
chained(state, (tr) => {
next = next.apply(tr);
});
return next.doc.textContent;
};
const first = runOnce();
const second = runOnce();
expect(first).toBe(second);
expect(first).toContain("a");
expect(first).toContain("b");
});
});
+3 -2
View File
@@ -19,8 +19,9 @@ export function chainTransactions(
dispatch?.(tr);
state = view && view.state !== before ? view.state : state.apply(tr);
};
const last = commands.pop();
commands.map((command) => command?.(state, dispatcher, view));
const list = [...commands];
const last = list.pop();
list.forEach((command) => command?.(state, dispatcher, view));
return last !== undefined && last(state, dispatch, view);
};
}
+9
View File
@@ -4,6 +4,7 @@ import {
getEmojiFromName,
loadEmojiData,
parseReactionShorthand,
snakeCase,
} from "./emoji";
beforeAll(async () => {
@@ -16,6 +17,14 @@ describe("getNameFromEmoji", () => {
});
});
describe("snakeCase", () => {
it("converts kebab case to snake case for consecutive segments", () => {
expect(snakeCase("a-b-c")).toBe("a_b_c");
expect(snakeCase("thinking-face")).toBe("thinking_face");
expect(snakeCase("no-dash")).toBe("no_dash");
});
});
describe("getEmojiFromName", () => {
it("returns the correct native character", () => {
expect(getEmojiFromName("thinking_face")).toBe("🤔");
+1 -1
View File
@@ -13,7 +13,7 @@ export const emojiMartToGemoji: Record<string, string> = {
* @param str The string to convert
* @returns The converted string
*/
export const snakeCase = (str: string) => str.replace(/(\w)-(\w)/g, "$1_$2");
export const snakeCase = (str: string) => str.replace(/(\w)-(?=\w)/g, "$1_");
/**
* A map of emoji shortcode to emoji character. The shortcode is snake cased
+8 -10
View File
@@ -143,6 +143,13 @@ export function getCellsInColumn(index: number) {
};
}
/**
* Get the positions of the cells in a row, one entry per column. A merged cell
* is repeated for every column it spans, so callers can index by column.
*
* @param index The row index.
* @returns A function that returns the cell positions for the given state.
*/
export function getCellsInRow(index: number) {
return (state: EditorState): number[] => {
if (!isInTable(state)) {
@@ -152,17 +159,8 @@ export function getCellsInRow(index: number) {
const rect = selectedRect(state);
const cells = [];
let previous;
for (let i = 0; i < rect.map.width; i += 1) {
const cell = rect.tableStart + rect.map.map[index * rect.map.width + i];
cells.push(cell);
// Ensure we don't add the same cell multiple times, this can happen
// if the row is selected and the table column has merged cells.
if (previous === cell) {
continue;
}
previous = cell;
cells.push(rect.tableStart + rect.map.map[index * rect.map.width + i]);
}
return cells;
+46
View File
@@ -1,4 +1,5 @@
import { Schema } from "prosemirror-model";
import env from "../env";
import type { ProsemirrorData } from "../types";
import type { CommentMark } from "./ProsemirrorHelper";
import { ProsemirrorHelper } from "./ProsemirrorHelper";
@@ -265,6 +266,40 @@ describe("ProsemirrorHelper", () => {
});
});
describe("attachmentsToAbsoluteUrls", () => {
it("converts every attachment url", () => {
const src =
"/api/attachments.redirect?id=12345678-1234-1234-1234-123456789012";
const data: ProsemirrorData = {
type: "doc",
content: [
{ type: "image", attrs: { src } },
{ type: "image", attrs: { src } },
{ type: "image", attrs: { src } },
],
};
const result = ProsemirrorHelper.attachmentsToAbsoluteUrls(data);
expect(result.content?.map((node) => node.attrs?.src)).toEqual([
env.URL + src,
env.URL + src,
env.URL + src,
]);
});
it("converts uppercase attachment urls", () => {
const src =
"/api/attachments.redirect?id=ABCDEF12-3456-789A-BCDE-F0123456789A";
const data: ProsemirrorData = {
type: "doc",
content: [{ type: "image", attrs: { src } }],
};
const result = ProsemirrorHelper.attachmentsToAbsoluteUrls(data);
expect(result.content?.[0].attrs?.src).toEqual(env.URL + src);
});
});
describe("removeMarks", () => {
it("should remove specified mark types from text nodes", () => {
const doc: ProsemirrorData = {
@@ -433,6 +468,17 @@ describe("ProsemirrorHelper", () => {
expect(result.childCount).toEqual(2);
});
it("keeps a titled leaf block at the end", () => {
const result = ProsemirrorHelper.trim(
doc(paragraph({ type: "text", text: "Hello" }), {
type: "video",
attrs: { title: "Clip" },
})
);
expect(result.childCount).toEqual(2);
});
});
describe("getHeadings", () => {
+9 -1
View File
@@ -517,7 +517,8 @@ export class ProsemirrorHelper {
* @returns The ProsemirrorData with absolute URLs for attachments
*/
static attachmentsToAbsoluteUrls(data: ProsemirrorData): ProsemirrorData {
const regex = new RegExp("^" + attachmentRedirectRegex.source);
// No "g" flag, as test() would then keep lastIndex between nodes.
const regex = new RegExp("^" + attachmentRedirectRegex.source, "i");
function replace(node: ProsemirrorData) {
if (
@@ -601,6 +602,13 @@ export class ProsemirrorHelper {
* text of leaf nodes such as mentions. Stops at the first text found.
*/
private static hasText(node: Node): boolean {
if (node.type.spec.leafText) {
return !!String(node.type.spec.leafText(node)).trim();
}
if (node.isText) {
return !!node.text?.trim();
}
let found = false;
node.descendants((child: Node) => {
+5
View File
@@ -45,4 +45,9 @@ describe("#parseCollectionSlug", () => {
"test-ANzZwgv2RG"
);
});
it("should return undefined when the collection segment is missing", () => {
expect(parseCollectionSlug("https://example.com/about")).toBeUndefined();
expect(parseCollectionSlug("/collection/")).toBeUndefined();
});
});
+5 -1
View File
@@ -21,5 +21,9 @@ export default function parseCollectionSlug(url: string) {
const split = parsed.split("/");
const indexOfCollection = split.indexOf("collection");
return split[indexOfCollection + 1] ?? undefined;
if (indexOfCollection === -1) {
return undefined;
}
const slug = split[indexOfCollection + 1];
return slug ? slug : undefined;
}
+6
View File
@@ -43,4 +43,10 @@ describe("#parseDocumentSlug", () => {
"my-doc-y4j4tR4UuV"
);
});
it("should return undefined when the doc segment is missing", () => {
expect(parseDocumentSlug("https://example.com/about")).toBeUndefined();
expect(parseDocumentSlug("/about")).toBeUndefined();
expect(parseDocumentSlug("/doc/")).toBeUndefined();
});
});
+5 -1
View File
@@ -21,5 +21,9 @@ export default function parseDocumentSlug(url: string) {
const split = parsed.split("/");
const indexOfDoc = split.indexOf("doc");
return split[indexOfDoc + 1] ?? undefined;
if (indexOfDoc === -1) {
return undefined;
}
const slug = split[indexOfDoc + 1];
return slug ? slug : undefined;
}
+9
View File
@@ -646,6 +646,15 @@ describe("#removeUrlPathSuffix", () => {
urlsUtils.removeUrlPathSuffix("/doc/my-doc-abc123/edit", "/edit")
).toBe("/doc/my-doc-abc123");
});
it("should leave the url unchanged for an empty suffix", () => {
expect(
urlsUtils.removeUrlPathSuffix("https://example.com/doc/my-doc-abc123", "")
).toBe("https://example.com/doc/my-doc-abc123");
expect(urlsUtils.removeUrlPathSuffix("/doc/my-doc-abc123", "")).toBe(
"/doc/my-doc-abc123"
);
});
});
describe("containsUrl", () => {
+3
View File
@@ -493,6 +493,9 @@ export function removeUrlFragment(url: string): string {
* @returns The url with the suffix removed from the end of its pathname.
*/
export function removeUrlPathSuffix(url: string, suffix: string): string {
if (!suffix) {
return url;
}
try {
const parsed = new URL(url);
if (parsed.pathname.endsWith(suffix)) {