/* * 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"; /* * Shared helpers for the DOM tests. They create jsdom documents, place the * caret or a selection the way a user would in the compose editor, and read * text back in a form that is easy to compare in assertions. */ const { JSDOM } = require("jsdom"); /** * Creates a new jsdom document whose body contains the given markup. * @param {string} bodyHtml Markup placed inside . * @param {import("jsdom").ConstructorOptions} [options] Options passed to the * JSDOM constructor, e.g. { runScripts: "outside-only" }. * @returns {{ dom: JSDOM, window: Window, doc: Document }} The JSDOM instance, * its window and its document. */ function createDocument(bodyHtml, options = {}) { const dom = new JSDOM( `${bodyHtml}`, options, ); return { dom, window: dom.window, doc: dom.window.document }; } /** * Places the caret (a collapsed selection) at node/offset and makes it the * only range of the document selection. * @param {Document} doc Document whose selection is changed. * @param {Node} node Container node of the caret. * @param {number} offset Character offset in a text node, or child index in * an element. * @returns {Range} The collapsed range that was added to the selection. */ function setCaret(doc, node, offset) { const range = doc.createRange(); range.setStart(node, offset); range.collapse(true); const selection = doc.getSelection(); selection.removeAllRanges(); selection.addRange(range); return range; } /** * Selects the content from (startNode, startOffset) to (endNode, endOffset) * and makes it the only range of the document selection. * @param {Document} doc Document whose selection is changed. * @param {Node} startNode Container node where the selection starts. * @param {number} startOffset Offset within startNode. * @param {Node} endNode Container node where the selection ends. * @param {number} endOffset Offset within endNode. * @returns {Range} The range that was added to the selection. */ function setSelection(doc, startNode, startOffset, endNode, endOffset) { const range = doc.createRange(); range.setStart(startNode, startOffset); range.setEnd(endNode, endOffset); const selection = doc.getSelection(); selection.removeAllRanges(); selection.addRange(range); return range; } /** * Returns the first text node below the element, in document order. * @param {Element} element Element to search. * @returns {Text | null} The first descendant text node, or null if there is * none. */ function firstText(element) { const walker = element.ownerDocument.createTreeWalker(element, 4); return walker.nextNode(); } /** * Returns the text content of a node with normalized whitespace: no-break * spaces become plain spaces, runs of whitespace collapse to one space, and * leading and trailing whitespace is removed. * @param {Node} node Node to read. * @returns {string} The normalized text. */ function text(node) { return node.textContent .replace(/\u00a0/g, " ") .replace(/\s+/g, " ") .trim(); } module.exports = { createDocument, setCaret, setSelection, firstText, text };