Skip to content

Commit

Permalink
properties ownerNode and href moved from CSSStyleSheet to StyleSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 30, 2023
1 parent 74f91c0 commit 43a7735
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 60 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
of the various WebClient properties should be clear in every case. This results in some minor changes the might have
some impact for the backward compatibility.
</action>
<action type="update" dev="rbri">
Properties ownerNode and href moved from CSSStyleSheet to StyleSheet.
</action>
<action type="update" dev="rbri">
Upgrade Apache commons-io to 2.15.0.
</action>
Expand Down
61 changes: 14 additions & 47 deletions src/main/java/org/htmlunit/javascript/host/css/CSSStyleSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.htmlunit.javascript.host.css;

import static org.htmlunit.BrowserVersionFeatures.STYLESHEET_ADD_RULE_RETURNS_POS;
import static org.htmlunit.BrowserVersionFeatures.STYLESHEET_HREF_EMPTY_IS_NULL;
import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
Expand All @@ -24,11 +23,8 @@

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -42,9 +38,6 @@
import org.htmlunit.cssparser.parser.InputSource;
import org.htmlunit.cssparser.parser.selector.SelectorList;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.HtmlLink;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.html.HtmlStyle;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxFunction;
Expand All @@ -71,15 +64,9 @@ public class CSSStyleSheet extends StyleSheet {

private static final Log LOG = LogFactory.getLog(CSSStyleSheet.class);

private static final Pattern NTH_NUMERIC = Pattern.compile("\\d+");
private static final Pattern NTH_COMPLEX = Pattern.compile("[+-]?\\d*n\\w*([+-]\\w\\d*)?");

/** The parsed stylesheet which this host object wraps. */
private final CssStyleSheet styleSheet_;

/** The HTML element which owns this stylesheet. */
private final HTMLElement ownerNode_;

/** The collection of rules defined in this style sheet. */
private CSSRuleList cssRules_;
private List<Integer> cssRulesIndexFix_;
Expand All @@ -89,8 +76,8 @@ public class CSSStyleSheet extends StyleSheet {
*/
@JsxConstructor({CHROME, EDGE, FF, FF_ESR})
public CSSStyleSheet() {
super(null);
styleSheet_ = new CssStyleSheet(null, (InputSource) null, null);
ownerNode_ = null;
}

/**
Expand All @@ -100,11 +87,12 @@ public CSSStyleSheet() {
* @param uri this stylesheet's URI (used to resolved contained @import rules)
*/
public CSSStyleSheet(final HTMLElement element, final InputSource source, final String uri) {
super(element);

setParentScope(element.getWindow());
setPrototype(getPrototype(CSSStyleSheet.class));

styleSheet_ = new CssStyleSheet(element.getDomNodeOrDie(), source, uri);
ownerNode_ = element;
}

/**
Expand All @@ -114,6 +102,8 @@ public CSSStyleSheet(final HTMLElement element, final InputSource source, final
* @param uri this stylesheet's URI (used to resolved contained @import rules)
*/
public CSSStyleSheet(final HTMLElement element, final String styleSheet, final String uri) {
super(element);

final Window win = element.getWindow();

CssStyleSheet css = null;
Expand All @@ -128,7 +118,6 @@ public CSSStyleSheet(final HTMLElement element, final String styleSheet, final S
setPrototype(getPrototype(CSSStyleSheet.class));

styleSheet_ = css;
ownerNode_ = element;
}

/**
Expand All @@ -139,10 +128,11 @@ public CSSStyleSheet(final HTMLElement element, final String styleSheet, final S
*/
public CSSStyleSheet(final HTMLElement element, final Scriptable parentScope,
final CssStyleSheet cssStyleSheet) {
super(element);

setParentScope(parentScope);
setPrototype(getPrototype(CSSStyleSheet.class));
styleSheet_ = cssStyleSheet;
ownerNode_ = element;
}

/**
Expand All @@ -158,8 +148,9 @@ public CssStyleSheet getCssStyleSheet() {
* @return the owner node
*/
@JsxGetter(IE)
@Override
public HTMLElement getOwnerNode() {
return ownerNode_;
return super.getOwnerNode();
}

/**
Expand All @@ -168,7 +159,7 @@ public HTMLElement getOwnerNode() {
*/
@JsxGetter(IE)
public HTMLElement getOwningElement() {
return ownerNode_;
return getOwnerNode();
}

/**
Expand All @@ -191,37 +182,12 @@ public CSSRuleList getCssRules() {
}

/**
* Returns the URL of the stylesheet.
* @return the URL of the stylesheet
* {@inheritDoc}
*/
@JsxGetter(IE)
@Override
public String getHref() {
if (ownerNode_ != null) {
final DomNode node = ownerNode_.getDomNodeOrDie();
if (node instanceof HtmlStyle) {
return null;
}
if (node instanceof HtmlLink) {
// <link rel="stylesheet" type="text/css" href="..." />
final HtmlLink link = (HtmlLink) node;
final String href = link.getHrefAttribute();
if ("".equals(href) && getBrowserVersion().hasFeature(STYLESHEET_HREF_EMPTY_IS_NULL)) {
return null;
}
// Expand relative URLs.
try {
final HtmlPage page = (HtmlPage) link.getPage();
final URL url = page.getFullyQualifiedUrl(href);
return url.toExternalForm();
}
catch (final MalformedURLException e) {
// Log the error and fall through to the return values below.
LOG.warn(e.getMessage(), e);
}
}
}

return getUri();
return super.getHref();
}

/**
Expand Down Expand Up @@ -371,6 +337,7 @@ public void removeRule(final int position) {
* For inline styles this is the page uri.
* @return this stylesheet's URI (used to resolved contained @import rules)
*/
@Override
public String getUri() {
return getCssStyleSheet().getUri();
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/org/htmlunit/javascript/host/css/StyleSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@
*/
package org.htmlunit.javascript.host.css;

import static org.htmlunit.BrowserVersionFeatures.STYLESHEET_HREF_EMPTY_IS_NULL;
import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.HtmlLink;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.html.HtmlStyle;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxGetter;
import org.htmlunit.javascript.host.html.HTMLElement;

/**
* A JavaScript object for {@code StyleSheet}.
Expand All @@ -32,10 +44,72 @@
@JsxClass
public class StyleSheet extends HtmlUnitScriptable {

private static final Log LOG = LogFactory.getLog(StyleSheet.class);

/** The HTML element which owns this stylesheet. */
private final HTMLElement ownerNode_;

/**
* Default constructor.
*/
@JsxConstructor({CHROME, EDGE, FF, FF_ESR})
public StyleSheet() {
ownerNode_ = null;
}

public StyleSheet(final HTMLElement ownerNode) {
ownerNode_ = ownerNode;
}

/**
* Returns the owner node.
* @return the owner node
*/
@JsxGetter
public HTMLElement getOwnerNode() {
return ownerNode_;
}

/**
* Returns the URL of the stylesheet.
* @return the URL of the stylesheet
*/
@JsxGetter
public String getHref() {
if (ownerNode_ != null) {
final DomNode node = ownerNode_.getDomNodeOrDie();
if (node instanceof HtmlStyle) {
return null;
}
if (node instanceof HtmlLink) {
// <link rel="stylesheet" type="text/css" href="..." />
final HtmlLink link = (HtmlLink) node;
final String href = link.getHrefAttribute();
if ("".equals(href) && getBrowserVersion().hasFeature(STYLESHEET_HREF_EMPTY_IS_NULL)) {
return null;
}
// Expand relative URLs.
try {
final HtmlPage page = (HtmlPage) link.getPage();
final URL url = page.getFullyQualifiedUrl(href);
return url.toExternalForm();
}
catch (final MalformedURLException e) {
// Log the error and fall through to the return values below.
LOG.warn(e.getMessage(), e);
}
}
}

return getUri();
}

/**
* Returns this stylesheet's URI (used to resolved contained @import rules).
* For inline styles this is the page uri.
* @return this stylesheet's URI (used to resolved contained @import rules)
*/
public String getUri() {
return null;
}
}
23 changes: 22 additions & 1 deletion src/test/java/org/htmlunit/general/ElementOwnPropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ private void testString(final String preparation, final String string) throws Ex
+ " @font-face { font-family: Delicious; src: url('Delicious-Bold.otf'); };\n"
+ " </style>\n"
+ " <style>\n"
+ " @import 'imp.css';\n"
+ " </style>\n"
+ " <style>\n"
+ " h3 { color: blue; }\n"
+ " </style>\n"

Expand Down Expand Up @@ -18572,6 +18575,24 @@ public void cssFontFaceRule() throws Exception {
testString("", "document.styleSheets[2].cssRules[0]");
}

/**
* Test CSSImportRule.
*
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "constructor(),href[GCE],layerName[GCE],media[GSCE],styleSheet[GCE]",
FF = "constructor(),href[GCE],layerName[GCE],media[GSCE],styleSheet[GCE],supportsText[GCE]",
FF_ESR = "constructor(),href[GCE],layerName[GCE],media[GSCE],styleSheet[GCE],supportsText[GCE]",
IE = "constructor[],href[GCE],media[GCE],styleSheet[GCE]")
@HtmlUnitNYI(CHROME = "constructor(),href[GCE],media[GCE],styleSheet[GCE]",
EDGE = "constructor(),href[GCE],media[GCE],styleSheet[GCE]",
FF = "constructor(),href[GCE],media[GCE],styleSheet[GCE]",
FF_ESR = "constructor(),href[GCE],media[GCE],styleSheet[GCE]")
public void cssImportRule() throws Exception {
testString("", "document.styleSheets[3].cssRules[0]");
}

/**
* Test CSSRule.
*
Expand All @@ -18588,6 +18609,6 @@ public void cssFontFaceRule() throws Exception {
FF = "constructor(),selectorText[GSCE],style[GCE]",
FF_ESR = "constructor(),selectorText[GSCE],style[GCE]")
public void cssStyleRule() throws Exception {
testString("", "document.styleSheets[3].cssRules[0]");
testString("", "document.styleSheets[4].cssRules[0]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ private void testString(final String preparation, final String string) throws Ex
+ " @font-face { font-family: Delicious; src: url('Delicious-Bold.otf'); };\n"
+ " </style>\n"
+ " <style>\n"
+ " @import 'imp.css';\n"
+ " </style>\n"
+ " <style>\n"
+ " h3 { color: blue; }\n"
+ " </style>\n"

Expand Down Expand Up @@ -3851,6 +3854,18 @@ public void cssFontFaceRule() throws Exception {
testString("", "document.styleSheets[2].cssRules[0]");
}

/**
* Test CSSImportRule.
*
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "Symbol(Symbol.toStringTag) [CSSImportRule]",
IE = "exception")
public void cssImportRule() throws Exception {
testString("", "document.styleSheets[3].cssRules[0]");
}

/**
* Test CSSRule.
*
Expand All @@ -3860,6 +3875,6 @@ public void cssFontFaceRule() throws Exception {
@Alerts(DEFAULT = "Symbol(Symbol.toStringTag) [CSSStyleRule]",
IE = "exception")
public void cssStyleRule() throws Exception {
testString("", "document.styleSheets[3].cssRules[0]");
testString("", "document.styleSheets[4].cssRules[0]");
}
}
Loading

0 comments on commit 43a7735

Please sign in to comment.