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

Commit 36f96ea

Browse files
author
nturgut
committed
Changing regular expressions to look simpler. Adding more unit tests.
1 parent 09a704c commit 36f96ea

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
@@ -103,9 +103,11 @@ class FontCollection {
103103
class FontManager {
104104
final List<Future<void>> _fontLoadingFutures = <Future<void>>[];
105105

106-
// Regular expression to detect punctuations. For example font family
107-
// 'Ahem!' falls into this category.
108-
static final RegExp punctuations = RegExp(r"[.,:;!`\/#\$\%\^&~\*=\-_(){}]");
106+
// Regular expression to detect a string with no punctuations.
107+
// For example font family 'Ahem!' does not fall into this category
108+
// so the family name will be wrapped in quotes.
109+
static final RegExp notPunctuation =
110+
RegExp(r"[a-z0-9\s]+", caseSensitive: false);
109111
// Regular expression to detect tokens starting with a digit.
110112
// For example font family 'Goudy Bookletter 1911' falls into this
111113
// category.
@@ -144,8 +146,8 @@ class FontManager {
144146
///
145147
/// In order to avoid all these browser compatibility issues this method:
146148
/// * Detects the family names that might cause a conflict.
147-
/// * Loads it with quotes.
148-
/// * Loads it again without quotes.
149+
/// * Loads it with the quotes.
150+
/// * Loads it again without the quotes.
149151
/// * For all the other family names [html.FontFace] is loaded only once.
150152
///
151153
/// See also:
@@ -157,16 +159,13 @@ class FontManager {
157159
String asset,
158160
Map<String, String> descriptors,
159161
) {
160-
// Fonts names when a package dependency is added has '/' in the family
161-
// names such as 'package/material_design_icons_flutter/...'
162-
if (family.contains('/') ||
163-
punctuations.hasMatch(family) ||
164-
startWithDigit.hasMatch(family)) {
165-
// Load a font family name with special chracters once here wrapped in
162+
if (startWithDigit.hasMatch(family) ||
163+
notPunctuation.stringMatch(family) != family) {
164+
// Load a font family name with special characters once here wrapped in
166165
// quotes.
167-
_loadFontFace("'$family'", asset, descriptors);
166+
_loadFontFace('\'$family\'', asset, descriptors);
168167
}
169-
// Load all font fonts, without quoted family names.
168+
// Load all fonts, without quoted family names.
170169
_loadFontFace(family, asset, descriptors);
171170
}
172171

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)