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)
This commit is contained in:
commit
9053327486
57 files changed
+9849
No files matched your search
@@ -0,0 +1,366 @@
|
||||
/*
|
||||
* 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 the DOM helpers in src/lib/quote.js: detecting mail quotes
|
||||
* (HTML blockquote and plain-text _moz_quote span), finding the quote or
|
||||
* comment around the caret, recognizing the gap left by Enter inside a
|
||||
* quote, filtering printable keys, and splitting a quote at the caret. Each
|
||||
* test builds a small reply in a jsdom document and places the caret or a
|
||||
* selection with the helpers from ./helpers/dom.js.
|
||||
*/
|
||||
|
||||
const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const quote = require("../src/lib/quote.js");
|
||||
const marker = require("../src/lib/marker.js");
|
||||
const { sanitizeSettings } = require("../src/lib/settings.js");
|
||||
const {
|
||||
createDocument,
|
||||
setCaret,
|
||||
setSelection,
|
||||
firstText,
|
||||
text,
|
||||
} = require("./helpers/dom.js");
|
||||
|
||||
// Sample HTML reply: own text on top, the attribution line, a quote with a
|
||||
// nested quote inside, and an empty paragraph below. The sample texts in this
|
||||
// file are German on purpose (a reply as a German mail client writes it);
|
||||
// only their structure matters.
|
||||
const REPLY = `
|
||||
<p id="own">Meine Antwort</p>
|
||||
<div class="moz-cite-prefix">Am 24.11.2026 schrieb Anna:</div>
|
||||
<blockquote type="cite" id="outer">
|
||||
<p id="q1">Erster Satz. Zweiter Satz.</p>
|
||||
<blockquote type="cite" id="inner"><p id="q2">Alt eins. Alt zwei.</p></blockquote>
|
||||
<p id="q3">Schluss.</p>
|
||||
</blockquote>
|
||||
<p id="after"><br></p>`;
|
||||
|
||||
test("isMailCite detects HTML and plain-text quotes", () => {
|
||||
const { doc } = createDocument(
|
||||
`<blockquote type="cite" id="a"></blockquote><blockquote id="b"></blockquote>` +
|
||||
`<blockquote type="CITE" id="c"></blockquote><span _moz_quote="true" id="d"></span>`,
|
||||
);
|
||||
assert.equal(quote.isMailCite(doc.getElementById("a")), true);
|
||||
assert.equal(quote.isMailCite(doc.getElementById("b")), false);
|
||||
assert.equal(quote.isMailCite(doc.getElementById("c")), true);
|
||||
assert.equal(quote.isMailCite(doc.getElementById("d")), true);
|
||||
assert.equal(quote.isMailCite(null), false);
|
||||
});
|
||||
|
||||
test("caretContext finds the outermost quote", () => {
|
||||
const { doc } = createDocument(REPLY);
|
||||
setCaret(doc, firstText(doc.getElementById("q2")), 3);
|
||||
const ctx = quote.caretContext(doc);
|
||||
assert.equal(ctx.cite.id, "outer");
|
||||
assert.equal(ctx.collapsed, true);
|
||||
assert.equal(ctx.comment, null);
|
||||
});
|
||||
|
||||
test("caretContext outside quotes and inside the user's own comment", () => {
|
||||
const { doc } = createDocument(
|
||||
`<p id="own">Text</p><div data-inline-comment="x" id="c"><span>[MM]:</span> Hallo</div>`,
|
||||
);
|
||||
setCaret(doc, firstText(doc.getElementById("own")), 2);
|
||||
assert.equal(quote.caretContext(doc).cite, null);
|
||||
setCaret(doc, doc.getElementById("c").lastChild, 2);
|
||||
const ctx = quote.caretContext(doc);
|
||||
assert.equal(ctx.cite, null);
|
||||
assert.equal(ctx.comment.id, "c");
|
||||
});
|
||||
|
||||
test("someone else's comment inside a quote counts as part of the quote", () => {
|
||||
const { doc } = createDocument(
|
||||
`<blockquote type="cite" id="q"><div data-inline-comment="alt" id="c">[AB]: alt</div></blockquote>`,
|
||||
);
|
||||
setCaret(doc, firstText(doc.getElementById("c")), 2);
|
||||
const ctx = quote.caretContext(doc);
|
||||
assert.equal(ctx.cite.id, "q");
|
||||
assert.equal(ctx.comment, null);
|
||||
});
|
||||
|
||||
test("caretContext without a selection", () => {
|
||||
const { doc } = createDocument("<p>x</p>");
|
||||
doc.getSelection().removeAllRanges();
|
||||
assert.equal(quote.caretContext(doc), null);
|
||||
});
|
||||
|
||||
test("isGapPosition: empty line between two parts of a quote", () => {
|
||||
const { doc } = createDocument(
|
||||
`<blockquote type="cite">A</blockquote><br id="gap"><blockquote type="cite">B</blockquote>`,
|
||||
);
|
||||
const body = doc.body;
|
||||
const index = Array.prototype.indexOf.call(
|
||||
body.childNodes,
|
||||
doc.getElementById("gap"),
|
||||
);
|
||||
const range = setCaret(doc, body, index);
|
||||
assert.equal(quote.isGapPosition(doc, range), true);
|
||||
});
|
||||
|
||||
test("isGapPosition: empty paragraph between quotes, caret inside the paragraph", () => {
|
||||
const { doc } = createDocument(
|
||||
`<blockquote type="cite">A</blockquote>\n<p id="gap"><br></p>\n<blockquote type="cite">B</blockquote>`,
|
||||
);
|
||||
const range = setCaret(doc, doc.getElementById("gap"), 0);
|
||||
assert.equal(quote.isGapPosition(doc, range), true);
|
||||
});
|
||||
|
||||
test("isGapPosition: not for a reply above or below the quote", () => {
|
||||
const { doc } = createDocument(
|
||||
`<p id="top"><br></p><div class="moz-cite-prefix">Am … schrieb:</div>` +
|
||||
`<blockquote type="cite">A</blockquote><p id="bottom"><br></p>`,
|
||||
);
|
||||
assert.equal(
|
||||
quote.isGapPosition(doc, setCaret(doc, doc.getElementById("top"), 0)),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
quote.isGapPosition(doc, setCaret(doc, doc.getElementById("bottom"), 0)),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("isGapPosition: not in a line that contains text", () => {
|
||||
const { doc } = createDocument(
|
||||
`<blockquote type="cite">A</blockquote><p id="t">Text</p><blockquote type="cite">B</blockquote>`,
|
||||
);
|
||||
const range = setCaret(doc, firstText(doc.getElementById("t")), 2);
|
||||
assert.equal(quote.isGapPosition(doc, range), false);
|
||||
});
|
||||
|
||||
test("isPrintableKey: characters yes, shortcuts no, AltGr yes", () => {
|
||||
/**
|
||||
* Builds a minimal keyboard event stand-in. The AltGr state is reported
|
||||
* through getModifierState("AltGraph"), as in a real KeyboardEvent.
|
||||
* @param {object} init Event properties such as key, ctrlKey or
|
||||
* isComposing, plus altGraph for the AltGr modifier state.
|
||||
* @returns {object} The fake event passed to isPrintableKey.
|
||||
*/
|
||||
const key = (init) => ({
|
||||
/**
|
||||
* Fake KeyboardEvent.getModifierState.
|
||||
* @param {string} name Modifier name, e.g. "AltGraph".
|
||||
* @returns {boolean | undefined} Truthy only for "AltGraph" when
|
||||
* init.altGraph is set.
|
||||
*/
|
||||
getModifierState: (name) => name === "AltGraph" && init.altGraph,
|
||||
...init,
|
||||
});
|
||||
assert.equal(quote.isPrintableKey(key({ key: "a" })), true);
|
||||
assert.equal(quote.isPrintableKey(key({ key: " " })), true);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "ü", shiftKey: true })), true);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "😀" })), true);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "c", ctrlKey: true })), false);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "v", metaKey: true })), false);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "x", altKey: true })), false);
|
||||
assert.equal(
|
||||
quote.isPrintableKey(
|
||||
key({ key: "@", ctrlKey: true, altKey: true, altGraph: true }),
|
||||
),
|
||||
true,
|
||||
);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "Enter" })), false);
|
||||
assert.equal(quote.isPrintableKey(key({ key: "Dead" })), false);
|
||||
assert.equal(
|
||||
quote.isPrintableKey(key({ key: "a", isComposing: true })),
|
||||
false,
|
||||
);
|
||||
assert.equal(quote.isPrintableKey(null), false);
|
||||
});
|
||||
|
||||
test("splitCiteAtRange splits in the middle of the text", () => {
|
||||
const { doc } = createDocument(REPLY);
|
||||
const range = setCaret(
|
||||
doc,
|
||||
firstText(doc.getElementById("q1")),
|
||||
"Erster Satz.".length,
|
||||
);
|
||||
const outer = doc.getElementById("outer");
|
||||
const marker = doc.createElement("div");
|
||||
marker.id = "comment";
|
||||
const { before, after } = quote.splitCiteAtRange(doc, range, outer, marker);
|
||||
assert.equal(before, outer);
|
||||
assert.equal(text(before), "Erster Satz.");
|
||||
assert.equal(marker.previousElementSibling, before);
|
||||
assert.equal(marker.nextElementSibling, after);
|
||||
assert.equal(after.getAttribute("type"), "cite");
|
||||
assert.match(text(after), /^Zweiter Satz\. Alt eins\. Alt zwei\. Schluss\.$/);
|
||||
});
|
||||
|
||||
test("splitCiteAtRange also splits the nested quote", () => {
|
||||
const { doc } = createDocument(REPLY);
|
||||
const range = setCaret(
|
||||
doc,
|
||||
firstText(doc.getElementById("q2")),
|
||||
"Alt eins.".length,
|
||||
);
|
||||
const outer = doc.getElementById("outer");
|
||||
const marker = doc.createElement("div");
|
||||
const { before, after } = quote.splitCiteAtRange(doc, range, outer, marker);
|
||||
assert.equal(text(before), "Erster Satz. Zweiter Satz. Alt eins.");
|
||||
assert.equal(text(after), "Alt zwei. Schluss.");
|
||||
// The split-off half keeps the nesting: outer quote > inner quote.
|
||||
const nested = after.querySelector('blockquote[type="cite"]');
|
||||
assert.ok(nested);
|
||||
assert.equal(text(nested), "Alt zwei.");
|
||||
assert.equal(before.querySelectorAll('blockquote[type="cite"]').length, 1);
|
||||
});
|
||||
|
||||
test("splitCiteAtRange at the start and at the end leaves no empty quotes", () => {
|
||||
const start = createDocument(
|
||||
`<blockquote type="cite" id="q"><p id="p">Nur Text</p></blockquote>`,
|
||||
);
|
||||
const rangeStart = setCaret(
|
||||
start.doc,
|
||||
firstText(start.doc.getElementById("p")),
|
||||
0,
|
||||
);
|
||||
const nodeStart = start.doc.createElement("div");
|
||||
const resultStart = quote.splitCiteAtRange(
|
||||
start.doc,
|
||||
rangeStart,
|
||||
start.doc.getElementById("q"),
|
||||
nodeStart,
|
||||
);
|
||||
assert.equal(resultStart.before, null);
|
||||
assert.equal(text(resultStart.after), "Nur Text");
|
||||
assert.equal(start.doc.body.firstElementChild, nodeStart);
|
||||
|
||||
const end = createDocument(
|
||||
`<blockquote type="cite" id="q"><p id="p">Nur Text</p></blockquote>`,
|
||||
);
|
||||
const rangeEnd = setCaret(
|
||||
end.doc,
|
||||
firstText(end.doc.getElementById("p")),
|
||||
"Nur Text".length,
|
||||
);
|
||||
const nodeEnd = end.doc.createElement("div");
|
||||
const resultEnd = quote.splitCiteAtRange(
|
||||
end.doc,
|
||||
rangeEnd,
|
||||
end.doc.getElementById("q"),
|
||||
nodeEnd,
|
||||
);
|
||||
assert.equal(resultEnd.after, null);
|
||||
assert.equal(end.doc.body.lastElementChild, nodeEnd);
|
||||
});
|
||||
|
||||
test("splitCiteAtRange adds '> ' in a plain-text quote", () => {
|
||||
const { doc } = createDocument(
|
||||
`<span _moz_quote="true" id="q">> Erste Zeile und mehr<br>> Zweite</span>`,
|
||||
);
|
||||
const quoteSpan = doc.getElementById("q");
|
||||
const range = setCaret(doc, quoteSpan.firstChild, "> Erste Zeile".length);
|
||||
const node = quote.createPlainCommentElement(doc, "[MM]: a", "id1");
|
||||
const { after } = quote.splitCiteAtRange(doc, range, quoteSpan, node);
|
||||
assert.equal(after.firstChild.data, "> und mehr");
|
||||
assert.equal(after.getAttribute("_moz_quote"), "true");
|
||||
assert.equal(node.getAttribute("data-inline-comment"), "id1");
|
||||
assert.equal(text(node), "[MM]: a");
|
||||
});
|
||||
|
||||
test("hasContent: a plain-text quote with only '>' counts as empty", () => {
|
||||
const { doc } = createDocument(
|
||||
`<span _moz_quote="true" id="a">> <br>></span><blockquote type="cite" id="b"><img src="x.png"></blockquote>`,
|
||||
);
|
||||
assert.equal(quote.hasContent(doc.getElementById("a")), false);
|
||||
assert.equal(quote.hasContent(doc.getElementById("b")), true);
|
||||
});
|
||||
|
||||
test("placeCaretAtEnd puts the caret after the comment text", () => {
|
||||
const { doc } = createDocument("<p>x</p>");
|
||||
const element = marker.createCommentNode(
|
||||
doc,
|
||||
"[MM]:",
|
||||
"a",
|
||||
sanitizeSettings({}),
|
||||
"k",
|
||||
);
|
||||
doc.body.append(element);
|
||||
quote.placeCaretAtEnd(doc, element);
|
||||
const selection = doc.getSelection();
|
||||
assert.equal(selection.anchorNode, element.lastChild);
|
||||
assert.equal(selection.anchorOffset, element.lastChild.data.length);
|
||||
assert.equal(quote.caretContext(doc).comment, element);
|
||||
});
|
||||
|
||||
test("placeCaretAtEnd in an element without text", () => {
|
||||
const { doc } = createDocument("<p>x</p>");
|
||||
const element = doc.createElement("div");
|
||||
element.append(doc.createElement("br"));
|
||||
doc.body.append(element);
|
||||
quote.placeCaretAtEnd(doc, element);
|
||||
const selection = doc.getSelection();
|
||||
assert.equal(selection.anchorNode, element);
|
||||
assert.equal(selection.anchorOffset, 1);
|
||||
});
|
||||
|
||||
test("splitCiteAtRange with a comment node from createCommentNode", () => {
|
||||
const { doc } = createDocument(REPLY);
|
||||
const range = setCaret(
|
||||
doc,
|
||||
firstText(doc.getElementById("q1")),
|
||||
"Erster Satz.".length,
|
||||
);
|
||||
const node = marker.createCommentNode(
|
||||
doc,
|
||||
"[MM]:",
|
||||
"a",
|
||||
sanitizeSettings({}),
|
||||
"c1",
|
||||
);
|
||||
const { before, after } = quote.splitCiteAtRange(
|
||||
doc,
|
||||
range,
|
||||
doc.getElementById("outer"),
|
||||
node,
|
||||
);
|
||||
assert.equal(node.previousElementSibling, before);
|
||||
assert.equal(node.nextElementSibling, after);
|
||||
assert.equal(quote.isComment(node), true);
|
||||
assert.equal(text(node), "[MM]: a");
|
||||
});
|
||||
|
||||
test("the DOM helpers no longer export createCommentElement", () => {
|
||||
assert.equal("createCommentElement" in quote, false);
|
||||
assert.equal("buildCommentHtml" in marker, false);
|
||||
});
|
||||
|
||||
test("isBlankNode: images and rules are content, also at top level", () => {
|
||||
const { doc } = createDocument(
|
||||
`<hr id="hr"><img id="img" src="x.png"><p id="p"><img src="y.png"></p><p id="e"> </p>`,
|
||||
);
|
||||
assert.equal(quote.isBlankNode(doc.getElementById("hr")), false);
|
||||
assert.equal(quote.isBlankNode(doc.getElementById("img")), false);
|
||||
assert.equal(quote.isBlankNode(doc.getElementById("p")), false);
|
||||
assert.equal(quote.isBlankNode(doc.getElementById("e")), true);
|
||||
});
|
||||
|
||||
test("isGapPosition: no gap when a rule separates the quote parts", () => {
|
||||
const { doc } = createDocument(
|
||||
`<blockquote type="cite">A</blockquote><hr><br id="gap"><blockquote type="cite">B</blockquote>`,
|
||||
);
|
||||
const index = Array.prototype.indexOf.call(
|
||||
doc.body.childNodes,
|
||||
doc.getElementById("gap"),
|
||||
);
|
||||
const range = setCaret(doc, doc.body, index);
|
||||
assert.equal(quote.isGapPosition(doc, range), false);
|
||||
});
|
||||
|
||||
test("caretContext: a text selection is not collapsed", () => {
|
||||
const { doc } = createDocument(REPLY);
|
||||
const q1 = firstText(doc.getElementById("q1"));
|
||||
setSelection(doc, q1, 0, q1, 5);
|
||||
assert.equal(quote.caretContext(doc).collapsed, false);
|
||||
});
|
||||
Reference in new issue
Block a user