Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix img width become infinity. #200

Merged
merged 2 commits into from Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration_tests/specs/dom/elements/img.ts
Expand Up @@ -14,6 +14,12 @@ describe('Tags img', () => {
document.body.appendChild(img);
});

it('should have not size when img not mounted', () => {
const img = document.createElement('img');
expect(img.width).toBe(0);
expect(img.height).toBe(0);
});

it('don\'t error when append child on img element', async (done) => {
let img = document.createElement('img');
img.src = 'https://gw.alicdn.com/tfs/TB1MRC_cvb2gK0jSZK9XXaEgFXa-1701-1535.png';
Expand Down
6 changes: 2 additions & 4 deletions webf/lib/src/html/img.dart
Expand Up @@ -209,7 +209,7 @@ class ImageElement extends Element {
// Width and height set through style declaration.
double? get _styleWidth {
String width = style.getPropertyValue(WIDTH);
if (width.isNotEmpty) {
if (width.isNotEmpty && isRendererAttached) {
CSSLengthValue len = CSSLength.parseLength(width, renderStyle, WIDTH);
return len.computedValue;
}
Expand All @@ -218,7 +218,7 @@ class ImageElement extends Element {

double? get _styleHeight {
String height = style.getPropertyValue(HEIGHT);
if (height.isNotEmpty) {
if (height.isNotEmpty && isRendererAttached) {
CSSLengthValue len = CSSLength.parseLength(height, renderStyle, HEIGHT);
return len.computedValue;
}
Expand All @@ -243,14 +243,12 @@ class ImageElement extends Element {
int get width {
// Width calc priority: style > attr > intrinsic.
final double borderBoxWidth = _styleWidth ?? _attrWidth ?? renderStyle.getWidthByAspectRatio();

return borderBoxWidth.isFinite ? borderBoxWidth.round() : 0;
}

int get height {
// Height calc priority: style > attr > intrinsic.
final double borderBoxHeight = _styleHeight ?? _attrHeight ?? renderStyle.getHeightByAspectRatio();

return borderBoxHeight.isFinite ? borderBoxHeight.round() : 0;
}

Expand Down