Skip to content

Commit f2fff35

Browse files
committed
small optimizations and better height calculation of empty body
1 parent 742038b commit f2fff35

File tree

4 files changed

+201
-16
lines changed

4 files changed

+201
-16
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.htmlunit.BrowserVersion;
4545
import org.htmlunit.BrowserVersionFeatures;
4646
import org.htmlunit.Page;
47+
import org.htmlunit.SgmlPage;
4748
import org.htmlunit.WebWindow;
4849
import org.htmlunit.css.CssPixelValueConverter.CssValue;
4950
import org.htmlunit.css.StyleAttributes.Definition;
@@ -104,6 +105,7 @@
104105
import org.htmlunit.html.HtmlNoLayer;
105106
import org.htmlunit.html.HtmlNoScript;
106107
import org.htmlunit.html.HtmlOutput;
108+
import org.htmlunit.html.HtmlPage;
107109
import org.htmlunit.html.HtmlPasswordInput;
108110
import org.htmlunit.html.HtmlPlainText;
109111
import org.htmlunit.html.HtmlRadioButtonInput;
@@ -1432,12 +1434,12 @@ public int getCalculatedHeight(final boolean includeBorder, final boolean includ
14321434
if (!element.isAttachedToPage()) {
14331435
return 0;
14341436
}
1435-
int height = getCalculatedHeight();
1437+
int height = getCalculatedHeight(element);
14361438
if (!"border-box".equals(getStyleAttribute(Definition.BOX_SIZING, true))) {
14371439
if (includeBorder) {
14381440
height += getBorderVertical();
14391441
}
1440-
else if (isScrollable(true, true) && !(element instanceof HtmlBody)) {
1442+
else if (isScrollable(element, true, true) && !(element instanceof HtmlBody)) {
14411443
height -= 17;
14421444
}
14431445

@@ -1452,14 +1454,12 @@ else if (isScrollable(true, true) && !(element instanceof HtmlBody)) {
14521454
* Returns the element's calculated height, taking both relevant CSS and the element's children into account.
14531455
* @return the element's calculated height, taking both relevant CSS and the element's children into account
14541456
*/
1455-
private int getCalculatedHeight() {
1457+
private int getCalculatedHeight(final DomElement element) {
14561458
final Integer cachedHeight = getCachedHeight();
14571459
if (cachedHeight != null) {
14581460
return cachedHeight.intValue();
14591461
}
14601462

1461-
final DomElement element = getDomElement();
1462-
14631463
if (element instanceof HtmlImage) {
14641464
return updateCachedHeight(((HtmlImage) element).getHeightOrDefault());
14651465
}
@@ -1473,7 +1473,7 @@ private int getCalculatedHeight() {
14731473
}
14741474
}
14751475

1476-
return updateCachedHeight(getEmptyHeight());
1476+
return updateCachedHeight(getEmptyHeight(element));
14771477
}
14781478

14791479
/**
@@ -1493,7 +1493,7 @@ public int getCalculatedWidth(final boolean includeBorder, final boolean include
14931493
if (includeBorder) {
14941494
width += getBorderHorizontal();
14951495
}
1496-
else if (isScrollable(false, true) && !(element instanceof HtmlBody)) {
1496+
else if (isScrollable(element, false, true) && !(element instanceof HtmlBody)) {
14971497
width -= 17;
14981498
}
14991499

@@ -1659,13 +1659,12 @@ else if (child instanceof DomText) {
16591659
* @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
16601660
* elements
16611661
*/
1662-
private int getEmptyHeight() {
1662+
private int getEmptyHeight(final DomElement element) {
16631663
final Integer cachedHeight2 = getCachedEmptyHeight();
16641664
if (cachedHeight2 != null) {
16651665
return cachedHeight2.intValue();
16661666
}
16671667

1668-
final DomElement element = getDomElement();
16691668
if (!element.mayBeDisplayed()) {
16701669
return updateCachedEmptyHeight(0);
16711670
}
@@ -1675,11 +1674,16 @@ private int getEmptyHeight() {
16751674
return updateCachedEmptyHeight(0);
16761675
}
16771676

1678-
final WebWindow webWindow = element.getPage().getEnclosingWindow();
1677+
final SgmlPage page = element.getPage();
1678+
final WebWindow webWindow = page.getEnclosingWindow();
16791679
final int windowHeight = webWindow.getInnerHeight();
16801680

16811681
if (element instanceof HtmlBody) {
1682-
return updateCachedEmptyHeight(windowHeight);
1682+
if (page instanceof HtmlPage && ((HtmlPage) page).isQuirksMode()) {
1683+
return updateCachedEmptyHeight(windowHeight);
1684+
}
1685+
1686+
return updateCachedEmptyHeight(0);
16831687
}
16841688

16851689
final boolean isInline = INLINE.equals(display) && !(element instanceof HtmlInlineFrame);
@@ -1922,15 +1926,14 @@ else if (ABSOLUTE.equals(position) || FIXED.equals(position)) {
19221926
* @return {@code true} if the element is scrollable along the specified axis
19231927
*/
19241928
public boolean isScrollable(final boolean horizontal) {
1925-
return isScrollable(horizontal, false);
1929+
return isScrollable(getDomElement(), horizontal, false);
19261930
}
19271931

19281932
/**
19291933
* @param ignoreSize whether to consider the content/calculated width/height
19301934
*/
1931-
private boolean isScrollable(final boolean horizontal, final boolean ignoreSize) {
1935+
private boolean isScrollable(final DomElement element, final boolean horizontal, final boolean ignoreSize) {
19321936
final boolean scrollable;
1933-
final DomElement element = getDomElement();
19341937

19351938
String overflow;
19361939
if (horizontal) {
@@ -1956,7 +1959,7 @@ private boolean isScrollable(final boolean horizontal, final boolean ignoreSize)
19561959
}
19571960

19581961
scrollable = (element instanceof HtmlBody || SCROLL.equals(overflow) || AUTO.equals(overflow))
1959-
&& (ignoreSize || getContentHeight() > getEmptyHeight());
1962+
&& (ignoreSize || getContentHeight() > getEmptyHeight(element));
19601963
}
19611964
return scrollable;
19621965
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ public void widthHeightPercent() throws Exception {
20552055
* Test for #942.
20562056
* @throws Exception if the test fails
20572057
*/
2058-
// @Test
2058+
@Test
20592059
@Alerts({"0 622 / 722", "1 622 / 722", "2 622 / 722", "3 622 / 722",
20602060
"4 622 / 722", "5 622 / 722", "6 622 / 722"})
20612061
public void endlessLoop() throws Exception {

src/test/java/org/htmlunit/javascript/host/dom/DocumentTest.java

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,4 +3762,177 @@ public void newDoc() throws Exception {
37623762

37633763
loadPageVerifyTitle2(html);
37643764
}
3765+
3766+
/**
3767+
* @throws Exception if the test fails
3768+
*/
3769+
@Test
3770+
@Alerts(CHROME = {"0", "0", "8", "1256"},
3771+
EDGE = {"0", "0", "8", "1248"},
3772+
FF = {"0", "0", "8", "1256"},
3773+
FF_ESR = {"0", "0", "8", "1260"})
3774+
@HtmlUnitNYI(CHROME = {"0", "0", "8", "1256"},
3775+
EDGE = {"0", "0", "8", "1256"},
3776+
FF = {"0", "0", "8", "1256"},
3777+
FF_ESR = {"0", "0", "8", "1256"})
3778+
public void documentElementBoundingClientRect() throws Exception {
3779+
final String html = DOCTYPE_HTML
3780+
+ "<html>"
3781+
+ "<body>\n"
3782+
+ "<script>\n"
3783+
+ LOG_TITLE_FUNCTION
3784+
+ " let rect = document.documentElement.getBoundingClientRect();\n"
3785+
+ " log(rect.top);\n"
3786+
+ " log(rect.left);\n"
3787+
+ " log(rect.bottom);\n"
3788+
+ " log(rect.right);\n"
3789+
+ "</script>\n"
3790+
+ "</body></html>";
3791+
3792+
loadPageVerifyTitle2(html);
3793+
}
3794+
3795+
/**
3796+
* @throws Exception if the test fails
3797+
*/
3798+
@Test
3799+
@Alerts(CHROME = {"0", "0", "621", "1256"},
3800+
EDGE = {"0", "0", "630", "1248"},
3801+
FF = {"0", "0", "8", "1256"},
3802+
FF_ESR = {"0", "0", "8", "1260"})
3803+
@HtmlUnitNYI(CHROME = {"0", "0", "613", "1256"},
3804+
EDGE = {"0", "0", "613", "1256"},
3805+
FF = {"0", "0", "613", "1256"},
3806+
FF_ESR = {"0", "0", "613", "1256"})
3807+
public void documentElementBoundingClientRectQuirks() throws Exception {
3808+
final String html =
3809+
"<html>"
3810+
+ "<body>\n"
3811+
+ "<script>\n"
3812+
+ LOG_TITLE_FUNCTION
3813+
+ " let rect = document.documentElement.getBoundingClientRect();\n"
3814+
+ " log(rect.top);\n"
3815+
+ " log(rect.left);\n"
3816+
+ " log(rect.bottom);\n"
3817+
+ " log(rect.right);\n"
3818+
+ "</script>\n"
3819+
+ "</body></html>";
3820+
3821+
loadPageVerifyTitle2(html);
3822+
}
3823+
3824+
3825+
/**
3826+
* @throws Exception if the test fails
3827+
*/
3828+
@Test
3829+
@Alerts(CHROME = {"0", "0", "8", "1256"},
3830+
EDGE = {"0", "0", "8", "1248"},
3831+
FF = {"0", "0", "8", "1256"},
3832+
FF_ESR = {"0", "0", "8", "1260"})
3833+
@HtmlUnitNYI(CHROME = {"0", "0", "8", "1256"},
3834+
EDGE = {"0", "0", "8", "1256"},
3835+
FF = {"0", "0", "8", "1256"},
3836+
FF_ESR = {"0", "0", "8", "1256"})
3837+
public void documentElementOffset() throws Exception {
3838+
final String html = DOCTYPE_HTML
3839+
+ "<html>"
3840+
+ "<body>\n"
3841+
+ "<script>\n"
3842+
+ LOG_TITLE_FUNCTION
3843+
+ " let doc = document.documentElement;\n"
3844+
+ " log(doc.offsetTop);\n"
3845+
+ " log(doc.offsetLeft);\n"
3846+
+ " log(doc.offsetHeight);\n"
3847+
+ " log(doc.offsetWidth);\n"
3848+
+ "</script>\n"
3849+
+ "</body></html>";
3850+
3851+
loadPageVerifyTitle2(html);
3852+
}
3853+
3854+
/**
3855+
* @throws Exception if the test fails
3856+
*/
3857+
@Test
3858+
@Alerts(CHROME = {"0", "0", "621", "1256"},
3859+
EDGE = {"0", "0", "630", "1248"},
3860+
FF = {"0", "0", "8", "1256"},
3861+
FF_ESR = {"0", "0", "8", "1260"})
3862+
@HtmlUnitNYI(CHROME = {"0", "0", "613", "1256"},
3863+
EDGE = {"0", "0", "613", "1256"},
3864+
FF = {"0", "0", "613", "1256"},
3865+
FF_ESR = {"0", "0", "613", "1256"})
3866+
public void documentElementOffsetQuirks() throws Exception {
3867+
final String html =
3868+
"<html>"
3869+
+ "<body>\n"
3870+
+ "<script>\n"
3871+
+ LOG_TITLE_FUNCTION
3872+
+ " let doc = document.documentElement;\n"
3873+
+ " log(doc.offsetTop);\n"
3874+
+ " log(doc.offsetLeft);\n"
3875+
+ " log(doc.offsetHeight);\n"
3876+
+ " log(doc.offsetWidth);\n"
3877+
+ "</script>\n"
3878+
+ "</body></html>";
3879+
3880+
loadPageVerifyTitle2(html);
3881+
}
3882+
3883+
/**
3884+
* @throws Exception if the test fails
3885+
*/
3886+
@Test
3887+
@Alerts(CHROME = {"0", "0", "621", "1256"},
3888+
EDGE = {"0", "0", "630", "1248"},
3889+
FF = {"0", "0", "675", "1256"},
3890+
FF_ESR = {"0", "0", "677", "1260"})
3891+
@HtmlUnitNYI(CHROME = {"0", "0", "605", "1256"},
3892+
EDGE = {"0", "0", "605", "1256"},
3893+
FF = {"0", "0", "605", "1256"},
3894+
FF_ESR = {"0", "0", "605", "1256"})
3895+
public void documentElementClientWidthHeight() throws Exception {
3896+
final String html = DOCTYPE_HTML
3897+
+ "<html>"
3898+
+ "<body>\n"
3899+
+ "<script>\n"
3900+
+ LOG_TITLE_FUNCTION
3901+
+ " log(document.documentElement.clientTop);\n"
3902+
+ " log(document.documentElement.clientLeft);\n"
3903+
+ " log(document.documentElement.clientHeight);\n"
3904+
+ " log(document.documentElement.clientWidth);\n"
3905+
+ "</script>\n"
3906+
+ "</body></html>";
3907+
3908+
loadPageVerifyTitle2(html);
3909+
}
3910+
3911+
/**
3912+
* @throws Exception if the test fails
3913+
*/
3914+
@Test
3915+
@Alerts(CHROME = {"0", "0", "621", "1256"},
3916+
EDGE = {"0", "0", "630", "1248"},
3917+
FF = {"0", "0", "8", "1256"},
3918+
FF_ESR = {"0", "0", "8", "1260"})
3919+
@HtmlUnitNYI(CHROME = {"0", "0", "605", "1256"},
3920+
EDGE = {"0", "0", "605", "1256"},
3921+
FF = {"0", "0", "605", "1256"},
3922+
FF_ESR = {"0", "0", "605", "1256"})
3923+
public void documentElementClientWidthHeightQuirks() throws Exception {
3924+
final String html =
3925+
"<html>"
3926+
+ "<body>\n"
3927+
+ "<script>\n"
3928+
+ LOG_TITLE_FUNCTION
3929+
+ " log(document.documentElement.clientTop);\n"
3930+
+ " log(document.documentElement.clientLeft);\n"
3931+
+ " log(document.documentElement.clientHeight);\n"
3932+
+ " log(document.documentElement.clientWidth);\n"
3933+
+ "</script>\n"
3934+
+ "</body></html>";
3935+
3936+
loadPageVerifyTitle2(html);
3937+
}
37653938
}

src/test/java/org/htmlunit/javascript/host/html/HTMLDocumentTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ public void compatMode_html() throws Exception {
173173
compatMode("<!DOCTYPE html>");
174174
}
175175

176+
/**
177+
* @throws Exception if the test fails
178+
*/
179+
@Test
180+
@Alerts("CSS1Compat")
181+
public void compatMode_htmlLowercase() throws Exception {
182+
compatMode("<!doctype html>");
183+
}
184+
176185
/**
177186
* @throws Exception if the test fails
178187
*/

0 commit comments

Comments
 (0)