From 6792c2ceae89fd373a1aaafbfe6144b8732cef47 Mon Sep 17 00:00:00 2001 From: David Iglesias Date: Thu, 29 Aug 2019 09:03:14 -0700 Subject: [PATCH] Don't crash while loading improperly formatted fonts on Safari (flutter/engine#11655) (cl/265763028) Fixes https://github.com/flutter/flutter/issues/39259 --- .../lib/src/engine/text/font_collection.dart | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart index b64a1e62732..94611b3b09a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart @@ -123,14 +123,27 @@ class _FontManager { String asset, Map descriptors, ) { - final html.FontFace fontFace = html.FontFace(family, asset, descriptors); - _fontLoadingFutures.add(fontFace - .load() - .then((_) => html.document.fonts.add(fontFace), onError: (dynamic e) { + // Safari crashes if you create a [html.FontFace] with a font family that + // is not correct CSS syntax. To ensure the font family is accepted on + // Safari, wrap it in quotes. + // See: https://drafts.csswg.org/css-fonts-3/#font-family-prop + if (browserEngine == BrowserEngine.webkit) { + family = "'$family'"; + } + // try/catch because `new FontFace` can crash with an improper font family. + try { + final html.FontFace fontFace = html.FontFace(family, asset, descriptors); + _fontLoadingFutures.add(fontFace + .load() + .then((_) => html.document.fonts.add(fontFace), onError: (dynamic e) { + html.window.console + .warn('Error while trying to load font family "$family":\n$e'); + return null; + })); + } catch (e) { html.window.console - .warn('Error while trying to load font family "$family":\n$e'); - return null; - })); + .warn('Error while loading font family "$family":\n$e'); + } } /// Returns a [Future] that completes when all fonts that have been