Permalink
Browse files

Remove UserAgent from DomHelper, replace with feature detection

UserAgent was required to construct a DomHelper, but the only thing it was
used for is in setStyle to determine if we're in an old IE and need to use a
different approach. I was using DomHelper in some code and finding it cumbersome
to have a UserAgent instance around as well. This goal is better accomplished
with a feature-detection approach anyway.

I've replaced the UserAgent arg with feature detection for getting/setting the
style attribute. This is a very similar approach to the one that jQuery uses to
detect the same feature.

This has the added side-effect of allowing many of the modules to no longer
construct a UserAgent instance using the parser, since they only used it for the
DomHelper, which simplifies things.
  • Loading branch information...
1 parent c2d39d8 commit f31011c798723bb50e2279511963a186ae5e86a1 Sean McBride committed Apr 18, 2012
@@ -1,8 +1,7 @@
var DomHelperTest = TestCase('DomHelperTest');
DomHelperTest.prototype.setUp = function() {
- this.domHelper_ = new webfont.DomHelper(document, new webfont.UserAgent("name", "version",
- "engine", "engineVersion", "platform", "platformVersion", true));
+ this.domHelper_ = new webfont.DomHelper(document);
};
DomHelperTest.prototype.testCreateElementNoAttr = function() {
@@ -90,4 +89,11 @@ DomHelperTest.prototype.testHasClassName = function() {
assertTrue(this.domHelper_.hasClassName(div, 'moo-meuh'));
assertFalse(this.domHelper_.hasClassName(div, 'meuh'));
assertFalse(this.domHelper_.hasClassName(div, 'missingClassName'));
-}
+};
+
+DomHelperTest.prototype.testSetStyle = function() {
+ var e = this.domHelper_.createElement('span')
+ this.domHelper_.setStyle(e, 'left:3px;top:1px;');
+ assertEquals('3px', e.style.left)
+ assertEquals('1px', e.style.top);
+};
@@ -79,8 +79,6 @@ webfont.AscenderScript.prototype.parseVariations = function(source){
};
window['WebFont'].addModule(webfont.AscenderScript.NAME, function(configuration) {
- var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
- var userAgent = userAgentParser.parse();
- var domHelper = new webfont.DomHelper(document, userAgent);
+ var domHelper = new webfont.DomHelper(document);
return new webfont.AscenderScript(domHelper, configuration);
});
View
@@ -2,12 +2,13 @@
* 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 {webfont.UserAgent} userAgent The current user agent.
* @constructor
*/
-webfont.DomHelper = function(doc, userAgent) {
+webfont.DomHelper = function(doc) {
this.document_ = doc;
- this.userAgent_ = userAgent;
+ /** @type boolean */
+ this.supportStyle_ = true;
+ this.detectSupport_();
};
/**
@@ -171,9 +172,21 @@ webfont.DomHelper.prototype.hasClassName = function(e, name) {
* @param {string} styleString The style string.
*/
webfont.DomHelper.prototype.setStyle = function(e, styleString) {
- if (this.userAgent_.getName() == "MSIE") {
- e.style.cssText = styleString;
- } else {
+ if (this.supportStyle_) {
e.setAttribute("style", styleString);
+ } else {
+ e.style.cssText = styleString;
}
};
+
+/**
+ * Do one-time detection for feature support in this browser.
+ * @private
+ */
+webfont.DomHelper.prototype.detectSupport_ = function() {
+ var e = this.document_.createElement('p');
+ e.innerHTML = '<a style="top:1px;">w</a>';
+
+ // Test for support of getting/setting style attr (IE uses cssText instead)
+ this.supportStyle_ = /top/.test(e.getElementsByTagName('a')[0].getAttribute('style'));
+};
@@ -5,7 +5,7 @@ var globalName = 'WebFont';
window[globalName] = (function() {
var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
var userAgent = userAgentParser.parse();
- var domHelper = new webfont.DomHelper(document, userAgent);
+ var domHelper = new webfont.DomHelper(document);
var asyncCall = function(func, timeout) { setTimeout(func, timeout); };
return new webfont.WebFont(domHelper, new webfont.FontModuleLoader(),
@@ -32,8 +32,6 @@ webfont.CustomCss.prototype.supportUserAgent = function(userAgent, support) {
};
window['WebFont'].addModule(webfont.CustomCss.NAME, function(configuration) {
- var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
- var userAgent = userAgentParser.parse();
- var domHelper = new webfont.DomHelper(document, userAgent);
+ var domHelper = new webfont.DomHelper(document);
return new webfont.CustomCss(domHelper, configuration);
});
@@ -56,8 +56,6 @@ webfont.FontdeckScript.prototype.load = function(onReady) {
};
window['WebFont'].addModule(webfont.FontdeckScript.NAME, function(configuration) {
- var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
- var userAgent = userAgentParser.parse();
- var domHelper = new webfont.DomHelper(document, userAgent);
+ var domHelper = new webfont.DomHelper(document);
return new webfont.FontdeckScript(window, domHelper, configuration);
});
@@ -52,5 +52,5 @@ window['WebFont'].addModule(webfont.GoogleFontApi.NAME, function(configuration)
var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
var userAgent = userAgentParser.parse();
return new webfont.GoogleFontApi(userAgent,
- new webfont.DomHelper(document, userAgent), configuration);
+ new webfont.DomHelper(document), configuration);
});
@@ -100,6 +100,6 @@ webfont.MonotypeScript.prototype.protocol = function () {
window['WebFont'].addModule(webfont.MonotypeScript.NAME, function (configuration) {
var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
var userAgent = userAgentParser.parse();
- var domHelper = new webfont.DomHelper(document, userAgent);
+ var domHelper = new webfont.DomHelper(document);
return new webfont.MonotypeScript(window, userAgent, domHelper, document, configuration);
});
@@ -54,9 +54,7 @@ webfont.TypekitScript.prototype.load = function(onReady) {
};
window['WebFont'].addModule(webfont.TypekitScript.NAME, function(configuration) {
- var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
- var userAgent = userAgentParser.parse();
- var domHelper = new webfont.DomHelper(document, userAgent);
+ var domHelper = new webfont.DomHelper(document);
return new webfont.TypekitScript(window, domHelper, configuration);
});

0 comments on commit f31011c

Please sign in to comment.