From 11c53de8a57dfee19e3792e28f3315916427a0bd Mon Sep 17 00:00:00 2001 From: Lucas Wiener Date: Mon, 19 Aug 2013 10:26:45 +0200 Subject: [PATCH 1/3] Added trim shim function. Closes #45 --- index.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 8623fa4..cae5eec 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,36 @@ module.exports = function(css, options){ var lineno = 1; var column = 1; + /** + * Shim-wrapper for trim. Will use native trim if supported, otherwise the trim + * found at https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js + * at commit 32ff9747d5baaa446d5a49d0078ed38fcff93ab0 + * + * Modified a bit to not pollute String prototype. + */ + + function trim(str) { + if (str === void 0 || str === null) { + throw new TypeError('trim called on null or undefined'); + } + + var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' + + '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' + + '\u2029\uFEFF'; + + if (!String.prototype.trim || ws.trim()) { + // http://blog.stevenlevithan.com/archives/faster-trim-javascript + // http://perfectionkills.com/whitespace-deviations/ + ws = "[" + ws + "]"; + var trimBeginRegexp = new RegExp("^" + ws + ws + "*"); + var trimEndRegexp = new RegExp(ws + ws + "*$"); + + return String(str).replace(trimBeginRegexp, "").replace(trimEndRegexp, ""); + } + + return String(str).trim(); + } + /** * Update lineno and column based on `str`. */ @@ -168,7 +198,7 @@ module.exports = function(css, options){ function selector() { var m = match(/^([^{]+)/); if (!m) return; - return m[0].trim().split(/\s*,\s*/); + return trim(m[0]).split(/\s*,\s*/); } /** @@ -193,7 +223,7 @@ module.exports = function(css, options){ var ret = pos({ type: 'declaration', property: prop, - value: val[0].trim() + value: trim(val[0]) }); // ; @@ -290,7 +320,7 @@ module.exports = function(css, options){ var m = match(/^@supports *([^{]+)/); if (!m) return; - var supports = m[1].trim(); + var supports = trim(m[1]); if (!open()) return error("@supports missing '{'"); @@ -314,7 +344,7 @@ module.exports = function(css, options){ var m = match(/^@media *([^{]+)/); if (!m) return; - var media = m[1].trim(); + var media = trim(m[1]); if (!open()) return error("@media missing '{'"); @@ -368,8 +398,8 @@ module.exports = function(css, options){ var m = match(/^@([-\w]+)?document *([^{]+)/); if (!m) return; - var vendor = (m[1] || '').trim(); - var doc = m[2].trim(); + var vendor = trim(m[1] || ''); + var doc = trim(m[2]); if (!open()) return error("@document missing '{'"); @@ -418,7 +448,7 @@ module.exports = function(css, options){ var m = match(new RegExp('^@' + name + ' *([^;\\n]+);')); if (!m) return; var ret = { type: name }; - ret[name] = m[1].trim(); + ret[name] = trim(m[1]); return pos(ret); } From d2e8fbb85193ab11d157bfc3f0b7e1f8d9ede677 Mon Sep 17 00:00:00 2001 From: Lucas Wiener Date: Mon, 19 Aug 2013 11:02:15 +0200 Subject: [PATCH 2/3] Now using .charAt instead of using strings as arrays. #45 --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index cae5eec..031392a 100644 --- a/index.js +++ b/index.js @@ -128,7 +128,7 @@ module.exports = function(css, options){ var rules = []; whitespace(); comments(rules); - while (css[0] != '}' && (node = atrule() || rule())) { + while (css.charAt(0) != '}' && (node = atrule() || rule())) { rules.push(node); comments(rules); } @@ -173,10 +173,10 @@ module.exports = function(css, options){ function comment() { var pos = position(); - if ('/' != css[0] || '*' != css[1]) return; + if ('/' != css.charAt(0) || '*' != css.charAt(1)) return; var i = 2; - while (null != css[i] && ('*' != css[i] || '/' != css[i + 1])) ++i; + while (null != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i; i += 2; var str = css.slice(2, i - 2); From c87d9ec5a87b086835bab7ba36ac0959b2848219 Mon Sep 17 00:00:00 2001 From: Lucas Wiener Date: Tue, 27 Aug 2013 11:41:14 +0200 Subject: [PATCH 3/3] Removed trim shim, and replaced it with regex trim --- index.js | 44 +++++++------------------------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/index.js b/index.js index 031392a..99c4806 100644 --- a/index.js +++ b/index.js @@ -9,36 +9,6 @@ module.exports = function(css, options){ var lineno = 1; var column = 1; - /** - * Shim-wrapper for trim. Will use native trim if supported, otherwise the trim - * found at https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js - * at commit 32ff9747d5baaa446d5a49d0078ed38fcff93ab0 - * - * Modified a bit to not pollute String prototype. - */ - - function trim(str) { - if (str === void 0 || str === null) { - throw new TypeError('trim called on null or undefined'); - } - - var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' + - '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' + - '\u2029\uFEFF'; - - if (!String.prototype.trim || ws.trim()) { - // http://blog.stevenlevithan.com/archives/faster-trim-javascript - // http://perfectionkills.com/whitespace-deviations/ - ws = "[" + ws + "]"; - var trimBeginRegexp = new RegExp("^" + ws + ws + "*"); - var trimEndRegexp = new RegExp(ws + ws + "*$"); - - return String(str).replace(trimBeginRegexp, "").replace(trimEndRegexp, ""); - } - - return String(str).trim(); - } - /** * Update lineno and column based on `str`. */ @@ -198,7 +168,7 @@ module.exports = function(css, options){ function selector() { var m = match(/^([^{]+)/); if (!m) return; - return trim(m[0]).split(/\s*,\s*/); + return m[0].replace(/^\s+|\s+$/g, '').split(/\s*,\s*/); } /** @@ -223,7 +193,7 @@ module.exports = function(css, options){ var ret = pos({ type: 'declaration', property: prop, - value: trim(val[0]) + value: val[0].replace(/^\s+|\s+$/g, '') }); // ; @@ -320,7 +290,7 @@ module.exports = function(css, options){ var m = match(/^@supports *([^{]+)/); if (!m) return; - var supports = trim(m[1]); + var supports = m[1].replace(/^\s+|\s+$/g, ''); if (!open()) return error("@supports missing '{'"); @@ -344,7 +314,7 @@ module.exports = function(css, options){ var m = match(/^@media *([^{]+)/); if (!m) return; - var media = trim(m[1]); + var media = m[1].replace(/^\s+|\s+$/g, ''); if (!open()) return error("@media missing '{'"); @@ -398,8 +368,8 @@ module.exports = function(css, options){ var m = match(/^@([-\w]+)?document *([^{]+)/); if (!m) return; - var vendor = trim(m[1] || ''); - var doc = trim(m[2]); + var vendor = (m[1] || '').replace(/^\s+|\s+$/g, ''); + var doc = m[2].replace(/^\s+|\s+$/g, ''); if (!open()) return error("@document missing '{'"); @@ -448,7 +418,7 @@ module.exports = function(css, options){ var m = match(new RegExp('^@' + name + ' *([^;\\n]+);')); if (!m) return; var ret = { type: name }; - ret[name] = trim(m[1]); + ret[name] = m[1].replace(/^\s+|\s+$/g, ''); return pos(ret); }