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: image width/height attribute #315

Merged
merged 1 commit into from
Apr 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions integration_tests/specs/dom/elements/img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,24 @@ describe('Tags img', () => {
done();
});

it('works with width/height attribute', async (done) => {
let image;
image = createElement(
'img',
{
src: 'assets/100x100-green.png'
},
);
image.setAttribute(
'width',
'100px'
);
image.setAttribute(
'height',
'100px'
);
BODY.appendChild(image);
await snapshot(0.1);
done();
});
});
28 changes: 16 additions & 12 deletions webf/lib/src/html/img.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class ImageElement extends Element {
properties['src'] = BindingObjectProperty(getter: () => src, setter: (value) => src = castToType<String>(value));
properties['loading'] =
BindingObjectProperty(getter: () => loading, setter: (value) => loading = castToType<String>(value));
properties['width'] = BindingObjectProperty(getter: () => width, setter: (value) => width = castToType<int>(value));
properties['width'] = BindingObjectProperty(getter: () => width, setter: (value) => widthValue = castToType<String>(value));
properties['height'] =
BindingObjectProperty(getter: () => height, setter: (value) => height = castToType<int>(value));
BindingObjectProperty(getter: () => height, setter: (value) => heightValue = castToType<String>(value));
properties['scaling'] =
BindingObjectProperty(getter: () => scaling, setter: (value) => scaling = castToType<String>(value));
properties['naturalWidth'] = BindingObjectProperty(getter: () => naturalWidth);
Expand All @@ -119,8 +119,8 @@ class ImageElement extends Element {

attributes['src'] = ElementAttributeProperty(setter: (value) => src = attributeToProperty<String>(value));
attributes['loading'] = ElementAttributeProperty(setter: (value) => loading = attributeToProperty<String>(value));
attributes['width'] = ElementAttributeProperty(setter: (value) => width = attributeToProperty<int>(value));
attributes['height'] = ElementAttributeProperty(setter: (value) => height = attributeToProperty<int>(value));
attributes['width'] = ElementAttributeProperty(setter: (value) => widthValue = attributeToProperty<String>(value));
attributes['height'] = ElementAttributeProperty(setter: (value) => heightValue = attributeToProperty<String>(value));
attributes['scaling'] = ElementAttributeProperty(setter: (value) => scaling = attributeToProperty<String>(value));
}

Expand Down Expand Up @@ -224,14 +224,20 @@ class ImageElement extends Element {
// Width and height set through attributes.
double? get _attrWidth {
if (hasAttribute(WIDTH)) {
return CSSLength.toDouble(getAttribute(WIDTH));
final width = getAttribute(WIDTH);
if (width != null) {
return CSSLength.parseLength(width, renderStyle, WIDTH).computedValue;
}
}
return null;
}

double? get _attrHeight {
if (hasAttribute(HEIGHT)) {
return CSSLength.toDouble(getAttribute(HEIGHT));
final height = getAttribute(HEIGHT);
if (height != null) {
return CSSLength.parseLength(height, renderStyle, HEIGHT).computedValue;
}
}
return null;
}
Expand Down Expand Up @@ -529,19 +535,17 @@ class ImageElement extends Element {
}
}

set width(int value) {
if (value.isNegative) value = 0;
internalSetAttribute(WIDTH, value.toString());
set widthValue(String value) {
internalSetAttribute(WIDTH, value);
if (_shouldScaling) {
_decode(updateImageProvider: true);
} else {
_resizeImage();
}
}

set height(int value) {
if (value.isNegative) value = 0;
internalSetAttribute(HEIGHT, value.toString());
set heightValue(String value) {
internalSetAttribute(HEIGHT, value);
if (_shouldScaling) {
_decode(updateImageProvider: true);
} else {
Expand Down