Skip to content

Commit bca4c02

Browse files
committed
async script tags are processed as postponedActions instead of afterLoadActions
1 parent a8c5cba commit bca4c02

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,10 @@ public void execute() {
120120
};
121121

122122
final AbstractJavaScriptEngine<?> engine = webClient.getJavaScriptEngine();
123-
if (element.hasAttribute("async") && !hasNoSrcAttrib
124-
&& !engine.isScriptRunning()) {
125-
final HtmlPage owningPage = element.getHtmlPageOrNull();
126-
owningPage.addAfterLoadAction(action);
123+
if (element.hasAttribute("async") && !hasNoSrcAttrib) {
124+
engine.addPostponedAction(action);
127125
}
128-
else if (element.hasAttribute("async") && !hasNoSrcAttrib
129-
|| postponed && !hasNoSrcAttrib) {
126+
else if (postponed && !hasNoSrcAttrib) {
130127
engine.addPostponedAction(action);
131128
}
132129
else {

src/test/java/org/htmlunit/javascript/host/html/HTMLScriptElementTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.htmlunit.WebDriverTestCase;
2121
import org.htmlunit.junit.BrowserRunner;
2222
import org.htmlunit.junit.annotation.Alerts;
23+
import org.htmlunit.junit.annotation.HtmlUnitNYI;
2324
import org.htmlunit.junit.annotation.NotYetImplemented;
2425
import org.htmlunit.util.MimeType;
2526
import org.junit.Test;
@@ -1240,6 +1241,10 @@ public void async2() throws Exception {
12401241
*/
12411242
@Test
12421243
@Alerts("0 3 2 1")
1244+
@HtmlUnitNYI(CHROME = "0 3 1 2",
1245+
EDGE = "0 3 1 2",
1246+
FF = "0 3 1 2",
1247+
FF_ESR = "0 3 1 2")
12431248
public void syncLoadsAsync() throws Exception {
12441249
final String html = "<html><body>\n"
12451250
+ "<script>\n"
@@ -1266,6 +1271,7 @@ public void syncLoadsAsync() throws Exception {
12661271
@Test
12671272
@Alerts(DEFAULT = "2 0 3 1",
12681273
FF_ESR = "0 3 2 1")
1274+
@HtmlUnitNYI(FF_ESR = "2 0 3 1")
12691275
public void asyncLoadsAsync() throws Exception {
12701276
final String html = "<html><body>\n"
12711277
+ "<script src='script.js' async></script>\n"
@@ -1293,24 +1299,25 @@ public void asyncLoadsAsync() throws Exception {
12931299
* @throws Exception if the test fails
12941300
*/
12951301
@Test
1296-
@Alerts("1 2 3")
1302+
@Alerts({"1", "2", "3"})
12971303
public void asyncFromAsyncTask() throws Exception {
12981304
final String html = "<html><body><script>\n"
1305+
+ LOG_TITLE_FUNCTION
12991306
+ "function addAsyncScript() {\n"
13001307
+ " var script = document.createElement('script');\n"
13011308
+ " script.src = 'js.js';\n"
13021309
+ " script.async = true;\n"
13031310
+ " document.head.appendChild(script);\n"
1304-
+ " document.title += ' 2';\n"
1311+
+ " log('2');\n"
13051312
+ "}\n"
13061313
+ "setTimeout(addAsyncScript, 5);\n"
1307-
+ "document.title += ' 1';\n"
1314+
+ " log('1');\n"
13081315
+ "</script></body></html>\n";
13091316

1310-
getMockWebConnection().setResponse(new URL(URL_FIRST, "js.js"), "document.title += ' 3';");
1317+
getMockWebConnection().setResponse(new URL(URL_FIRST, "js.js"), "log('3')");
13111318

13121319
final WebDriver driver = loadPage2(html);
1313-
assertTitle(driver, getExpectedAlerts()[0]);
1320+
verifyTitle2(DEFAULT_WAIT_TIME, driver, getExpectedAlerts());
13141321
}
13151322

13161323
/**

0 commit comments

Comments
 (0)