Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fontdeck module - small change #37

Merged
merged 5 commits into from Nov 29, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/webfontloader/demo/public/fontdeck.html
Expand Up @@ -17,7 +17,7 @@
}
WebFont.load({
fontdeck: {
id: '2282'
id: 2282
},
loading: function() {
progress('loading');
Expand Down
85 changes: 71 additions & 14 deletions src-test/fontdeck/fontdeck_script_test.js
Expand Up @@ -5,10 +5,39 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() {
'id': '2282'
};
var apiResponse = {
'css':'http://fontdeck.com/s/css/uH5+KWQnibDTJRYggGJ9XZLTAgw/webfontloader/2282.css',
'provides':[
{'font_size_adjust':null,'weight':'normal','style':'normal','name':'Fertigo Pro Regular'},
{'font_size_adjust':'0.5','weight':'bold','style':'italic','name':'Bodoni Display Bold Italic'}
"domain" : "localhost",
"cssurl" : "http://f.fontdeck.com/s/css/03BmCXiV2AHwX/Rp+OBFTfD2oFs/localhost/2282.css",
"project" : 2282,
"cssbase" : "http://f.fontdeck.com/s/css/03BmCXiV2AHwX/Rp+OBFTfD2oFs",
"fonts" : [
{
"font_family" : "'Fertigo Pro Regular', Fertigo, Constantia, Palatino, serif",
"font_size_adjust" : 0.508,
"name" : "Fertigo Pro Regular",
"style" : "normal",
"weight" : "normal",
"font_urls" : {
"eot" : "http://f.fontdeck.com/f/1/SUlFR0tid0kAA2vb11Ly/IGWDK+wV8TMAfV0J1Ej1J1GFRT1bssqrn6a.eot",
"ttf" : "http://f.fontdeck.com/f/1/SUlFR0tid0kAA2vb11Ly/IGWDK+wV8TMAfV0J1Ej1J1GFRT1bssqrn6a.ttf",
"woff" : "http://f.fontdeck.com/f/1/SUlFR0tid0kAA2vb11Ly/IGWDK+wV8TMAfV0J1Ej1J1GFRT1bssqrn6a.woff",
"svg" : "http://f.fontdeck.com/f/1/SUlFR0tid0kAA2vb11Ly/IGWDK+wV8TMAfV0J1Ej1J1GFRT1bssqrn6a.svg#104"
},
"id" : 104
},
{
"font_family" : "'Bodoni Display Bold Italic', Georgia, 'Times New Roman', Times, serif",
"font_size_adjust" : 0.45,
"name" : "Bodoni Display Bold Italic",
"style" : "italic",
"weight" : "bold",
"font_urls" : {
"eot" : "http://f.fontdeck.com/f/1/azJEbTVyc1QAA11+CAE5C93+l/bAQx1ipRo6Maba19w3Yy5ng+qVWlfj.eot",
"ttf" : "http://f.fontdeck.com/f/1/azJEbTVyc1QAA11+CAE5C93+l/bAQx1ipRo6Maba19w3Yy5ng+qVWlfj.ttf",
"woff" : "http://f.fontdeck.com/f/1/azJEbTVyc1QAA11+CAE5C93+l/bAQx1ipRo6Maba19w3Yy5ng+qVWlfj.woff",
"svg" : "http://f.fontdeck.com/f/1/azJEbTVyc1QAA11+CAE5C93+l/bAQx1ipRo6Maba19w3Yy5ng+qVWlfj.svg#2256"
},
"id" : 2256
}
]
};
var insert = '';
Expand All @@ -19,13 +48,18 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() {
},
createScriptSrc: function(srcLink) {
src = srcLink;
}
};
var global = {
location: {
protocol: 'https:'
},
createCssLink: function(cssLink) {
css = cssLink;
return '<link href="' + css + '" type="text/css" />';
document: {
location: {
hostname: 'test-host-name'
}
}
};
var global = {};
var fontdeck = new webfont.FontdeckScript(global, fakeDomHelper, configuration);

// supportUserAgent
Expand All @@ -34,16 +68,39 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() {

fontdeck.supportUserAgent(userAgent, function(support) { isSupport = support; });
assertEquals('head', insert);
assertEquals('http://fontdeck.com/api/v1/project-info?project=2282&domain=localhost&callback=window.__webfontfontdeckmodule__[2282]', src);
assertEquals('https://f.fontdeck.com/s/css/js/test-host-name/2282.js', src);
assertEquals(null, isSupport);

assertNotNull(global.__webfontfontdeckmodule__);
assertNotNull(global.__webfontfontdeckmodule__['2282']);

// Call the callback function passing in dummy API response.
global.__webfontfontdeckmodule__['2282'](apiResponse);
global.__webfontfontdeckmodule__['2282'](true, apiResponse);

assertEquals(fontdeck.fontFamilies_, [apiResponse.fonts[0].name, apiResponse.fonts[1].name]);
assertEquals(fontdeck.fontVariations_[apiResponse.fonts[0].name], ['n4']);
assertEquals(fontdeck.fontVariations_[apiResponse.fonts[1].name], ['i7']);

assertEquals(true, isSupport);
};

FontdeckScriptTest.prototype.testNoProjectId = function() {
var configuration = {
'id': null
};
var insert = '';
var src = '';
var fakeDomHelper = {};
var global = {};
var fontdeck = new webfont.FontdeckScript(global, fakeDomHelper, configuration);

// supportUserAgent
var userAgent = 'user agent';
var isSupport = null;

assertEquals(fontdeck.fontFamilies_, [apiResponse.provides[0].name, apiResponse.provides[1].name]);
assertEquals(fontdeck.fontVariations_[apiResponse.provides[0].name], ['n4']);
assertEquals(fontdeck.fontVariations_[apiResponse.provides[1].name], ['i7']);
};
fontdeck.supportUserAgent(userAgent, function(support) { isSupport = support; });

assertEquals(fontdeck.fontFamilies_, []);
assertEquals(fontdeck.fontVariations_, []);
assertEquals(true, isSupport);
}
30 changes: 13 additions & 17 deletions src/fontdeck/fontdeck_script.js
Expand Up @@ -12,16 +12,16 @@ webfont.FontdeckScript = function(global, domHelper, configuration) {

webfont.FontdeckScript.NAME = 'fontdeck';
webfont.FontdeckScript.HOOK = '__webfontfontdeckmodule__';
webfont.FontdeckScript.API = 'http://fontdeck.com/api/v1/project-info?'
webfont.FontdeckScript.API = '//f.fontdeck.com/s/css/js/';

webfont.FontdeckScript.prototype.getScriptSrc = function(projectId) {
var protocol = 'https:' == this.global_.location.protocol ? 'https:' : 'http:';
var api = this.configuration_['api'] || webfont.FontdeckScript.API;
return api + 'project=' + projectId + '&domain=' + document.location.hostname + '&callback=window.__webfontfontdeckmodule__[' + projectId + ']';
return protocol + api + this.global_.document.location.hostname + '/' + projectId + '.js';
};

webfont.FontdeckScript.prototype.supportUserAgent = function(userAgent, support) {
var projectId = this.configuration_['id'];
var families = this.configuration_['families'] || null;
var self = this;

if (projectId) {
Expand All @@ -30,20 +30,16 @@ webfont.FontdeckScript.prototype.supportUserAgent = function(userAgent, support)
this.global_[webfont.FontdeckScript.HOOK] = {};
}

// The API will call this function with a link to the CSS
// and a list of supported fonts.
this.global_[webfont.FontdeckScript.HOOK][projectId] = function(data) {
self.domHelper_.insertInto('head', self.domHelper_.createCssLink(data['css']));
for (var i = 0, j = data['provides'].length; i < j; ++i) {
var font = data['provides'][i];
self.fontFamilies_.push(font['name']);
self.fontVariations_[font['name']] = [self.fvd_.compact("font-weight:" + font['weight'] + ";font-style:" + font['style'])];
}
// If families were passed into load, then use them instead.
if (families !== null) {
self.fontFamilies_ = families;
}
support(true);
// Fontdeck will call this function to indicate support status
// and what fonts are provided.
this.global_[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) {
for (var i = 0, j = data['fonts'].length; i<j; ++i) {
var font = data['fonts'][i];
// Add the FVDs
self.fontFamilies_.push(font['name']);
self.fontVariations_[font['name']] = [self.fvd_.compact("font-weight:" + font['weight'] + ";font-style:" + font['style'])];
}
support(fontdeckSupports);
};

// Call the Fontdeck API.
Expand Down