Skip to content

Commit f011f0b

Browse files
committed
cleanup and a bit more correct caching
1 parent 3a592a0 commit f011f0b

1 file changed

Lines changed: 47 additions & 70 deletions

File tree

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

Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,24 @@ public class ComputedCssStyleDeclaration extends AbstractCssStyleDeclaration {
195195
Definition.WIDOWS,
196196
Definition.WORD_SPACING);
197197

198+
/** Default font sizes for heading elements, used when no explicit font-size is set. */
199+
private static final Map<Class<? extends HtmlElement>, String> HEADING_DEFAULT_FONT_SIZES = Map.of(
200+
HtmlHeading1.class, "32px",
201+
HtmlHeading2.class, "24px",
202+
HtmlHeading3.class, "19px",
203+
HtmlHeading4.class, "16px",
204+
HtmlHeading5.class, "13px",
205+
HtmlHeading6.class, "11px");
206+
198207
/** Denotes a value which should be returned as is. */
199208
public static final String EMPTY_FINAL = new String("");
200209

201210
/** The computed, cached width of the element to which this computed style belongs (no padding, borders, etc.). */
202211
private Integer width_;
203212

213+
/** The computed, cached shrink-wrapped width (used by getBoundingClientRect()). */
214+
private Integer shrinkWrapWidth_;
215+
204216
/**
205217
* The computed, cached height of the element to which this computed style belongs (no padding, borders, etc.),
206218
* taking child elements into account.
@@ -1599,18 +1611,18 @@ else if (isScrollable(element, false, true) && !(element instanceof HtmlBody)) {
15991611
}
16001612

16011613
private int getCalculatedWidth(final DomElement element, final boolean shrinkWrapBlock) {
1602-
final Integer cachedWidth = getCachedWidth();
1603-
if (cachedWidth != null) {
1604-
return cachedWidth.intValue();
1614+
final Integer cached = shrinkWrapBlock ? getCachedShrinkWrapWidth() : getCachedWidth();
1615+
if (cached != null) {
1616+
return cached.intValue();
16051617
}
16061618

16071619
if (!element.mayBeDisplayed()) {
1608-
return updateCachedWidth(0);
1620+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(0) : updateCachedWidth(0);
16091621
}
16101622

16111623
final String display = getDisplay();
16121624
if (NONE.equals(display)) {
1613-
return updateCachedWidth(0);
1625+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(0) : updateCachedWidth(0);
16141626
}
16151627

16161628
final int width;
@@ -1622,21 +1634,23 @@ private int getCalculatedWidth(final DomElement element, final boolean shrinkWra
16221634
&& parent instanceof HtmlElement) {
16231635
// hack: TODO find a way to specify default values for different tags
16241636
if (element instanceof HtmlCanvas) {
1625-
return updateCachedWidth(300);
1637+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(300) : updateCachedWidth(300);
16261638
}
16271639

16281640
// iframes have a default width of 300px (like canvas)
16291641
if (element instanceof HtmlInlineFrame iframe) {
16301642
final String widthAttribute = iframe.getAttributeDirect("width");
16311643
if (DomElement.ATTRIBUTE_NOT_DEFINED != widthAttribute) {
1632-
return updateCachedWidth(CssPixelValueConverter.pixelValue(widthAttribute));
1644+
final int w = CssPixelValueConverter.pixelValue(widthAttribute);
1645+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(w) : updateCachedWidth(w);
16331646
}
16341647

1635-
return updateCachedWidth(300);
1648+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(300) : updateCachedWidth(300);
16361649
}
16371650

16381651
if (element instanceof HtmlFrame) {
1639-
return updateCachedWidth(element.getPage().getEnclosingWindow().getInnerWidth());
1652+
final int w = element.getPage().getEnclosingWindow().getInnerWidth();
1653+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(w) : updateCachedWidth(w);
16401654
}
16411655

16421656
// Width not explicitly set.
@@ -1740,7 +1754,7 @@ else if (AUTO.equals(styleWidth)) {
17401754
});
17411755
}
17421756

1743-
return updateCachedWidth(width);
1757+
return shrinkWrapBlock ? updateCachedShrinkWrapWidth(width) : updateCachedWidth(width);
17441758
}
17451759

17461760
private static boolean hasOnlyInlineOrTextChildren(final DomElement element) {
@@ -1974,68 +1988,13 @@ else if (element instanceof HtmlRuby) {
19741988
}
19751989
}
19761990
else {
1977-
final String fontSize;
1991+
final String headingDefault = HEADING_DEFAULT_FONT_SIZES.get(element.getClass());
1992+
final boolean isHeading = headingDefault != null;
19781993

1979-
boolean isHeading = false;
1980-
if (element instanceof HtmlHeading1) {
1981-
isHeading = true;
1982-
final String value = getStyleAttribute(Definition.FONT_SIZE, false);
1983-
if (value.isEmpty()) {
1984-
fontSize = "32px";
1985-
}
1986-
else {
1987-
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
1988-
}
1989-
}
1990-
else if (element instanceof HtmlHeading2) {
1991-
isHeading = true;
1992-
final String value = getStyleAttribute(Definition.FONT_SIZE, false);
1993-
if (value.isEmpty()) {
1994-
fontSize = "24px";
1995-
}
1996-
else {
1997-
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
1998-
}
1999-
}
2000-
else if (element instanceof HtmlHeading3) {
2001-
isHeading = true;
2002-
final String value = getStyleAttribute(Definition.FONT_SIZE, false);
2003-
if (value.isEmpty()) {
2004-
fontSize = "19px";
2005-
}
2006-
else {
2007-
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
2008-
}
2009-
}
2010-
else if (element instanceof HtmlHeading4) {
2011-
isHeading = true;
2012-
final String value = getStyleAttribute(Definition.FONT_SIZE, false);
2013-
if (value.isEmpty()) {
2014-
fontSize = "16px";
2015-
}
2016-
else {
2017-
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
2018-
}
2019-
}
2020-
else if (element instanceof HtmlHeading5) {
2021-
isHeading = true;
2022-
final String value = getStyleAttribute(Definition.FONT_SIZE, false);
2023-
if (value.isEmpty()) {
2024-
fontSize = "13px";
2025-
}
2026-
else {
2027-
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
2028-
}
2029-
}
2030-
else if (element instanceof HtmlHeading6) {
2031-
isHeading = true;
1994+
final String fontSize;
1995+
if (isHeading) {
20321996
final String value = getStyleAttribute(Definition.FONT_SIZE, false);
2033-
if (value.isEmpty()) {
2034-
fontSize = "11px";
2035-
}
2036-
else {
2037-
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
2038-
}
1997+
fontSize = value.isEmpty() ? headingDefault : getStyleAttribute(Definition.FONT_SIZE, true);
20391998
}
20401999
else {
20412000
fontSize = getStyleAttribute(Definition.FONT_SIZE, true);
@@ -2352,6 +2311,24 @@ public int updateCachedWidth(final int width) {
23522311
return width;
23532312
}
23542313

2314+
/**
2315+
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
2316+
* @return the cached width
2317+
*/
2318+
public Integer getCachedShrinkWrapWidth() {
2319+
return shrinkWrapWidth_;
2320+
}
2321+
2322+
/**
2323+
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
2324+
* @param width the new value
2325+
* @return the param width
2326+
*/
2327+
public int updateCachedShrinkWrapWidth(final int width) {
2328+
shrinkWrapWidth_ = Integer.valueOf(width);
2329+
return width;
2330+
}
2331+
23552332
/**
23562333
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
23572334
* @return the cached height

0 commit comments

Comments
 (0)