Skip to content

Instantly share code, notes, and snippets.

@kangax
Created September 8, 2009 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kangax/a430ee32c65f10e7500c to your computer and use it in GitHub Desktop.
Save kangax/a430ee32c65f10e7500c to your computer and use it in GitHub Desktop.
diff --git a/src/features.js b/src/features.js
index 6637d29..279837d 100644
--- a/src/features.js
+++ b/src/features.js
@@ -408,6 +408,17 @@
data[1] = !!pattern.lastIndex;
return data[0] || data[1];
},
+
+ 'STRING_PROTOTYPE_TRIM_UNICODE_CHARS_BUGGY': function() {
+ var whitespaceChars = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002'+
+ '\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029';
+
+ var trim = String.prototype.trim;
+ if (typeof trim === 'function') {
+ return trim.call(whitespaceChars).length !== 0;
+ }
+ return false;
+ },
'STRING_REPLACE_COHERSE_FUNCTION_TO_STRING': function() {
// true for Safari 2
diff --git a/src/lang/string.js b/src/lang/string.js
index 96cafa5..9c6579c 100644
--- a/src/lang/string.js
+++ b/src/lang/string.js
@@ -354,7 +354,7 @@
var sMap = Fuse.RegExp.specialCharMap.s;
// ECMA-5 15.5.4.20
- if (!plugin.trim)
+ if (!plugin.trim || Fuse.Env.Bug('STRING_PROTOTYPE_TRIM_UNICODE_CHARS_BUGGY'))
plugin.trim = function trim() {
if (this == null) throw new TypeError;
var string = String(this), start = -1, end = string.length;
@@ -368,7 +368,7 @@
};
// non-standard
- if (!plugin.trimLeft)
+ if (!plugin.trimLeft || Fuse.Env.Bug('STRING_PROTOTYPE_TRIM_UNICODE_CHARS_BUGGY'))
plugin.trimLeft = function trimLeft() {
if (this == null) throw new TypeError;
var string = String(this), start = -1;
@@ -378,7 +378,7 @@
return Fuse.String(string.slice(start));
};
- if (!plugin.trimRight)
+ if (!plugin.trimRight || Fuse.Env.Bug('STRING_PROTOTYPE_TRIM_UNICODE_CHARS_BUGGY'))
plugin.trimRight = function trimRight() {
if (this == null) throw new TypeError;
var string = String(this), end = string.length;
diff --git a/test/unit/string_test.js b/test/unit/string_test.js
index b9c6546..5df8804 100644
--- a/test/unit/string_test.js
+++ b/test/unit/string_test.js
@@ -1,3 +1,7 @@
+// All ES3 whitespace chars (7.2 - WhiteSpace, and 7.3 - LineTerminator)
+var whitespaceCharsES3 = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002'+
+ '\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029';
+
new Test.Unit.Runner({
'testInterpret': function(){
@@ -438,10 +442,7 @@ new Test.Unit.Runner({
this.assertEqual('hello world', Fuse.String('hello world').trim());
this.assertEqual('hello \n world', Fuse.String(' hello \n world ').trim());
this.assertEqual('', Fuse.String(' ').trim());
-
- // Ensure trim removes all whitespace and line terminators
- // IE doesn't understand '\v' so replace it with '\x0B'
- this.assertEqual('hello', Fuse.String(' \n\r\t\x0B\f\xA0 hello \xA0\n \r\t\f\x0B').trim());
+ this.assertEqual('', Fuse.String(whitespaceCharsES3).trim());
},
'testTrimLeft': function() {
@@ -449,7 +450,7 @@ new Test.Unit.Runner({
this.assertEqual('hello world', Fuse.String('hello world').trimLeft());
this.assertEqual('hello \n world ', Fuse.String(' hello \n world ').trimLeft());
this.assertEqual('', Fuse.String(' ').trimLeft());
- this.assertEqual('hello', Fuse.String(' \n\r\t\x0B\f\xA0 hello').trimLeft());
+ this.assertEqual('hello', Fuse.String(whitespaceCharsES3 + 'hello').trimLeft());
},
'testTrimRight': function() {
@@ -457,7 +458,7 @@ new Test.Unit.Runner({
this.assertEqual('hello world', Fuse.String('hello world').trimRight());
this.assertEqual(' hello \n world', Fuse.String(' hello \n world ').trimRight());
this.assertEqual('', Fuse.String(' ').trimRight());
- this.assertEqual('hello', Fuse.String('hello \xA0\n \r\t\f\x0B').trimRight());
+ this.assertEqual('hello', Fuse.String('hello' + whitespaceCharsES3).trimRight());
},
'testStripTags': function() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment