From f44254cc7ea3a796bd00eab7d9ffade5ec96dacb Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Mon, 5 Dec 2011 12:02:21 -0800 Subject: [PATCH] Use native String#trim when available Uses a method similar to jQuery.trim in jQuery 1.7.1. --- mustache.js | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/mustache.js b/mustache.js index 038a9f7..715422c 100644 --- a/mustache.js +++ b/mustache.js @@ -5,15 +5,39 @@ */ var Mustache = function() { - var regexCache = {}; - var Renderer = function() {}; - var _toString = Object.prototype.toString; Array.isArray = Array.isArray || function (obj) { return _toString.call(obj) == "[object Array]"; } + var _trim = String.prototype.trim, trim; + + if (_trim) { + trim = function (text) { + return text == null ? "" : _trim.call(text); + } + } else { + var trimLeft, trimRight; + + // IE doesn't match non-breaking spaces with \s. + if ((/\S/).test("\xA0")) { + trimLeft = /^[\s\xA0]+/; + trimRight = /[\s\xA0]+$/; + } else { + trimLeft = /^\s+/; + trimRight = /\s+$/; + } + + trim = function (text) { + return text == null ? "" : + text.toString().replace(trimLeft, "").replace(trimRight, ""); + } + } + + var regexCache = {}; + var Renderer = function () {}; + Renderer.prototype = { otag: "{{", ctag: "}}", @@ -111,7 +135,7 @@ var Mustache = function() { Tries to find a partial in the curent scope and render it */ render_partial: function(name, context, partials) { - name = this.trim(name); + name = trim(name); if(!partials || partials[name] === undefined) { throw({message: "unknown_partial '" + name + "'"}); } @@ -267,7 +291,7 @@ var Mustache = function() { from the view object */ find: function(name, context) { - name = this.trim(name); + name = trim(name); // Checks whether a value is thruthy or false or 0 function is_kinda_truthy(bool) { @@ -360,13 +384,6 @@ var Mustache = function() { return a && typeof a == "object"; }, - /* - Gets rid of leading and trailing whitespace - */ - trim: function(s) { - return s.replace(/^\s*|\s*$/g, ""); - }, - /* Why, why, why? Because IE. Cry, cry cry. */