WIP fixing tests

This commit is contained in:
evilchili
2026-05-28 23:54:44 -07:00
parent 781af3cc1e
commit 59436a7d39
2 changed files with 84 additions and 32 deletions
+34 -16
View File
@@ -401,7 +401,13 @@ export class RibbitEditor extends Ribbit {
prefixSpan.className = rule.isList ? LIST_PREFIX_CLASS : DELIM_CLASS;
prefixSpan.textContent = line.slice(0, prefixLength);
block.appendChild(prefixSpan);
block.appendChild(this.#parseInline(line.slice(prefixLength)));
const content = line.slice(prefixLength);
if (content) {
block.appendChild(this.#parseInline(content));
} else {
block.appendChild(document.createTextNode('\u200B'));
}
return block;
}
@@ -542,27 +548,38 @@ export class RibbitEditor extends Ribbit {
*/
#updateCurrentBlock(): void {
const block = this.#findCurrentBlock();
if (!block) {
return;
}
// Preserve the caret position across the rebuild
if (!block) return;
const caretOffset = this.#getCaretOffset(block);
// Normalize   that browsers insert in contentEditable.
// Without this, NBSP characters in textContent break pattern
// matching for block classifiers like "## " or "> ".
const lineText = block.textContent!.replace(/\u00A0/g, ' ');
const newBlock = this.#buildBlock(lineText);
block.className = newBlock.className;
block.innerHTML = '';
while (newBlock.firstChild) {
block.appendChild(newBlock.firstChild);
}
while (newBlock.firstChild) block.appendChild(newBlock.firstChild);
// Place caret after any prefix span, never inside it
const prefixSpan = block.firstElementChild;
const prefixLen = (prefixSpan?.classList.contains(DELIM_CLASS) ||
prefixSpan?.classList.contains(LIST_PREFIX_CLASS))
? prefixSpan.textContent!.length : 0;
this.#restoreCaret(block, caretOffset);
this.#updateEditingContext();
if (caretOffset <= prefixLen && prefixSpan) {
const sel = window.getSelection()!;
const range = document.createRange();
const next = prefixSpan.nextSibling;
if (next && next.nodeType === 3) {
range.setStart(next as Text, 0);
} else {
range.setStartAfter(prefixSpan);
}
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
} else {
this.#restoreCaret(block, caretOffset);
}
}
// ── Keyboard handling ──────────────────────────────────────────────────────
@@ -831,7 +848,8 @@ export class RibbitEditor extends Ribbit {
if (block.dataset.macro) {
return block.dataset.source || '';
}
return block.textContent || '';
return block.textContent!.replace(/\u200B/g, '');
}
}