From 22987bea12763f57d6f7651dae16028cfaf35a4c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Jun 2015 03:44:13 +0530 Subject: [PATCH] More complete definitions for the trim functions --- src/duktape/__init__.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/duktape/__init__.py b/src/duktape/__init__.py index cdd4eb0811..ff8d36cc43 100644 --- a/src/duktape/__init__.py +++ b/src/duktape/__init__.py @@ -103,9 +103,33 @@ class Context(object): self.eval(''' console = { log: function() { print(Array.prototype.join.call(arguments, ' ')); } }; Duktape.modSearch = function (id, require, exports, module) { return Duktape.load_file(id); } - String.prototype.trimLeft = function() { return this.replace(/^\s+/, ''); }; - String.prototype.trimRight = function() { return this.replace(/\s+$/, ''); }; - String.prototype.trim = function() { return this.replace(/^\s+/, '').replace(/\s+$/, ''); }; + if (!String.prototype.trim) { + (function() { + // Make sure we trim BOM and NBSP + var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; + String.prototype.trim = function() { + return this.replace(rtrim, ''); + }; + })(); + } + if (!String.prototype.trimLeft) { + (function() { + // Make sure we trim BOM and NBSP + var rtrim = /^[\s\uFEFF\xA0]+/g; + String.prototype.trimLeft = function() { + return this.replace(rtrim, ''); + }; + })(); + } + if (!String.prototype.trimRight) { + (function() { + // Make sure we trim BOM and NBSP + var rtrim = /[\s\uFEFF\xA0]+$/g; + String.prototype.trimRight = function() { + return this.replace(rtrim, ''); + }; + })(); + } Duktape.readfile = function(path, encoding) { var x = Duktape.pyreadfile(path, encoding); var data = x[0]; var errcode = x[1]; var errmsg = x[2];