From: <rb...@us...> - 2017-12-30 20:13:16
|
Revision: 15051 http://sourceforge.net/p/htmlunit/code/15051 Author: rbri Date: 2017-12-30 20:13:13 +0000 (Sat, 30 Dec 2017) Log Message: ----------- CSSStyleSheet#addRule fixed error handling CSSStyleDeclaration#cssText throws an NPE when in stylesheet mode CSSStyleDeclaration#cssText usually return a ';' after the last rule make one method private Issue 1943 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheetTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-12-29 20:51:09 UTC (rev 15050) +++ trunk/htmlunit/src/changes/changes.xml 2017-12-30 20:13:13 UTC (rev 15051) @@ -7,6 +7,18 @@ </properties> <body> + <release version="2.30" date="xx, 2018" description="Bugfixes"> + <action type="fix" dev="rbri" issue="1943"> + JavaScript: CSSStyleSheet#addRule fixed error handling + </action> + <action type="fix" dev="rbri"> + JavaScript: CSSStyleDeclaration#cssText throws an NPE when in stylesheet mode + </action> + <action type="fix" dev="rbri"> + JavaScript: CSSStyleDeclaration#cssText usually return a ';' after the last rule + </action> + </release> + <release version="2.29" date="December 28, 2017" description="Bugfixes, Chrome 63, WebStart support"> <action type="add" dev="rbri"> JavaScript: TextEncoder/TextDecoder impl added. Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java 2017-12-29 20:51:09 UTC (rev 15050) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java 2017-12-30 20:13:13 UTC (rev 15051) @@ -273,7 +273,7 @@ * Initializes the object. * @param htmlElement the element that this style describes */ - void initialize(final Element element) { + private void initialize(final Element element) { // Initialize. WebAssert.notNull("htmlElement", element); jsElement_ = element; @@ -1217,6 +1217,13 @@ */ @JsxGetter public String getCssText() { + if (styleDeclaration_ != null) { + final String text = styleDeclaration_.getCssText(); + if (styleDeclaration_.getLength() > 0) { + return text + ";"; + } + return text; + } return jsElement_.getDomNodeOrDie().getAttribute("style"); } @@ -1226,6 +1233,10 @@ */ @JsxSetter public void setCssText(final String value) { + if (styleDeclaration_ != null) { + styleDeclaration_.setCssText(value); + return; + } jsElement_.getDomNodeOrDie().setAttribute("style", value); } 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-12-29 20:51:09 UTC (rev 15050) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2017-12-30 20:13:13 UTC (rev 15051) @@ -1176,7 +1176,7 @@ */ @JsxFunction({IE, CHROME}) public int addRule(final String selector, final String rule) { - final String completeRule = selector + " {" + rule + "}"; + String completeRule = selector + " {" + rule + "}"; try { initCssRules(); wrapped_.insertRule(completeRule, wrapped_.getCssRules().getLength()); @@ -1183,7 +1183,16 @@ refreshCssRules(); } catch (final DOMException e) { - throw Context.throwAsScriptRuntimeEx(e); + // in case of error try with an empty rule + completeRule = selector + " {}"; + try { + initCssRules(); + wrapped_.insertRule(completeRule, wrapped_.getCssRules().getLength()); + refreshCssRules(); + } + catch (final DOMException ex) { + throw Context.throwAsScriptRuntimeEx(ex); + } } return -1; } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheetTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheetTest.java 2017-12-29 20:51:09 UTC (rev 15050) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheetTest.java 2017-12-30 20:13:13 UTC (rev 15051) @@ -191,8 +191,8 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = {"1", "false", "false", "0", "2", "p"}, - FF = {"1", "false", "true", "0", "2", "p"}) + @Alerts(DEFAULT = {"1", "false", "false", "0", "2", "p", "vertical-align: top;"}, + FF = {"1", "false", "true", "0", "2", "p", "vertical-align: top;"}) public void addRule_insertRule() throws Exception { final String html = "<html><head><title>foo</title><script>\n" + "function doTest() {\n" @@ -205,9 +205,10 @@ + " if (s.insertRule)\n" + " alert(s.insertRule('div { color: red; }', 0));\n" + " else\n" - + " alert(s.addRule('div', 'color: red;', 1));\n" + + " alert(s.addRule('div', 'color: red;'));\n" + " alert(rules.length);\n" + " alert(rules[1].selectorText);\n" + + " alert(rules[1].style.cssText);\n" + "}</script>\n" + "<style id='myStyle'>p { vertical-align:top }</style>\n" + "</head><body onload='doTest()'>\n" @@ -217,12 +218,45 @@ } /** + * Minimal test for addRule / insertRule. + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = {"1", "-1", "div", "", "2"}, + IE = {"1", "1", "div", "", "2"}, + FF = {"1", "1"}) + public void addRuleInvalidRule() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + "<script>\n" + + " function doTest() {\n" + + " var f = document.getElementById('myStyle');\n" + + " var s = f.sheet ? f.sheet : f.styleSheet;\n" + + " var rules = s.cssRules || s.rules;\n" + + " alert(rules.length);\n" + + " if (s.addRule) {\n" + + " alert(s.addRule('div', 'invalid'));\n" + + " alert(rules[rules.length - 1].selectorText);\n" + + " alert(rules[rules.length - 1].style.cssText);\n" + + " }\n" + + " alert(rules.length);\n" + + " }\n" + + " </script>\n" + + " <style id='myStyle'>p { vertical-align:top }</style>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** * Minimal test for removeRule / deleteRule. * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = {"2", "false", "false", "undefined", "1", "div"}, - FF = {"2", "false", "true", "undefined", "1", "div"}) + @Alerts(DEFAULT = {"2", "false", "false", "undefined", "1", "div", "color: red;"}, + FF = {"2", "false", "true", "undefined", "1", "div", "color: red;"}) public void removeRule_deleteRule() throws Exception { final String html = "<html><head><title>foo</title><script>\n" + "function doTest() {\n" @@ -238,6 +272,7 @@ + " alert(s.removeRule(0));\n" + " alert(rules.length);\n" + " alert(rules[0].selectorText);\n" + + " alert(rules[0].style.cssText);\n" + "}</script>\n" + "<style id='myStyle'>p { vertical-align:top } div { color: red; }</style>\n" + "</head><body onload='doTest()'>\n" @@ -277,7 +312,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({"2", "1", "div"}) + @Alerts({"2", "1", "div", "color: red;"}) public void deleteRuleIgnored() throws Exception { final String html = "<html><head><title>foo</title><script>\n" + "function doTest() {\n" @@ -292,6 +327,7 @@ + " s.removeRule(0);\n" + " alert(rules.length);\n" + " alert(rules[0].selectorText);\n" + + " alert(rules[0].style.cssText);\n" + " } catch(err) { alert('exception'); }\n" + "}</script>\n" + "<style id='myStyle'>\n" @@ -309,7 +345,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({"2", "1", "p"}) + @Alerts({"2", "1", "p", "vertical-align: top;"}) public void deleteRuleIgnoredLast() throws Exception { final String html = "<html><head><title>foo</title><script>\n" + "function doTest() {\n" @@ -324,6 +360,7 @@ + " s.removeRule(1);\n" + " alert(rules.length);\n" + " alert(rules[0].selectorText);\n" + + " alert(rules[0].style.cssText);\n" + " } catch(err) { alert('exception'); }\n" + "}</script>\n" + "<style id='myStyle'>\n" @@ -342,7 +379,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts({"2", ".testStyleDef", ".testStyle"}) + @Alerts({"2", ".testStyleDef", "height: 42px;", ".testStyle", "width: 24px;"}) public void insertRuleLeadingWhitespace() throws Exception { final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ @@ -356,7 +393,9 @@ + " s.insertRule(' .testStyleDef { height: 42px; }', 0);\n" + " alert(rules.length);\n" + " alert(rules[0].selectorText);\n" + + " alert(rules[0].style.cssText);\n" + " alert(rules[1].selectorText);\n" + + " alert(rules[1].style.cssText);\n" + " }\n" + "}</script>\n" + "<style id='myStyle'></style>\n" |