webfontloader 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGELOG +4 -0
  2. data/lib/webfontloader.rb +1 -1
  3. data/spec/ascender/ascenderscript_spec.js +24 -1
  4. data/spec/core/eventdispatcher_spec.js +15 -11
  5. data/spec/core/font_spec.js +83 -192
  6. data/spec/core/fontruler_spec.js +7 -4
  7. data/spec/core/fontwatcher_spec.js +78 -61
  8. data/spec/core/fontwatchrunner_spec.js +85 -33
  9. data/spec/core/webfont_spec.js +224 -0
  10. data/spec/custom/customcss_spec.js +5 -2
  11. data/spec/deps.js +13 -13
  12. data/spec/fontdeck/fontdeckscript_spec.js +4 -6
  13. data/spec/fonts/sourcesansc.css +1 -0
  14. data/spec/fonts/sourcesanscbold.css +1 -0
  15. data/spec/fonts/sourcesanscbold.otf +0 -0
  16. data/spec/google/fontapiparser_spec.js +58 -178
  17. data/spec/google/googlefontapi_spec.js +14 -42
  18. data/spec/google/lastresortwebkitfontwatchrunner_spec.js +12 -10
  19. data/spec/index.html +5 -3
  20. data/spec/monotype/monotypescript_spec.js +3 -2
  21. data/spec/typekit/typekitscript_spec.js +9 -4
  22. data/src/ascender/ascender_script.js +43 -26
  23. data/src/core/eventdispatcher.js +23 -23
  24. data/src/core/font.js +80 -99
  25. data/src/core/fontmoduleloader.js +1 -1
  26. data/src/core/fontruler.js +10 -20
  27. data/src/core/fontwatcher.js +24 -46
  28. data/src/core/fontwatchrunner.js +13 -13
  29. data/src/core/initialize.js +0 -10
  30. data/src/core/webfont.js +134 -0
  31. data/src/custom/customcss.js +14 -10
  32. data/src/fontdeck/fontdeck_script.js +7 -9
  33. data/src/google/fontapiparser.js +11 -15
  34. data/src/google/googlefontapi.js +1 -2
  35. data/src/google/lastresortwebkitfontwatchrunner.js +15 -13
  36. data/src/modules.yml +2 -3
  37. data/src/monotype/monotype_script.js +9 -8
  38. data/src/typekit/typekit_script.js +17 -7
  39. data/webfontloader.gemspec +7 -6
  40. metadata +9 -8
  41. data/spec/core/cssfontfamilyname_spec.js +0 -38
  42. data/spec/core/fontvariationdescription_spec.js +0 -67
  43. data/src/core/cssfontfamilyname.js +0 -33
  44. data/src/core/fontvariationdescription.js +0 -140
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ v1.4.1 (April 8, 2013)
2
+ * Internal rewrite of font and variations
3
+ * Fix for the configurable timeout on the Google module
4
+
1
5
  v1.4.0 (March 28, 2013)
2
6
  * Stop exporting the `addModule` API to dynamically add modules (it didn't work anyway.)
3
7
  * Stop checking the height when monitoring for font load. This turned out to be inconsistent across platforms.
data/lib/webfontloader.rb CHANGED
@@ -3,7 +3,7 @@ require 'yaml'
3
3
  require 'webfontloader/modules'
4
4
 
5
5
  module WebFontLoader
6
- VERSION = '1.4.0'
6
+ VERSION = '1.4.1'
7
7
 
8
8
  ProjectRoot = File.expand_path(File.dirname(__FILE__) + "/..")
9
9
 
@@ -1,5 +1,6 @@
1
1
  describe('AscenderScript', function () {
2
- var AscenderScript = webfont.AscenderScript;
2
+ var AscenderScript = webfont.AscenderScript,
3
+ Font = webfont.Font;
3
4
 
4
5
  var configuration = {
5
6
  key: 'ec2de397-11ae-4c10-937f-bf94283a70c1',
@@ -29,6 +30,14 @@ describe('AscenderScript', function () {
29
30
  expect(fakeDomHelper.insertInto.calls[0].args[0]).toEqual('head');
30
31
  });
31
32
 
33
+ it('should pass the fonts correctly', function () {
34
+ expect(fakeOnReady).toHaveBeenCalledWith([
35
+ new Font('AndyBold', 'n4'),
36
+ new Font('Arial', 'n7'),
37
+ new Font('Arial', 'n4')
38
+ ]);
39
+ });
40
+
32
41
  it('should parse variations correctly', function () {
33
42
  expect(ascenderScript.parseVariations('regular')).toEqual(['n4']);
34
43
  expect(ascenderScript.parseVariations('bold')).toEqual(['n7']);
@@ -39,5 +48,19 @@ describe('AscenderScript', function () {
39
48
  expect(ascenderScript.parseVariations('regular,,bold')).toEqual(['n4', 'n7']);
40
49
  expect(ascenderScript.parseVariations('n4,n7')).toEqual(['n4', 'n7']);
41
50
  });
51
+
52
+ it('should parse font families correctly', function () {
53
+ expect(ascenderScript.parseFamilyAndVariations('Arial')).toEqual([new Font('Arial')]);
54
+ expect(ascenderScript.parseFamilyAndVariations('Arial:bold,regular')).toEqual([new Font('Arial', 'n7'), new Font('Arial', 'n4')]);
55
+ expect(ascenderScript.parseFamilyAndVariations('Arial:n4,n7')).toEqual([new Font('Arial', 'n4'), new Font('Arial', 'n7')]);
56
+ });
57
+
58
+ it('should parse multiple font families correctly', function () {
59
+ expect(ascenderScript.parseFamiliesAndVariations(['Arial', 'Sans:n4,n7'])).toEqual([
60
+ new Font('Arial', 'n4'),
61
+ new Font('Sans', 'n4'),
62
+ new Font('Sans', 'n7')
63
+ ]);
64
+ });
42
65
  })
43
66
  });
@@ -1,10 +1,12 @@
1
1
  describe('EventDispatcher', function () {
2
2
  var EventDispatcher = webfont.EventDispatcher,
3
3
  DomHelper = webfont.DomHelper,
4
+ Font = webfont.Font,
4
5
  domHelper = new DomHelper(window),
5
6
  element = null
6
7
  eventDispatcher = null,
7
8
  namespace = 'ns',
9
+ font = null,
8
10
  nullFn = function () {},
9
11
  callbacks = {
10
12
  loading: nullFn,
@@ -19,6 +21,8 @@ describe('EventDispatcher', function () {
19
21
  element = domHelper.createElement();
20
22
  eventDispatcher = new EventDispatcher(domHelper, element, callbacks, namespace);
21
23
 
24
+ font = new Font('My Family', 'n4');
25
+
22
26
  spyOn(callbacks, 'loading');
23
27
  spyOn(callbacks, 'active');
24
28
  spyOn(callbacks, 'inactive');
@@ -43,7 +47,7 @@ describe('EventDispatcher', function () {
43
47
 
44
48
  describe('#dispatchFontLoading', function () {
45
49
  beforeEach(function () {
46
- eventDispatcher.dispatchFontLoading('My Family', 'n4');
50
+ eventDispatcher.dispatchFontLoading(font);
47
51
  });
48
52
 
49
53
  it('should call the correct callback', function () {
@@ -57,7 +61,7 @@ describe('EventDispatcher', function () {
57
61
 
58
62
  describe('#dispatchFontInactive', function () {
59
63
  beforeEach(function () {
60
- eventDispatcher.dispatchFontInactive('My Family', 'n4');
64
+ eventDispatcher.dispatchFontInactive(font);
61
65
  });
62
66
 
63
67
  it('should call the correct callback', function () {
@@ -71,8 +75,8 @@ describe('EventDispatcher', function () {
71
75
 
72
76
  describe('#dispatchFontInactive - with loading class', function () {
73
77
  beforeEach(function () {
74
- eventDispatcher.dispatchFontLoading('My Family', 'n4');
75
- eventDispatcher.dispatchFontInactive('My Family', 'n4');
78
+ eventDispatcher.dispatchFontLoading(font);
79
+ eventDispatcher.dispatchFontInactive(font);
76
80
  });
77
81
 
78
82
  it('should set the correct class name', function () {
@@ -82,8 +86,8 @@ describe('EventDispatcher', function () {
82
86
 
83
87
  describe('#dispatchFontInactive - with active class', function () {
84
88
  beforeEach(function () {
85
- eventDispatcher.dispatchFontActive('My Family', 'n4');
86
- eventDispatcher.dispatchFontInactive('My Family', 'n4');
89
+ eventDispatcher.dispatchFontActive(font);
90
+ eventDispatcher.dispatchFontInactive(font);
87
91
  });
88
92
 
89
93
  it('should not append the inactive class name', function () {
@@ -97,7 +101,7 @@ describe('EventDispatcher', function () {
97
101
 
98
102
  describe('#dispatchFontActive', function () {
99
103
  beforeEach(function () {
100
- eventDispatcher.dispatchFontActive('My Family', 'n4');
104
+ eventDispatcher.dispatchFontActive(font);
101
105
  });
102
106
 
103
107
  it('should call the correct callback', function () {
@@ -111,8 +115,8 @@ describe('EventDispatcher', function () {
111
115
 
112
116
  describe('#dispatchFontActive - with loading class', function () {
113
117
  beforeEach(function () {
114
- eventDispatcher.dispatchFontLoading('My Family', 'n4');
115
- eventDispatcher.dispatchFontActive('My Family', 'n4');
118
+ eventDispatcher.dispatchFontLoading(font);
119
+ eventDispatcher.dispatchFontActive(font);
116
120
  });
117
121
 
118
122
  it('should set the correct class name', function () {
@@ -122,8 +126,8 @@ describe('EventDispatcher', function () {
122
126
 
123
127
  describe('#dispatchFontActive - with inactive class', function () {
124
128
  beforeEach(function () {
125
- eventDispatcher.dispatchFontInactive('My Family', 'n4');
126
- eventDispatcher.dispatchFontActive('My Family', 'n4');
129
+ eventDispatcher.dispatchFontInactive(font);
130
+ eventDispatcher.dispatchFontActive(font);
127
131
  });
128
132
 
129
133
  it('should set the correct class', function () {
@@ -1,223 +1,114 @@
1
- describe('WebFont', function () {
2
- var WebFont = webfont.WebFont,
3
- UserAgent = webfont.UserAgent,
4
- BrowserInfo = webfont.BrowserInfo,
5
- FontModuleLoader = webfont.FontModuleLoader,
6
- fontModuleLoader = null,
7
- userAgent = null;
8
-
9
- beforeEach(function () {
10
- userAgent = new UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, new BrowserInfo(true, false, false));
11
- fontModuleLoader = new FontModuleLoader();
12
- });
1
+ describe('Font', function () {
2
+ var Font = webfont.Font;
13
3
 
14
- describe('font load', function () {
15
- var font = null,
16
- testModule = null;
4
+ describe('#quote', function () {
5
+ var quote = function (font) {
6
+ return new Font(font).getCssName();
7
+ };
17
8
 
18
- beforeEach(function () {
19
- font = new WebFont(window, fontModuleLoader, userAgent);
20
- font.addModule('test', function (conf, domHelper) {
21
- testModule = new function () {
22
- this.conf = conf;
23
- this.domHelper = domHelper;
24
- this.loadCalled = true;
25
- this.supportUserAgentCalled = false;
26
- };
9
+ it('should quote names with spaces', function () {
10
+ expect(quote('My Family')).toEqual("'My Family'");
11
+ });
27
12
 
28
- testModule.load = function (onReady) {
29
- this.loadCalled = true;
30
- onReady([]);
31
- };
13
+ it('should quote names with spaces and double quotes', function () {
14
+ expect(quote('"My Family"')).toEqual("'My Family'");
15
+ });
32
16
 
33
- testModule.supportUserAgent = function (ua, support) {
34
- this.supportUserAgentCalled = true;
35
- support(true);
36
- };
17
+ it('should quote names with spaces and single quotes', function () {
18
+ expect(quote("'My Family'")).toEqual("'My Family'");
19
+ });
37
20
 
38
- return testModule;
39
- });
21
+ it('should quote multiple single quoted names separated with a comma', function () {
22
+ expect(quote("'family 1','family 2'")).toEqual("'family 1','family 2'");
40
23
  });
41
24
 
42
- it('should not start loading', function () {
43
- expect(font.moduleFailedLoading_).toEqual(0);
44
- expect(font.moduleLoading_).toEqual(0);
25
+ it('should quote multiple single quoted names separated with a comma and space', function () {
26
+ expect(quote("'family 1', 'family 2'")).toEqual("'family 1','family 2'");
45
27
  });
46
28
 
47
- it('should fail to load a module', function () {
48
- var loading = jasmine.createSpy('loading');
29
+ it('should not quote when there is no space', function () {
30
+ expect(quote('MyFamily')).toEqual('MyFamily');
31
+ });
49
32
 
50
- font.load({
51
- test: {
52
- somedata: 'in french a cow says meuh'
53
- },
54
- loading: loading
55
- });
33
+ it('should remove quotes when they are unnecesssary', function () {
34
+ expect(quote('"MyFamily"')).toEqual('MyFamily');
35
+ });
56
36
 
57
- expect(font.moduleFailedLoading_).toEqual(1);
58
- expect(font.moduleLoading_).toEqual(0);
59
- expect(testModule).not.toBeNull();
37
+ it('should not quote multiple names when there is no space', function () {
38
+ expect(quote("'family-1', 'family-2'")).toEqual('family-1,family-2');
39
+ });
40
+ });
60
41
 
61
- expect(testModule.conf).not.toBeUndefined();
62
- expect(testModule.conf).not.toBeNull();
42
+ describe('#getCssVariation', function () {
43
+ function toCss(fvd) {
44
+ return new Font('My Family', fvd).getCssVariation();
45
+ }
63
46
 
64
- expect(testModule.domHelper).not.toBeNull();
65
- expect(testModule.domHelper).not.toBeUndefined();
47
+ it('should expand font-style', function () {
48
+ expect(toCss('n4')).toEqual('font-style:normal;font-weight:400;');
49
+ expect(toCss('i4')).toEqual('font-style:italic;font-weight:400;');
50
+ expect(toCss('o4')).toEqual('font-style:oblique;font-weight:400;');
51
+ });
66
52
 
67
- expect(testModule.domHelper.getMainWindow()).toEqual(window);
68
- expect(testModule.domHelper.getLoadWindow()).toEqual(window);
53
+ it('should expand weights correctly', function () {
54
+ for (var i = 1; i < 10; i += 1) {
55
+ expect(toCss('n' + i)).toEqual('font-style:normal;font-weight:' + (i * 100) + ';');
56
+ }
57
+ });
69
58
 
70
- expect(testModule.conf.somedata).toEqual('in french a cow says meuh');
71
- expect(testModule.loadCalled).toBe(true);
72
- expect(testModule.supportUserAgentCalled).toBe(true);
73
- expect(loading).toHaveBeenCalled();
59
+ it('should not expand incorrect input', function () {
60
+ expect(toCss('')).toEqual('font-style:normal;font-weight:400;');
61
+ expect(toCss('n')).toEqual('font-style:normal;font-weight:400;');
62
+ expect(toCss('1')).toEqual('font-style:normal;font-weight:400;');
63
+ expect(toCss('n1x')).toEqual('font-style:normal;font-weight:400;');
74
64
  });
75
65
  });
76
66
 
77
- describe('font load with context', function () {
78
- var font = null,
79
- testModule = null,
80
- fakeMainWindow = {};
67
+ describe('parseCssVariation', function () {
68
+ function toFvd(css) {
69
+ return Font.parseCssVariation(css);
70
+ }
81
71
 
82
- beforeEach(function () {
83
- font = new WebFont(fakeMainWindow, fontModuleLoader, userAgent);
84
- font.addModule('test', function (conf, domHelper) {
85
- testModule = new function () {
86
- this.domHelper = domHelper;
87
- };
88
- testModule.load = function () {};
89
- testModule.supportUserAgent = function (ua, support) {
90
- support(true);
91
- };
72
+ it('should default to n4 when there is no description', function () {
73
+ expect(toFvd('')).toEqual('n4');
74
+ expect(toFvd(null)).toEqual('n4');
75
+ expect(toFvd(undefined)).toEqual('n4');
76
+ });
92
77
 
93
- return testModule;
94
- });
78
+ it('should compact font style', function () {
79
+ expect(toFvd('font-style: normal;')).toEqual('n4');
80
+ expect(toFvd('font-style: italic;')).toEqual('i4');
81
+ expect(toFvd('font-style: oblique;')).toEqual('o4');
95
82
  });
96
83
 
97
- it('should load with the correct context', function () {
98
- font.load({
99
- test: {
100
- somedata: 'in french a cow says meuh'
101
- },
102
- context: window
103
- });
84
+ it('should return the default value when font-style is incorrect', function () {
85
+ expect(toFvd('font-style: other;')).toEqual('n4');
86
+ });
104
87
 
105
- expect(testModule.domHelper).not.toBeNull();
106
- expect(testModule.domHelper).not.toBeUndefined();
88
+ it('should compact font weight', function () {
89
+ expect(toFvd('font-weight: normal;')).toEqual('n4');
90
+ expect(toFvd('font-weight: bold;')).toEqual('n7');
91
+ for (var i = 1; i < 10; i += 1) {
92
+ expect(toFvd('font-weight: ' + (i * 100) + ';')).toEqual('n' + i);
93
+ }
94
+ });
107
95
 
108
- expect(testModule.domHelper.getMainWindow()).toEqual(fakeMainWindow);
109
- expect(testModule.domHelper.getLoadWindow()).toEqual(window);
96
+ it('should return the default value when font-weight is incorrect', function () {
97
+ expect(toFvd('font-weight: 140;')).toEqual('n4');
98
+ expect(toFvd('font-weight: other;')).toEqual('n4');
110
99
  });
111
- });
112
100
 
113
- describe('module failed to provide families and descriptions because it did not initialize properly', function () {
114
- var font = null,
115
- testModule = null,
116
- inactive = jasmine.createSpy('inactive'),
117
- active = jasmine.createSpy('active');
118
-
119
- beforeEach(function () {
120
- jasmine.Clock.useMock();
121
- font = new WebFont(window, fontModuleLoader, new UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, new BrowserInfo(true, false)));
122
- font.addModule('test', function (conf, domHelper) {
123
- testModule = new function () {
124
- this.conf = conf;
125
- this.families = [];
126
- this.descriptions = {};
127
- };
128
-
129
- testModule.getFontWatchRunnerCtor = function () {
130
- function FakeFontWatchRunner(activeCallback, inactiveCallback) {
131
- this.inactive = inactiveCallback;
132
- this.active = activeCallback;
133
- };
134
-
135
- FakeFontWatchRunner.prototype.start = function () {
136
- if (conf.id) {
137
- this.active('Font1', 'n4');
138
- } else {
139
- this.inactive('Font1', 'n4');
140
- }
141
- };
142
-
143
- return FakeFontWatchRunner;
144
- };
145
-
146
- testModule.supportUserAgent = function (userAgent, support) {
147
- if (conf.id) {
148
- // The monotype module only initializes font
149
- // and description if there is a kit id.
150
- this.families = ['Font1'];
151
- this.description = { 'Font1': ['n4'] };
152
- }
153
- support(true);
154
- };
155
- testModule.load = function (onReady) {
156
- onReady(this.families, this.description);
157
- };
158
-
159
- return testModule;
160
- });
161
- });
162
-
163
- it('should load with a project id', function () {
164
- font.load({
165
- test: {
166
- id: 'hello world'
167
- },
168
- inactive: inactive,
169
- active: active
170
- });
171
-
172
- jasmine.Clock.tick(1);
173
-
174
- expect(testModule).not.toBeNull();
175
- expect(active).toHaveBeenCalled();
176
- });
177
-
178
- it('should not load without a project id', function () {
179
- font.load({
180
- test: {
181
- },
182
- inactive: inactive,
183
- active: active
184
- });
185
-
186
- jasmine.Clock.tick(1);
187
-
188
- expect(testModule).not.toBeNull();
189
- expect(inactive).toHaveBeenCalled();
101
+ it('should compact multiple properties', function () {
102
+ expect(toFvd('font-weight: bold; font-style: italic;')).toEqual('i7');
103
+ expect(toFvd('; font-weight: bold; font-style: italic;')).toEqual('i7');
104
+ expect(toFvd('font-style:italic;font-weight:bold;')).toEqual('i7');
105
+ expect(toFvd(' font-style: italic ;\n\nfont-weight: bold; ')).toEqual('i7');
190
106
  });
191
- });
192
107
 
193
- describe('font inactive', function () {
194
- var font = null,
195
- testModule = null;
196
-
197
- beforeEach(function () {
198
- font = new WebFont(window, fontModuleLoader, new UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, new BrowserInfo(false, false, false)));
199
- font.addModule('test', function (conf, domHelper) {
200
- testModule = new function () {
201
- this.conf = conf;
202
- this.loadCalled = false;
203
- };
204
- testModule.load = function () {};
205
- return testModule;
206
- });
207
- });
208
-
209
- it('should load with the correct context', function () {
210
- var inactive = jasmine.createSpy('inactive');
211
-
212
- font.load({
213
- test: {
214
- somedata: 'in french a cow says meuh'
215
- },
216
- inactive: inactive
217
- });
218
-
219
- expect(testModule).toBeNull()
220
- expect(inactive).toHaveBeenCalled();
108
+ it('should return default values for incorrect individual properties', function () {
109
+ expect(toFvd('src: url(/font.otf)')).toEqual('n4');
110
+ expect(toFvd('font-weight: 900; src: url(/font.otf);')).toEqual('n9');
111
+ expect(toFvd('font-weight: 800; font-stretch:condensed;')).toEqual('n8');
221
112
  });
222
113
  });
223
114
  });