Skip to content

Build your personal knowledge base with Trilium Notes

test(attributes): cover the attribute clipboard service

Elian Doran16a70538f685
diff --git a/apps/client/src/services/attribute_clipboard.spec.ts b/apps/client/src/services/attribute_clipboard.spec.ts
index bba7e6c6d9..c81660f161 100644
--- a/apps/client/src/services/attribute_clipboard.spec.ts
+++ b/apps/client/src/services/attribute_clipboard.spec.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
// Only the titles of the notes relations point at are read, and only to make the links readable.
const titles: Record<string, string> = { abc123: "Some note", evil: "Tom & <Jerry>" };
@@ -10,6 +10,7 @@ vi.mock("./froca", () => ({
import attribute_parser, { Attribute } from "./attribute_parser";
import {
+ copyAttributesToClipboard,
getHeldAttributes,
getPreprocessedData,
mergePastedAttributes,
@@ -80,6 +81,79 @@ describe("serializeAttributesAsHtml", () => {
expect(getPreprocessedData(html)).toBe("#author=Elian ~parent=#root/abc123");
});
+
+ it("leaves out a relation with no target, as the text does", () => {
+ expect(serializeAttributesAsHtml([ relation("parent", ""), label("kept", "yes") ])).toBe("#kept=yes");
+ });
+});
+
+describe("copyAttributesToClipboard", () => {
+ const copied = [ label("author", "Elian") ];
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ Reflect.deleteProperty(document, "execCommand");
+ writeAttributes(null, []);
+ });
+
+ it("writes the text through the clipboard API where there is one", async () => {
+ const writeText = vi.fn(async (_text: string) => {});
+ vi.spyOn(navigator, "clipboard", "get").mockReturnValue({ writeText } as unknown as Clipboard);
+ const execCommand = stubExecCommand(() => true);
+
+ await copyAttributesToClipboard(copied);
+
+ expect(writeText).toHaveBeenCalledWith("#author=Elian");
+ expect(execCommand).not.toHaveBeenCalled();
+ expect(getHeldAttributes()).toEqual(copied);
+ });
+
+ it("falls back to the copy command when the API refuses, giving the focus back afterwards", async () => {
+ const writeText = vi.fn(async (_text: string) => {
+ throw new Error("Not allowed");
+ });
+ vi.spyOn(navigator, "clipboard", "get").mockReturnValue({ writeText } as unknown as Clipboard);
+ const button = document.createElement("button");
+ document.body.append(button);
+ button.focus();
+
+ let selectedText: string | undefined;
+ const execCommand = stubExecCommand(() => {
+ selectedText = document.querySelector("textarea")?.value;
+ return true;
+ });
+
+ try {
+ await copyAttributesToClipboard(copied);
+
+ expect(execCommand).toHaveBeenCalledWith("copy");
+ expect(selectedText).toBe("#author=Elian");
+ expect(document.querySelector("textarea")).toBeNull();
+ expect(document.activeElement).toBe(button);
+ } finally {
+ button.remove();
+ }
+ });
+
+ it("still holds the attributes where neither way reaches the system clipboard", async () => {
+ vi.spyOn(navigator, "clipboard", "get").mockReturnValue(undefined as unknown as Clipboard);
+ vi.spyOn(document, "activeElement", "get").mockReturnValue(null);
+ stubExecCommand(() => {
+ throw new Error("Not supported");
+ });
+
+ await copyAttributesToClipboard(copied);
+
+ expect(document.querySelector("textarea")).toBeNull();
+ expect(getHeldAttributes()).toEqual(copied);
+ });
+
+ /** happy-dom has no `document.execCommand`, so each test defines its own. */
+ function stubExecCommand(implementation: (command: string) => boolean) {
+ const execCommand = vi.fn(implementation);
+ Object.defineProperty(document, "execCommand", { value: execCommand, configurable: true });
+ return execCommand;
+ }
});
describe("readAttributes", () => {
@@ -196,6 +270,26 @@ describe("mergePastedAttributes", () => {
.toEqual([ "read" ]);
});
+ it("reads an attribute the parser left without a value or inheritability as empty and not inherited", () => {
+ const { attributes, added } = mergePastedAttributes(
+ [ { type: "label", name: "tag" } ],
+ [ { type: "label", name: "label:tag" }, { type: "label", name: "tag" }, { type: "label", name: "flag" } ],
+ () => true
+ );
+
+ // `label:tag` with no value defines a single value, so the bare `tag` replaces the note's own.
+ expect(attributes).toEqual([
+ { type: "label", name: "tag", value: "", isInheritable: false },
+ { type: "label", name: "label:tag", value: "", isInheritable: false },
+ { type: "label", name: "flag", value: "", isInheritable: false }
+ ]);
+ expect(added).toBe(2);
+
+ // Pasted into a set that already holds the empty value, it is not added a second time.
+ expect(mergePastedAttributes([ { type: "label", name: "flag" } ], [ { type: "label", name: "flag" } ], () => true).added)
+ .toBe(0);
+ });
+
it("tells a label from a relation of the same name", () => {
const { attributes } = mergePastedAttributes([ label("owner", "me") ], [ relation("owner", "abc123") ], single);
diff --git a/apps/client/src/services/attribute_clipboard.ts b/apps/client/src/services/attribute_clipboard.ts
index d9cf1ad64f..95d8767991 100644
--- a/apps/client/src/services/attribute_clipboard.ts
+++ b/apps/client/src/services/attribute_clipboard.ts
@@ -260,5 +260,6 @@ export function getPreprocessedData(currentValue: string) {
.replace(/&nbsp;/g, " "); // otherwise textContent below outputs non-breaking space in unicode
// A `DOMParser` document is inert: pasted markup such as `<img onerror>` neither loads nor runs.
+ /* v8 ignore next -- an element's `textContent` is never null; the fallback satisfies the DOM types */
return new DOMParser().parseFromString(str, "text/html").body.textContent ?? "";
}