Files
Oliver_Herb 9053327486 Inline Comment 0.2.0 - initial public release
Thunderbird/Betterbird add-on (Manifest V3): typing inside a quote splits
the quote and inserts your own text with a marker such as [OH]:.

- English and German user interface
- Licensed under the Mozilla Public License 2.0
- English documentation, JSDoc lint gate, 87 tests (node:test + jsdom)
2026-11-24 12:07:09 +01:00

212 lines
7.5 KiB
JavaScript

/*
* Copyright 2026 Oliver Herb
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
"use strict";
/*
* Unit tests for src/lib/marker.js: initials from names and addresses,
* template placeholders, the marker variables, and the comment node that is
* inserted into the mail (structure, inline styles, escaping). The pure
* string functions are called directly; the DOM functions run against a
* jsdom document.
*/
const { test } = require("node:test");
const assert = require("node:assert/strict");
const { JSDOM } = require("jsdom");
const marker = require("../src/lib/marker.js");
const { sanitizeSettings } = require("../src/lib/settings.js");
const { createDocument } = require("./helpers/dom.js");
test("initials from the display name", () => {
assert.equal(marker.initialsOf("Max Mustermann", ""), "MM");
assert.equal(marker.initialsOf("Jürgen Beispiel", ""), "JB");
assert.equal(marker.initialsOf("Hans-Peter Müller", ""), "HPM");
assert.equal(marker.initialsOf("Dr. Max Mustermann", ""), "MM");
assert.equal(marker.initialsOf("Dipl.-Ing. Anna Berg", ""), "AB");
assert.equal(marker.initialsOf("Oliver Herb (ACME)", ""), "OH");
assert.equal(marker.initialsOf("ölaf über", ""), "ÖÜ");
});
test("initials for 'Last name, First name' and a company suffix", () => {
assert.equal(marker.initialsOf("Herb, Oliver", ""), "OH");
assert.equal(marker.initialsOf("Oliver Herb, Example Corp", ""), "OH");
});
test("initials from the address when there is no name", () => {
assert.equal(marker.initialsOf("", "jane.doe@example.com"), "JD");
assert.equal(marker.initialsOf("", "info@example.com"), "I");
assert.equal(marker.initialsOf("", ""), "?");
assert.equal(marker.initialsOf(undefined, undefined), "?");
});
test("at most four initials", () => {
assert.equal(marker.initialsOf("A B C D E F", ""), "ABCD");
});
test("placeholders are replaced, unknown ones are kept", () => {
const vars = {
name: "Max",
initials: "MM",
email: "m@example.com",
date: "24.11.2026",
time: "09:30",
};
assert.equal(marker.renderTemplate("[{initials}]:", vars), "[MM]:");
assert.equal(
marker.renderTemplate(
"{name} <{email}> {date} {time} {initials}{initials}",
vars,
),
"Max <m@example.com> 24.11.2026 09:30 MMMM",
);
assert.equal(marker.renderTemplate("{foo} {name}", vars), "{foo} Max");
assert.equal(marker.renderTemplate("{name}", {}), "{name}");
});
test("markerVariables: name falls back to the address, date in the requested locale format", () => {
const now = new Date(2026, 10, 24, 9, 5);
const vars = marker.markerVariables(
{ name: "", email: "max@example.com" },
now,
"de-DE",
);
assert.equal(vars.name, "max@example.com");
assert.equal(vars.initials, "M");
assert.equal(vars.date, "24.11.2026");
assert.equal(vars.time, "09:05");
const none = marker.markerVariables(null, now, "de-DE");
assert.equal(none.initials, "?");
});
test("createCommentNode: structure, inline CSS only, id", () => {
const { doc } = createDocument("");
const settings = sanitizeSettings({});
const node = marker.createCommentNode(doc, "[MM]:", "a", settings, "ic-1");
assert.equal(node.ownerDocument, doc);
assert.equal(node.isConnected, false);
assert.equal(node.localName, "div");
assert.deepEqual(
[...node.attributes].map((attribute) => attribute.name),
["data-inline-comment", "style"],
);
assert.equal(node.getAttribute("data-inline-comment"), "ic-1");
assert.equal(node.getAttribute("style"), marker.commentStyle(settings));
assert.match(node.getAttribute("style"), /^color:#0056b3;/);
assert.equal(node.childNodes.length, 2);
const [span, textNode] = node.childNodes;
assert.equal(span.localName, "span");
assert.deepEqual(
[...span.attributes].map((attribute) => attribute.name),
["data-inline-comment-marker", "style"],
);
assert.equal(span.getAttribute("data-inline-comment-marker"), "");
assert.equal(
span.getAttribute("style"),
"color:#003d80;font-weight:bold;font-style:normal;",
);
assert.equal(span.textContent, "[MM]:");
assert.equal(textNode.nodeType, doc.TEXT_NODE);
assert.equal(textNode.data, "\u00a0a");
});
test("createCommentNode: outerHTML has the inline-CSS comment markup", () => {
const { doc } = createDocument("");
const settings = sanitizeSettings({});
const html = marker.createCommentNode(
doc,
"[MM]:",
"a b",
settings,
"ic-1",
).outerHTML;
assert.equal(
html,
`<div data-inline-comment="ic-1" style="${marker.commentStyle(settings)}">` +
`<span data-inline-comment-marker="" style="${marker.markerStyle(settings)}">[MM]:</span>` +
"&nbsp;a&nbsp;b</div>",
);
assert.doesNotMatch(html, /class=|<style/);
});
test("createCommentNode: marker, text and id never become markup", () => {
const { doc } = createDocument("");
const settings = sanitizeSettings({});
const markerText = '[<img src=x onerror="alert(1)">]:';
const id = 'x" onmouseover="alert(1)';
const node = marker.createCommentNode(
doc,
markerText,
"<b>&amp;</b>",
settings,
id,
);
assert.equal(node.querySelector("img, b"), null);
assert.equal(node.hasAttribute("onmouseover"), false);
assert.equal(node.getAttribute("data-inline-comment"), id);
assert.equal(node.firstChild.textContent, markerText);
assert.equal(node.lastChild.data, "\u00a0<b>&amp;</b>");
// The serialized form (used for execCommand "insertHTML") parses back to
// the very same node: no element or attribute can be smuggled in.
const parsed = JSDOM.fragment(node.outerHTML).firstChild;
assert.ok(parsed.isEqualNode(node));
assert.equal(parsed.querySelector("img, b"), null);
assert.equal(parsed.hasAttribute("onmouseover"), false);
});
test("createCommentNode: highlight, italic and blank lines", () => {
const { doc } = createDocument("");
const settings = sanitizeSettings({
highlight: true,
highlightColor: "#ffeeaa",
textItalic: true,
blankLineBefore: true,
blankLineAfter: true,
markerBold: false,
});
const node = marker.createCommentNode(doc, "[MM]:", " ", settings, "x");
const style = node.getAttribute("style");
assert.match(style, /background-color:#ffeeaa;padding:2px 4px;/);
assert.match(style, /font-style:italic;/);
assert.match(style, /margin:1em 0 1em 0;/);
assert.equal(
node.firstChild.getAttribute("style"),
"color:#003d80;font-weight:normal;font-style:normal;",
);
assert.equal(node.lastChild.data, "\u00a0\u00a0");
assert.match(node.outerHTML, /&nbsp;&nbsp;<\/div>$/);
});
test("createCommentNode accepts non-string values", () => {
const { doc } = createDocument("");
const node = marker.createCommentNode(doc, 42, 7, sanitizeSettings({}), 1);
assert.equal(node.getAttribute("data-inline-comment"), "1");
assert.equal(node.firstChild.textContent, "42");
assert.equal(node.lastChild.data, "\u00a07");
});
test("every documented variable is replaced by renderTemplate", () => {
for (const variable of marker.VARIABLES) {
assert.equal(
marker.renderTemplate(`[{${variable}}]`, { [variable]: "X" }),
"[X]",
variable,
);
}
});
test("buildCommentText for plain text", () => {
const plain = sanitizeSettings({});
assert.equal(marker.buildCommentText("[MM]:", "a", plain), "[MM]: a");
const spaced = sanitizeSettings({ blankLineBefore: true });
assert.equal(marker.buildCommentText("[MM]:", "a", spaced), "\n[MM]: a");
});