Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2c19ba6

Browse files
author
nturgut
committed
Changing regular expressions to look simpler. Adding more unit tests.
1 parent b47feac commit 2c19ba6

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

lib/web_ui/lib/src/engine/text/font_collection.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ class FontCollection {
9999
class FontManager {
100100
final List<Future<void>> _fontLoadingFutures = <Future<void>>[];
101101

102-
// Regular expression to detect punctuations. For example font family
103-
// 'Ahem!' falls into this category.
104-
static final RegExp punctuations = RegExp(r"[.,:;!`\/#\$\%\^&~\*=\-_(){}]");
102+
// Regular expression to detect a string with no punctuations.
103+
// For example font family 'Ahem!' does not fall into this category
104+
// so the family name will be wrapped in quotes.
105+
static final RegExp notPunctuation =
106+
RegExp(r"[a-z0-9\s]+", caseSensitive: false);
105107
// Regular expression to detect tokens starting with a digit.
106108
// For example font family 'Goudy Bookletter 1911' falls into this
107109
// category.
@@ -140,8 +142,8 @@ class FontManager {
140142
///
141143
/// In order to avoid all these browser compatibility issues this method:
142144
/// * Detects the family names that might cause a conflict.
143-
/// * Loads it with quotes.
144-
/// * Loads it again without quotes.
145+
/// * Loads it with the quotes.
146+
/// * Loads it again without the quotes.
145147
/// * For all the other family names [html.FontFace] is loaded only once.
146148
///
147149
/// See also:
@@ -153,16 +155,13 @@ class FontManager {
153155
String asset,
154156
Map<String, String> descriptors,
155157
) {
156-
// Fonts names when a package dependency is added has '/' in the family
157-
// names such as 'package/material_design_icons_flutter/...'
158-
if (family.contains('/') ||
159-
punctuations.hasMatch(family) ||
160-
startWithDigit.hasMatch(family)) {
161-
// Load a font family name with special chracters once here wrapped in
158+
if (startWithDigit.hasMatch(family) ||
159+
notPunctuation.stringMatch(family) != family) {
160+
// Load a font family name with special characters once here wrapped in
162161
// quotes.
163-
_loadFontFace("'$family'", asset, descriptors);
162+
_loadFontFace('\'$family\'', asset, descriptors);
164163
}
165-
// Load all font fonts, without quoted family names.
164+
// Load all fonts, without quoted family names.
166165
_loadFontFace(family, asset, descriptors);
167166
}
168167

lib/web_ui/test/text/font_collection_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ void main() {
3737
expect(fontFamilyList.first, 'Ahem');
3838
});
3939

40+
test('Register Asset with white space in the family name', () async {
41+
final String _testFontFamily = "Ahem ahem ahem";
42+
final List<String> fontFamilyList = List<String>();
43+
44+
fontManager.registerAsset(
45+
_testFontFamily, 'url($_testFontUrl)', const <String, String>{});
46+
await fontManager.ensureFontsLoaded();
47+
html.document.fonts
48+
.forEach((html.FontFace f, html.FontFace f2, html.FontFaceSet s) {
49+
fontFamilyList.add(f.family);
50+
});
51+
52+
expect(fontFamilyList.length, equals(1));
53+
expect(fontFamilyList.first, 'Ahem ahem ahem');
54+
});
55+
56+
test('Register Asset with capital case letters', () async {
57+
final String _testFontFamily = "AhEm";
58+
final List<String> fontFamilyList = List<String>();
59+
60+
fontManager.registerAsset(
61+
_testFontFamily, 'url($_testFontUrl)', const <String, String>{});
62+
await fontManager.ensureFontsLoaded();
63+
html.document.fonts
64+
.forEach((html.FontFace f, html.FontFace f2, html.FontFaceSet s) {
65+
fontFamilyList.add(f.family);
66+
});
67+
68+
expect(fontFamilyList.length, equals(1));
69+
expect(fontFamilyList.first, 'AhEm');
70+
});
71+
4072
test('Register Asset twice with special character slash', () async {
4173
final String _testFontFamily = '/Ahem';
4274
final List<String> fontFamilyList = List<String>();

0 commit comments

Comments
 (0)