Skip to content

Commit 85f20b1

Browse files
committed
various fixes for the height calculation (issue #941)
1 parent 03c7406 commit 85f20b1

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

src/main/java/org/htmlunit/css/ComputedCssStyleDeclaration.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -752,15 +752,20 @@ public String getHeight() {
752752
if (!elem.isAttachedToPage()) {
753753
return "";
754754
}
755-
final int windowHeight = elem.getPage().getEnclosingWindow().getInnerHeight();
756-
return CssPixelValueConverter
757-
.pixelString(elem, new CssPixelValueConverter.CssValue(0, windowHeight) {
758-
@Override
759-
public String get(final ComputedCssStyleDeclaration style) {
760-
final String offsetHeight = style.getCalculatedHeight(true, true) + "px";
761-
return defaultIfEmpty(style.getStyleAttribute(Definition.HEIGHT, true), offsetHeight, AUTO);
762-
}
763-
});
755+
756+
final ComputedCssStyleDeclaration style = elem.getPage().getEnclosingWindow().getComputedStyle(elem, null);
757+
final String styleValue = style.getStyleAttribute(Definition.HEIGHT, true);
758+
759+
if (styleValue == null || styleValue.isEmpty() || AUTO.equals(styleValue) || styleValue.endsWith("%")) {
760+
final String calculatedHeight = style.getCalculatedHeight(false, false) + "px";
761+
return calculatedHeight;
762+
}
763+
764+
if (styleValue.endsWith("px")) {
765+
return styleValue;
766+
}
767+
768+
return CssPixelValueConverter.pixelValue(styleValue) + "px";
764769
}
765770

766771
/**

src/test/java/org/htmlunit/javascript/host/css/CSSStyleSheetTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,4 +2003,34 @@ public void brokenExternalCSS() throws Exception {
20032003
getMockWebConnection().setResponse(URL_SECOND, "body { font-weight: 900\\9; }");
20042004
loadPageVerifyTitle2(html);
20052005
}
2006+
2007+
/**
2008+
* Test for #941.
2009+
* @throws Exception if the test fails
2010+
*/
2011+
@Test
2012+
@Alerts({"true", "true"})
2013+
public void widthHeightPercent() throws Exception {
2014+
final String html = "<html>\n"
2015+
+ " <head>"
2016+
+ " <style>#testDiv { width: 50%; height: 50%; background-color: blue; }</style>\n"
2017+
+ " </head>"
2018+
+ " <body>\n"
2019+
+ " <div id='testDiv'>Test Div</div>\n"
2020+
2021+
+ "<script>\n"
2022+
+ LOG_TITLE_FUNCTION
2023+
+ " let elem = document.getElementById('testDiv');\n"
2024+
+ " let sty = window.getComputedStyle(elem, null);\n"
2025+
+ " let w = (window.innerWidth / 2) - parseInt(sty.width, 10);\n"
2026+
+ " log(10 > w);\n"
2027+
+ " let h = (window.innerHeight / 2) - parseInt(sty.height, 10);\n"
2028+
+ " log(10 > h);\n"
2029+
+ "</script>\n"
2030+
2031+
+ " </body>\n"
2032+
+ "</html>";
2033+
2034+
loadPageVerifyTitle2(html);
2035+
}
20062036
}

src/test/java/org/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public void lengthsConvertedToPixels() throws Exception {
328328
+ LOG_TITLE_FUNCTION
329329
+ "var d = document.getElementById('d');\n"
330330
+ "var cs = window.getComputedStyle(d, null);\n"
331+
331332
+ "log(d.style.width + ' ' + cs.width);\n"
332333
+ "log(d.style.height + ' ' + cs.height);\n"
333334
+ "log(d.style.borderBottomWidth + ' ' + cs.borderBottomWidth);\n"
@@ -2503,7 +2504,7 @@ public void offsetHeightTable() throws Exception {
25032504
* @throws Exception if an error occurs
25042505
*/
25052506
@Test
2506-
@Alerts({"false", "false"})
2507+
@Alerts({"18px", "18px"})
25072508
public void height() throws Exception {
25082509
final String html = "<html>\n"
25092510
+ "<head>\n"
@@ -2517,9 +2518,9 @@ public void height() throws Exception {
25172518
+ " function test() {\n"
25182519
+ " var div = document.getElementById('myDiv');\n"
25192520
+ " var style = window.getComputedStyle(div, null);\n"
2520-
+ " log(style.height == '0px');\n"
2521+
+ " log(style.height);\n"
25212522
+ " div.className = 'autoheight';\n"
2522-
+ " log(style.height == '0px');\n"
2523+
+ " log(style.height);\n"
25232524
+ " }\n"
25242525
+ "</script></head>\n"
25252526
+ "<body onload='test()'>\n"

src/test/java/org/htmlunit/javascript/host/css/property/ElementOffsetHeightTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ public void offsetHeightManualLineBreaks() throws Exception {
175175
* @throws Exception if the test fails
176176
*/
177177
@Test
178-
@Alerts({"549", "273"})
179-
@HtmlUnitNYI(CHROME = {"552", "294"},
180-
EDGE = {"552", "294"},
181-
FF = {"552", "294"},
182-
FF_ESR = {"552", "294"})
178+
@Alerts({"300", "549", "945", "60", "273", "938", "35"})
179+
@HtmlUnitNYI(CHROME = {"300", "552", "9690", "60", "294", "6885", "43"},
180+
EDGE = {"300", "552", "9690", "60", "294", "6885", "43"},
181+
FF = {"300", "552", "9690", "60", "294", "6885", "43"},
182+
FF_ESR = {"300", "552", "9690", "60", "294", "6885", "43"})
183183
public void issue124() throws Exception {
184184
final String html
185185
= "<!DOCTYPE html>\n"
@@ -215,10 +215,14 @@ public void issue124() throws Exception {
215215
+ " var titleSizer = document.querySelector('.title-sizer');\n"
216216
+ " var title = document.querySelector('.title');\n"
217217
+ " var titleHeight = titleSizer.offsetHeight;\n"
218-
+ " var titleFontSize = getAttributeValue(titleSizer, 'fontSize');\n"
219218
+ " var titleHeightGoal = getAttributeValue(titleSizer, 'height');\n"
219+
+ " var titleFontSize = getAttributeValue(titleSizer, 'fontSize');\n"
220220

221-
+ " log(titleHeight);\r\n"
221+
+ " log(titleHeightGoal);\n"
222+
223+
+ " log(titleHeight);\n"
224+
+ " log(titleSizer.offsetWidth);\n"
225+
+ " log(titleFontSize);\n"
222226

223227
+ " while (titleHeight > titleHeightGoal) {\n"
224228
+ " titleFontSize -= 1;\n"
@@ -227,6 +231,8 @@ public void issue124() throws Exception {
227231
+ " }\n"
228232

229233
+ " log(titleHeight);\n"
234+
+ " log(titleSizer.offsetWidth);\n"
235+
+ " log(titleFontSize);\n"
230236
+ " </script>\n"
231237
+ "</html>";
232238

0 commit comments

Comments
 (0)