Skip to content

Commit 7e164d9

Browse files
committed
introduce PostponedActionsBlocker
1 parent bfe9acc commit 7e164d9

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/main/java/org/htmlunit/html/DomElement.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,10 @@ public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, f
10381038
mouseDown(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
10391039
}
10401040

1041+
AbstractJavaScriptEngine.PostponedActionsBlocker blocker = null;
10411042
final AbstractJavaScriptEngine<?> jsEngine = webClient.getJavaScriptEngine();
10421043
if (webClient.isJavaScriptEnabled()) {
1043-
jsEngine.holdPosponedActions();
1044+
blocker = jsEngine.blockPostponedActions();
10441045
}
10451046
try {
10461047
if (handleFocus) {
@@ -1090,7 +1091,8 @@ else if (this instanceof HtmlOption) {
10901091
click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
10911092
}
10921093
finally {
1093-
if (webClient.isJavaScriptEnabled()) {
1094+
if (blocker != null) {
1095+
blocker.release();
10941096
jsEngine.processPostponedActions();
10951097
}
10961098
}

src/main/java/org/htmlunit/javascript/AbstractJavaScriptEngine.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ public interface AbstractJavaScriptEngine<SCRIPT> {
107107
/**
108108
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
109109
* Indicates that no postponed action should be executed.
110+
*
111+
* @return {@link PostponedActionsBlocker}
110112
*/
111-
void holdPosponedActions();
113+
PostponedActionsBlocker blockPostponedActions();
112114

113115
/**
114116
* Compiles the specified JavaScript code in the context of a given scope.
@@ -153,4 +155,10 @@ Object execute(HtmlPage page,
153155
*/
154156
HtmlUnitContextFactory getContextFactory();
155157

158+
/**
159+
* Helper for blocking the execution of postponed actions for some time.
160+
*/
161+
interface PostponedActionsBlocker {
162+
void release();
163+
}
156164
}

src/main/java/org/htmlunit/javascript/JavaScriptEngine.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,6 @@ public final Object run(final Context cx) {
917917
}
918918

919919
private void doProcessPostponedActions() {
920-
holdPostponedActions_ = false;
921-
922920
final WebClient webClient = getWebClient();
923921
if (webClient == null) {
924922
// shutdown was already called
@@ -1032,11 +1030,16 @@ protected void handleJavaScriptTimeoutError(final HtmlPage page, final TimeoutEr
10321030

10331031
/**
10341032
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
1035-
* Indicates that no postponed action should be executed.
1036-
*/
1033+
* {@inheritDoc}
1034+
*/
10371035
@Override
1038-
public void holdPosponedActions() {
1036+
public PostponedActionsBlocker blockPostponedActions() {
1037+
if (holdPostponedActions_) {
1038+
return new LeafPostponedActionsBlocker();
1039+
}
1040+
10391041
holdPostponedActions_ = true;
1042+
return new RootPostponedActionsBlocker(this);
10401043
}
10411044

10421045
/**
@@ -1385,4 +1388,33 @@ public static String evaluateProxyAutoConfig(final BrowserVersion browserVersion
13851388
return toString(result);
13861389
}
13871390
}
1391+
1392+
/**
1393+
* {@link PostponedActionsBlocker} - the first requested in the current context.
1394+
*/
1395+
private final class RootPostponedActionsBlocker implements PostponedActionsBlocker {
1396+
private final JavaScriptEngine jsEngine_;
1397+
1398+
private RootPostponedActionsBlocker(final JavaScriptEngine jsEngine) {
1399+
jsEngine_ = jsEngine;
1400+
}
1401+
1402+
@Override
1403+
public void release() {
1404+
jsEngine_.holdPostponedActions_ = false;
1405+
}
1406+
}
1407+
1408+
/**
1409+
* {@link PostponedActionsBlocker} - noop blocker.
1410+
*/
1411+
private final class LeafPostponedActionsBlocker implements PostponedActionsBlocker {
1412+
1413+
private LeafPostponedActionsBlocker() {
1414+
}
1415+
1416+
@Override
1417+
public void release() {
1418+
}
1419+
}
13881420
}

0 commit comments

Comments
 (0)