Skip to content

Commit

Permalink
Symbol.iterator property is now available on CSSStyleDeclaration/Comp…
Browse files Browse the repository at this point in the history
…utedCSSStyleDeclaration
  • Loading branch information
rbri committed Jan 3, 2024
1 parent 405e483 commit b4e914f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="3.10.0" date="December xx, 2023" description="Chrome/Edge 120, Firefox 120, Bugfixes">
<action type="add" dev="rbri">
Symbol.iterator property is now available on CSSStyleDeclaration/ComputedCSSStyleDeclaration.
</action>
<action type="fix" dev="rbri">
SymbolConstants are readonly.
</action>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/htmlunit/javascript/HtmlUnitScriptable.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ public BrowserVersion getBrowserVersion() {
if (node != null) {
return node.getPage().getWebClient().getBrowserVersion();
}
return getWindow().getWebWindow().getWebClient().getBrowserVersion();

final Window window = getWindow();
if (window != null) {
final WebWindow webWindow = window.getWebWindow();
if (webWindow != null) {
return webWindow.getWebClient().getBrowserVersion();
}
}

return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ private static void configureSymbols(final ClassConfiguration config,

final Callable symbolFunction = scriptable.has(methodName, scriptable)
? (Callable) scriptable.get(methodName, scriptable)
: new FunctionObject(symbol.toString(), method, scriptable);
: new FunctionObject(methodName, method, scriptable);
scriptable.defineProperty(symbol, symbolFunction, ScriptableObject.DONTENUM);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.htmlunit.BrowserVersion;
import org.htmlunit.corejs.javascript.ES6Iterator;
import org.htmlunit.corejs.javascript.NativeArrayIterator;
import org.htmlunit.corejs.javascript.ScriptRuntime;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.ScriptableObject;
Expand All @@ -142,6 +144,7 @@
import org.htmlunit.javascript.configuration.JsxFunction;
import org.htmlunit.javascript.configuration.JsxGetter;
import org.htmlunit.javascript.configuration.JsxSetter;
import org.htmlunit.javascript.configuration.JsxSymbol;
import org.htmlunit.javascript.host.Element;

/**
Expand Down Expand Up @@ -1286,6 +1289,15 @@ public Object item(final int index) {
return styleDeclaration_.item(index);
}

/**
* Returns an Iterator allowing to go through all keys contained in this object.
* @return an {@link NativeArrayIterator}
*/
@JsxSymbol(value = {CHROME, EDGE, FF, FF_ESR}, symbolName = "iterator")
public ES6Iterator values() {
return new NativeArrayIterator(getParentScope(), this, NativeArrayIterator.ARRAY_ITERATOR_TYPE.VALUES);
}

/**
* Gets the {@code letterSpacing} style attribute.
* @return the style attribute
Expand Down Expand Up @@ -1580,9 +1592,12 @@ public boolean has(final String name, final Scriptable start) {
return super.has(name, start);
}

final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
if (style != null) {
return true;
final BrowserVersion browserVersion = getBrowserVersion();
if (browserVersion != null) {
final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
if (style != null) {
return true;
}
}

return super.has(name, start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.htmlunit.BrowserVersion;
import org.htmlunit.Page;
import org.htmlunit.WebWindow;
import org.htmlunit.corejs.javascript.ES6Iterator;
import org.htmlunit.css.ComputedCssStyleDeclaration;
import org.htmlunit.css.CssPixelValueConverter;
import org.htmlunit.css.CssPixelValueConverter.CssValue;
Expand Down Expand Up @@ -109,6 +110,7 @@
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxSymbol;
import org.htmlunit.javascript.configuration.JsxSymbolConstant;
import org.htmlunit.javascript.host.Element;
import org.htmlunit.javascript.host.dom.Text;
Expand Down Expand Up @@ -162,6 +164,17 @@ protected ComputedCssStyleDeclaration getCssStyleDeclaration() {
return (ComputedCssStyleDeclaration) super.getCssStyleDeclaration();
}

/**
* {@inheritDoc}
*
* This method does nothing as the object is read-only.
*/
@Override
@JsxSymbol(value = {FF, FF_ESR}, symbolName = "iterator")
public ES6Iterator values() {
return super.values();
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2975,10 +2975,6 @@ public void nodeListButtonLabels() throws Exception {
FF = "Symbol(Symbol.iterator) [WC] [function],Symbol(Symbol.toStringTag) [C] [CSS2Properties]",
FF_ESR = "Symbol(Symbol.iterator) [WC] [function],Symbol(Symbol.toStringTag) [C] [CSS2Properties]",
IE = "exception")
@HtmlUnitNYI(CHROME = "Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]",
EDGE = "Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]",
FF = "Symbol(Symbol.toStringTag) [C] [CSS2Properties]",
FF_ESR = "Symbol(Symbol.toStringTag) [C] [CSS2Properties]")
public void computedStyle() throws Exception {
testString("", "window.getComputedStyle(document.body)");
}
Expand All @@ -2993,10 +2989,8 @@ public void computedStyle() throws Exception {
FF = "Symbol(Symbol.iterator) [WC] [function],Symbol(Symbol.toStringTag) [C] [CSS2Properties]",
FF_ESR = "Symbol(Symbol.iterator) [WC] [function],Symbol(Symbol.toStringTag) [C] [CSS2Properties]",
IE = "exception")
@HtmlUnitNYI(CHROME = "Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]",
EDGE = "Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]",
FF = "Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]",
FF_ESR = "Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]")
@HtmlUnitNYI(FF = "Symbol(Symbol.iterator) [WC] [function],Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]",
FF_ESR = "Symbol(Symbol.iterator) [WC] [function],Symbol(Symbol.toStringTag) [C] [CSSStyleDeclaration]")
public void cssStyleDeclaration() throws Exception {
testString("", "document.body.style");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.apache.commons.lang3.StringUtils;
import org.htmlunit.WebDriverTestCase;
import org.htmlunit.html.HtmlPageTest;
import org.htmlunit.junit.BrowserRunner;
import org.htmlunit.junit.BrowserRunner.Alerts;
import org.htmlunit.junit.BrowserRunner.HtmlUnitNYI;
Expand Down Expand Up @@ -445,4 +446,42 @@ public void setLengthStrictMode() throws Exception {

loadPageVerifyTitle2(html);
}

/**
* @throws Exception on test failure
*/
@Test
@Alerts(DEFAULT = {"function values() { [native code] }", "no for..of", "display"},
IE = "no for..of")
public void iterator() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n"
+ "</head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var style = document.body.style;\n"

+ " if (typeof Symbol != 'undefined') {\n"
+ " log(style[Symbol.iterator]);\n"
+ " }\n"

+ " if (!style.forEach) {\n"
+ " log('no for..of');\n"
+ " }\n"

+ " if (typeof Symbol === 'undefined') {\n"
+ " return;\n"
+ " }\n"

+ " for (var i of style) {\n"
+ " log(i);\n"
+ " }\n"
+ " }\n"
+ "</script>\n"
+ "</head><body onload='test()' style='display: inline'>\n"
+ " <div></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.commons.io.IOUtils;
import org.htmlunit.WebDriverTestCase;
import org.htmlunit.html.HtmlPageTest;
import org.htmlunit.junit.BrowserRunner;
import org.htmlunit.junit.BrowserRunner.Alerts;
import org.htmlunit.junit.BrowserRunner.HtmlUnitNYI;
Expand Down Expand Up @@ -2914,4 +2915,44 @@ public void getOffsetHeightInvalidSelector() throws Exception {

loadPageVerifyTitle2(html);
}

/**
* @throws Exception on test failure
*/
@Test
@Alerts(DEFAULT = {"function values() { [native code] }", "no for..of", "true"},
IE = "no for..of")
public void iterator() throws Exception {
final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n"
+ "</head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var style = window.getComputedStyle(document.body, null);\n"

+ " if (typeof Symbol != 'undefined') {\n"
+ " log(style[Symbol.iterator]);\n"
+ " }\n"

+ " if (!style.forEach) {\n"
+ " log('no for..of');\n"
+ " }\n"

+ " if (typeof Symbol === 'undefined') {\n"
+ " return;\n"
+ " }\n"

+ " var count = 0;\n"
+ " for (var i of style) {\n"
+ " count++;\n"
+ " }\n"
+ " log(count > 0);"
+ " }\n"
+ "</script>\n"
+ "</head><body onload='test()' style='display: inline'>\n"
+ " <div></div>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}
}

0 comments on commit b4e914f

Please sign in to comment.