Skip to content

Build your personal knowledge base with Trilium Notes

test(attributes): cover the attribute editor's text helpers

Elian Dorana6dbda172290
diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.spec.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.spec.tsx
new file mode 100644
index 0000000000..87064d86a1
--- /dev/null
+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.spec.tsx
@@ -0,0 +1,73 @@
+import type { ModelPosition } from "@triliumnext/ckeditor5";
+import { describe, expect, it, vi } from "vitest";
+
+import server from "../../../services/server";
+import { fetchAttributeNames, getClickIndex, getPreprocessedData, renderAttributeName } from "./AttributeEditor";
+
+describe("fetchAttributeNames", () => {
+ it("prefixes each name with the marker that inserts it, and asks for the names by an escaped query", async () => {
+ const get = vi.fn(async () => [ "author", "priority" ]);
+ server.get = get as unknown as typeof server.get;
+
+ expect(await fetchAttributeNames("relation", "a b")).toEqual([
+ { id: "~author", name: "author" },
+ { id: "~priority", name: "priority" }
+ ]);
+ expect(get).toHaveBeenCalledExactlyOnceWith("attribute-names/?type=relation&query=a%20b");
+
+ expect((await fetchAttributeNames("label", "")).map((item) => item.id)).toEqual([ "#author", "#priority" ]);
+ });
+});
+
+describe("renderAttributeName", () => {
+ it("marks a name Trilium reads for itself, and leaves an invented one as bare text", () => {
+ const builtin = renderAttributeName("label", "archived");
+
+ // `ck-button_with-text` keeps the label visible, and the reset opt-out keeps the badge styled.
+ expect(builtin.classList.contains("ck-button_with-text")).toBe(true);
+ expect(builtin.querySelector(".attr-name-suggestion")?.classList.contains("ck-reset_all-excluded")).toBe(true);
+ expect(builtin.querySelector(".attr-name-suggestion-name")?.textContent).toBe("archived");
+ expect(builtin.querySelector(".ext-badge.outline")).not.toBeNull();
+
+ const invented = renderAttributeName("label", "myOwnLabel");
+ expect(invented.querySelector(".attr-name-suggestion-name")?.textContent).toBe("myOwnLabel");
+ expect(invented.querySelector(".ext-badge")).toBeNull();
+
+ // A name that is a built-in of the other type is not one here.
+ expect(renderAttributeName("relation", "archived").querySelector(".ext-badge")).toBeNull();
+ });
+});
+
+describe("getPreprocessedData", () => {
+ it("reduces a reference link back to the note path it stands for, and resolves the entities around it", () => {
+ expect(getPreprocessedData(
+ "<p>#author=Elian&nbsp;~parent=<a class=\"reference-link\" href=\"#root/abc123\">Some note</a>&nbsp;</p>"
+ )).toBe("#author=Elian ~parent=#root/abc123 ");
+
+ // A link to anywhere else is not a note path, so it is left to the text extraction below it.
+ expect(getPreprocessedData("<a href=\"https://example.com\">Example</a>")).toBe("Example");
+ expect(getPreprocessedData("#a&amp;b")).toBe("#a&b");
+ });
+});
+
+describe("getClickIndex", () => {
+ it("measures every sibling before the pressed one as the text it stands for", () => {
+ const reference = { name: "reference", getAttribute: () => "#root/abc123" };
+ const text = { data: "#author=Elian ", previousSibling: null };
+ // …#author=Elian ~parent=#root/abc123 |~other
+ const pressed = {
+ data: "~other",
+ startOffset: 3,
+ previousSibling: { data: "~parent=", previousSibling: { ...reference, previousSibling: text } }
+ };
+
+ // The offset the editor reports counts the reference as a single node; the index does not.
+ const index = getClickIndex({ offset: 5, textNode: pressed } as unknown as ModelPosition);
+ expect(index).toBe(2 + "#author=Elian ".length + "~parent=".length + "#root/abc123".length + 1);
+
+ // A press in the first node of all is its own offset.
+ expect(getClickIndex({ offset: 4, textNode: text } as unknown as ModelPosition)).toBe(4);
+ // A press where there is no text node at all still resolves to a position.
+ expect(getClickIndex({ offset: 7, textNode: null } as unknown as ModelPosition)).toBe(7);
+ });
+});
diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
index bfe2c235b8..8818e852d9 100644
--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
@@ -444,7 +444,7 @@ interface AttributeNameItem {
name: string;
}
-async function fetchAttributeNames(type: "label" | "relation", queryText: string): Promise<AttributeNameItem[]> {
+export async function fetchAttributeNames(type: "label" | "relation", queryText: string): Promise<AttributeNameItem[]> {
const names = await server.get<string[]>(`attribute-names/?type=${type}&query=${encodeURIComponent(queryText)}`);
const marker = type === "label" ? "#" : "~";
@@ -460,7 +460,7 @@ async function fetchAttributeNames(type: "label" | "relation", queryText: string
* per row would leak. The badge markup is mirrored from `Badge` rather than restyled, so that both
* surfaces still take their look from the one stylesheet.
*/
-function renderAttributeName(type: "label" | "relation", name: string) {
+export function renderAttributeName(type: "label" | "relation", name: string) {
const button = document.createElement("button");
button.type = "button";
button.tabIndex = -1;
@@ -495,7 +495,8 @@ function renderAttributeName(type: "label" | "relation", name: string) {
return button;
}
-function getPreprocessedData(currentValue: string) {
+/** The attributes as plain text: reference links back down to their note path, entities resolved. */
+export function getPreprocessedData(currentValue: string) {
const str = currentValue
.replace(/<a[^>]+href="(#[A-Za-z0-9_/]*)"[^>]*>[^<]*<\/a>/g, "$1")
.replace(/&nbsp;/g, " "); // otherwise .text() below outputs non-breaking space in unicode
@@ -503,7 +504,12 @@ function getPreprocessedData(currentValue: string) {
return $("<div>").html(str).text();
}
-function getClickIndex(pos: ModelPosition) {
+/**
+ * Where the press landed in {@link getPreprocessedData}'s text, which is what the parsed attributes
+ * carry their offsets in: the editor's own offset counts a reference link as one node, so every
+ * sibling before the pressed one is measured as the text it stands for.
+ */
+export function getClickIndex(pos: ModelPosition) {
let clickIndex = pos.offset - (pos.textNode?.startOffset ?? 0);
let curNode: ModelNode | Text | ModelElement | null = pos.textNode;