Skip to content

Commit 11c53de

Browse files
committed
Added trim shim function. Closes #45
1 parent a153bba commit 11c53de

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

index.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@ module.exports = function(css, options){
99
var lineno = 1;
1010
var column = 1;
1111

12+
/**
13+
* Shim-wrapper for trim. Will use native trim if supported, otherwise the trim
14+
* found at https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js
15+
* at commit 32ff9747d5baaa446d5a49d0078ed38fcff93ab0
16+
*
17+
* Modified a bit to not pollute String prototype.
18+
*/
19+
20+
function trim(str) {
21+
if (str === void 0 || str === null) {
22+
throw new TypeError('trim called on null or undefined');
23+
}
24+
25+
var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' +
26+
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' +
27+
'\u2029\uFEFF';
28+
29+
if (!String.prototype.trim || ws.trim()) {
30+
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
31+
// http://perfectionkills.com/whitespace-deviations/
32+
ws = "[" + ws + "]";
33+
var trimBeginRegexp = new RegExp("^" + ws + ws + "*");
34+
var trimEndRegexp = new RegExp(ws + ws + "*$");
35+
36+
return String(str).replace(trimBeginRegexp, "").replace(trimEndRegexp, "");
37+
}
38+
39+
return String(str).trim();
40+
}
41+
1242
/**
1343
* Update lineno and column based on `str`.
1444
*/
@@ -168,7 +198,7 @@ module.exports = function(css, options){
168198
function selector() {
169199
var m = match(/^([^{]+)/);
170200
if (!m) return;
171-
return m[0].trim().split(/\s*,\s*/);
201+
return trim(m[0]).split(/\s*,\s*/);
172202
}
173203

174204
/**
@@ -193,7 +223,7 @@ module.exports = function(css, options){
193223
var ret = pos({
194224
type: 'declaration',
195225
property: prop,
196-
value: val[0].trim()
226+
value: trim(val[0])
197227
});
198228

199229
// ;
@@ -290,7 +320,7 @@ module.exports = function(css, options){
290320
var m = match(/^@supports *([^{]+)/);
291321

292322
if (!m) return;
293-
var supports = m[1].trim();
323+
var supports = trim(m[1]);
294324

295325
if (!open()) return error("@supports missing '{'");
296326

@@ -314,7 +344,7 @@ module.exports = function(css, options){
314344
var m = match(/^@media *([^{]+)/);
315345

316346
if (!m) return;
317-
var media = m[1].trim();
347+
var media = trim(m[1]);
318348

319349
if (!open()) return error("@media missing '{'");
320350

@@ -368,8 +398,8 @@ module.exports = function(css, options){
368398
var m = match(/^@([-\w]+)?document *([^{]+)/);
369399
if (!m) return;
370400

371-
var vendor = (m[1] || '').trim();
372-
var doc = m[2].trim();
401+
var vendor = trim(m[1] || '');
402+
var doc = trim(m[2]);
373403

374404
if (!open()) return error("@document missing '{'");
375405

@@ -418,7 +448,7 @@ module.exports = function(css, options){
418448
var m = match(new RegExp('^@' + name + ' *([^;\\n]+);'));
419449
if (!m) return;
420450
var ret = { type: name };
421-
ret[name] = m[1].trim();
451+
ret[name] = trim(m[1]);
422452
return pos(ret);
423453
}
424454

0 commit comments

Comments
 (0)