From 72452cf42ad8fb23685fc0c9cdfe9f697da7bc84 Mon Sep 17 00:00:00 2001 From: Vincent De Oliveira Date: Tue, 30 Jun 2015 18:24:36 +0200 Subject: [PATCH] Fix browsers detection (IEs) --- lib/pixrem.js | 27 +++++++++++++++------------ spec/pixrem-spec.js | 4 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/pixrem.js b/lib/pixrem.js index d048567..2ef8464 100755 --- a/lib/pixrem.js +++ b/lib/pixrem.js @@ -34,16 +34,19 @@ Pixrem.prototype.postcss = function (css) { var vendor = require('postcss/lib/vendor'); var browsers = browserslist(_options.browsers); - // if IE9+ + // detect IE versions needed + var isIElte8, isIEgte9, isIE9_10; + if (detectBrowser(browsers, 'ie <= 8')) { + isIElte8 = true; + } if (detectBrowser(browsers, 'ie >= 9')) { - // if IE9 or IE10 - var specialCaseIE = false; - if (detectBrowser(browsers, 'ie 9, ie 10')) { - specialCaseIE = true; - } else { - return; - } + isIEgte9 = true; + } + if (detectBrowser(browsers, 'ie 9, ie 10')) { + isIE9_10 = true; } + // no IE versions needed, skip + if (!isIElte8 && !isIEgte9 && !isIE9_10) { return; } if (_options.html) { // First, check root font-size @@ -65,7 +68,7 @@ Pixrem.prototype.postcss = function (css) { css.eachRule(function (rule) { // if options.at-rules is false AND it's not IE9-10: skip @rules - if (!_options.atrules && !specialCaseIE) { + if (!_options.atrules && !isIE9_10) { if (rule.type === 'atrule' || (rule.parent && rule.parent.type === 'atrule')) { return; } } @@ -79,10 +82,10 @@ Pixrem.prototype.postcss = function (css) { var prop = vendor.unprefixed(decl.prop); // replace rems only if needed var isFontShorthand = (prop === 'font'); - var isSpecialCaseIE9 = (specialCaseIE && (isPseudoElement || isFontShorthand)); - var isUseless = (!specialCaseIE && !(_VALUES.test(value) || _PROPS.test(prop))); + var isSpecialCaseIE9_10 = (isIE9_10 && (isPseudoElement || isFontShorthand)); + var isUseless = (!isIE9_10 && !(_VALUES.test(value) || _PROPS.test(prop))); - if ( isSpecialCaseIE9 || isUseless ) { + if ( isSpecialCaseIE9_10 || isUseless ) { value = value.replace(_remgex, function ($1) { // Round decimal pixels down to match webkit and opera behavior: diff --git a/spec/pixrem-spec.js b/spec/pixrem-spec.js index e338c38..4c60d73 100644 --- a/spec/pixrem-spec.js +++ b/spec/pixrem-spec.js @@ -229,10 +229,10 @@ describe('pixrem', function () { expect(processed).toBe(expected); }); - it('should not add fallback when IE8 is not in scope', function () { + it('should not add fallback when IE8- are not in scope', function () { var css = '.rule{width: 2rem}'; var expected = '.rule{width: 2rem}'; - var processed = pixrem.process(css, undefined, {browsers: 'last 2 versions'}); + var processed = pixrem.process(css, undefined, {browsers: 'firefox 28'}); expect(processed).toBe(expected); });