From 07c780bd9507c1019f19d2d3a40a6ab5cf708a0f Mon Sep 17 00:00:00 2001 From: Ferhat Date: Thu, 29 Oct 2020 09:54:49 -0700 Subject: [PATCH] [web] Assign default natural width/height for svgs that report 0,0 on firefox and ie11 (#22184) --- .../lib/src/engine/html_image_codec.dart | 14 ++++++++++++-- .../engine/image/html_image_codec_test.dart | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/lib/src/engine/html_image_codec.dart b/lib/web_ui/lib/src/engine/html_image_codec.dart index db1fa461852ea..c7a5e7e911b3d 100644 --- a/lib/web_ui/lib/src/engine/html_image_codec.dart +++ b/lib/web_ui/lib/src/engine/html_image_codec.dart @@ -39,10 +39,20 @@ class HtmlCodec implements ui.Codec { js_util.setProperty(imgElement, 'decoding', 'async'); imgElement.decode().then((dynamic _) { chunkCallback?.call(100, 100); + int naturalWidth = imgElement.naturalWidth; + int naturalHeight = imgElement.naturalHeight; + // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=700533. + if (naturalWidth == 0 && naturalHeight == 0 && ( + browserEngine == BrowserEngine.firefox || + browserEngine == BrowserEngine.ie11)) { + const int kDefaultImageSizeFallback = 300; + naturalWidth = kDefaultImageSizeFallback; + naturalHeight = kDefaultImageSizeFallback; + } final HtmlImage image = HtmlImage( imgElement, - imgElement.naturalWidth, - imgElement.naturalHeight, + naturalWidth, + naturalHeight, ); completer.complete(SingleFrameInfo(image)); }).catchError((dynamic e) { diff --git a/lib/web_ui/test/engine/image/html_image_codec_test.dart b/lib/web_ui/test/engine/image/html_image_codec_test.dart index 331462571a573..8723bb00d7552 100644 --- a/lib/web_ui/test/engine/image/html_image_codec_test.dart +++ b/lib/web_ui/test/engine/image/html_image_codec_test.dart @@ -80,6 +80,24 @@ void testMain() async { await codec.getNextFrame(); expect(buffer.toString(), '0/100,100/100,'); }); + + /// Regression test for Firefox/ie11 + /// https://github.com/flutter/flutter/issues/66412 + test('Returns nonzero natural width/height', () async { + final HtmlCodec codec = HtmlCodec( + 'data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHZpZXdCb3g9I' + 'jAgMCAyNCAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48dG' + 'l0bGU+QWJzdHJhY3QgaWNvbjwvdGl0bGU+PHBhdGggZD0iTTEyIDBjOS42MDEgMCAx' + 'MiAyLjM5OSAxMiAxMiAwIDkuNjAxLTIuMzk5IDEyLTEyIDEyLTkuNjAxIDAtMTItMi' + '4zOTktMTItMTJDMCAyLjM5OSAyLjM5OSAwIDEyIDB6bS0xLjk2OSAxOC41NjRjMi41' + 'MjQuMDAzIDQuNjA0LTIuMDcgNC42MDktNC41OTUgMC0yLjUyMS0yLjA3NC00LjU5NS' + '00LjU5NS00LjU5NVM1LjQ1IDExLjQ0OSA1LjQ1IDEzLjk2OWMwIDIuNTE2IDIuMDY1' + 'IDQuNTg4IDQuNTgxIDQuNTk1em04LjM0NC0uMTg5VjUuNjI1SDUuNjI1djIuMjQ3aD' + 'EwLjQ5OHYxMC41MDNoMi4yNTJ6bS04LjM0NC02Ljc0OGEyLjM0MyAyLjM0MyAwIDEx' + 'LS4wMDIgNC42ODYgMi4zNDMgMi4zNDMgMCAwMS4wMDItNC42ODZ6Ii8+PC9zdmc+'); + final ui.FrameInfo frameInfo = await codec.getNextFrame(); + expect(frameInfo.image.width, isNot(0)); + }); }); group('ImageCodecUrl', () {