/* * 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 = `
Meine Antwort
Erster Satz. Zweiter Satz.
Alt eins. Alt zwei.
Schluss.
Text
`, ); 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("[AB]: alt
x
"); doc.getSelection().removeAllRanges(); assert.equal(quote.caretContext(doc), null); }); test("isGapPosition: empty line between two parts of a quote", () => { const { doc } = createDocument( `A
B`, ); 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( `
A\n
B`, ); 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( `
A
A
Text
B`, ); 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( `
`, ); 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( `Nur Text
`, ); 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( `> Erste Zeile und mehrNur Text
`, ); 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("
x
"); 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("x
"); 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( `

`, ); 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( `
A
B`, ); 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); });