From: <rb...@us...> - 2013-12-30 11:56:56
|
Revision: 8919 http://sourceforge.net/p/htmlunit/code/8919 Author: rbri Date: 2013-12-30 11:56:52 +0000 (Mon, 30 Dec 2013) Log Message: ----------- Return value fixed for element.height and element.width when element is not attached to the page. Element.currentStyle returns null for elements not attached to the page. This fixes also some jQuery tests Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/changes/changes.xml 2013-12-30 11:56:52 UTC (rev 8919) @@ -8,6 +8,12 @@ <body> <release version="2.14" date="???" description="Bugfixes"> + <action type="fix" dev="rbri"> + Return value fixed for element.height and element.width when element is not attached to the page. + </action> + <action type="fix" dev="rbri"> + Element.currentStyle returns null for elements not attached to the page. + </action> <action type="update" dev="rbri"> Upgrade commons-codec to 1.9. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java 2013-12-30 11:56:52 UTC (rev 8919) @@ -543,6 +543,9 @@ */ @JsxGetter(@WebBrowser(IE)) public ComputedCSSStyleDeclaration getCurrentStyle() { + if (!getDomNodeOrDie().isDirectlyAttachedToPage()) { + return null; + } return getWindow().getComputedStyle(this, null); } 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 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleDeclaration.java 2013-12-30 11:56:52 UTC (rev 8919) @@ -4466,7 +4466,7 @@ * @return the CSS attribute value for the specified element */ public final String get(final Element element) { - final ComputedCSSStyleDeclaration style = element.getCurrentStyle(); + final ComputedCSSStyleDeclaration style = element.getWindow().getComputedStyle(element, null); final String value = get(style); return value; } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java 2013-12-30 11:56:52 UTC (rev 8919) @@ -602,7 +602,8 @@ if (value.isEmpty()) { final Element parent = getElement().getParentElement(); if (parent != null) { - value = parent.getCurrentStyle().getFontSize(); + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + value = style.getFontSize(); } } if (value.isEmpty()) { @@ -660,7 +661,12 @@ */ @Override public String getHeight() { - return pixelString(getElement(), new CssValue(getElement().getWindow().getWebWindow().getInnerHeight()) { + final Element elem = getElement(); + if (!elem.getDomNodeOrDie().isDirectlyAttachedToPage()) { + return "auto"; + } + final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight(); + return pixelString(elem, new CssValue(windowHeight) { @Override public String get(final ComputedCSSStyleDeclaration style) { return defaultIfEmpty(style.getStyleAttribute("height"), "362px"); } @@ -1026,29 +1032,32 @@ if ("none".equals(getDisplay())) { return "auto"; } - final int windowWidth = getElement().getWindow().getWebWindow().getInnerWidth(); - final String defaultWidth; - if (getBrowserVersion().hasFeature(CSS_DEFAULT_WIDTH_AUTO)) { - defaultWidth = "auto"; + + final Element elem = getElement(); + if (!elem.getDomNodeOrDie().isDirectlyAttachedToPage()) { + return "auto"; } - else { - defaultWidth = windowWidth + "px"; - } - return pixelString(getElement(), new CssValue(windowWidth) { - @Override public String get(final ComputedCSSStyleDeclaration style) { + + final int windowWidth = elem.getWindow().getWebWindow().getInnerWidth(); + return pixelString(elem, new CssValue(windowWidth) { + @Override + public String get(final ComputedCSSStyleDeclaration style) { final String value = style.getStyleAttribute(WIDTH); if (StringUtils.isEmpty(value)) { - if (!getBrowserVersion().hasFeature(CSS_DEFAULT_WIDTH_AUTO) - && "absolute".equals(getStyleAttribute("position"))) { - final DomNode domNode = getDomNodeOrDie(); - final String content = domNode.getTextContent(); + if (getBrowserVersion().hasFeature(CSS_DEFAULT_WIDTH_AUTO)) { + return "auto"; + } + + if ("absolute".equals(getStyleAttribute("position"))) { + final String content = getDomNodeOrDie().getTextContent(); // do this only for small content // at least for empty div's this is more correct if (null != content && (content.length() < 13)) { return (content.length() * 7) + "px"; } } - return defaultWidth; + + return getWindowDefaultValue() + "px"; } return value; } @@ -1176,7 +1185,8 @@ for (final DomNode child : childs) { if (child.getScriptObject() instanceof HTMLElement) { final HTMLElement e = (HTMLElement) child.getScriptObject(); - final int w = e.getCurrentStyle().getCalculatedWidth(true, true); + final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null); + final int w = style.getCalculatedWidth(true, true); width += w; } else if (child.getScriptObject() instanceof Text) { @@ -1332,7 +1342,7 @@ final ScriptableObject scriptObj = child.getScriptObject(); if (scriptObj instanceof HTMLElement) { final HTMLElement e = (HTMLElement) scriptObj; - final ComputedCSSStyleDeclaration style = e.getCurrentStyle(); + final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null); final String pos = style.getPositionWithInheritance(); if ("static".equals(pos) || "relative".equals(pos)) { lastFlowing = style; @@ -1426,7 +1436,7 @@ } if (prev != null) { final HTMLElement e = (HTMLElement) ((HtmlElement) prev).getScriptObject(); - final ComputedCSSStyleDeclaration style = e.getCurrentStyle(); + final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null); top = style.getTop(true, false, false) + style.getCalculatedHeight(true, true); } // If the position is relative, we also need to add the specified "top" displacement. @@ -1483,13 +1493,15 @@ else if ("absolute".equals(p) && !"auto".equals(r)) { // Need to calculate the horizontal displacement caused by *all* siblings. final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - final int parentWidth = parent.getCurrentStyle().getCalculatedWidth(false, false); + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + final int parentWidth = style.getCalculatedWidth(false, false); left = parentWidth - pixelValue(r); } else if ("fixed".equals(p) && "auto".equals(l)) { // Fixed to the location at which the browser puts it via normal element flowing. final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - left = pixelValue(parent.getCurrentStyle().getLeftWithInheritance()); + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + left = pixelValue(style.getLeftWithInheritance()); } else if ("static".equals(p)) { // We need to calculate the horizontal displacement caused by *previous* siblings. @@ -1497,7 +1509,7 @@ for (DomNode n = getDomNodeOrDie(); n != null; n = n.getPreviousSibling()) { if (n.getScriptObject() instanceof HTMLElement) { final HTMLElement e = (HTMLElement) n.getScriptObject(); - final ComputedCSSStyleDeclaration style = e.getCurrentStyle(); + final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null); final String d = style.getDisplay(); if ("block".equals(d)) { break; @@ -1546,7 +1558,13 @@ if ("inherit".equals(p)) { if (getBrowserVersion().hasFeature(CAN_INHERIT_CSS_PROPERTY_VALUES)) { final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - p = (parent != null ? parent.getCurrentStyle().getPositionWithInheritance() : "static"); + if (parent == null) { + p = "static"; + } + else { + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + p = style.getPositionWithInheritance(); + } } else { p = "static"; @@ -1564,7 +1582,13 @@ if ("inherit".equals(left)) { if (getBrowserVersion().hasFeature(CAN_INHERIT_CSS_PROPERTY_VALUES)) { final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - left = (parent != null ? parent.getCurrentStyle().getLeftWithInheritance() : "auto"); + if (parent == null) { + left = "auto"; + } + else { + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + left = style.getLeftWithInheritance(); + } } else { left = "auto"; @@ -1582,7 +1606,13 @@ if ("inherit".equals(right)) { if (getBrowserVersion().hasFeature(CAN_INHERIT_CSS_PROPERTY_VALUES)) { final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - right = (parent != null ? parent.getCurrentStyle().getRightWithInheritance() : "auto"); + if (parent == null) { + right = "auto"; + } + else { + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + right = style.getRightWithInheritance(); + } } else { right = "auto"; @@ -1600,7 +1630,13 @@ if ("inherit".equals(top)) { if (getBrowserVersion().hasFeature(CAN_INHERIT_CSS_PROPERTY_VALUES)) { final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - top = (parent != null ? parent.getCurrentStyle().getTopWithInheritance() : "auto"); + if (parent == null) { + top = "auto"; + } + else { + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + top = style.getTopWithInheritance(); + } } else { top = "auto"; @@ -1618,7 +1654,13 @@ if ("inherit".equals(bottom)) { if (getBrowserVersion().hasFeature(CAN_INHERIT_CSS_PROPERTY_VALUES)) { final HTMLElement parent = (HTMLElement) getElement().getParentElement(); - bottom = (parent != null ? parent.getCurrentStyle().getBottomWithInheritance() : "auto"); + if (parent == null) { + bottom = "auto"; + } + else { + final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null); + bottom = style.getBottomWithInheritance(); + } } else { bottom = "auto"; Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/ElementTest.java 2013-12-30 11:56:52 UTC (rev 8919) @@ -1109,4 +1109,26 @@ loadPageWithAlerts2(html); } + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "undefined", "undefined" }, IE = { "available", "null" }) + public void currentStyle() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " var e = document.getElementById('tester');\n" + + " alert(e.currentStyle ? 'available' : e.currentStyle);\n" + + " e = document.createElement('div');\n" + + " alert(e.currentStyle ? 'available' : e.currentStyle);\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + " <div id='tester'></div>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java 2013-12-30 11:56:52 UTC (rev 8919) @@ -1219,6 +1219,28 @@ } /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "auto", "auto" }, IE8 = { "-", "-" }) + public void widthAndHeightDisconnected() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " var e = document.createElement('div');\n" + + " var style = window.getComputedStyle ? window.getComputedStyle(e, null) : e.currentStyle;\n" + + " alert(style ? style.width : '-');\n" + + " alert(style ? style.height : '-');\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } + + /** * @throws Exception if the test fails */ @Test Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java 2013-12-29 18:52:56 UTC (rev 8918) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java 2013-12-30 11:56:52 UTC (rev 8919) @@ -6523,7 +6523,6 @@ */ @Test @Alerts("0, 6, 6") - @NotYetImplemented(FF) public void dimensions__innerWidth__() throws Exception { runTest("dimensions: innerWidth()"); } @@ -6534,7 +6533,6 @@ */ @Test @Alerts("0, 6, 6") - @NotYetImplemented public void dimensions__innerHeight__() throws Exception { runTest("dimensions: innerHeight()"); } @@ -6545,7 +6543,6 @@ */ @Test @Alerts("0, 11, 11") - @NotYetImplemented(FF) public void dimensions__outerWidth__() throws Exception { runTest("dimensions: outerWidth()"); } @@ -6596,7 +6593,6 @@ */ @Test @Alerts("0, 11, 11") - @NotYetImplemented public void dimensions__outerHeight__() throws Exception { runTest("dimensions: outerHeight()"); } |