From ade5e25aee35aed5306598d2db3321925936f110 Mon Sep 17 00:00:00 2001 From: Ryan Wolf Date: Wed, 28 Nov 2012 17:36:43 -0800 Subject: [PATCH 1/9] Add optional context argument to WebFont.load. Now possible to specify which iframe to wait for font load in. --- src-test/core/fonttest.js | 19 +++--------- src-test/fontdeck/fontdeck_script_test.js | 32 ++++++++++++-------- src-test/monotype/monotype_script_test.js | 36 +++++++++++++++++------ src-test/typekit/typekit_script_test.js | 24 +++++++++++---- src/ascender/ascender_script.js | 3 +- src/core/domhelper.js | 7 +++++ src/core/font.js | 13 ++++---- src/core/fontmoduleloader.js | 4 +-- src/core/initialize.js | 4 +-- src/custom/customcss.js | 3 +- src/fontdeck/fontdeck_script.js | 9 +++--- src/google/googlefontapi.js | 5 ++-- src/monotype/monotype_script.js | 9 +++--- src/typekit/typekit_script.js | 9 +++--- 14 files changed, 101 insertions(+), 76 deletions(-) diff --git a/src-test/core/fonttest.js b/src-test/core/fonttest.js index bf6a5712..9683c707 100644 --- a/src-test/core/fonttest.js +++ b/src-test/core/fonttest.js @@ -1,25 +1,14 @@ var FontTest = TestCase('FontTest'); FontTest.prototype.setUp = function() { - this.fakeHtmlElement_ = { className: '' }; - this.fakeDomHelper_ = { - appendClassName: function() {}, - removeClassName: function() {}, - hasClassName: function() {}, - createElement: function(name) { - return document.createElement(name); - }, - insertInto: function() {}, - removeElement: function() {} - }; this.fontModuleLoader_ = new webfont.FontModuleLoader(); }; FontTest.prototype.testFontLoad = function() { var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, true); - var font = new webfont.WebFont(this.fakeDomHelper_, this.fontModuleLoader_, - this.fakeHtmlElement_, function(func, timeout) { func(); }, userAgent); + var font = new webfont.WebFont(this.fontModuleLoader_, + function(func, timeout) { func(); }, userAgent); var self = this; var testModule = null; @@ -66,8 +55,8 @@ FontTest.prototype.testFontLoad = function() { FontTest.prototype.testFontInactive = function() { var userAgent = new webfont.UserAgent('Firefox', '3.0', false); - var font = new webfont.WebFont(this.fakeDomHelper_, this.fontModuleLoader_, - this.fakeHtmlElement_, function(func, timeout) { func(); }, userAgent); + var font = new webfont.WebFont(this.fontModuleLoader_, + function(func, timeout) { func(); }, userAgent); var self = this; var testModule; diff --git a/src-test/fontdeck/fontdeck_script_test.js b/src-test/fontdeck/fontdeck_script_test.js index 841da7a4..3501b3e2 100644 --- a/src-test/fontdeck/fontdeck_script_test.js +++ b/src-test/fontdeck/fontdeck_script_test.js @@ -42,14 +42,6 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { }; var insert = ''; var src = ''; - var fakeDomHelper = { - insertInto: function(tag, e) { - insert = tag; - }, - createScriptSrc: function(srcLink) { - src = srcLink; - } - }; var global = { location: { protocol: 'https:' @@ -60,7 +52,18 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { } } }; - var fontdeck = new webfont.FontdeckScript(global, fakeDomHelper, configuration); + var fakeDomHelper = { + insertInto: function(tag, e) { + insert = tag; + }, + createScriptSrc: function(srcLink) { + src = srcLink; + }, + getWindow: function() { + return global; + } + }; + var fontdeck = new webfont.FontdeckScript(fakeDomHelper, configuration); // supportUserAgent var userAgent = 'user agent'; @@ -90,9 +93,12 @@ FontdeckScriptTest.prototype.testNoProjectId = function() { }; var insert = ''; var src = ''; - var fakeDomHelper = {}; - var global = {}; - var fontdeck = new webfont.FontdeckScript(global, fakeDomHelper, configuration); + var fakeDomHelper = { + getWindow: function() { + return {}; + } + }; + var fontdeck = new webfont.FontdeckScript(fakeDomHelper, configuration); // supportUserAgent var userAgent = 'user agent'; @@ -103,4 +109,4 @@ FontdeckScriptTest.prototype.testNoProjectId = function() { assertEquals(fontdeck.fontFamilies_, []); assertEquals(fontdeck.fontVariations_, []); assertEquals(true, isSupport); -} \ No newline at end of file +} diff --git a/src-test/monotype/monotype_script_test.js b/src-test/monotype/monotype_script_test.js index 145be14e..fe6582be 100644 --- a/src-test/monotype/monotype_script_test.js +++ b/src-test/monotype/monotype_script_test.js @@ -4,7 +4,6 @@ MonotypeScriptTest.prototype.testIfProtocolMethodIsReturningProperly = function var fakeDocument = { location: { protocol: "https:" } }; - var global = {}; // should be window in actual situation. var script = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); var config = { projectId: '01e2ff27-25bf-4801-a23e-73d328e6c7cc' }; @@ -22,17 +21,21 @@ MonotypeScriptTest.prototype.testIfProtocolMethodIsReturningProperly = function if (script.onload) { script.onload(); } + }, + getWindow: function () { + // should be window in actual situation. + return {}; } }; - var monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocument, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); assertEquals("https:", monotypeScript.protocol()); fakeDocument = { location: { protocol: "http:" } }; - monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocument, config); + monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); assertEquals("http:", monotypeScript.protocol()); }; @@ -59,6 +62,9 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { if (script.onload) { script.onload(); } + }, + getWindow: function () { + return global; } }; @@ -77,7 +83,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { } var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocument, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { families = fontFamilies; @@ -117,6 +123,9 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { if (script.onload) { script.onload(); } + }, + getWindow: function () { + return global; } }; @@ -136,7 +145,7 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { } var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocument1, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument1, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { families = fontFamilies; @@ -150,7 +159,7 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { //one page can have multiple projects, but not 2 projects with same projectId. config = { projectId: '01e2ff27-25bf-4801-a23e-73d328e6c7c1', api: "http://fast.fonts.com/jsapidev" }; - var monotypeScript2 = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocument2, config); + var monotypeScript2 = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument2, config); monotypeScript2.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript2.load(function (fontFamilies) { families = fontFamilies; @@ -182,6 +191,9 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { if (script.onload) { script.onload(); } + }, + getWindow: function () { + return global; } }; @@ -207,7 +219,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocHelper, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { @@ -243,6 +255,9 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlH if (script.onload) { script.onload(); } + }, + getWindow: function () { + return global; } }; @@ -262,7 +277,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlH var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocument, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { @@ -297,6 +312,9 @@ MonotypeScriptTest.prototype.testWithoutProjectId = function () { if (script.onload) { script.onload(); } + }, + getWindow: function () { + return global; } }; @@ -308,7 +326,7 @@ MonotypeScriptTest.prototype.testWithoutProjectId = function () { var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(global, userAgent, fakeDomHelper, fakeDocHelper, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { diff --git a/src-test/typekit/typekit_script_test.js b/src-test/typekit/typekit_script_test.js index 58305aa7..38b4ced2 100644 --- a/src-test/typekit/typekit_script_test.js +++ b/src-test/typekit/typekit_script_test.js @@ -6,16 +6,19 @@ TypekitScriptTest.prototype.testSupportAndLoadLifecycle = function() { }; var insert = ''; var src = ''; + var global = {}; var fakeDomHelper = { insertInto: function(tag, e) { insert = tag; }, createScriptSrc: function(srcLink) { src = srcLink; + }, + getWindow: function() { + return global; } }; - var global = {}; - var typeKit = new webfont.TypekitScript(global, fakeDomHelper, configuration); + var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); // supportUserAgent var userAgent = 'user agent'; @@ -65,16 +68,19 @@ TypekitScriptTest.prototype.testLoadWithVariations = function() { }; var insert = ''; var src = ''; + var global = {}; var fakeDomHelper = { insertInto: function(tag, e) { insert = tag; }, createScriptSrc: function(srcLink) { src = srcLink; + }, + getWindow: function() { + return global; } }; - var global = {}; - var typeKit = new webfont.TypekitScript(global, fakeDomHelper, configuration); + var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); // supportUserAgent var userAgent = 'user agent'; @@ -129,9 +135,12 @@ TypekitScriptTest.prototype.testAlternateApi = function() { }, createScriptSrc: function(srcLink) { src = srcLink; + }, + getWindow: function() { + return {}; } }; - var typeKit = new webfont.TypekitScript({}, fakeDomHelper, configuration); + var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); var userAgent = 'user agent'; var isSupport = null; @@ -152,9 +161,12 @@ TypekitScriptTest.prototype.testNoKitId = function() { }, createScriptSrc: function(srcLink) { src = srcLink; + }, + getWindow: function() { + return {}; } }; - var typeKit = new webfont.TypekitScript({}, fakeDomHelper, configuration); + var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); var userAgent = 'user agent'; var isSupport = null; diff --git a/src/ascender/ascender_script.js b/src/ascender/ascender_script.js index dad558ec..bbc2a99b 100644 --- a/src/ascender/ascender_script.js +++ b/src/ascender/ascender_script.js @@ -78,7 +78,6 @@ webfont.AscenderScript.prototype.parseVariations = function(source){ return variations; }; -globalNamespaceObject.addModule(webfont.AscenderScript.NAME, function(configuration) { - var domHelper = new webfont.DomHelper(document); +globalNamespaceObject.addModule(webfont.AscenderScript.NAME, function(configuration, domHelper) { return new webfont.AscenderScript(domHelper, configuration); }); diff --git a/src/core/domhelper.js b/src/core/domhelper.js index e98877f8..399a95ff 100644 --- a/src/core/domhelper.js +++ b/src/core/domhelper.js @@ -194,3 +194,10 @@ webfont.DomHelper.prototype.hasSupportForStyle_ = function() { } return this.supportForStyle_ }; + +/** + * @return {Window} The window for the helper's document. + */ +webfont.DomHelper.prototype.getWindow = function() { + return this.document_.defaultView || this.document_.parentWindow; +}; diff --git a/src/core/font.js b/src/core/font.js index fc01c6ee..eaa48771 100644 --- a/src/core/font.js +++ b/src/core/font.js @@ -1,11 +1,8 @@ /** * @constructor */ -webfont.WebFont = function(domHelper, fontModuleLoader, htmlElement, asyncCall, - userAgent) { - this.domHelper_ = domHelper; +webfont.WebFont = function(fontModuleLoader, asyncCall, userAgent) { this.fontModuleLoader_ = fontModuleLoader; - this.htmlElement_ = htmlElement; this.asyncCall_ = asyncCall; this.userAgent_ = userAgent; this.moduleLoading_ = 0; @@ -16,7 +13,11 @@ webfont.WebFont.prototype.addModule = function(name, factory) { this.fontModuleLoader_.addModuleFactory(name, factory); }; -webfont.WebFont.prototype.load = function(configuration) { +webfont.WebFont.prototype.load = function(configuration, opt_context) { + var context = opt_context || window; + this.domHelper_ = new webfont.DomHelper(context.document); + this.htmlElement_ = context.document.documentElement; + var eventDispatcher = new webfont.EventDispatcher( this.domHelper_, this.htmlElement_, configuration); @@ -66,7 +67,7 @@ webfont.WebFont.prototype.onModuleReady_ = function(eventDispatcher, fontWatcher }; webfont.WebFont.prototype.load_ = function(eventDispatcher, configuration) { - var modules = this.fontModuleLoader_.getModules(configuration), + var modules = this.fontModuleLoader_.getModules(configuration, this.domHelper_), self = this; this.moduleFailedLoading_ = this.moduleLoading_ = modules.length; diff --git a/src/core/fontmoduleloader.js b/src/core/fontmoduleloader.js index 292327d2..ac25b5a1 100644 --- a/src/core/fontmoduleloader.js +++ b/src/core/fontmoduleloader.js @@ -9,7 +9,7 @@ webfont.FontModuleLoader.prototype.addModuleFactory = function(name, factory) { this.modules_[name] = factory; }; -webfont.FontModuleLoader.prototype.getModules = function(configuration) { +webfont.FontModuleLoader.prototype.getModules = function(configuration, domHelper) { var modules = []; for (var key in configuration) { @@ -17,7 +17,7 @@ webfont.FontModuleLoader.prototype.getModules = function(configuration) { var moduleFactory = this.modules_[key]; if (moduleFactory) { - modules.push(moduleFactory(configuration[key])); + modules.push(moduleFactory(configuration[key], domHelper)); } } } diff --git a/src/core/initialize.js b/src/core/initialize.js index af007c9c..21d794e5 100644 --- a/src/core/initialize.js +++ b/src/core/initialize.js @@ -5,11 +5,9 @@ var globalName = 'WebFont'; var globalNamespaceObject = window[globalName] = (function() { var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document); var userAgent = userAgentParser.parse(); - var domHelper = new webfont.DomHelper(document); var asyncCall = function(func, timeout) { setTimeout(func, timeout); }; - return new webfont.WebFont(domHelper, new webfont.FontModuleLoader(), - document.documentElement, asyncCall, userAgent); + return new webfont.WebFont(new webfont.FontModuleLoader(), asyncCall, userAgent); })(); // Export the public API. diff --git a/src/custom/customcss.js b/src/custom/customcss.js index 9db15ab2..0b9b1d63 100644 --- a/src/custom/customcss.js +++ b/src/custom/customcss.js @@ -31,7 +31,6 @@ webfont.CustomCss.prototype.supportUserAgent = function(userAgent, support) { return support(userAgent.isSupportingWebFont()); }; -globalNamespaceObject.addModule(webfont.CustomCss.NAME, function(configuration) { - var domHelper = new webfont.DomHelper(document); +globalNamespaceObject.addModule(webfont.CustomCss.NAME, function(configuration, domHelper) { return new webfont.CustomCss(domHelper, configuration); }); diff --git a/src/fontdeck/fontdeck_script.js b/src/fontdeck/fontdeck_script.js index 29728ac4..52ade419 100644 --- a/src/fontdeck/fontdeck_script.js +++ b/src/fontdeck/fontdeck_script.js @@ -1,8 +1,8 @@ /** * @constructor */ -webfont.FontdeckScript = function(global, domHelper, configuration) { - this.global_ = global; +webfont.FontdeckScript = function(domHelper, configuration) { + this.global_ = domHelper.getWindow(); this.domHelper_ = domHelper; this.configuration_ = configuration; this.fontFamilies_ = []; @@ -55,7 +55,6 @@ webfont.FontdeckScript.prototype.load = function(onReady) { onReady(this.fontFamilies_, this.fontVariations_); }; -globalNamespaceObject.addModule(webfont.FontdeckScript.NAME, function(configuration) { - var domHelper = new webfont.DomHelper(document); - return new webfont.FontdeckScript(window, domHelper, configuration); +globalNamespaceObject.addModule(webfont.FontdeckScript.NAME, function(configuration, domHelper) { + return new webfont.FontdeckScript(domHelper, configuration); }); diff --git a/src/google/googlefontapi.js b/src/google/googlefontapi.js index ce44b31c..1fc219e2 100644 --- a/src/google/googlefontapi.js +++ b/src/google/googlefontapi.js @@ -48,9 +48,8 @@ webfont.GoogleFontApi.prototype.insertLink_ = function(onReady) { fontApiParser.getFontTestStrings()); }; -globalNamespaceObject.addModule(webfont.GoogleFontApi.NAME, function(configuration) { +globalNamespaceObject.addModule(webfont.GoogleFontApi.NAME, function(configuration, domHelper) { var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document); var userAgent = userAgentParser.parse(); - return new webfont.GoogleFontApi(userAgent, new webfont.DomHelper(document), - configuration); + return new webfont.GoogleFontApi(userAgent, domHelper, configuration); }); diff --git a/src/monotype/monotype_script.js b/src/monotype/monotype_script.js index 9d631a35..e8d807a8 100644 --- a/src/monotype/monotype_script.js +++ b/src/monotype/monotype_script.js @@ -9,8 +9,8 @@ projectId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'//this is your Fonts.com Web fo /** * @constructor */ -webfont.MonotypeScript = function (global, userAgent, domHelper, doc, configuration) { - this.global_ = global; +webfont.MonotypeScript = function (userAgent, domHelper, doc, configuration) { + this.global_ = domHelper.getWindow(); this.userAgent_ = userAgent; this.domHelper_ = domHelper; this.doc_ = doc; @@ -97,9 +97,8 @@ webfont.MonotypeScript.prototype.protocol = function () { return defaultProtocol; }; -globalNamespaceObject.addModule(webfont.MonotypeScript.NAME, function (configuration) { +globalNamespaceObject.addModule(webfont.MonotypeScript.NAME, function (configuration, domHelper) { var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document); var userAgent = userAgentParser.parse(); - var domHelper = new webfont.DomHelper(document); - return new webfont.MonotypeScript(window, userAgent, domHelper, document, configuration); + return new webfont.MonotypeScript(userAgent, domHelper, document, configuration); }); diff --git a/src/typekit/typekit_script.js b/src/typekit/typekit_script.js index 45656946..314b22da 100644 --- a/src/typekit/typekit_script.js +++ b/src/typekit/typekit_script.js @@ -1,8 +1,8 @@ /** * @constructor */ -webfont.TypekitScript = function(global, domHelper, configuration) { - this.global_ = global; +webfont.TypekitScript = function(domHelper, configuration) { + this.global_ = domHelper.getWindow(); this.domHelper_ = domHelper; this.configuration_ = configuration; this.fontFamilies_ = []; @@ -53,8 +53,7 @@ webfont.TypekitScript.prototype.load = function(onReady) { onReady(this.fontFamilies_, this.fontVariations_); }; -globalNamespaceObject.addModule(webfont.TypekitScript.NAME, function(configuration) { - var domHelper = new webfont.DomHelper(document); - return new webfont.TypekitScript(window, domHelper, configuration); +globalNamespaceObject.addModule(webfont.TypekitScript.NAME, function(configuration, domHelper) { + return new webfont.TypekitScript(domHelper, configuration); }); From 1e7293b145a0391948b1af8516b01c037bdef8d9 Mon Sep 17 00:00:00 2001 From: Ryan Wolf Date: Mon, 3 Dec 2012 22:55:52 -0800 Subject: [PATCH 2/9] Added getConfigWindow and getProtocol to domHelper Can now distinguish between window fonts will load in and window webfontloaded loaded in. Having two windows meant that determining the protocol was no longer a oneliner, so moved into a function. --- src-test/ascender/ascender_script_test.js | 3 + src-test/core/domhelpertest.js | 37 +++++++++- src-test/fontdeck/fontdeck_script_test.js | 22 +++--- src-test/google/fontapiurlbuildertest.js | 10 +-- src-test/google/googlefontapitest.js | 12 ++++ src-test/monotype/monotype_script_test.js | 86 ++++++----------------- src-test/typekit/typekit_script_test.js | 12 ++++ src/ascender/ascender_script.js | 2 +- src/core/domhelper.js | 34 +++++++-- src/core/font.js | 2 +- src/fontdeck/fontdeck_script.js | 15 ++-- src/google/fontapiurlbuilder.js | 4 +- src/google/googlefontapi.js | 2 +- src/monotype/monotype_script.js | 28 ++------ src/typekit/typekit_script.js | 10 +-- 15 files changed, 155 insertions(+), 124 deletions(-) diff --git a/src-test/ascender/ascender_script_test.js b/src-test/ascender/ascender_script_test.js index b2974135..71f860f0 100644 --- a/src-test/ascender/ascender_script_test.js +++ b/src-test/ascender/ascender_script_test.js @@ -14,6 +14,9 @@ AscenderScriptTest.prototype.testLoadAndFamilyVariations = function(){ createCssLink: function(cssLink) { css = cssLink; return ''; + }, + getProtocol: function() { + return 'http:'; } }; diff --git a/src-test/core/domhelpertest.js b/src-test/core/domhelpertest.js index 34d5095b..1f3e2fcf 100644 --- a/src-test/core/domhelpertest.js +++ b/src-test/core/domhelpertest.js @@ -1,7 +1,7 @@ var DomHelperTest = TestCase('DomHelperTest'); DomHelperTest.prototype.setUp = function() { - this.domHelper_ = new webfont.DomHelper(document); + this.domHelper_ = new webfont.DomHelper(window, window); }; DomHelperTest.prototype.testCreateElementNoAttr = function() { @@ -106,3 +106,38 @@ DomHelperTest.prototype.testHasSupportForStyle = function() { this.domHelper_.supportForStyle_ = true; assertTrue(this.domHelper_.hasSupportForStyle_()); }; + +DomHelperTest.prototype.testGetProtocol = function() { + var fakeWindow = { + location: { + protocol: 'https:' + } + }; + var domHelper = new webfont.DomHelper(fakeWindow, fakeWindow); + assertEquals('https:', domHelper.getProtocol()); +}; + +DomHelperTest.prototype.testGetProtocolHttpDefault = function() { + var fakeWindow = { + location: { + protocol: 'file:' + } + }; + var domHelper = new webfont.DomHelper(fakeWindow, fakeWindow); + assertEquals('http:', domHelper.getProtocol()); +}; + +DomHelperTest.prototype.testGetProtocolIframeFallback = function() { + var fakeWindow = { + location: { + protocol: 'about:' + } + }; + var fakeConfigWindow = { + location: { + protocol: 'https:' + } + }; + var domHelper = new webfont.DomHelper(fakeWindow, fakeConfigWindow); + assertEquals('https:', domHelper.getProtocol()); +}; diff --git a/src-test/fontdeck/fontdeck_script_test.js b/src-test/fontdeck/fontdeck_script_test.js index 3501b3e2..fca333e1 100644 --- a/src-test/fontdeck/fontdeck_script_test.js +++ b/src-test/fontdeck/fontdeck_script_test.js @@ -41,17 +41,11 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { ] }; var insert = ''; - var src = ''; + // No hostname to verify fallback behavior for empty iframe. var global = { - location: { - protocol: 'https:' - }, - document: { - location: { - hostname: 'test-host-name' - } - } + location: {} }; + var src = ''; var fakeDomHelper = { insertInto: function(tag, e) { insert = tag; @@ -61,6 +55,16 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { }, getWindow: function() { return global; + }, + getConfigWindow: function() { + return { + location: { + hostname: 'test-host-name' + } + }; + }, + getProtocol: function() { + return 'https:'; } }; var fontdeck = new webfont.FontdeckScript(fakeDomHelper, configuration); diff --git a/src-test/google/fontapiurlbuildertest.js b/src-test/google/fontapiurlbuildertest.js index f117c819..2282b406 100644 --- a/src-test/google/fontapiurlbuildertest.js +++ b/src-test/google/fontapiurlbuildertest.js @@ -2,7 +2,7 @@ var FontApiUrlBuilderTest = TestCase('FontApiUrlBuilderTest'); FontApiUrlBuilderTest.prototype.testThrowsExceptionIfNoFontFamilies = function() { - var fontApiUrlBuilder = new webfont.FontApiUrlBuilder("http://moo"); + var fontApiUrlBuilder = new webfont.FontApiUrlBuilder("http://moo", "http:"); try { fontApiUrlBuilder.build(); @@ -13,14 +13,14 @@ FontApiUrlBuilderTest.prototype.testThrowsExceptionIfNoFontFamilies = }; FontApiUrlBuilderTest.prototype.testBuildProperUrl = function() { - var fontApiUrlBuilder = new webfont.FontApiUrlBuilder("http://moo"); + var fontApiUrlBuilder = new webfont.FontApiUrlBuilder("http://moo", "http:"); fontApiUrlBuilder.setFontFamilies([ 'Font1', 'Font2' ]); assertEquals('http://moo?family=Font1%7CFont2', fontApiUrlBuilder.build()); }; FontApiUrlBuilderTest.prototype.testBuildProperDefaultUrl = function() { - var fontApiUrlBuilder = new webfont.FontApiUrlBuilder(); + var fontApiUrlBuilder = new webfont.FontApiUrlBuilder(undefined, "http:"); fontApiUrlBuilder.setFontFamilies([ 'Font1', 'Font2' ]); assertEquals("http:" + webfont.FontApiUrlBuilder.DEFAULT_API_URL + @@ -29,7 +29,7 @@ FontApiUrlBuilderTest.prototype.testBuildProperDefaultUrl = function() { FontApiUrlBuilderTest.prototype.testBuildProperUrlWithSubsets = function() { - var fontApiUrlBuilder = new webfont.FontApiUrlBuilder(); + var fontApiUrlBuilder = new webfont.FontApiUrlBuilder(undefined, "http:"); fontApiUrlBuilder.setFontFamilies([ 'Font1:bold:greek,cyrillic', 'Font2:italic', 'Font3' ]); @@ -40,7 +40,7 @@ FontApiUrlBuilderTest.prototype.testBuildProperUrlWithSubsets = function() { FontApiUrlBuilderTest.prototype.testBuildProperUrlWithSubsetsNoVariations = function() { - var fontApiUrlBuilder = new webfont.FontApiUrlBuilder(); + var fontApiUrlBuilder = new webfont.FontApiUrlBuilder(undefined, "http:"); fontApiUrlBuilder.setFontFamilies([ 'Font1:bold,italic:greek,cyrillic', 'Font2:italic', 'Font3::latin' ]); diff --git a/src-test/google/googlefontapitest.js b/src-test/google/googlefontapitest.js index 1bf52e2e..0e5d8644 100644 --- a/src-test/google/googlefontapitest.js +++ b/src-test/google/googlefontapitest.js @@ -9,6 +9,9 @@ GoogleFontApiTest.prototype.testCallOnReadyWithFontFamilyLoading = function() { }, createCssLink: function(csslink) { link = csslink; + }, + getProtocol: function() { + return 'http:'; } }; var userAgent = new webfont.UserAgent("Test", "1.0", true); @@ -53,6 +56,9 @@ GoogleFontApiTest.prototype.testCallOnReadyWithFontFamilyLoadingApiUrlChanged = }, createCssLink: function(csslink) { link = csslink; + }, + getProtocol: function() { + return 'http:'; } }; var userAgent = new webfont.UserAgent("Test", "1.0", true); @@ -95,6 +101,9 @@ GoogleFontApiTest.prototype.testSpacesReplacedByPlus = function() { }, createCssLink: function(csslink) { link = csslink; + }, + getProtocol: function() { + return 'http:'; } }; var userAgent = new webfont.UserAgent("Test", "1.0", true); @@ -137,6 +146,9 @@ GoogleFontApiTest.prototype.testLoadWithVariations = function() { }, createCssLink: function(csslink) { link = csslink; + }, + getProtocol: function() { + return 'http:'; } }; var userAgent = new webfont.UserAgent("Test", "1.0", true); diff --git a/src-test/monotype/monotype_script_test.js b/src-test/monotype/monotype_script_test.js index fe6582be..234a5dc3 100644 --- a/src-test/monotype/monotype_script_test.js +++ b/src-test/monotype/monotype_script_test.js @@ -1,49 +1,7 @@ var MonotypeScriptTest = TestCase('MonotypeScriptTest'); -MonotypeScriptTest.prototype.testIfProtocolMethodIsReturningProperly = function () { - var fakeDocument = { - location: { protocol: "https:" } - }; - var script = null; - var userAgent = new webfont.UserAgent("Test", "1.0", true); - var config = { projectId: '01e2ff27-25bf-4801-a23e-73d328e6c7cc' }; - var fakedom = []; - var fakeDomHelper = { - createScriptSrc: function (s) { - script = { src: s }; - return script; - }, - insertInto: function (tag, elem) { - fakedom[tag].push(elem); - global[webfont.MonotypeScript.HOOK + config.projectId] = function () { - return ["aachen bold", "kid print regualr"]; - }; - if (script.onload) { - script.onload(); - } - }, - getWindow: function () { - // should be window in actual situation. - return {}; - } - }; - - var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); - assertEquals("https:", monotypeScript.protocol()); - - fakeDocument = { - location: { protocol: "http:" } - }; - - monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); - assertEquals("http:", monotypeScript.protocol()); -}; - MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { var fakedom = { 'head': [], 'body': [] }; - var fakeDocument = { - location: { protocol: "http:" } - }; var script = null; var global = {}; // should be window in actual situation. var families = null; @@ -65,6 +23,9 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { }, getWindow: function () { return global; + }, + getProtocol: function () { + return "http:"; } }; @@ -83,7 +44,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { } var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { families = fontFamilies; @@ -99,12 +60,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { //If current page is browsed using https protocol, the added script should be requested with SSL. MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { var fakedom = { 'head': [], 'body': [] }; - var fakeDocument1 = { - location: { protocol: "https:" } - }; - var fakeDocument2 = { - location: { protocol: "http:" } - }; + var fakeProtocol = "https:"; var script = null; var global = {}; // should be window in actual situation. var families = null; @@ -126,6 +82,9 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { }, getWindow: function () { return global; + }, + getProtocol: function () { + return fakeProtocol; } }; @@ -145,7 +104,7 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { } var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument1, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { families = fontFamilies; @@ -159,7 +118,8 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { //one page can have multiple projects, but not 2 projects with same projectId. config = { projectId: '01e2ff27-25bf-4801-a23e-73d328e6c7c1', api: "http://fast.fonts.com/jsapidev" }; - var monotypeScript2 = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument2, config); + fakeProtocol = "http:"; + var monotypeScript2 = new webfont.MonotypeScript(userAgent, fakeDomHelper, config); monotypeScript2.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript2.load(function (fontFamilies) { families = fontFamilies; @@ -194,11 +154,8 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { }, getWindow: function () { return global; - } - }; - - var fakeDocHelper = { - protocol: function () { + }, + getProtocol: function () { return "http:"; } }; @@ -219,7 +176,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocHelper, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { @@ -236,7 +193,6 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { //If current page is browsed using https protocol, the added script should be requested with SSL. MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlHasCorrectSSL = function () { var fakedom = { 'head': [], 'body': [] }; - var fakeDocument = { location: { protocol: "https:"} }; var global = {}; // should be window in actual situation. var script = null; var families = null; @@ -258,6 +214,9 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlH }, getWindow: function () { return global; + }, + getProtocol: function () { + return "https:"; } }; @@ -277,7 +236,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlH var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocument, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { @@ -315,18 +274,15 @@ MonotypeScriptTest.prototype.testWithoutProjectId = function () { }, getWindow: function () { return global; - } - }; - - var fakeDocHelper = { - protocol: function () { + }, + getProtocol: function () { return "http:"; } }; var isSupport = null; var userAgent = new webfont.UserAgent("Test", "1.0", true); - var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, fakeDocHelper, config); + var monotypeScript = new webfont.MonotypeScript(userAgent, fakeDomHelper, config); monotypeScript.supportUserAgent(userAgent, function (support) { isSupport = support; }); monotypeScript.load(function (fontFamilies) { diff --git a/src-test/typekit/typekit_script_test.js b/src-test/typekit/typekit_script_test.js index 38b4ced2..33dd6bc0 100644 --- a/src-test/typekit/typekit_script_test.js +++ b/src-test/typekit/typekit_script_test.js @@ -16,6 +16,9 @@ TypekitScriptTest.prototype.testSupportAndLoadLifecycle = function() { }, getWindow: function() { return global; + }, + getProtocol: function() { + return 'http:'; } }; var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); @@ -78,6 +81,9 @@ TypekitScriptTest.prototype.testLoadWithVariations = function() { }, getWindow: function() { return global; + }, + getProtocol: function() { + return 'http:'; } }; var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); @@ -138,6 +144,9 @@ TypekitScriptTest.prototype.testAlternateApi = function() { }, getWindow: function() { return {}; + }, + getProtocol: function() { + return 'http:'; } }; var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); @@ -164,6 +173,9 @@ TypekitScriptTest.prototype.testNoKitId = function() { }, getWindow: function() { return {}; + }, + getProtocol: function() { + return 'http:'; } }; var typeKit = new webfont.TypekitScript(fakeDomHelper, configuration); diff --git a/src/ascender/ascender_script.js b/src/ascender/ascender_script.js index bbc2a99b..121214e6 100644 --- a/src/ascender/ascender_script.js +++ b/src/ascender/ascender_script.js @@ -33,7 +33,7 @@ webfont.AscenderScript.prototype.supportUserAgent = function(userAgent, support) webfont.AscenderScript.prototype.load = function(onReady) { var key = this.configuration_['key']; - var protocol = (('https:' == document.location.protocol) ? 'https:' : 'http:'); + var protocol = this.domHelper_.getProtocol(); var url = protocol + '//webfonts.fontslive.com/css/' + key + '.css'; this.domHelper_.insertInto('head', this.domHelper_.createCssLink(url)); var fv = this.parseFamiliesAndVariations(this.configuration_['families']); diff --git a/src/core/domhelper.js b/src/core/domhelper.js index 399a95ff..c94aab75 100644 --- a/src/core/domhelper.js +++ b/src/core/domhelper.js @@ -1,11 +1,16 @@ /** * Handles common DOM manipulation tasks. The aim of this library is to cover * the needs of typical font loading. Not more, not less. - * @param {HTMLDocument} doc The HTML document we'll manipulate. + * @param {Window} window The window we'll load the font in. + * @param {Window} configWindow The window webfontloader loaded in. * @constructor */ -webfont.DomHelper = function(doc) { - this.document_ = doc; +webfont.DomHelper = function(window, configWindow) { + this.window_ = window; + this.configWindow_ = configWindow; + + /** @type {HTMLDocument} */ + this.document_ = this.window_.document; /** @type {boolean|undefined} */ this.supportForStyle_ = undefined; @@ -196,8 +201,27 @@ webfont.DomHelper.prototype.hasSupportForStyle_ = function() { }; /** - * @return {Window} The window for the helper's document. + * @return {Window} The window we'll load the font in. */ webfont.DomHelper.prototype.getWindow = function() { - return this.document_.defaultView || this.document_.parentWindow; + return this.window_; +}; + +/** + * @return {Window} The window webfontloader loaded in. + */ +webfont.DomHelper.prototype.getConfigWindow = function() { + return this.configWindow_; +}; + +/** + * @return {string} The protocol (http: or https:) to request resources in. + */ +webfont.DomHelper.prototype.getProtocol = function() { + var protocol = this.window_.location.protocol; + // For empty iframes, fallback to config window's protocol. + if (protocol == 'about:') { + protocol = this.configWindow_.location.protocol; + } + return protocol == 'https:' ? 'https:' : 'http:'; }; diff --git a/src/core/font.js b/src/core/font.js index eaa48771..7179360c 100644 --- a/src/core/font.js +++ b/src/core/font.js @@ -15,7 +15,7 @@ webfont.WebFont.prototype.addModule = function(name, factory) { webfont.WebFont.prototype.load = function(configuration, opt_context) { var context = opt_context || window; - this.domHelper_ = new webfont.DomHelper(context.document); + this.domHelper_ = new webfont.DomHelper(context.document, window); this.htmlElement_ = context.document.documentElement; var eventDispatcher = new webfont.EventDispatcher( diff --git a/src/fontdeck/fontdeck_script.js b/src/fontdeck/fontdeck_script.js index 52ade419..b779add2 100644 --- a/src/fontdeck/fontdeck_script.js +++ b/src/fontdeck/fontdeck_script.js @@ -2,7 +2,6 @@ * @constructor */ webfont.FontdeckScript = function(domHelper, configuration) { - this.global_ = domHelper.getWindow(); this.domHelper_ = domHelper; this.configuration_ = configuration; this.fontFamilies_ = []; @@ -15,24 +14,28 @@ webfont.FontdeckScript.HOOK = '__webfontfontdeckmodule__'; 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 protocol = this.domHelper_.getProtocol(); + // For empty iframes, fall back to config window's hostname. + var hostname = this.domHelper_.getWindow().location.hostname || + this.domHelper_.getConfigWindow().location.hostname; var api = this.configuration_['api'] || webfont.FontdeckScript.API; - return protocol + api + this.global_.document.location.hostname + '/' + projectId + '.js'; + return protocol + api + hostname + '/' + projectId + '.js'; }; webfont.FontdeckScript.prototype.supportUserAgent = function(userAgent, support) { var projectId = this.configuration_['id']; + var window = this.domHelper_.getWindow(); var self = this; if (projectId) { // Provide data to Fontdeck for processing. - if (!this.global_[webfont.FontdeckScript.HOOK]) { - this.global_[webfont.FontdeckScript.HOOK] = {}; + if (!window[webfont.FontdeckScript.HOOK]) { + window[webfont.FontdeckScript.HOOK] = {}; } // Fontdeck will call this function to indicate support status // and what fonts are provided. - this.global_[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) { + window[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) { for (var i = 0, j = data['fonts'].length; i Date: Tue, 4 Dec 2012 22:36:45 -0800 Subject: [PATCH 3/9] Fix type annotation to get rid of warning --- src/core/domhelper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/domhelper.js b/src/core/domhelper.js index c94aab75..e260f946 100644 --- a/src/core/domhelper.js +++ b/src/core/domhelper.js @@ -9,7 +9,7 @@ webfont.DomHelper = function(window, configWindow) { this.window_ = window; this.configWindow_ = configWindow; - /** @type {HTMLDocument} */ + /** @type {Document} */ this.document_ = this.window_.document; /** @type {boolean|undefined} */ From beb6d918c78e785a8ebfcb0ec839c6d9ae568ee8 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Tue, 4 Dec 2012 23:12:54 -0800 Subject: [PATCH 4/9] Rename window methods to "main" and "load" + fix tests --- src-test/core/domhelpertest.js | 24 +++++++++++------ src-test/core/eventdispatchertest.js | 2 +- src-test/fontdeck/fontdeck_script_test.js | 24 ++++++++--------- src-test/monotype/monotype_script_test.js | 10 +++---- src-test/typekit/typekit_script_test.js | 8 +++--- src/core/domhelper.js | 33 ++++++++++++----------- src/core/font.js | 2 +- src/fontdeck/fontdeck_script.js | 14 +++++----- src/monotype/monotype_script.js | 8 +++--- src/typekit/typekit_script.js | 8 +++--- 10 files changed, 71 insertions(+), 62 deletions(-) diff --git a/src-test/core/domhelpertest.js b/src-test/core/domhelpertest.js index 1f3e2fcf..356b86b2 100644 --- a/src-test/core/domhelpertest.js +++ b/src-test/core/domhelpertest.js @@ -1,7 +1,7 @@ var DomHelperTest = TestCase('DomHelperTest'); DomHelperTest.prototype.setUp = function() { - this.domHelper_ = new webfont.DomHelper(window, window); + this.domHelper_ = new webfont.DomHelper(window); }; DomHelperTest.prototype.testCreateElementNoAttr = function() { @@ -107,13 +107,21 @@ DomHelperTest.prototype.testHasSupportForStyle = function() { assertTrue(this.domHelper_.hasSupportForStyle_()); }; +DomHelperTest.prototype.testGetWindows = function() { + var fakeMainWindow = 'main window'; + var fakeLoadWindow = 'load window'; + var domHelper = new webfont.DomHelper(fakeMainWindow, fakeLoadWindow); + assertEquals('main window', domHelper.getMainWindow()); + assertEquals('load window', domHelper.getLoadWindow()); +}; + DomHelperTest.prototype.testGetProtocol = function() { var fakeWindow = { location: { protocol: 'https:' } }; - var domHelper = new webfont.DomHelper(fakeWindow, fakeWindow); + var domHelper = new webfont.DomHelper(fakeWindow); assertEquals('https:', domHelper.getProtocol()); }; @@ -123,21 +131,21 @@ DomHelperTest.prototype.testGetProtocolHttpDefault = function() { protocol: 'file:' } }; - var domHelper = new webfont.DomHelper(fakeWindow, fakeWindow); + var domHelper = new webfont.DomHelper(fakeWindow); assertEquals('http:', domHelper.getProtocol()); }; DomHelperTest.prototype.testGetProtocolIframeFallback = function() { - var fakeWindow = { + var fakeMainWindow = { location: { - protocol: 'about:' + protocol: 'https:' } }; - var fakeConfigWindow = { + var fakeLoadWindow = { location: { - protocol: 'https:' + protocol: 'about:' } }; - var domHelper = new webfont.DomHelper(fakeWindow, fakeConfigWindow); + var domHelper = new webfont.DomHelper(fakeMainWindow, fakeLoadWindow); assertEquals('https:', domHelper.getProtocol()); }; diff --git a/src-test/core/eventdispatchertest.js b/src-test/core/eventdispatchertest.js index cdd3e6c7..096d83fd 100644 --- a/src-test/core/eventdispatchertest.js +++ b/src-test/core/eventdispatchertest.js @@ -15,7 +15,7 @@ EventDispatcherTest.prototype.setUp = function() { var self = this; this.eventDispatcher_ = new webfont.EventDispatcher(new webfont.DomHelper( - document), this.fakeHtmlElement_, { + window), this.fakeHtmlElement_, { loading: function() { self.loadingEventCalled_ = true; }, diff --git a/src-test/fontdeck/fontdeck_script_test.js b/src-test/fontdeck/fontdeck_script_test.js index fca333e1..8465d662 100644 --- a/src-test/fontdeck/fontdeck_script_test.js +++ b/src-test/fontdeck/fontdeck_script_test.js @@ -41,9 +41,10 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { ] }; var insert = ''; - // No hostname to verify fallback behavior for empty iframe. var global = { - location: {} + location: { + hostname: 'test-host-name' + } }; var src = ''; var fakeDomHelper = { @@ -53,16 +54,15 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getWindow: function() { - return global; - }, - getConfigWindow: function() { + getLoadWindow: function() { + // No hostname to verify fallback behavior for empty iframe return { - location: { - hostname: 'test-host-name' - } + location: {} }; }, + getMainWindow: function() { + return global; + }, getProtocol: function() { return 'https:'; } @@ -87,7 +87,7 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { 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); }; @@ -98,7 +98,7 @@ FontdeckScriptTest.prototype.testNoProjectId = function() { var insert = ''; var src = ''; var fakeDomHelper = { - getWindow: function() { + getMainWindow: function() { return {}; } }; @@ -109,7 +109,7 @@ FontdeckScriptTest.prototype.testNoProjectId = function() { var isSupport = null; fontdeck.supportUserAgent(userAgent, function(support) { isSupport = support; }); - + assertEquals(fontdeck.fontFamilies_, []); assertEquals(fontdeck.fontVariations_, []); assertEquals(true, isSupport); diff --git a/src-test/monotype/monotype_script_test.js b/src-test/monotype/monotype_script_test.js index 234a5dc3..e6e36d27 100644 --- a/src-test/monotype/monotype_script_test.js +++ b/src-test/monotype/monotype_script_test.js @@ -21,7 +21,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { script.onload(); } }, - getWindow: function () { + getMainWindow: function () { return global; }, getProtocol: function () { @@ -80,7 +80,7 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { script.onload(); } }, - getWindow: function () { + getMainWindow: function () { return global; }, getProtocol: function () { @@ -152,7 +152,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { script.onload(); } }, - getWindow: function () { + getMainWindow: function () { return global; }, getProtocol: function () { @@ -212,7 +212,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlH script.onload(); } }, - getWindow: function () { + getMainWindow: function () { return global; }, getProtocol: function () { @@ -272,7 +272,7 @@ MonotypeScriptTest.prototype.testWithoutProjectId = function () { script.onload(); } }, - getWindow: function () { + getMainWindow: function () { return global; }, getProtocol: function () { diff --git a/src-test/typekit/typekit_script_test.js b/src-test/typekit/typekit_script_test.js index 33dd6bc0..9d80dd1e 100644 --- a/src-test/typekit/typekit_script_test.js +++ b/src-test/typekit/typekit_script_test.js @@ -14,7 +14,7 @@ TypekitScriptTest.prototype.testSupportAndLoadLifecycle = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getWindow: function() { + getMainWindow: function() { return global; }, getProtocol: function() { @@ -79,7 +79,7 @@ TypekitScriptTest.prototype.testLoadWithVariations = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getWindow: function() { + getMainWindow: function() { return global; }, getProtocol: function() { @@ -142,7 +142,7 @@ TypekitScriptTest.prototype.testAlternateApi = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getWindow: function() { + getMainWindow: function() { return {}; }, getProtocol: function() { @@ -171,7 +171,7 @@ TypekitScriptTest.prototype.testNoKitId = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getWindow: function() { + getMainWindow: function() { return {}; }, getProtocol: function() { diff --git a/src/core/domhelper.js b/src/core/domhelper.js index e260f946..da0eaec0 100644 --- a/src/core/domhelper.js +++ b/src/core/domhelper.js @@ -1,16 +1,17 @@ /** * Handles common DOM manipulation tasks. The aim of this library is to cover * the needs of typical font loading. Not more, not less. - * @param {Window} window The window we'll load the font in. - * @param {Window} configWindow The window webfontloader loaded in. + * @param {Window} mainWindow The main window webfontloader.js is loaded in. + * @param {Window=} opt_loadWindow The window we'll load the font into. By + * default, the main window is used. * @constructor */ -webfont.DomHelper = function(window, configWindow) { - this.window_ = window; - this.configWindow_ = configWindow; +webfont.DomHelper = function(mainWindow, opt_loadWindow) { + this.mainWindow_ = mainWindow; + this.loadWindow_ = opt_loadWindow || mainWindow; /** @type {Document} */ - this.document_ = this.window_.document; + this.document_ = this.loadWindow_.document; /** @type {boolean|undefined} */ this.supportForStyle_ = undefined; @@ -33,7 +34,7 @@ webfont.DomHelper.prototype.createElement = function(elem, opt_attr, if (opt_attr.hasOwnProperty(attr)) { if (attr == "style") { this.setStyle(domElement, opt_attr[attr]); - } else { + } else { domElement.setAttribute(attr, opt_attr[attr]); } } @@ -201,27 +202,27 @@ webfont.DomHelper.prototype.hasSupportForStyle_ = function() { }; /** - * @return {Window} The window we'll load the font in. + * @return {Window} The main window webfontloader.js is loaded in (for config). */ -webfont.DomHelper.prototype.getWindow = function() { - return this.window_; +webfont.DomHelper.prototype.getMainWindow = function() { + return this.mainWindow_; }; /** - * @return {Window} The window webfontloader loaded in. + * @return {Window} The window that we're loading the font(s) into. */ -webfont.DomHelper.prototype.getConfigWindow = function() { - return this.configWindow_; +webfont.DomHelper.prototype.getLoadWindow = function() { + return this.loadWindow_; }; /** * @return {string} The protocol (http: or https:) to request resources in. */ webfont.DomHelper.prototype.getProtocol = function() { - var protocol = this.window_.location.protocol; - // For empty iframes, fallback to config window's protocol. + var protocol = this.loadWindow_.location.protocol; + // For empty iframes, fallback to main window's protocol. if (protocol == 'about:') { - protocol = this.configWindow_.location.protocol; + protocol = this.mainWindow_.location.protocol; } return protocol == 'https:' ? 'https:' : 'http:'; }; diff --git a/src/core/font.js b/src/core/font.js index 7179360c..f4e3f6e3 100644 --- a/src/core/font.js +++ b/src/core/font.js @@ -15,7 +15,7 @@ webfont.WebFont.prototype.addModule = function(name, factory) { webfont.WebFont.prototype.load = function(configuration, opt_context) { var context = opt_context || window; - this.domHelper_ = new webfont.DomHelper(context.document, window); + this.domHelper_ = new webfont.DomHelper(window, context); this.htmlElement_ = context.document.documentElement; var eventDispatcher = new webfont.EventDispatcher( diff --git a/src/fontdeck/fontdeck_script.js b/src/fontdeck/fontdeck_script.js index b779add2..9621233f 100644 --- a/src/fontdeck/fontdeck_script.js +++ b/src/fontdeck/fontdeck_script.js @@ -15,27 +15,27 @@ webfont.FontdeckScript.API = '//f.fontdeck.com/s/css/js/'; webfont.FontdeckScript.prototype.getScriptSrc = function(projectId) { var protocol = this.domHelper_.getProtocol(); - // For empty iframes, fall back to config window's hostname. - var hostname = this.domHelper_.getWindow().location.hostname || - this.domHelper_.getConfigWindow().location.hostname; + // For empty iframes, fall back to main window's hostname. + var hostname = this.domHelper_.getLoadWindow().location.hostname || + this.domHelper_.getMainWindow().location.hostname; var api = this.configuration_['api'] || webfont.FontdeckScript.API; return protocol + api + hostname + '/' + projectId + '.js'; }; webfont.FontdeckScript.prototype.supportUserAgent = function(userAgent, support) { var projectId = this.configuration_['id']; - var window = this.domHelper_.getWindow(); + var mainWindow = this.domHelper_.getMainWindow(); var self = this; if (projectId) { // Provide data to Fontdeck for processing. - if (!window[webfont.FontdeckScript.HOOK]) { - window[webfont.FontdeckScript.HOOK] = {}; + if (!mainWindow[webfont.FontdeckScript.HOOK]) { + mainWindow[webfont.FontdeckScript.HOOK] = {}; } // Fontdeck will call this function to indicate support status // and what fonts are provided. - window[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) { + mainWindow[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) { for (var i = 0, j = data['fonts'].length; i Date: Wed, 5 Dec 2012 00:00:14 -0800 Subject: [PATCH 5/9] Add test for using custom context with web font loader --- src-test/core/fonttest.js | 65 ++++++++++++++++++++++++++++++--------- src/core/font.js | 15 ++++++--- src/core/initialize.js | 3 +- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src-test/core/fonttest.js b/src-test/core/fonttest.js index 9683c707..99333713 100644 --- a/src-test/core/fonttest.js +++ b/src-test/core/fonttest.js @@ -7,32 +7,32 @@ FontTest.prototype.setUp = function() { FontTest.prototype.testFontLoad = function() { var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, true); - var font = new webfont.WebFont(this.fontModuleLoader_, + var font = new webfont.WebFont(window, this.fontModuleLoader_, function(func, timeout) { func(); }, userAgent); - var self = this; var testModule = null; - font.addModule('test', function(conf) { + font.addModule('test', function(conf, domHelper) { testModule = new function() { this.conf = conf; + this.domHelper = domHelper; this.loadCalled = false; this.supportUserAgentCalled = false; - }; - testModule.load = function(onReady) { - this.loadCalled = true; - onReady([]); }; - testModule.supportUserAgent = function(ua, support) { - this.supportUserAgentCalled = true; - support(true); - }; - return testModule; + testModule.load = function(onReady) { + this.loadCalled = true; + onReady([]); + }; + testModule.supportUserAgent = function(ua, support) { + this.supportUserAgentCalled = true; + support(true); + }; + return testModule; }); - var loadingEventCalled = false; assertEquals(0, font.moduleFailedLoading_); assertEquals(0, font.moduleLoading_); + var loadingEventCalled = false; font.load({ test: { somedata: 'in french a cow says meuh' @@ -47,17 +47,52 @@ FontTest.prototype.testFontLoad = function() { assertNotNull(testModule); assertNotUndefined(testModule.conf); assertNotNull(testModule.conf); + assertNotUndefined(testModule.domHelper); + assertNotNull(testModule.domHelper); + assertSame(window, testModule.domHelper.getMainWindow()); + assertSame(window, testModule.domHelper.getLoadWindow()); assertEquals('in french a cow says meuh', testModule.conf.somedata); assertTrue(testModule.loadCalled); assertTrue(testModule.supportUserAgentCalled); assertTrue(loadingEventCalled); }; +FontTest.prototype.testFontLoadWithContext = function() { + var fakeMainWindow = {}; + + var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', + 'Macintosh', '10.6', undefined, true); + var font = new webfont.WebFont(fakeMainWindow, this.fontModuleLoader_, + function(func, timeout) { func(); }, userAgent); + var testModule = null; + + font.addModule('test', function(conf, domHelper) { + testModule = new function() { + this.domHelper = domHelper; + }; + testModule.load = function() {}; + testModule.supportUserAgent = function(ua, support) { + support(true); + }; + return testModule; + }); + + font.load({ + test: { + somedata: 'in french a cow says meuh' + } + }, window); + + assertNotUndefined(testModule.domHelper); + assertNotNull(testModule.domHelper); + assertSame(fakeMainWindow, testModule.domHelper.getMainWindow()); + assertSame(window, testModule.domHelper.getLoadWindow()); +}; + FontTest.prototype.testFontInactive = function() { var userAgent = new webfont.UserAgent('Firefox', '3.0', false); - var font = new webfont.WebFont(this.fontModuleLoader_, + var font = new webfont.WebFont(window, this.fontModuleLoader_, function(func, timeout) { func(); }, userAgent); - var self = this; var testModule; font.addModule('test', function(conf) { diff --git a/src/core/font.js b/src/core/font.js index f4e3f6e3..172a2dbb 100644 --- a/src/core/font.js +++ b/src/core/font.js @@ -1,7 +1,13 @@ /** + * @param {Window} mainWindow The main application window containing + * webfontloader.js. + * @param {webfont.FontModuleLoader} fontModuleLoader A loader instance to use. + * @param {function(function(), number=)} asyncCall An async function to use. + * @param {webfont.UserAgent} userAgent The detected user agent to load for. * @constructor */ -webfont.WebFont = function(fontModuleLoader, asyncCall, userAgent) { +webfont.WebFont = function(mainWindow, fontModuleLoader, asyncCall, userAgent) { + this.mainWindow_ = mainWindow; this.fontModuleLoader_ = fontModuleLoader; this.asyncCall_ = asyncCall; this.userAgent_ = userAgent; @@ -14,12 +20,11 @@ webfont.WebFont.prototype.addModule = function(name, factory) { }; webfont.WebFont.prototype.load = function(configuration, opt_context) { - var context = opt_context || window; - this.domHelper_ = new webfont.DomHelper(window, context); - this.htmlElement_ = context.document.documentElement; + var context = opt_context || this.mainWindow_; + this.domHelper_ = new webfont.DomHelper(this.mainWindow_, context); var eventDispatcher = new webfont.EventDispatcher( - this.domHelper_, this.htmlElement_, configuration); + this.domHelper_, context.document.documentElement, configuration); if (this.userAgent_.isSupportingWebFont()) { this.load_(eventDispatcher, configuration); diff --git a/src/core/initialize.js b/src/core/initialize.js index 21d794e5..b362cfee 100644 --- a/src/core/initialize.js +++ b/src/core/initialize.js @@ -5,9 +5,10 @@ var globalName = 'WebFont'; var globalNamespaceObject = window[globalName] = (function() { var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document); var userAgent = userAgentParser.parse(); + var fontModuleLoader = new webfont.FontModuleLoader(); var asyncCall = function(func, timeout) { setTimeout(func, timeout); }; - return new webfont.WebFont(new webfont.FontModuleLoader(), asyncCall, userAgent); + return new webfont.WebFont(window, fontModuleLoader, asyncCall, userAgent); })(); // Export the public API. From e158272db0d23e283ac823099b83aabeb94cd503 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 5 Dec 2012 00:04:51 -0800 Subject: [PATCH 6/9] Use a "context" config option instead of a second argument --- src-test/core/fonttest.js | 5 +++-- src/core/font.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src-test/core/fonttest.js b/src-test/core/fonttest.js index 99333713..d304660c 100644 --- a/src-test/core/fonttest.js +++ b/src-test/core/fonttest.js @@ -80,8 +80,9 @@ FontTest.prototype.testFontLoadWithContext = function() { font.load({ test: { somedata: 'in french a cow says meuh' - } - }, window); + }, + context: window + }); assertNotUndefined(testModule.domHelper); assertNotNull(testModule.domHelper); diff --git a/src/core/font.js b/src/core/font.js index 172a2dbb..526a7a52 100644 --- a/src/core/font.js +++ b/src/core/font.js @@ -19,8 +19,8 @@ webfont.WebFont.prototype.addModule = function(name, factory) { this.fontModuleLoader_.addModuleFactory(name, factory); }; -webfont.WebFont.prototype.load = function(configuration, opt_context) { - var context = opt_context || this.mainWindow_; +webfont.WebFont.prototype.load = function(configuration) { + var context = configuration['context'] || this.mainWindow_; this.domHelper_ = new webfont.DomHelper(this.mainWindow_, context); var eventDispatcher = new webfont.EventDispatcher( From 16ea85ba19adfd38099567a1fd8b1e76fd638e1a Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 5 Dec 2012 15:24:00 -0800 Subject: [PATCH 7/9] Modules with hooks needs them to be set in the window where fonts are loading This was a misunderstanding of how hooks work on my part. Whoops! @ryanwolf was right the first time. --- src-test/fontdeck/fontdeck_script_test.js | 18 +++++++++--------- src-test/monotype/monotype_script_test.js | 10 +++++----- src-test/typekit/typekit_script_test.js | 8 ++++---- src/fontdeck/fontdeck_script.js | 8 ++++---- src/monotype/monotype_script.js | 6 +++--- src/typekit/typekit_script.js | 10 +++++----- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src-test/fontdeck/fontdeck_script_test.js b/src-test/fontdeck/fontdeck_script_test.js index 8465d662..a631084e 100644 --- a/src-test/fontdeck/fontdeck_script_test.js +++ b/src-test/fontdeck/fontdeck_script_test.js @@ -42,9 +42,8 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { }; var insert = ''; var global = { - location: { - hostname: 'test-host-name' - } + // No hostname to verify fallback behavior for empty iframe + location: {} }; var src = ''; var fakeDomHelper = { @@ -55,13 +54,14 @@ FontdeckScriptTest.prototype.testSupportAndLoadLifecycle = function() { src = srcLink; }, getLoadWindow: function() { - // No hostname to verify fallback behavior for empty iframe - return { - location: {} - }; + return global; }, getMainWindow: function() { - return global; + return { + location: { + hostname: 'test-host-name' + } + }; }, getProtocol: function() { return 'https:'; @@ -98,7 +98,7 @@ FontdeckScriptTest.prototype.testNoProjectId = function() { var insert = ''; var src = ''; var fakeDomHelper = { - getMainWindow: function() { + getLoadWindow: function() { return {}; } }; diff --git a/src-test/monotype/monotype_script_test.js b/src-test/monotype/monotype_script_test.js index e6e36d27..c0ae8ea3 100644 --- a/src-test/monotype/monotype_script_test.js +++ b/src-test/monotype/monotype_script_test.js @@ -21,7 +21,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAdded = function () { script.onload(); } }, - getMainWindow: function () { + getLoadWindow: function () { return global; }, getProtocol: function () { @@ -80,7 +80,7 @@ MonotypeScriptTest.prototype.testIfScriptTagHasCorrectSSL = function () { script.onload(); } }, - getMainWindow: function () { + getLoadWindow: function () { return global; }, getProtocol: function () { @@ -152,7 +152,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurl = function () { script.onload(); } }, - getMainWindow: function () { + getLoadWindow: function () { return global; }, getProtocol: function () { @@ -212,7 +212,7 @@ MonotypeScriptTest.prototype.testIfScriptTagIsAddedWithoutApiurlAndTheScriptUrlH script.onload(); } }, - getMainWindow: function () { + getLoadWindow: function () { return global; }, getProtocol: function () { @@ -272,7 +272,7 @@ MonotypeScriptTest.prototype.testWithoutProjectId = function () { script.onload(); } }, - getMainWindow: function () { + getLoadWindow: function () { return global; }, getProtocol: function () { diff --git a/src-test/typekit/typekit_script_test.js b/src-test/typekit/typekit_script_test.js index 9d80dd1e..b4fd21cd 100644 --- a/src-test/typekit/typekit_script_test.js +++ b/src-test/typekit/typekit_script_test.js @@ -14,7 +14,7 @@ TypekitScriptTest.prototype.testSupportAndLoadLifecycle = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getMainWindow: function() { + getLoadWindow: function() { return global; }, getProtocol: function() { @@ -79,7 +79,7 @@ TypekitScriptTest.prototype.testLoadWithVariations = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getMainWindow: function() { + getLoadWindow: function() { return global; }, getProtocol: function() { @@ -142,7 +142,7 @@ TypekitScriptTest.prototype.testAlternateApi = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getMainWindow: function() { + getLoadWindow: function() { return {}; }, getProtocol: function() { @@ -171,7 +171,7 @@ TypekitScriptTest.prototype.testNoKitId = function() { createScriptSrc: function(srcLink) { src = srcLink; }, - getMainWindow: function() { + getLoadWindow: function() { return {}; }, getProtocol: function() { diff --git a/src/fontdeck/fontdeck_script.js b/src/fontdeck/fontdeck_script.js index 9621233f..222be6b8 100644 --- a/src/fontdeck/fontdeck_script.js +++ b/src/fontdeck/fontdeck_script.js @@ -24,18 +24,18 @@ webfont.FontdeckScript.prototype.getScriptSrc = function(projectId) { webfont.FontdeckScript.prototype.supportUserAgent = function(userAgent, support) { var projectId = this.configuration_['id']; - var mainWindow = this.domHelper_.getMainWindow(); + var loadWindow = this.domHelper_.getLoadWindow(); var self = this; if (projectId) { // Provide data to Fontdeck for processing. - if (!mainWindow[webfont.FontdeckScript.HOOK]) { - mainWindow[webfont.FontdeckScript.HOOK] = {}; + if (!loadWindow[webfont.FontdeckScript.HOOK]) { + loadWindow[webfont.FontdeckScript.HOOK] = {}; } // Fontdeck will call this function to indicate support status // and what fonts are provided. - mainWindow[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) { + loadWindow[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) { for (var i = 0, j = data['fonts'].length; i Date: Wed, 5 Dec 2012 16:21:16 -0800 Subject: [PATCH 8/9] Fix demos and add demos for iframe loading demonstrations Adds demos for loading in iframes so we can ensure it works for all the modules. --- .../demo/public/ascender-iframe.html | 46 +++++++++++++++++++ lib/webfontloader/demo/public/blank.html | 9 ++++ .../demo/public/custom-iframe.html | 41 +++++++++++++++++ lib/webfontloader/demo/public/custom.html | 20 ++++---- .../demo/public/google-iframe.html | 40 ++++++++++++++++ lib/webfontloader/demo/public/index.html | 17 ++++++- .../demo/public/monotype-iframe.html | 46 +++++++++++++++++++ .../demo/public/typekit-iframe.html | 41 +++++++++++++++++ lib/webfontloader/demo/public/typekit.html | 16 +++---- 9 files changed, 255 insertions(+), 21 deletions(-) create mode 100644 lib/webfontloader/demo/public/ascender-iframe.html create mode 100644 lib/webfontloader/demo/public/blank.html create mode 100644 lib/webfontloader/demo/public/custom-iframe.html create mode 100644 lib/webfontloader/demo/public/google-iframe.html create mode 100644 lib/webfontloader/demo/public/monotype-iframe.html create mode 100644 lib/webfontloader/demo/public/typekit-iframe.html diff --git a/lib/webfontloader/demo/public/ascender-iframe.html b/lib/webfontloader/demo/public/ascender-iframe.html new file mode 100644 index 00000000..3146ee1c --- /dev/null +++ b/lib/webfontloader/demo/public/ascender-iframe.html @@ -0,0 +1,46 @@ + + + + Ascender Module Demo + + + + + + + + + +
+ +

+ Hide Page | + Reload Cached +

+

+ Note: use "localhost" when testing web fonts. +

+

+ The goal of this page is to show how fonts load from Ascender in a child + iframe. +

+ + + diff --git a/lib/webfontloader/demo/public/blank.html b/lib/webfontloader/demo/public/blank.html new file mode 100644 index 00000000..0923a947 --- /dev/null +++ b/lib/webfontloader/demo/public/blank.html @@ -0,0 +1,9 @@ + + + + + Blank page + + + + diff --git a/lib/webfontloader/demo/public/custom-iframe.html b/lib/webfontloader/demo/public/custom-iframe.html new file mode 100644 index 00000000..f5795e19 --- /dev/null +++ b/lib/webfontloader/demo/public/custom-iframe.html @@ -0,0 +1,41 @@ + + + + Custom Module + + + + + + + + +
+

+ Hide Page | + Reload Cached +

+

+ The goal of this page is to show how fonts load from a custom module in a + child iframe. +

+ + + diff --git a/lib/webfontloader/demo/public/custom.html b/lib/webfontloader/demo/public/custom.html index 5e21ff79..1cd28139 100644 --- a/lib/webfontloader/demo/public/custom.html +++ b/lib/webfontloader/demo/public/custom.html @@ -6,7 +6,7 @@ + + + + + + +
+

+ Hide Page | + Reload Cached +

+

+ The goal of this page is demonstrate fonts loading from Google via + Javascript into a child iframe. +

+ + diff --git a/lib/webfontloader/demo/public/index.html b/lib/webfontloader/demo/public/index.html index ed469a3a..39fa71c7 100644 --- a/lib/webfontloader/demo/public/index.html +++ b/lib/webfontloader/demo/public/index.html @@ -16,7 +16,7 @@

WebFont Loader Demos

Modules

- WebFont Loader provides modules to load fonts from many places. + WebFont Loader provides modules to load fonts from many places.

  1. Google / CSS Link: Load fonts from Google with a link tag. Consider this a base case for font loading.
  2. @@ -25,7 +25,20 @@

    Modules

  3. Custom / WebFont Loader: Load fonts from your own CSS with WebFont Loader.
  4. Ascender / WebFont Loader: Load fonts from Ascender with WebFont Loader.
  5. Fontdeck / WebFont Loader: Load fonts from Fontdeck with WebFont Loader.
  6. -
  7. monotype / WebFont Loader: Load fonts from webfonts.fonts.com with WebFont Loader.
  8. +
  9. Monotype / WebFont Loader: Load fonts from webfonts.fonts.com with WebFont Loader.
  10. +
+ +

Modules in Iframes

+

+ WebFont Loader provides the ability to load fonts in child iframes using modules, instead of the main window. +

+
    +
  1. Google / WebFont Loader: Load fonts from Google in a child iframe with WebFont Loader.
  2. +
  3. Typekit / WebFont Loader: Load fonts from Typekit in a child iframe with WebFont Loader.
  4. +
  5. Custom / WebFont Loader: Load fonts from your own CSS in a child iframe with WebFont Loader.
  6. +
  7. Ascender / WebFont Loader: Load fonts from Ascender in a child iframe with WebFont Loader.
  8. +
  9. Fontdeck / WebFont Loader: Their demo fonts seem to be broken at the moment, so we don't have an iframe demo of this module.
  10. +
  11. Monotype / WebFont Loader: Load fonts from webfonts.fonts.com in a child iframe with WebFont Loader.

Events

diff --git a/lib/webfontloader/demo/public/monotype-iframe.html b/lib/webfontloader/demo/public/monotype-iframe.html new file mode 100644 index 00000000..cbc9dfd9 --- /dev/null +++ b/lib/webfontloader/demo/public/monotype-iframe.html @@ -0,0 +1,46 @@ + + + + + + + + + + + + +
+ +

+ + Hide Page | + + Reload Cached + +

+ +

+ The goal of this page is to show how monotype fonts load in a child iframe. +

+ +

+ You must use "localhost" when testing monotype fonts. +

+ + diff --git a/lib/webfontloader/demo/public/typekit-iframe.html b/lib/webfontloader/demo/public/typekit-iframe.html new file mode 100644 index 00000000..26c942d9 --- /dev/null +++ b/lib/webfontloader/demo/public/typekit-iframe.html @@ -0,0 +1,41 @@ + + + + + + + + + + + + +
+

+ Hide Page | + Reload Cached +

+

+ The goal of this page is to show how Typekit fonts load into an iframe. +

+

+ You must load the fonts on "localhost" for this demo to work. +

+ + diff --git a/lib/webfontloader/demo/public/typekit.html b/lib/webfontloader/demo/public/typekit.html index be80d037..153a9418 100644 --- a/lib/webfontloader/demo/public/typekit.html +++ b/lib/webfontloader/demo/public/typekit.html @@ -6,8 +6,7 @@ @@ -16,15 +15,14 @@ h1 { visibility: hidden; } - .wf-arialblack-n4-active h1 { - font-family: 'Arial Black'; + .wf-futurapt-n7-active h1 { visibility: visible; }

- Hello World. I am Arial Black. + Hello World. I am Futura PT.


@@ -32,10 +30,10 @@

Reload Cached

- The goal of this page is to show how Typekit fonts load. Note that it uses - a minimal Typekit script in order to reduce dependencies. This script - simply provides the system font 'Arial Black' instead of loading a web - font. + The goal of this page is to show how Typekit fonts load. +

+

+ You must load this page on "localhost" in order for the fonts to load.

From 87e88c6de2a91a2535ecab609313b4e1673f68bc Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 5 Dec 2012 16:31:37 -0800 Subject: [PATCH 9/9] Update the readme with info about this new context configuration option --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4ac15cc2..a61fd7a2 100644 --- a/README.md +++ b/README.md @@ -67,12 +67,29 @@ this. Learn more about [events][evt]. +## Manage loading everywhere + +Usually, it's easiest to include a copy of webfontloader in every window where +fonts are needed, so that each window manages its own fonts. However, if you +need to have a single window manage fonts for multiple same-origin child windows +or iframes that are built up using JavaScript, webfontloader supports that as +well. Just use the optional `context` configuration option and give it a +reference to the target window for loading: + + ## A common ground WebFont Loader aims to provide a common interface for font loading. Today it -works with Google, Typekit, Fonts.com Web fonts and your own CSS. Learn more in -[transitions][trn]. +works with Google, Typekit, Ascender, Fontdeck, Fonts.com Web fonts and your own +CSS. Learn more in [transitions][trn]. ## More to see @@ -125,11 +142,11 @@ Then, run the tests. * That's it! - ## Authors * Ryan Carver / ryan@typekit.com * Jeremie Lenfant-engelmann / jeremiele@google.com +* Sean McBride / sean@typekit.com ## License