diff --git a/src/main/java/org/htmlunit/html/HtmlLink.java b/src/main/java/org/htmlunit/html/HtmlLink.java
index 5fb6c9c2224..042887bcf33 100644
--- a/src/main/java/org/htmlunit/html/HtmlLink.java
+++ b/src/main/java/org/htmlunit/html/HtmlLink.java
@@ -30,6 +30,7 @@
import org.htmlunit.WebClient;
import org.htmlunit.WebRequest;
import org.htmlunit.WebResponse;
+import org.htmlunit.css.CssStyleSheet;
import org.htmlunit.javascript.AbstractJavaScriptEngine;
import org.htmlunit.javascript.PostponedAction;
import org.htmlunit.javascript.host.event.Event;
@@ -55,6 +56,12 @@ public class HtmlLink extends HtmlElement {
/** The HTML tag represented by this element. */
public static final String TAG_NAME = "link";
+ /**
+ * The associated style sheet (only valid for links of type
+ * <link rel="stylesheet" type="text/css" href="..." />
).
+ */
+ private CssStyleSheet sheet_;
+
/**
* Creates an instance of HtmlLink
*
@@ -324,6 +331,18 @@ public void execute() {
}
}
+ /**
+ * Returns the associated style sheet (only valid for links of type
+ * <link rel="stylesheet" type="text/css" href="..." />
).
+ * @return the associated style sheet
+ */
+ public CssStyleSheet getSheet() {
+ if (sheet_ == null) {
+ sheet_ = CssStyleSheet.loadStylesheet(this, this, null);
+ }
+ return sheet_;
+ }
+
/**
* @return true if the rel attribute is 'stylesheet'
*/
diff --git a/src/main/java/org/htmlunit/html/HtmlStyle.java b/src/main/java/org/htmlunit/html/HtmlStyle.java
index 10fc691486d..bb237b716b0 100644
--- a/src/main/java/org/htmlunit/html/HtmlStyle.java
+++ b/src/main/java/org/htmlunit/html/HtmlStyle.java
@@ -14,9 +14,17 @@
*/
package org.htmlunit.html;
+import java.io.IOException;
+import java.io.StringReader;
import java.util.Map;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.htmlunit.Cache;
import org.htmlunit.SgmlPage;
+import org.htmlunit.css.CssStyleSheet;
+import org.htmlunit.cssparser.dom.CSSStyleSheetImpl;
+import org.htmlunit.cssparser.parser.InputSource;
/**
* Wrapper for the HTML element "style".
@@ -29,9 +37,13 @@
*/
public class HtmlStyle extends HtmlElement {
+ private static final Log LOG = LogFactory.getLog(HtmlStyle.class);
+
/** The HTML tag represented by this element. */
public static final String TAG_NAME = "style";
+ private CssStyleSheet sheet_;
+
/**
* Creates an instance of HtmlStyle
*
@@ -110,4 +122,33 @@ public DisplayStyle getDefaultStyleDisplay() {
public boolean mayBeDisplayed() {
return false;
}
+
+ /**
+ * @return the referenced style sheet
+ */
+ public CssStyleSheet getSheet() {
+ if (sheet_ != null) {
+ return sheet_;
+ }
+
+ final Cache cache = getPage().getWebClient().getCache();
+ final CSSStyleSheetImpl cached = cache.getCachedStyleSheet(getTextContent());
+ final String uri = getPage().getWebResponse().getWebRequest().getUrl().toExternalForm();
+
+ if (cached != null) {
+ sheet_ = new CssStyleSheet(this, cached, uri);
+ }
+ else {
+ final String css = getTextContent();
+ try (InputSource source = new InputSource(new StringReader(css))) {
+ sheet_ = new CssStyleSheet(this, source, uri);
+ cache.cache(css, sheet_.getWrappedSheet());
+ }
+ catch (final IOException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+
+ return sheet_;
+ }
}
diff --git a/src/main/java/org/htmlunit/javascript/host/html/HTMLLinkElement.java b/src/main/java/org/htmlunit/javascript/host/html/HTMLLinkElement.java
index 1e751932672..95df6ab77f2 100644
--- a/src/main/java/org/htmlunit/javascript/host/html/HTMLLinkElement.java
+++ b/src/main/java/org/htmlunit/javascript/host/html/HTMLLinkElement.java
@@ -60,6 +60,14 @@ public class HTMLLinkElement extends HTMLElement {
public HTMLLinkElement() {
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public HtmlLink getDomNodeOrDie() {
+ return (HtmlLink) super.getDomNodeOrDie();
+ }
+
/**
* Sets the href property.
* @param href href attribute value
@@ -75,7 +83,7 @@ public void setHref(final String href) {
*/
@JsxGetter
public String getHref() {
- final HtmlLink link = (HtmlLink) getDomNodeOrDie();
+ final HtmlLink link = getDomNodeOrDie();
final String href = link.getHrefAttribute();
if (href.isEmpty()) {
return href;
@@ -103,7 +111,7 @@ public void setRel(final String rel) {
*/
@JsxGetter
public String getRel() {
- return ((HtmlLink) getDomNodeOrDie()).getRelAttribute();
+ return getDomNodeOrDie().getRelAttribute();
}
/**
@@ -121,7 +129,7 @@ public void setRev(final String rel) {
*/
@JsxGetter
public String getRev() {
- return ((HtmlLink) getDomNodeOrDie()).getRevAttribute();
+ return getDomNodeOrDie().getRevAttribute();
}
/**
@@ -139,7 +147,7 @@ public void setType(final String type) {
*/
@JsxGetter
public String getType() {
- return ((HtmlLink) getDomNodeOrDie()).getTypeAttribute();
+ return getDomNodeOrDie().getTypeAttribute();
}
/**
@@ -150,9 +158,8 @@ public String getType() {
public CSSStyleSheet getSheet() {
if (sheet_ == null) {
try {
- final CssStyleSheet sheet =
- CssStyleSheet.loadStylesheet(getDomNodeOrDie(), (HtmlLink) getDomNodeOrDie(), null);
- sheet_ = new CSSStyleSheet(this, this.getWindow(), sheet);
+ final CssStyleSheet sheet = getDomNodeOrDie().getSheet();
+ sheet_ = new CSSStyleSheet(this, getWindow(), sheet);
}
catch (final RuntimeException e) {
// Got something unexpected; we can throw an exception in this case.
@@ -219,5 +226,4 @@ public boolean isDisabled() {
public void setDisabled(final boolean disabled) {
super.setDisabled(disabled);
}
-
}
diff --git a/src/main/java/org/htmlunit/javascript/host/html/HTMLStyleElement.java b/src/main/java/org/htmlunit/javascript/host/html/HTMLStyleElement.java
index 176b8ca2ab2..d2d31401e35 100644
--- a/src/main/java/org/htmlunit/javascript/host/html/HTMLStyleElement.java
+++ b/src/main/java/org/htmlunit/javascript/host/html/HTMLStyleElement.java
@@ -19,15 +19,12 @@
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
-import org.htmlunit.Cache;
import org.htmlunit.css.CssStyleSheet;
-import org.htmlunit.cssparser.dom.CSSStyleSheetImpl;
import org.htmlunit.html.HtmlStyle;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxGetter;
import org.htmlunit.javascript.configuration.JsxSetter;
-import org.htmlunit.javascript.host.Window;
import org.htmlunit.javascript.host.css.CSSStyleSheet;
/**
@@ -62,20 +59,7 @@ public CSSStyleSheet getSheet() {
}
final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
- final String css = style.getTextContent();
-
- final Window window = getWindow();
- final Cache cache = window.getWebWindow().getWebClient().getCache();
- final CSSStyleSheetImpl cached = cache.getCachedStyleSheet(css);
- final String uri = getDomNodeOrDie().getPage().getWebResponse().getWebRequest()
- .getUrl().toExternalForm();
- if (cached != null) {
- sheet_ = new CSSStyleSheet(this, window, new CssStyleSheet(style, cached, uri));
- }
- else {
- sheet_ = new CSSStyleSheet(this, css, uri);
- cache.cache(css, sheet_.getCssStyleSheet().getWrappedSheet());
- }
+ sheet_ = new CSSStyleSheet(this, getWindow(), style.getSheet());
return sheet_;
}