blocks and
elements, which
+// `textContent` joins without a separator, so a recorded value would read
+// "AlphaBeta" for two lines. Read the value exactly the way `bsk fill` verifies
+// it (`FILL_VALUE_FUNCTION` in `tools/interaction.ts`): typed layouts are read
+// literally, other rich structures keep their rendered text.
+function editorValue(el: HTMLElement): string {
+ const collapses = ["normal", "nowrap"].includes(getComputedStyle(el).whiteSpace);
+ const isInline = (node: Node) => node.nodeType === Node.TEXT_NODE || node.nodeName === "BR";
+ const isLine = (node: Node) =>
+ node.nodeName === "DIV" &&
+ getComputedStyle(node as Element).display === "block" &&
+ [...node.childNodes].every(isInline);
+ const nodes = [...el.childNodes];
+ let value: string;
+ if (nodes.every((node) => isInline(node) || isLine(node))) {
+ const lines: Node[][] = [];
+ let open: Node[] | undefined;
+ for (const node of nodes) {
+ if (isLine(node)) {
+ lines.push([...node.childNodes]);
+ open = undefined;
+ } else {
+ if (!open) lines.push((open = []));
+ open.push(node);
+ }
+ }
+ value = lines
+ .flatMap((line) => {
+ let text = line
+ .map((node, index) => {
+ if (node.nodeName !== "BR") return (node as CharacterData).data;
+ return index < line.length - 1 ? "\n" : "";
+ })
+ .join("");
+ if (collapses) text = text.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
+ // Without a
, an empty line has no height.
+ return text === "" && !line.some((node) => node.nodeName === "BR") ? [] : [text];
+ })
+ .join("\n");
+ } else {
+ // An editor holding only an empty line, such as
, is empty.
+ const rendered = el.innerText;
+ value = rendered === "\n" && el.textContent === "" ? "" : rendered;
+ }
+ return collapses ? value.replace(/\u00a0/g, " ") : value;
+}
+
function fillableValue(el: FillableElement): string {
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
return el.value;
}
- return el.textContent ?? "";
+ return editorValue(el);
}
function hoverTriggerAttrs(el: Element): Record
{