Skip to content

Commit 9cf2933

Browse files
committed
fix: use findMatchingTag to get the end position as it is more robust
1 parent 4cc200e commit 9cf2933

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,24 @@ define(function (require, exports, module) {
4444
}
4545

4646
const editor = currLiveDoc.editor;
47-
const range = HTMLInstrumentation.getPositionFromTagId(editor, message.tagId);
48-
if (!range) {
47+
// get the start range from the getPositionFromTagId function
48+
// and we get the end range from the findMatchingTag function
49+
// NOTE: we cannot get the end range from getPositionFromTagId
50+
// because on non-beautified code getPositionFromTagId may not provide correct end position
51+
const startRange = HTMLInstrumentation.getPositionFromTagId(editor, message.tagId);
52+
if(!startRange) {
53+
return;
54+
}
55+
56+
const endRange = CodeMirror.findMatchingTag(editor._codeMirror, startRange.from);
57+
if (!endRange) {
4958
return;
5059
}
5160

52-
const text = editor.getTextBetween(range.from, range.to);
61+
const startPos = startRange.from;
62+
const endPos = endRange.close.to;
63+
64+
const text = editor.getTextBetween(startPos, endPos);
5365
let splittedText;
5466

5567
// we need to find the content boundaries to find exactly where the content starts and where it ends
@@ -61,7 +73,7 @@ define(function (require, exports, module) {
6173
// if the text split was done successfully, apply the edit
6274
if (splittedText && splittedText.length === 2) {
6375
const finalText = splittedText[0] + message.newContent + splittedText[1];
64-
editor.replaceRange(finalText, range.from, range.to);
76+
editor.replaceRange(finalText, startPos, endPos);
6577
} else {
6678
console.error("Live preview text edit operation failed.");
6779
}

0 commit comments

Comments
 (0)