Skip to content

Commit ffd8967

Browse files
committed
fix: handle race conditions accessing elements in DOM
Fixed the browser to not appear to be 'ready' to set the body before the text is set. Otherwise, because the content is set asynchronously in a browser, there is a race between setting the text and writing the body. Also, check for the document not being null before accessing some of its properties.
1 parent 024d833 commit ffd8967

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/hover/FocusableBrowserInformationControl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@ private void updateBrowserSize(final Browser browser) {
102102
Point hint = computeSizeHint();
103103
setSize(hint.x, hint.y);
104104

105-
if (!"complete".equals(safeEvaluate(browser, "return document.readyState"))) { //$NON-NLS-1$ //$NON-NLS-2$
105+
if (!"complete".equals(safeEvaluate(browser, "return document.documentElement ? document.readyState : 'no content';"))) { //$NON-NLS-1$ //$NON-NLS-2$
106106
UI.getDisplay().timerExec(200, () -> updateBrowserSize(browser));
107107
return;
108108
}
109109

110110
safeExecute(browser, "document.getElementsByTagName(\"html\")[0].style.whiteSpace = \"nowrap\""); //$NON-NLS-1$
111111
Double width = 20
112-
+ (safeEvaluate(browser, "return document.body.scrollWidth;") instanceof Double evaluated ? evaluated //$NON-NLS-1$
112+
+ (safeEvaluate(browser, "return document.body ? document.body.scrollWidth : null;") instanceof Double evaluated ? evaluated //$NON-NLS-1$
113113
: 0);
114114
setSize(width.intValue(), hint.y);
115115

116116
safeExecute(browser, "document.getElementsByTagName(\"html\")[0].style.whiteSpace = \"normal\""); //$NON-NLS-1$
117-
Double height = safeEvaluate(browser, "return document.body.scrollHeight;") instanceof Double evaluated //$NON-NLS-1$
117+
Double height = safeEvaluate(browser, "return document.body ? document.body.scrollHeight : null;") instanceof Double evaluated //$NON-NLS-1$
118118
? evaluated
119119
: 0d;
120120
Object marginTop = safeEvaluate(browser, "return window.getComputedStyle(document.body).marginTop;"); //$NON-NLS-1$

0 commit comments

Comments
 (0)