Skip to content

Commit cea5144

Browse files
committed
Event 'onload' is trigger by FF ESR also if the frame content was not shown because of csp
make tests more robust
1 parent d729870 commit cea5144

File tree

4 files changed

+67
-53
lines changed

4 files changed

+67
-53
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<body>
1010
<release version="4.11.0" date="March xx, 2025" description="Chrome/Edge 134, Firefox 136, Rhino RegExp, less dependencies, Bugfixes">
11+
<action type="fix" dev="rbri">
12+
Event 'onload' is trigger by FF ESR also if the frame content was not shown because of csp.
13+
</action>
1114
<action type="add" dev="rbri">
1215
Initial support for font sizes 'xx-small', 'x-small', 'small', 'medium', 'large',
1316
'x-large', 'xx-large', 'xxx-large'., 'smaller', and 'larger'.

src/main/java/org/htmlunit/BrowserVersionFeatures.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,6 @@ public enum BrowserVersionFeatures {
310310
@BrowserFeature({FF, FF_ESR})
311311
JS_EVENT_KEYBOARD_CTOR_WHICH,
312312

313-
/**
314-
* Do not trigger the onload event if the frame content
315-
* was not shown because of the csp. */
316-
@BrowserFeature(FF_ESR)
317-
JS_EVENT_LOAD_SUPPRESSED_BY_CONTENT_SECURIRY_POLICY,
318-
319313
/** form.dispatchEvent(e) submits the form if the event is of type 'submit'. */
320314
@BrowserFeature({FF, FF_ESR})
321315
JS_FORM_DISPATCHEVENT_SUBMITS,

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package org.htmlunit.html;
1616

1717
import static org.htmlunit.BrowserVersionFeatures.EVENT_FOCUS_ON_LOAD;
18-
import static org.htmlunit.BrowserVersionFeatures.JS_EVENT_LOAD_SUPPRESSED_BY_CONTENT_SECURIRY_POLICY;
1918
import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
2019

2120
import java.io.File;
@@ -1288,12 +1287,6 @@ else if (Event.TYPE_READY_STATE_CHANGE.equals(eventType)) {
12881287
event = new BeforeUnloadEvent(frame, eventType);
12891288
}
12901289
else {
1291-
// ff does not trigger the onload event in this case
1292-
if (PageDenied.BY_CONTENT_SECURIRY_POLICY == fw.getPageDenied()
1293-
&& hasFeature(JS_EVENT_LOAD_SUPPRESSED_BY_CONTENT_SECURIRY_POLICY)) {
1294-
return true;
1295-
}
1296-
12971290
event = new Event(frame, eventType);
12981291
}
12991292
// This fires the "load" event for the <frame> element which, like all non-window

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

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ public void onLoadGetsIFrameElementByIdInParent() throws Exception {
130130
+ "<iframe id='myIFrame' src='frame.html'></iframe></body></html>";
131131

132132
final String frameContent = DOCTYPE_HTML
133-
+ "<html><head><title>Frame</title><script>\n"
133+
+ "<html><head>\n"
134+
+ "<title>Frame</title>\n"
135+
+ "<script>\n"
136+
+ LOG_WINDOW_NAME_FUNCTION
134137
+ "function doTest() {\n"
135-
+ " alert(parent.document.getElementById('myIFrame').tagName);\n"
138+
+ " log(parent.document.getElementById('myIFrame').tagName);\n"
136139
+ "}\n</script></head>\n"
137140
+ "<body onload='doTest()'>\n"
138141
+ "</body></html>";
@@ -141,7 +144,8 @@ public void onLoadGetsIFrameElementByIdInParent() throws Exception {
141144

142145
webConnection.setDefaultResponse(frameContent);
143146

144-
loadPageWithAlerts2(firstContent);
147+
loadPage2(firstContent);
148+
verifyWindowName2(getWebDriver(), getExpectedAlerts());
145149
}
146150

147151
/**
@@ -244,9 +248,19 @@ public void iFrameReinitialized() throws Exception {
244248
+ "</body></html>";
245249

246250
final String frame1 = DOCTYPE_HTML
247-
+ "<html><head><script>window.foo = 123; alert(window.foo);</script></head></html>";
251+
+ "<html><head>\n"
252+
+ "<script>\n"
253+
+ LOG_WINDOW_NAME_FUNCTION
254+
+ "window.foo = 123; log(window.foo);\n"
255+
+ "</script>\n"
256+
+ "</head></html>";
248257
final String frame2 = DOCTYPE_HTML
249-
+ "<html><head><script>alert(window.foo);</script></head></html>";
258+
+ "<html><head>\n"
259+
+ "<script>\n"
260+
+ LOG_WINDOW_NAME_FUNCTION
261+
+ "log(window.foo);\n"
262+
+ "</script>\n"
263+
+ "</head></html>";
250264

251265
final String[] alerts = getExpectedAlerts();
252266
final MockWebConnection webConnection = getMockWebConnection();
@@ -255,10 +269,10 @@ public void iFrameReinitialized() throws Exception {
255269
webConnection.setResponse(new URL(URL_FIRST, "2.html"), frame2);
256270

257271
final WebDriver driver = loadPage2(html);
258-
verifyAlerts(driver, alerts[0]);
272+
verifyWindowName2(driver, alerts[0]);
259273

260274
driver.findElement(By.id("test")).click();
261-
verifyAlerts(driver, alerts[1]);
275+
verifyWindowName2(driver, alerts[1]);
262276

263277
assertEquals(3, getMockWebConnection().getRequestCount());
264278
}
@@ -429,14 +443,20 @@ public void body() throws Exception {
429443
+ "</body></html>";
430444

431445
final String frame = DOCTYPE_HTML
432-
+ "<html><head><script>alert(document.body);</script></head>\n"
433-
+ "<body><script>alert(document.body);</script></html>";
446+
+ "<html><head>\n"
447+
+ "<script>\n"
448+
+ LOG_WINDOW_NAME_FUNCTION
449+
+ "log(document.body);\n"
450+
+ "</script>\n"
451+
+ "</head>\n"
452+
+ "<body><script>log(document.body);</script></html>";
434453

435454
final MockWebConnection webConnection = getMockWebConnection();
436455

437456
webConnection.setDefaultResponse(frame);
438457

439-
loadPageWithAlerts2(html);
458+
loadPage2(html);
459+
verifyWindowName2(getWebDriver(), getExpectedAlerts());
440460
}
441461

442462
/**
@@ -574,13 +594,15 @@ public void settingSrc() throws Exception {
574594
@Alerts("iframe onload")
575595
public void writeTriggersOnload() throws Exception {
576596
final String html = DOCTYPE_HTML
577-
+ "<html><head><title>First</title><script>\n"
597+
+ "<html><head>\n"
598+
+ "<script>\n"
578599
+ "function test() {\n"
600+
+ LOG_TITLE_FUNCTION
579601
+ " var iframe = document.createElement('iframe');\n"
580602
+ " var content = 'something';\n"
581603
+ " document.body.appendChild(iframe);\n"
582604

583-
+ " iframe.onload = function() {alert('iframe onload')};\n"
605+
+ " iframe.onload = function() {log('iframe onload')};\n"
584606
+ " iframe.contentWindow.document.open('text/html', 'replace');\n"
585607
+ " iframe.contentWindow.document.write(content);\n"
586608
+ " iframe.contentWindow.document.close();\n"
@@ -591,7 +613,7 @@ public void writeTriggersOnload() throws Exception {
591613

592614
final WebDriver driver = loadPage2(html);
593615
driver.findElement(By.id("clickme")).click();
594-
verifyAlerts(driver, getExpectedAlerts());
616+
verifyTitle2(driver, getExpectedAlerts());
595617
}
596618

597619
/**
@@ -739,8 +761,7 @@ public void deny() throws Exception {
739761
* @throws Exception if the test fails
740762
*/
741763
@Test
742-
@Alerts(DEFAULT = {"loaded", "null"},
743-
FF_ESR = "null")
764+
@Alerts({"loaded", "null"})
744765
public void csp_None() throws Exception {
745766
retrictByHeader(
746767
new NameValuePair(HttpHeader.CONTENT_SECURIRY_POLICY, "frame-ancestors 'none';"),
@@ -784,8 +805,7 @@ public void csp_Url() throws Exception {
784805
* @throws Exception if the test fails
785806
*/
786807
@Test
787-
@Alerts(DEFAULT = {"loaded", "null"},
788-
FF_ESR = "null")
808+
@Alerts({"loaded", "null"})
789809
public void csp_UrlDifferentPort() throws Exception {
790810
retrictByHeader(
791811
new NameValuePair(HttpHeader.CONTENT_SECURIRY_POLICY, "frame-ancestors 'self';"),
@@ -796,8 +816,7 @@ public void csp_UrlDifferentPort() throws Exception {
796816
* @throws Exception if the test fails
797817
*/
798818
@Test
799-
@Alerts(DEFAULT = {"loaded", "null"},
800-
FF_ESR = "null")
819+
@Alerts({"loaded", "null"})
801820
public void csp_many() throws Exception {
802821
retrictByHeader(
803822
new NameValuePair(HttpHeader.CONTENT_SECURIRY_POLICY,
@@ -810,16 +829,17 @@ private void retrictByHeader(final NameValuePair header, final URL contentUrl) t
810829
+ "<html>\n"
811830
+ "<head>\n"
812831
+ " <script>\n"
832+
+ LOG_WINDOW_NAME_FUNCTION
813833
+ " function check() {\n"
814834
+ " try {\n"
815-
+ " alert(document.getElementById(\"frame1\").contentDocument);\n"
816-
+ " } catch(e) { alert('error'); }\n"
835+
+ " log(document.getElementById(\"frame1\").contentDocument);\n"
836+
+ " } catch(e) { log('error'); }\n"
817837
+ " }\n"
818838
+ " </script>\n"
819839
+ "</head>\n"
820840
+ "<body>\n"
821841
+ " <iframe id='frame1' src='" + contentUrl + "' "
822-
+ "onLoad='alert(\"loaded\")' onError='alert(\"error\")'></iframe>\n"
842+
+ "onLoad='log(\"loaded\")' onError='log(\"error\")'></iframe>\n"
823843
+ " <button type='button' id='clickme' onClick='check()'>Click me</a>\n"
824844
+ "</body>\n"
825845
+ "</html>";
@@ -836,11 +856,11 @@ private void retrictByHeader(final NameValuePair header, final URL contentUrl) t
836856
200, "OK", MimeType.TEXT_HTML, headers);
837857

838858
final String[] expectedAlerts = getExpectedAlerts();
839-
setExpectedAlerts(Arrays.copyOf(expectedAlerts, expectedAlerts.length - 1));
840-
final WebDriver driver = loadPageWithAlerts2(html, new URL(URL_FIRST, "path"));
859+
final WebDriver driver = loadPage2(html, new URL(URL_FIRST, "path"));
860+
verifyWindowName2(driver, Arrays.copyOf(expectedAlerts, expectedAlerts.length - 1));
841861

842862
driver.findElement(By.id("clickme")).click();
843-
verifyAlerts(driver, expectedAlerts[expectedAlerts.length - 1]);
863+
verifyWindowName2(driver, expectedAlerts);
844864

845865
assertEquals(2, getMockWebConnection().getRequestCount());
846866
}
@@ -854,28 +874,28 @@ public void recursive() throws Exception {
854874
final String html = DOCTYPE_HTML
855875
+ "<html>\n"
856876
+ "<head>\n"
857-
+ " <title>Deny</title>\n"
858877
+ " <script>\n"
878+
+ LOG_TITLE_FUNCTION
859879
+ " function check() {\n"
860880
+ " try {\n"
861-
+ " alert(document.getElementById(\"frame1\").contentDocument);\n"
862-
+ " } catch(e) { alert('error'); }\n"
881+
+ " log(document.getElementById(\"frame1\").contentDocument);\n"
882+
+ " } catch(e) { log('error'); }\n"
863883
+ " }\n"
864884
+ " </script>\n"
865885
+ "</head>\n"
866886
+ "<body>\n"
867887
+ " <iframe id='frame1' src='" + URL_FIRST + "' "
868-
+ "onLoad='alert(\"loaded\")'></iframe>\n"
888+
+ "onLoad='log(\"loaded\")'></iframe>\n"
869889
+ " <button type='button' id='clickme' onClick='check()'>Click me</a>\n"
870890
+ "</body>\n"
871891
+ "</html>";
872892

873893
final String[] expectedAlerts = getExpectedAlerts();
874-
setExpectedAlerts(Arrays.copyOf(expectedAlerts, 1));
875-
final WebDriver driver = loadPageWithAlerts2(html);
894+
final WebDriver driver = loadPage2(html);
895+
verifyTitle2(driver, expectedAlerts[0]);
876896

877897
driver.findElement(By.id("clickme")).click();
878-
verifyAlerts(driver, expectedAlerts[1]);
898+
verifyTitle2(driver, expectedAlerts[0], expectedAlerts[1]);
879899

880900
assertEquals(Integer.parseInt(expectedAlerts[2]), getMockWebConnection().getRequestCount());
881901
}
@@ -894,11 +914,13 @@ public void recursiveContent() throws Exception {
894914
final String html = DOCTYPE_HTML
895915
+ "<html>\n"
896916
+ "<head>\n"
897-
+ " <title>Deny</title>\n"
917+
+ " <script>\n"
918+
+ LOG_TITLE_FUNCTION
919+
+ " </script>\n"
898920
+ "</head>\n"
899921
+ "<body>\n"
900922
+ " <iframe id='frame1' src='content.html' "
901-
+ "onLoad='alert(\"loaded\")'></iframe>\n"
923+
+ "onLoad='log(\"loaded\")'></iframe>\n"
902924
+ "</body>\n"
903925
+ "</html>";
904926

@@ -913,8 +935,8 @@ public void recursiveContent() throws Exception {
913935
getMockWebConnection().setDefaultResponse(content);
914936

915937
final String[] expectedAlerts = getExpectedAlerts();
916-
setExpectedAlerts(Arrays.copyOf(expectedAlerts, 1));
917-
loadPageWithAlerts2(html);
938+
loadPage2(html);
939+
verifyTitle2(getWebDriver(), expectedAlerts[0]);
918940

919941
assertEquals(Integer.parseInt(expectedAlerts[1]), getMockWebConnection().getRequestCount());
920942
}
@@ -934,11 +956,13 @@ public void recursiveContentRedirectHeader() throws Exception {
934956
final String html = DOCTYPE_HTML
935957
+ "<html>\n"
936958
+ "<head>\n"
937-
+ " <title>Deny</title>\n"
959+
+ " <script>\n"
960+
+ LOG_TITLE_FUNCTION
961+
+ " </script>\n"
938962
+ "</head>\n"
939963
+ "<body>\n"
940964
+ " <iframe id='frame1' src='content.html' "
941-
+ "onLoad='alert(\"loaded\")'></iframe>\n"
965+
+ "onLoad='log(\"loaded\")'></iframe>\n"
942966
+ "</body>\n"
943967
+ "</html>";
944968

@@ -959,8 +983,8 @@ public void recursiveContentRedirectHeader() throws Exception {
959983
302, "Moved", MimeType.TEXT_HTML, headers);
960984

961985
final String[] expectedAlerts = getExpectedAlerts();
962-
setExpectedAlerts(Arrays.copyOf(expectedAlerts, 1));
963-
loadPageWithAlerts2(html);
986+
loadPage2(html);
987+
verifyTitle2(getWebDriver(), expectedAlerts[0]);
964988

965989
assertEquals(Integer.parseInt(expectedAlerts[1]), getMockWebConnection().getRequestCount());
966990
}

0 commit comments

Comments
 (0)