Skip to content

Commit 739c9f8

Browse files
committed
fix: sometimes the html live doc goes into an inconsistant state
1. Fixed BaseServer.remove() Bug (BaseServer.js:149) Before: Used lookup result as key for deletion After: Correctly uses the key string returned by _documentKey() This fix ensures LiveHTMLDocuments are actually removed from the cache when closed, preventing memory leaks and stale references. 2. Fixed _markText() Function (HTMLInstrumentation.js:823-847) ✩ PRIMARY FIX Key change: Now detects when an editor has no marks and allows marking even when fullBuild = false The logic now: - Checks if the editor has any existing tagID marks - Allows marking if: DOM is a full build OR editor has no marks - Still refuses to re-mark if editor already has marks and DOM is stale (preserves safety) This solves the core issue where switching editor panes or reopening documents would leave the new editor without marks. 3. Added Defensive Fallback (HTMLInstrumentation.js:758-769) Added a safety net that: - Detects when markCache is empty but DOM has nodes - Automatically forces re-marking if this happens - Logs a warning so we know it triggered How This Fixes the Issue The error "couldn't find existing mark for tagID" occurred because: 1. Document edited -> cache has fullBuild = false 2. User switches panes -> new editor has NO marks 3. Old _markText() refused to mark the new editor 4. generateInstrumentedHTML() tried to use non-existent marks -> ERROR Now: - _markText() detects the editor has no marks and marks it anyway (even with fullBuild = false) - If marks are somehow still missing, the defensive fallback catches it - BaseServer properly cleans up old documents
1 parent 1bf3852 commit 739c9f8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,19 @@ define(function (require, exports, module) {
754754
markCache[mark.tagID] = { mark: mark, range: mark.find() };
755755
}
756756
});
757+
758+
// DEFENSIVE CHECK: If we expected marks but found none, force re-marking
759+
if (Object.keys(markCache).length === 0 && dom.nodeMap && Object.keys(dom.nodeMap).length > 0) {
760+
console.warn("generateInstrumentedHTML: No marks found, forcing re-mark");
761+
_markTextFromDOM(editor, dom);
762+
// Rebuild markCache with new marks
763+
markCache = {};
764+
editor._codeMirror.getAllMarks().forEach(function (mark) {
765+
if (mark.tagID) {
766+
markCache[mark.tagID] = { mark: mark, range: mark.find() };
767+
}
768+
});
769+
}
757770
}
758771

759772
// Walk through the dom nodes and insert the 'data-brackets-id' attribute at the
@@ -827,7 +840,18 @@ define(function (require, exports, module) {
827840
if (!dom) {
828841
console.error("Couldn't find the dom for " + editor.document.file.fullPath);
829842
return;
830-
} else if (!dom.fullBuild) {
843+
}
844+
845+
// Check if editor has any tagID marks
846+
var existingMarks = editor._codeMirror.getAllMarks().filter(function (mark) {
847+
return mark.hasOwnProperty("tagID");
848+
});
849+
850+
// Allow marking if:
851+
// 1. DOM is a full build (offsets are accurate), OR
852+
// 2. Editor has no marks (new editor instance needs marking)
853+
if (!dom.fullBuild && existingMarks.length > 0) {
854+
// Editor already has marks and DOM is stale - don't re-mark
831855
console.error("Tried to mark text from a stale DOM for " + editor.document.file.fullPath);
832856
return;
833857
}

src/LiveDevelopment/Servers/BaseServer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ define(function (require, exports, module) {
146146
return;
147147
}
148148

149-
var key = this._liveDocuments[this._documentKey(liveDocument.doc.file.fullPath)];
149+
const key = this._documentKey(liveDocument.doc.file.fullPath);
150150

151-
if (key) {
151+
if (this._liveDocuments[key]) {
152152
delete this._liveDocuments[key];
153153
}
154154
};

0 commit comments

Comments
 (0)