From 53beb7c9715b748afd155864a1477c5f60c8c34e Mon Sep 17 00:00:00 2001 From: James Edward Lewis II Date: Fri, 26 Jun 2015 02:05:12 -0400 Subject: [PATCH] update String#trim spec compliance Check for two commonly erroneously trimmed characters instead of one, and check for erroneously failing to trim the trimmable whitespace characters. I would also replace `if (typeof this === 'undefined' || this === null)` with `if (this == null)` but I don't know whether that's allowed by this library's style rules. --- es6-shim.js | 15 +++++++-------- test/string.js | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/es6-shim.js b/es6-shim.js index 37ab863e..4a737d20 100644 --- a/es6-shim.js +++ b/es6-shim.js @@ -644,16 +644,15 @@ overrideNative(String.prototype, 'includes', StringPrototypeShims.includes); } - var hasStringTrimBug = '\u0085'.trim().length !== 1; + // whitespace from: http://es5.github.io/#x15.5.4.20 + // implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324 + var wsp = '\t\n\v\f\r \xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005' + + '\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF'; + var hasStringTrimBug = wsp.trim() || '\x85\u002B'.trim().length !== 2; if (hasStringTrimBug) { delete String.prototype.trim; - // whitespace from: http://es5.github.io/#x15.5.4.20 - // implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324 - 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' - ].join(''); + var ws = '\t-\r \xA0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F' + + '\u3000\uFEFF'; var trimRegexp = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g'); defineProperties(String.prototype, { trim: function trim() { diff --git a/test/string.js b/test/string.js index a1feae5d..467c4018 100644 --- a/test/string.js +++ b/test/string.js @@ -587,13 +587,14 @@ var runStringTests = function () { }); it('should trim the correct characters', function () { - var whitespace = '\u0009' + '\u000b' + '\u000c' + '\u0020' + - '\u00a0' + '\u1680' + '\u2000' + '\u2001' + - '\u2002' + '\u2003' + '\u2004' + '\u2005' + - '\u2006' + '\u2007' + '\u2008' + '\u2009' + - '\u200A' + '\u202f' + '\u205f' + '\u3000'; + var whitespace = '\u0009' + '\u000B' + '\u000C' + '\u0020' + + '\u00A0' + '\u1680' + '\u180E' + '\u2000' + + '\u2001' + '\u2002' + '\u2003' + '\u2004' + + '\u2005' + '\u2006' + '\u2007' + '\u2008' + + '\u2009' + '\u200A' + '\u202F' + '\u205F' + + '\u3000' + '\uFEFF'; - var lineTerminators = '\u000a' + '\u000d' + '\u2028' + '\u2029'; + var lineTerminators = '\u000A' + '\u000D' + '\u2028' + '\u2029'; var trimmed = (whitespace + lineTerminators).trim(); expect(trimmed).to.have.property('length', 0); @@ -606,6 +607,12 @@ var runStringTests = function () { expect(trimmed).to.equal('\u0085'); }); + it('should not trim U+200B', function () { + var trimmed = '\u200B'.trim(); + expect(trimmed).to.have.property('length', 1); + expect(trimmed).to.equal('\u200B'); + }); + it('should trim on both sides', function () { var trimmed = ' a '.trim(); expect(trimmed).to.have.property('length', 1);