From 670fb4d9e8017b80520792e7f1b65cc2d5f04897 Mon Sep 17 00:00:00 2001 From: Jacob Cassidy Date: Tue, 15 Sep 2026 08:51:03 +0700 Subject: [PATCH] Fix broken HTML tags when inserting a `` element at the end of the last HTML element in the editor field. --- src/wrapper/commands.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/wrapper/commands.js b/src/wrapper/commands.js index 80deb05..1109373 100644 --- a/src/wrapper/commands.js +++ b/src/wrapper/commands.js @@ -15,6 +15,21 @@ function wrap2(begin, end) { return; } + // To keep inline inside a selected block, even at its edges, keep the original range instead of expanding it around the parent div. + if (begin === "" && end === "" && !range.collapsed) { + const preview = range.cloneContents(); + const blocks = "div,p,pre,blockquote,ul,ol,li,table,h1,h2,h3,h4,h5,h6"; + if (!preview.querySelector(blocks)) { + const code = document.createElement("code"); + code.appendChild(range.extractContents()); + range.insertNode(code); + range.selectNodeContents(code); + selection.removeAllRanges(); + selection.addRange(range); + return; + } + } + let startParent = range.startContainer.parentNode; if ( startParent !== base