webfontloader 1.5.8 → 1.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +5 -0
- data/README.md +18 -0
- data/lib/webfontloader.rb +1 -1
- data/spec/core/eventdispatcher_spec.js +47 -17
- data/spec/core/useragentparser_spec.js +212 -470
- data/spec/modules/monotype_spec.js +0 -3
- data/spec/modules/typekit_spec.js +14 -38
- data/src/core/eventdispatcher.js +71 -57
- data/src/core/fontruler.js +1 -1
- data/src/core/fontwatcher.js +27 -15
- data/src/core/useragent.js +3 -39
- data/src/core/useragentparser.js +15 -58
- data/src/core/webfont.js +20 -5
- data/src/modules/typekit.js +24 -27
- data/webfontloader.gemspec +3 -2
- data/webfontloader.js +31 -0
- metadata +3 -2
@@ -34,13 +34,10 @@ describe('modules.Monotype', function () {
|
|
34
34
|
useragent = new UserAgent(
|
35
35
|
'Firefox',
|
36
36
|
new Version(3, 6),
|
37
|
-
'3.6',
|
38
37
|
'Gecko',
|
39
38
|
new Version(1, 9, 3),
|
40
|
-
'1.9.3',
|
41
39
|
'Macintosh',
|
42
40
|
new Version(10, 6),
|
43
|
-
'10.6',
|
44
41
|
undefined,
|
45
42
|
new BrowserInfo(true, false, false, false)
|
46
43
|
);
|
@@ -12,65 +12,41 @@ describe('modules.Typekit', function () {
|
|
12
12
|
support = null;
|
13
13
|
|
14
14
|
beforeEach(function () {
|
15
|
-
global = {
|
15
|
+
global = {
|
16
|
+
Typekit: {
|
17
|
+
config: {
|
18
|
+
fn: ['Font1', ['n4'], 'Font2', ['n4', 'n7']]
|
19
|
+
},
|
20
|
+
load: jasmine.createSpy('load')
|
21
|
+
}
|
22
|
+
};
|
16
23
|
|
17
24
|
support = jasmine.createSpy('support');
|
18
25
|
|
19
26
|
load = jasmine.createSpy('load');
|
20
27
|
|
21
28
|
fakeDomHelper = {
|
22
|
-
loadScript: jasmine.createSpy('loadScript'),
|
29
|
+
loadScript: jasmine.createSpy('loadScript').andCallFake(function (url, cb) {
|
30
|
+
cb(null);
|
31
|
+
}),
|
23
32
|
getLoadWindow: jasmine.createSpy('getLoadWindow').andReturn(global),
|
24
33
|
getProtocol: jasmine.createSpy('getProtocol').andReturn('http:')
|
25
34
|
};
|
26
35
|
});
|
27
36
|
|
28
|
-
it('
|
37
|
+
it('should load with variations', function () {
|
29
38
|
var typekit = new Typekit(fakeDomHelper, configuration);
|
30
39
|
|
31
40
|
typekit.supportUserAgent('useragent', support);
|
32
41
|
|
33
42
|
expect(fakeDomHelper.loadScript).toHaveBeenCalled();
|
34
43
|
expect(fakeDomHelper.loadScript.calls[0].args[0]).toEqual('http://use.typekit.net/abc.js');
|
35
|
-
expect(support).not.toHaveBeenCalled();
|
36
|
-
|
37
|
-
expect(global.__webfonttypekitmodule__).not.toBeNull();
|
38
|
-
expect(global.__webfonttypekitmodule__['abc']).not.toBeNull();
|
39
|
-
|
40
|
-
global.__webfonttypekitmodule__['abc'](function (ua, config, init) {
|
41
|
-
expect(ua).toEqual('useragent');
|
42
|
-
expect(config).toEqual(configuration);
|
43
|
-
expect(init).not.toBeNull();
|
44
|
-
init(true, ['Font1', 'Font2'], {});
|
45
|
-
});
|
46
|
-
|
47
|
-
expect(support).toHaveBeenCalled();
|
48
|
-
|
49
|
-
typekit.load(load);
|
50
|
-
|
51
|
-
expect(load).toHaveBeenCalledWith([new Font('Font1'), new Font('Font2')]);
|
52
|
-
});
|
53
|
-
|
54
|
-
it('should load with variations', function () {
|
55
|
-
var typekit = new Typekit(fakeDomHelper, configuration);
|
56
|
-
|
57
|
-
typekit.supportUserAgent('useragent', support);
|
58
|
-
|
59
|
-
global.__webfonttypekitmodule__['abc'](function (ua, config, init) {
|
60
|
-
init(true, ['Font1', 'Font2'], {
|
61
|
-
'Font1': ['n7', 'i7']
|
62
|
-
});
|
63
|
-
});
|
64
|
-
|
65
44
|
expect(support).toHaveBeenCalled();
|
66
45
|
|
46
|
+
expect(global.Typekit.load).toHaveBeenCalled();
|
67
47
|
typekit.load(load);
|
68
48
|
|
69
|
-
expect(load).toHaveBeenCalledWith([
|
70
|
-
new Font('Font1', 'n7'),
|
71
|
-
new Font('Font1', 'i7'),
|
72
|
-
new Font('Font2', 'n4')
|
73
|
-
]);
|
49
|
+
expect(load).toHaveBeenCalledWith([new Font('Font1', 'n4'), new Font('Font2', 'n4'), new Font('Font2', 'n7')]);
|
74
50
|
});
|
75
51
|
|
76
52
|
it('should load through the alternative API', function () {
|
data/src/core/eventdispatcher.js
CHANGED
@@ -12,15 +12,18 @@ goog.require('webfont.CssClassName');
|
|
12
12
|
* @param {HTMLElement} htmlElement
|
13
13
|
* @param {Object} callbacks
|
14
14
|
* @param {string=} opt_namespace
|
15
|
+
* @param {boolean=} opt_dispatchEvents Set to false to not call any callbacks. Defaults to true.
|
16
|
+
* @param {boolean=} opt_setClasses Set to false to not set classes on the HTML element. Defaults to true.
|
15
17
|
* @constructor
|
16
18
|
*/
|
17
|
-
webfont.EventDispatcher = function(domHelper, htmlElement, callbacks,
|
18
|
-
opt_namespace) {
|
19
|
+
webfont.EventDispatcher = function(domHelper, htmlElement, callbacks, opt_namespace, opt_dispatchEvents, opt_setClasses) {
|
19
20
|
this.domHelper_ = domHelper;
|
20
21
|
this.htmlElement_ = htmlElement;
|
21
22
|
this.callbacks_ = callbacks;
|
22
23
|
this.namespace_ = opt_namespace || webfont.EventDispatcher.DEFAULT_NAMESPACE;
|
23
24
|
this.cssClassName_ = new webfont.CssClassName('-');
|
25
|
+
this.dispatchEvents_ = opt_dispatchEvents !== false;
|
26
|
+
this.setClasses_ = opt_setClasses !== false;
|
24
27
|
};
|
25
28
|
|
26
29
|
/**
|
@@ -60,11 +63,13 @@ goog.scope(function () {
|
|
60
63
|
* Dispatch the loading event and append the loading class name.
|
61
64
|
*/
|
62
65
|
EventDispatcher.prototype.dispatchLoading = function() {
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
66
|
+
if (this.setClasses_) {
|
67
|
+
this.domHelper_.updateClassName(this.htmlElement_,
|
68
|
+
[
|
69
|
+
this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.LOADING)
|
70
|
+
]
|
71
|
+
);
|
72
|
+
}
|
68
73
|
|
69
74
|
this.dispatch_(webfont.EventDispatcher.LOADING);
|
70
75
|
};
|
@@ -74,14 +79,15 @@ goog.scope(function () {
|
|
74
79
|
* @param {webfont.Font} font
|
75
80
|
*/
|
76
81
|
EventDispatcher.prototype.dispatchFontLoading = function(font) {
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
82
|
+
if (this.setClasses_) {
|
83
|
+
this.domHelper_.updateClassName(this.htmlElement_,
|
84
|
+
[
|
85
|
+
this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.LOADING)
|
86
|
+
]
|
87
|
+
);
|
88
|
+
}
|
89
|
+
|
90
|
+
this.dispatch_(webfont.EventDispatcher.FONT + webfont.EventDispatcher.LOADING, font);
|
85
91
|
};
|
86
92
|
|
87
93
|
/**
|
@@ -90,16 +96,18 @@ goog.scope(function () {
|
|
90
96
|
* @param {webfont.Font} font
|
91
97
|
*/
|
92
98
|
EventDispatcher.prototype.dispatchFontActive = function(font) {
|
93
|
-
this.
|
94
|
-
this.
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
99
|
+
if (this.setClasses_) {
|
100
|
+
this.domHelper_.updateClassName(
|
101
|
+
this.htmlElement_,
|
102
|
+
[
|
103
|
+
this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.ACTIVE)
|
104
|
+
],
|
105
|
+
[
|
106
|
+
this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.LOADING),
|
107
|
+
this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.INACTIVE)
|
108
|
+
]
|
109
|
+
);
|
110
|
+
}
|
103
111
|
|
104
112
|
this.dispatch_(webfont.EventDispatcher.FONT + webfont.EventDispatcher.ACTIVE, font);
|
105
113
|
};
|
@@ -111,20 +119,22 @@ goog.scope(function () {
|
|
111
119
|
* @param {webfont.Font} font
|
112
120
|
*/
|
113
121
|
EventDispatcher.prototype.dispatchFontInactive = function(font) {
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
122
|
+
if (this.setClasses_) {
|
123
|
+
var hasFontActive = this.domHelper_.hasClassName(this.htmlElement_,
|
124
|
+
this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.ACTIVE)
|
125
|
+
),
|
126
|
+
add = [],
|
127
|
+
remove = [
|
128
|
+
this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.LOADING)
|
129
|
+
];
|
130
|
+
|
131
|
+
if (!hasFontActive) {
|
132
|
+
add.push(this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.INACTIVE));
|
133
|
+
}
|
121
134
|
|
122
|
-
|
123
|
-
add.push(this.cssClassName_.build(this.namespace_, font.getName(), font.getVariation().toString(), webfont.EventDispatcher.INACTIVE));
|
135
|
+
this.domHelper_.updateClassName(this.htmlElement_, add, remove);
|
124
136
|
}
|
125
137
|
|
126
|
-
this.domHelper_.updateClassName(this.htmlElement_, add, remove);
|
127
|
-
|
128
138
|
this.dispatch_(webfont.EventDispatcher.FONT + webfont.EventDispatcher.INACTIVE, font);
|
129
139
|
};
|
130
140
|
|
@@ -133,20 +143,22 @@ goog.scope(function () {
|
|
133
143
|
* inactive class name (unless the active class name is already present).
|
134
144
|
*/
|
135
145
|
EventDispatcher.prototype.dispatchInactive = function() {
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
146
|
+
if (this.setClasses_) {
|
147
|
+
var hasActive = this.domHelper_.hasClassName(this.htmlElement_,
|
148
|
+
this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.ACTIVE)
|
149
|
+
),
|
150
|
+
add = [],
|
151
|
+
remove = [
|
152
|
+
this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.LOADING)
|
153
|
+
];
|
154
|
+
|
155
|
+
if (!hasActive) {
|
156
|
+
add.push(this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.INACTIVE));
|
157
|
+
}
|
143
158
|
|
144
|
-
|
145
|
-
add.push(this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.INACTIVE));
|
159
|
+
this.domHelper_.updateClassName(this.htmlElement_, add, remove);
|
146
160
|
}
|
147
161
|
|
148
|
-
this.domHelper_.updateClassName(this.htmlElement_, add, remove);
|
149
|
-
|
150
162
|
this.dispatch_(webfont.EventDispatcher.INACTIVE);
|
151
163
|
};
|
152
164
|
|
@@ -155,15 +167,17 @@ goog.scope(function () {
|
|
155
167
|
* class name, and append the active class name.
|
156
168
|
*/
|
157
169
|
EventDispatcher.prototype.dispatchActive = function() {
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
170
|
+
if (this.setClasses_) {
|
171
|
+
this.domHelper_.updateClassName(this.htmlElement_,
|
172
|
+
[
|
173
|
+
this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.ACTIVE)
|
174
|
+
],
|
175
|
+
[
|
176
|
+
this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.LOADING),
|
177
|
+
this.cssClassName_.build(this.namespace_, webfont.EventDispatcher.INACTIVE)
|
178
|
+
]
|
179
|
+
);
|
180
|
+
}
|
167
181
|
|
168
182
|
this.dispatch_(webfont.EventDispatcher.ACTIVE);
|
169
183
|
};
|
@@ -173,7 +187,7 @@ goog.scope(function () {
|
|
173
187
|
* @param {webfont.Font=} opt_font
|
174
188
|
*/
|
175
189
|
EventDispatcher.prototype.dispatch_ = function(event, opt_font) {
|
176
|
-
if (this.callbacks_[event]) {
|
190
|
+
if (this.dispatchEvents_ && this.callbacks_[event]) {
|
177
191
|
if (opt_font) {
|
178
192
|
this.callbacks_[event](opt_font.getName(), opt_font.getVariation());
|
179
193
|
} else {
|
data/src/core/fontruler.js
CHANGED
@@ -38,7 +38,7 @@ goog.scope(function () {
|
|
38
38
|
* @return {string}
|
39
39
|
*/
|
40
40
|
FontRuler.prototype.computeStyleString_ = function(font) {
|
41
|
-
return "display:block;position:absolute;top
|
41
|
+
return "display:block;position:absolute;top:0px;left:0px;visibility:hidden;" +
|
42
42
|
"font-size:300px;width:auto;height:auto;line-height:normal;margin:0;" +
|
43
43
|
"padding:0;font-variant:normal;white-space:nowrap;font-family:" +
|
44
44
|
font.getCssName() + ";" + font.getCssVariation();
|
data/src/core/fontwatcher.js
CHANGED
@@ -28,14 +28,15 @@ goog.scope(function () {
|
|
28
28
|
/**
|
29
29
|
* Watches a set of font families.
|
30
30
|
* @param {Array.<webfont.Font>} fonts The fonts to watch.
|
31
|
-
* @param {
|
31
|
+
* @param {webfont.FontTestStrings} fontTestStrings The font test strings for
|
32
32
|
* each family.
|
33
33
|
* @param {Object.<String, boolean>} metricCompatibleFonts
|
34
34
|
* @param {boolean} last True if this is the last set of fonts to watch.
|
35
35
|
*/
|
36
36
|
FontWatcher.prototype.watchFonts = function(fonts,
|
37
37
|
fontTestStrings, metricCompatibleFonts, last) {
|
38
|
-
var length = fonts.length
|
38
|
+
var length = fonts.length,
|
39
|
+
testStrings = fontTestStrings || {};
|
39
40
|
|
40
41
|
if (length === 0 && last) {
|
41
42
|
this.eventDispatcher_.dispatchInactive();
|
@@ -50,22 +51,33 @@ goog.scope(function () {
|
|
50
51
|
|
51
52
|
for (var i = 0; i < fonts.length; i++) {
|
52
53
|
var font = fonts[i],
|
53
|
-
|
54
|
+
testString = testStrings[font.getName()];
|
54
55
|
|
55
56
|
this.eventDispatcher_.dispatchFontLoading(font);
|
56
57
|
|
57
58
|
var fontWatchRunner = null;
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
60
|
+
// We've disabled the native font watch runner for now. The
|
61
|
+
// reason is that its behaviour is slightly different from
|
62
|
+
// the non-native version in that it returns immediately if
|
63
|
+
// a @font-face rule is not in the document. The non-native
|
64
|
+
// version keeps polling the page. A lot of modules depend
|
65
|
+
// on the ability to start font watching before actually
|
66
|
+
// loading the fonts, so they fail in this case (which is
|
67
|
+
// related to browser support; figuring out when a
|
68
|
+
// stylesheet has loaded reliably). Until that issue is
|
69
|
+
// resolved we'll keep the native font disabled.
|
70
|
+
//
|
71
|
+
//if (this.browserInfo_.hasNativeFontLoading()) {
|
72
|
+
// fontWatchRunner = new NativeFontWatchRunner(
|
73
|
+
// goog.bind(this.fontActive_, this),
|
74
|
+
// goog.bind(this.fontInactive_, this),
|
75
|
+
// this.domHelper_,
|
76
|
+
// font,
|
77
|
+
// this.timeout_,
|
78
|
+
// fontTestString
|
79
|
+
// );
|
80
|
+
//} else {
|
69
81
|
fontWatchRunner = new FontWatchRunner(
|
70
82
|
goog.bind(this.fontActive_, this),
|
71
83
|
goog.bind(this.fontInactive_, this),
|
@@ -74,9 +86,9 @@ goog.scope(function () {
|
|
74
86
|
this.browserInfo_,
|
75
87
|
this.timeout_,
|
76
88
|
metricCompatibleFonts,
|
77
|
-
|
89
|
+
testString
|
78
90
|
);
|
79
|
-
}
|
91
|
+
//}
|
80
92
|
|
81
93
|
fontWatchRunner.start();
|
82
94
|
}
|
data/src/core/useragent.js
CHANGED
@@ -12,13 +12,10 @@ goog.provide('webfont.UserAgent');
|
|
12
12
|
* @export
|
13
13
|
* @param {string} name
|
14
14
|
* @param {webfont.Version} version
|
15
|
-
* @param {string} versionString
|
16
15
|
* @param {string} engine
|
17
16
|
* @param {webfont.Version} engineVersion
|
18
|
-
* @param {string} engineVersionString
|
19
17
|
* @param {string} platform
|
20
18
|
* @param {webfont.Version} platformVersion
|
21
|
-
* @param {string} platformVersionString
|
22
19
|
* @param {number|undefined} documentMode
|
23
20
|
* @param {!webfont.BrowserInfo} browserInfo
|
24
21
|
* @constructor
|
@@ -26,24 +23,18 @@ goog.provide('webfont.UserAgent');
|
|
26
23
|
webfont.UserAgent = function(
|
27
24
|
name,
|
28
25
|
version,
|
29
|
-
versionString,
|
30
26
|
engine,
|
31
27
|
engineVersion,
|
32
|
-
engineVersionString,
|
33
28
|
platform,
|
34
29
|
platformVersion,
|
35
|
-
platformVersionString,
|
36
30
|
documentMode,
|
37
31
|
browserInfo) {
|
38
32
|
this.name_ = name;
|
39
33
|
this.version_ = version;
|
40
|
-
this.versionString_ = versionString;
|
41
34
|
this.engine_ = engine;
|
42
35
|
this.engineVersion_ = engineVersion;
|
43
|
-
this.engineVersionString_ = engineVersionString;
|
44
36
|
this.platform_ = platform;
|
45
37
|
this.platformVersion_ = platformVersion;
|
46
|
-
this.platformVersionString_ = platformVersionString;
|
47
38
|
this.documentMode_ = documentMode;
|
48
39
|
this.browserInfo_ = browserInfo;
|
49
40
|
};
|
@@ -59,19 +50,10 @@ goog.scope(function () {
|
|
59
50
|
return this.name_;
|
60
51
|
};
|
61
52
|
|
62
|
-
/**
|
63
|
-
* @export
|
64
|
-
* @deprecated
|
65
|
-
* @return {string}
|
66
|
-
*/
|
67
|
-
UserAgent.prototype.getVersion = function() {
|
68
|
-
return this.versionString_;
|
69
|
-
};
|
70
|
-
|
71
53
|
/**
|
72
54
|
* @return {webfont.Version}
|
73
55
|
*/
|
74
|
-
UserAgent.prototype.
|
56
|
+
UserAgent.prototype.getVersion = function() {
|
75
57
|
return this.version_;
|
76
58
|
};
|
77
59
|
|
@@ -83,19 +65,10 @@ goog.scope(function () {
|
|
83
65
|
return this.engine_;
|
84
66
|
};
|
85
67
|
|
86
|
-
/**
|
87
|
-
* @export
|
88
|
-
* @deprecated
|
89
|
-
* @return {string}
|
90
|
-
*/
|
91
|
-
UserAgent.prototype.getEngineVersion = function() {
|
92
|
-
return this.engineVersionString_;
|
93
|
-
};
|
94
|
-
|
95
68
|
/**
|
96
69
|
* @return {webfont.Version}
|
97
70
|
*/
|
98
|
-
UserAgent.prototype.
|
71
|
+
UserAgent.prototype.getEngineVersion = function() {
|
99
72
|
return this.engineVersion_;
|
100
73
|
};
|
101
74
|
|
@@ -107,19 +80,10 @@ goog.scope(function () {
|
|
107
80
|
return this.platform_;
|
108
81
|
};
|
109
82
|
|
110
|
-
/**
|
111
|
-
* @export
|
112
|
-
* @deprecated
|
113
|
-
* @return {string}
|
114
|
-
*/
|
115
|
-
UserAgent.prototype.getPlatformVersion = function() {
|
116
|
-
return this.platformVersionString_;
|
117
|
-
};
|
118
|
-
|
119
83
|
/**
|
120
84
|
* @return {webfont.Version}
|
121
85
|
*/
|
122
|
-
UserAgent.prototype.
|
86
|
+
UserAgent.prototype.getPlatformVersion = function() {
|
123
87
|
return this.platformVersion_;
|
124
88
|
};
|
125
89
|
|