Skip to content

Commit

Permalink
WIP: move some code into the non js model (issue #708)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 26, 2024
1 parent 0d3e021 commit d858cd7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 64 deletions.
108 changes: 54 additions & 54 deletions src/main/java/org/htmlunit/css/ComputedCssStyleDeclaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,60 @@ public int getLength() {
return elementStyleDeclaration_.getLength();
}

/**
* @return the width
*/
@Override
public String getWidth() {
if (NONE.equals(getDisplay())) {
return AUTO;
}

final DomElement domElem = getDomElement();
if (!domElem.isAttachedToPage()) {
if (hasFeature(CSS_STYLE_PROP_DISCONNECTED_IS_EMPTY)) {
return "";
}
if (getStyleAttribute(WIDTH, true).isEmpty()) {
return AUTO;
}
}

final int windowWidth = domElem.getPage().getEnclosingWindow().getInnerWidth();
return CssPixelValueConverter.pixelString(domElem, new CssPixelValueConverter.CssValue(0, windowWidth) {
@Override
public String get(final ComputedCssStyleDeclaration style) {
final String value = style.getStyleAttribute(WIDTH, true);
if (StringUtils.isEmpty(value)) {
final String position = getStyleAttribute(POSITION, true);
if (ABSOLUTE.equals(position) || FIXED.equals(position)) {
final String content = domElem.getVisibleText();
// 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";
}
}

int windowDefaultValue = getWindowDefaultValue();
if (domElem instanceof HtmlBody) {
windowDefaultValue -= 16;
}
return windowDefaultValue + "px";
}
else if (AUTO.equals(value)) {
int windowDefaultValue = getWindowDefaultValue();
if (domElem instanceof HtmlBody) {
windowDefaultValue -= 16;
}
return windowDefaultValue + "px";
}

return value;
}
});
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -494,60 +548,6 @@ public String getFontSize() {
return value;
}

/**
* @return the width
*/
@Override
public String getWidth() {
if (NONE.equals(getDisplay())) {
return AUTO;
}

final DomElement domElem = getDomElement();
if (!domElem.isAttachedToPage()) {
if (hasFeature(CSS_STYLE_PROP_DISCONNECTED_IS_EMPTY)) {
return "";
}
if (getStyleAttribute(WIDTH, true).isEmpty()) {
return AUTO;
}
}

final int windowWidth = domElem.getPage().getEnclosingWindow().getInnerWidth();
return CssPixelValueConverter.pixelString(domElem, new CssPixelValueConverter.CssValue(0, windowWidth) {
@Override
public String get(final ComputedCssStyleDeclaration style) {
final String value = style.getStyleAttribute(WIDTH, true);
if (StringUtils.isEmpty(value)) {
final String position = getStyleAttribute(POSITION, true);
if (ABSOLUTE.equals(position) || FIXED.equals(position)) {
final String content = domElem.getVisibleText();
// 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";
}
}

int windowDefaultValue = getWindowDefaultValue();
if (domElem instanceof HtmlBody) {
windowDefaultValue -= 16;
}
return windowDefaultValue + "px";
}
else if (AUTO.equals(value)) {
int windowDefaultValue = getWindowDefaultValue();
if (domElem instanceof HtmlBody) {
windowDefaultValue -= 16;
}
return windowDefaultValue + "px";
}

return value;
}
});
}

/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span>
* @return the cached width
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ public String getWidth() {
if (styleDeclaration_ == null) {
return null; // prototype
}
return styleDeclaration_.getStyleAttribute(Definition.WIDTH, true);
return styleDeclaration_.getWidth();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,14 +697,6 @@ public String getPosition() {
return defaultIfEmpty(super.getPosition(), Definition.POSITION);
}

/**
* {@inheritDoc}
*/
@Override
public String getWidth() {
return getCssStyleDeclaration().getWidth();
}

/**
* Returns the element's width in pixels, possibly including its padding and border.
* @param includeBorder whether or not to include the border width in the returned value
Expand Down Expand Up @@ -750,7 +742,7 @@ private int getCalculatedWidth() {
}

final int width;
final String styleWidth = super.getWidth();
final String styleWidth = getCssStyleDeclaration().getStyleAttribute(Definition.WIDTH, true);
final DomNode parent = element.getParentNode();

// width is ignored for inline elements
Expand Down

0 comments on commit d858cd7

Please sign in to comment.