From: <rb...@us...> - 2017-12-07 17:06:24
|
Revision: 14993 http://sourceforge.net/p/htmlunit/code/14993 Author: rbri Date: 2017-12-07 17:06:21 +0000 (Thu, 07 Dec 2017) Log Message: ----------- check option cssEnabled before downloading a css file Issue 1935 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClientOptions.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/StyleSheetList.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLinkTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-12-07 16:34:47 UTC (rev 14992) +++ trunk/htmlunit/src/changes/changes.xml 2017-12-07 17:06:21 UTC (rev 14993) @@ -8,6 +8,9 @@ <body> <release version="2.29" date="xx, 2017" description=""> + <action type="fix" dev="rbri" issue="1935"> + Check option cssEnabled before downloading a css file (regression from 1927). + </action> <action type="fix" dev="rbri" issue="1840"> JavaScript: Error.captureStackTrace is not available in FF and IE. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClientOptions.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClientOptions.java 2017-12-07 16:34:47 UTC (rev 14992) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClientOptions.java 2017-12-07 17:06:21 UTC (rev 14993) @@ -230,6 +230,8 @@ /** * Enables/disables CSS support. By default, this property is enabled. + * If disabled HtmlUnit will not download the linked css files and also + * not triggered the associated onload/onerror events. * * @param enabled {@code true} to enable CSS support */ Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java 2017-12-07 16:34:47 UTC (rev 14992) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java 2017-12-07 17:06:21 UTC (rev 14993) @@ -274,7 +274,8 @@ LOG.debug("Link node added: " + asXml()); } - if (!StyleSheetList.isStyleSheetLink(this)) { + if (!getPage().getWebClient().getOptions().isCssEnabled() + || !StyleSheetList.isStyleSheetLink(this)) { return; } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/StyleSheetList.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/StyleSheetList.java 2017-12-07 16:34:47 UTC (rev 14992) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/StyleSheetList.java 2017-12-07 17:06:21 UTC (rev 14993) @@ -128,10 +128,9 @@ setPrototype(getPrototype(getClass())); final WebClient webClient = getWindow().getWebWindow().getWebClient(); - final boolean cssEnabled = webClient.getOptions().isCssEnabled(); - final boolean onlyActive = webClient.getBrowserVersion().hasFeature(JS_STYLESHEETLIST_ACTIVE_ONLY); - if (cssEnabled) { + if (webClient.getOptions().isCssEnabled()) { + final boolean onlyActive = webClient.getBrowserVersion().hasFeature(JS_STYLESHEETLIST_ACTIVE_ONLY); nodes_ = new HTMLCollection(document.getDomNodeOrDie(), true) { @Override protected boolean isMatching(final DomNode node) { Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLinkTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLinkTest.java 2017-12-07 16:34:47 UTC (rev 14992) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLinkTest.java 2017-12-07 17:06:21 UTC (rev 14993) @@ -14,10 +14,13 @@ */ package com.gargoylesoftware.htmlunit.html; +import java.net.URL; + import org.junit.Test; import org.junit.runner.RunWith; import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; import com.gargoylesoftware.htmlunit.HttpHeader; import com.gargoylesoftware.htmlunit.SimpleWebTestCase; import com.gargoylesoftware.htmlunit.WebResponse; @@ -48,4 +51,74 @@ assertEquals(page.getUrl().toExternalForm(), respCss.getWebRequest().getAdditionalHeaders().get(HttpHeader.REFERER)); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts("body onLoad") + public void onLoad() throws Exception { + getWebClientWithMockWebConnection().getOptions().setCssEnabled(false); + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), ""); + + final String html + = "<html>\n" + + "<head>\n" + + " <link rel='stylesheet' href='simple.css' " + + "onload='alert(\"onLoad\")' onerror='alert(\"onError\")'>\n" + + "</head>\n" + + "<body onload='alert(\"body onLoad\")'>\n" + + "</body>\n" + + "</html>"; + + loadPageWithAlerts(html); + assertEquals(1, getMockWebConnection().getRequestCount()); + } + + /** + * @throws Exception if an error occurs + */ + @Test + public void onLoadDynamic() throws Exception { + getWebClientWithMockWebConnection().getOptions().setCssEnabled(false); + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), ""); + + final String html + = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " var dynLink = document.createElement('link');\n" + + " dynLink.rel = 'stylesheet';\n" + + " dynLink.type = 'text/css';\n" + + " dynLink.href = 'simple.css';" + + " dynLink.onload = function (e) { log(\"onLoad \" + e) };\n" + + " dynLink.onerror = function (e) { log(\"onError \" + e) };\n" + + " document.head.appendChild(dynLink);\n" + + + " var dynLink = document.createElement('link');\n" + + " dynLink.rel = 'stylesheet';\n" + + " dynLink.type = 'text/css';\n" + + " dynLink.href = 'unknown.css';" + + " dynLink.onload = function (e) { log(\"onLoad \" + e) };\n" + + " dynLink.onerror = function (e) { log(\"onError \" + e) };\n" + + " document.head.appendChild(dynLink);\n" + + " }\n" + + + " function log(x) {\n" + + " document.getElementById('log').value += x + '\\n';\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'></body>\n" + + " <textarea id='log' cols='80' rows='40'></textarea>\n" + + "</body>\n" + + "</html>"; + + final HtmlPage page = loadPageWithAlerts(html); + final String text = page.getElementById("log").getAttribute("value").trim().replaceAll("\r", ""); + assertEquals(String.join("\n", getExpectedAlerts()), text); + + assertEquals(1, getMockWebConnection().getRequestCount()); + } } |