From: <rb...@us...> - 2017-10-31 10:29:22
|
Revision: 14880 http://sourceforge.net/p/htmlunit/code/14880 Author: rbri Date: 2017-10-31 10:29:19 +0000 (Tue, 31 Oct 2017) Log Message: ----------- stylesheets are always loaded on page load Issue 1927 Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLLinkElement.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLink2Test.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java 2017-10-27 17:39:23 UTC (rev 14879) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlLink.java 2017-10-31 10:29:19 UTC (rev 14880) @@ -21,6 +21,8 @@ import java.net.URL; import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.http.HttpStatus; import com.gargoylesoftware.htmlunit.SgmlPage; @@ -27,10 +29,15 @@ import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.WebResponse; +import com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine; +import com.gargoylesoftware.htmlunit.javascript.PostponedAction; +import com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet; +import com.gargoylesoftware.htmlunit.javascript.host.css.StyleSheetList; import com.gargoylesoftware.htmlunit.javascript.host.event.Event; import com.gargoylesoftware.htmlunit.javascript.host.event.Event2; import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLinkElement; import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLinkElement2; +import com.gargoylesoftware.htmlunit.xml.XmlPage; /** * Wrapper for the HTML element "link". <b>Note:</b> This is not a clickable link, @@ -45,7 +52,7 @@ * @author Ronald Brill */ public class HtmlLink extends HtmlElement { - // private static final Log LOG = LogFactory.getLog(HtmlLink.class); + private static final Log LOG = LogFactory.getLog(HtmlLink.class); /** The HTML tag represented by this element. */ public static final String TAG_NAME = "link"; @@ -249,38 +256,44 @@ } } -// /** -// * {@inheritDoc} -// */ -// @Override -// protected void onAllChildrenAddedToPage(final boolean postponed) { -// if (getOwnerDocument() instanceof XmlPage) { -// return; -// } -// if (LOG.isDebugEnabled()) { -// LOG.debug("Link node added: " + asXml()); -// } -// -// final PostponedAction action = new PostponedAction(getPage(), "Loading of link " + this) { -// @Override -// public void execute() { -// } -// }; -// -// final AbstractJavaScriptEngine<?> engine = getPage().getWebClient().getJavaScriptEngine(); -// if (postponed) { -// engine.addPostponedAction(action); -// } -// else { -// try { -// action.execute(); -// } -// catch (final RuntimeException e) { -// throw e; -// } -// catch (final Exception e) { -// throw new RuntimeException(e); -// } -// } -// } + /** + * {@inheritDoc} + */ + @Override + protected void onAllChildrenAddedToPage(final boolean postponed) { + if (getOwnerDocument() instanceof XmlPage) { + return; + } + if (LOG.isDebugEnabled()) { + LOG.debug("Link node added: " + asXml()); + } + + if (!StyleSheetList.isStyleSheetLink(this)) { + return; + } + + final PostponedAction action = new PostponedAction(getPage(), "Loading of link " + this) { + @Override + public void execute() { + final HTMLLinkElement linkElem = HtmlLink.this.getScriptableObject(); + CSSStyleSheet.loadStylesheet(linkElem, HtmlLink.this, null); + } + }; + + final AbstractJavaScriptEngine<?> engine = getPage().getWebClient().getJavaScriptEngine(); + if (postponed) { + engine.addPostponedAction(action); + } + else { + try { + action.execute(); + } + catch (final RuntimeException e) { + throw e; + } + catch (final Exception e) { + throw new RuntimeException(e); + } + } + } } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2017-10-27 17:39:23 UTC (rev 14879) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2017-10-31 10:29:19 UTC (rev 14880) @@ -102,7 +102,6 @@ import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.host.Element; -import com.gargoylesoftware.htmlunit.javascript.host.Window; import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument; import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement; import com.gargoylesoftware.htmlunit.util.UrlUtils; @@ -277,7 +276,7 @@ final String uri = (uri_ != null) ? uri_ : e.getPage().getUrl().toExternalForm(); final String href = importRule.getHref(); final String url = UrlUtils.resolveUrl(uri, href); - sheet = loadStylesheet(getWindow(), ownerNode_, null, url); + sheet = loadStylesheet(ownerNode_, null, url); imports_.put(importRule, sheet); } @@ -301,14 +300,12 @@ /** * Loads the stylesheet at the specified link or href. - * @param window the current window * @param element the parent DOM element * @param link the stylesheet's link (may be {@code null} if a <tt>url</tt> is specified) * @param url the stylesheet's url (may be {@code null} if a <tt>link</tt> is specified) * @return the loaded stylesheet */ - public static CSSStyleSheet loadStylesheet(final Window window, final HTMLElement element, - final HtmlLink link, final String url) { + public static CSSStyleSheet loadStylesheet(final HTMLElement element, final HtmlLink link, final String url) { CSSStyleSheet sheet; final HtmlPage page = (HtmlPage) element.getDomNodeOrDie().getPage(); String uri = page.getUrl().toExternalForm(); // fallback uri for exceptions Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLLinkElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLLinkElement.java 2017-10-27 17:39:23 UTC (rev 14879) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLLinkElement.java 2017-10-31 10:29:19 UTC (rev 14880) @@ -141,7 +141,7 @@ */ public CSSStyleSheet getSheet() { if (sheet_ == null) { - sheet_ = CSSStyleSheet.loadStylesheet(getWindow(), this, (HtmlLink) getDomNodeOrDie(), null); + sheet_ = CSSStyleSheet.loadStylesheet(this, (HtmlLink) getDomNodeOrDie(), null); } return sheet_; } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLink2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLink2Test.java 2017-10-27 17:39:23 UTC (rev 14879) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlLink2Test.java 2017-10-31 10:29:19 UTC (rev 14880) @@ -14,7 +14,6 @@ */ package com.gargoylesoftware.htmlunit.html; -import static com.gargoylesoftware.htmlunit.BrowserRunner.TestedBrowser.CHROME; import static com.gargoylesoftware.htmlunit.BrowserRunner.TestedBrowser.IE; import java.net.URL; @@ -136,7 +135,6 @@ */ @Test @Alerts({"onLoad", "body onLoad"}) - @NotYetImplemented(CHROME) public void onLoadMediaPrint() throws Exception { getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), ""); onLoadOnError("rel='stylesheet' href='simple.css' media='print'"); @@ -157,7 +155,6 @@ */ @Test @Alerts({"onLoad", "body onLoad"}) - @NotYetImplemented(CHROME) public void onLoadMediaQueryNotMatch() throws Exception { getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), ""); onLoadOnError("rel='stylesheet' href='simple.css' media='(max-width: 10px)'"); @@ -200,10 +197,42 @@ + " <link " + attribs + " onload='alert(\"onLoad\")' onerror='alert(\"onError\")'>\n" + "</head>\n" - + "<body onload='window.getComputedStyle(document.body); alert(\"body onLoad\")'>\n" + + "<body onload='alert(\"body onLoad\")'>\n" + "</body>\n" + "</html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"onLoad1", "onLoadJs1", "onLoad2", "body onLoad;"}) + public void onLoadOrder() throws Exception { + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple1.css"), ""); + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple2.css"), ""); + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple1.js"), "var x=1;"); + + final String html + = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function log(x) {\n" + + " document.title += x + ';';\n" + + " }\n" + + " </script>\n" + + " <link rel='stylesheet' href='simple1.css' onload='log(\"onLoad1\")'>\n" + + " <script type='text/javascript' src='simple1.js' onload='log(\"onLoadJs1\")'></script>\n" + + " <link rel='stylesheet' href='simple2.css' onload='log(\"onLoad2\")'>\n" + + "</head>\n" + + "<body onload='log(\"body onLoad\")'>\n" + + "</body>\n" + + "</html>"; + + final WebDriver driver = loadPage2(html); + Thread.sleep(200); + final String text = driver.getTitle(); + assertEquals(String.join(";", getExpectedAlerts()), text); + } } |