Skip to content

Commit

Permalink
Merge pull request #132 from typekit/bs-fix-monotype
Browse files Browse the repository at this point in the history
Fix script loading in monotype module.
  • Loading branch information
bramstein committed May 23, 2013
2 parents 60a5cb3 + 7cadf28 commit 1cb333a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
15 changes: 6 additions & 9 deletions spec/modules/monotype_spec.js
Expand Up @@ -25,10 +25,7 @@ describe('modules.Monotype', function () {
createElement: jasmine.createSpy('createElement').andReturn(script),
insertInto: jasmine.createSpy('insertInto'),
getLoadWindow: jasmine.createSpy('getLoadWindow').andReturn(global),
getProtocol: jasmine.createSpy('getProtocol').andReturn('http:'),
loadScript: jasmine.createSpy('loadScript').andCallFake(function (src, cb) {
cb();
})
getProtocol: jasmine.createSpy('getProtocol').andReturn('http:')
};
support = jasmine.createSpy('support');
load = jasmine.createSpy('load');
Expand All @@ -54,14 +51,14 @@ describe('modules.Monotype', function () {
return [{fontfamily: 'aachen bold'}, {fontfamily: 'kid print regular'}];
};

monotype = new Monotype(useragent, fakeDomHelper, configuration);
monotype.supportUserAgent(useragent, support);
monotype.load(load);
script.onload();
});


it('should create a script element', function () {
expect(fakeDomHelper.loadScript).toHaveBeenCalled();
expect(fakeDomHelper.loadScript.calls[0].args[0]).toEqual('http://fast.fonts.com/jsapidev/01e2ff27-25bf-4801-a23e-73d328e6c7cc.js');
expect(support).toHaveBeenCalled();
expect(fakeDomHelper.createElement).toHaveBeenCalledWith('script');
expect(script.src).toEqual('http://fast.fonts.com/jsapidev/01e2ff27-25bf-4801-a23e-73d328e6c7cc.js');
expect(load).toHaveBeenCalledWith([new Font('aachen bold'), new Font('kid print regular')]);
});
});
19 changes: 17 additions & 2 deletions src/modules/monotype.js
Expand Up @@ -49,7 +49,10 @@ goog.scope(function () {
var projectId = self.configuration_['projectId'];
var version = self.configuration_['version'];
if (projectId) {
var loadWindow = this.domHelper_.getLoadWindow();
var loadWindow = self.domHelper_.getLoadWindow(),
sc = self.domHelper_.createElement("script");

sc["id"] = Monotype.SCRIPTID + projectId;

function onload() {
if (loadWindow[Monotype.HOOK + projectId]) {
Expand All @@ -62,7 +65,19 @@ goog.scope(function () {
}
support(userAgent.getBrowserInfo().hasWebFontSupport());
}
self.domHelper_.loadScript(self.getScriptSrc(projectId, version), onload);

var done = false;

sc["onload"] = sc["onreadystatechange"] = function () {
if (!done && (!this["readyState"] || this["readyState"] === "loaded" || this["readyState"] === "complete")) {
done = true;
onload();
sc["onload"] = sc["onreadystatechange"] = null;
}
};

sc["src"] = self.getScriptSrc(projectId, version);
this.domHelper_.insertInto('head', sc);
}
else {
support(true);
Expand Down

0 comments on commit 1cb333a

Please sign in to comment.