Commit c9b8999c authored by fs@opera.com's avatar fs@opera.com

HTMLImageElement.natural{Width,Height} should return intrinsic dimensions

HTML says:

 "The IDL attributes naturalWidth and naturalHeight must return the
  intrinsic width and height of the image, in CSS pixels, if the image is
  available, or else 0."

Change HTMLImageElement::natural{Width,Height} to request the intrinsic
size from ImageResource::imageSizeForRenderer by passing IntrinsicSize as
the third parameter. This makes the attributes return more appropriate
values for SVG-images in <img>.

BUG=396955

Review URL: https://codereview.chromium.org/422503002

git-svn-id: svn://svn.chromium.org/blink/trunk@179030 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent fefa5851
This is a testharness.js-based test.
PASS naturalWidth/Height of SVG in <img>, width/height in pixels
FAIL naturalWidth/Height of SVG in <img>, width in pixels; height unspecified assert_equals: expected "500x0" but got "300x150"
FAIL naturalWidth/Height of SVG in <img>, width in pixels; percentage height assert_equals: expected "500x0" but got "300x150"
PASS naturalWidth/Height of SVG in <img>, width/height in pixels; viewBox
FAIL naturalWidth/Height of SVG in <img>, width/height unspecified; viewBox assert_equals: expected "0x0" but got "300x150"
FAIL naturalWidth/Height of SVG in <img>, width in pixels; height unspecified; viewBox assert_equals: expected "400x0" but got "400x300"
Harness: the test ran to completion.
<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<style>
img {
width: 200px;
}
</style>
<body></body>
<script>
function makeSvgImageUrl(sizingAttributes) {
var s = "<svg xmlns='http://www.w3.org/2000/svg' ";
s += sizingAttributes;
s += "><circle cx='50%' cy='50%' r='50%' fill='blue'/></svg>";
return "data:image/svg+xml," + encodeURIComponent(s);
}
function assertImageDimensions(img, expected) {
assert_equals(img.naturalWidth + "x" + img.naturalHeight, expected.width + "x" + expected.height);
}
function makeTest(sizingAttributes, expected, description) {
var t = async_test("naturalWidth/Height of SVG in <img>, " + description);
var img = document.body.appendChild(new Image());
img.onload = t.step_func(function() {
assertImageDimensions(img, expected);
requestAnimationFrame(function() {
setTimeout(t.step_func(function() {
assertImageDimensions(img, expected);
t.done();
}), 0);
});
});
img.src = makeSvgImageUrl(sizingAttributes);
}
makeTest("width='500' height='400'", { width: 500, height: 400 }, "width/height in pixels");
makeTest("width='500'", { width: 500, height: 0 }, "width in pixels; height unspecified");
makeTest("width='500' height='100%'", { width: 500, height: 0 }, "width in pixels; percentage height");
makeTest("width='500' height='400' viewBox='0 0 800 600'", { width: 500, height: 400 }, "width/height in pixels; viewBox");
makeTest("viewBox='0 0 800 600'", { width: 0, height: 0 }, "width/height unspecified; viewBox");
makeTest("width='400' viewBox='0 0 800 600'", { width: 400, height: 0 }, "width in pixels; height unspecified; viewBox");
</script>
...@@ -374,7 +374,7 @@ int HTMLImageElement::naturalWidth() const ...@@ -374,7 +374,7 @@ int HTMLImageElement::naturalWidth() const
if (!imageLoader().image()) if (!imageLoader().image())
return 0; return 0;
return imageLoader().image()->imageSizeForRenderer(renderer(), 1.0f).width(); return imageLoader().image()->imageSizeForRenderer(renderer(), 1.0f, ImageResource::IntrinsicSize).width();
} }
int HTMLImageElement::naturalHeight() const int HTMLImageElement::naturalHeight() const
...@@ -382,7 +382,7 @@ int HTMLImageElement::naturalHeight() const ...@@ -382,7 +382,7 @@ int HTMLImageElement::naturalHeight() const
if (!imageLoader().image()) if (!imageLoader().image())
return 0; return 0;
return imageLoader().image()->imageSizeForRenderer(renderer(), 1.0f).height(); return imageLoader().image()->imageSizeForRenderer(renderer(), 1.0f, ImageResource::IntrinsicSize).height();
} }
const String& HTMLImageElement::currentSrc() const const String& HTMLImageElement::currentSrc() const
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment