Skip to content

Commit

Permalink
move more css code away from the javascript peers
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 14, 2023
1 parent bae95ff commit 05e40ec
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/htmlunit/html/HtmlLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
* <code>&lt;link rel="stylesheet" type="text/css" href="..." /&gt;</code>).
*/
private CssStyleSheet sheet_;

/**
* Creates an instance of HtmlLink
*
Expand Down Expand Up @@ -324,6 +331,18 @@ public void execute() {
}
}

/**
* Returns the associated style sheet (only valid for links of type
* <code>&lt;link rel="stylesheet" type="text/css" href="..." /&gt;</code>).
* @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'
*/
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/htmlunit/html/HtmlStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -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
*
Expand Down Expand Up @@ -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_;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -103,7 +111,7 @@ public void setRel(final String rel) {
*/
@JsxGetter
public String getRel() {
return ((HtmlLink) getDomNodeOrDie()).getRelAttribute();
return getDomNodeOrDie().getRelAttribute();
}

/**
Expand All @@ -121,7 +129,7 @@ public void setRev(final String rel) {
*/
@JsxGetter
public String getRev() {
return ((HtmlLink) getDomNodeOrDie()).getRevAttribute();
return getDomNodeOrDie().getRevAttribute();
}

/**
Expand All @@ -139,7 +147,7 @@ public void setType(final String type) {
*/
@JsxGetter
public String getType() {
return ((HtmlLink) getDomNodeOrDie()).getTypeAttribute();
return getDomNodeOrDie().getTypeAttribute();
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -219,5 +226,4 @@ public boolean isDisabled() {
public void setDisabled(final boolean disabled) {
super.setDisabled(disabled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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_;
}
Expand Down

0 comments on commit 05e40ec

Please sign in to comment.