From 240adc699ae3792a84143acfabb3611193c5abba Mon Sep 17 00:00:00 2001 From: James Edward Lewis II Date: Fri, 26 Jun 2015 02:28:46 -0400 Subject: [PATCH] Update String#trim spec compliance Test for incorrect trimming of NEL instead of just ZWSP, reduce the number of string concatenations, and more quickly determine `hasTrimWhitespaceBug` to be false in the usual pre-ES5 case. I would change `if (typeof this === 'undefined' || this === null)` to `if (this == null)` but I don't know whether that's allowed by this library's style guide. --- es5-shim.js | 18 ++++++++++-------- tests/spec/s-string.js | 11 ++++++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/es5-shim.js b/es5-shim.js index 21fa5ebb..8dd43889 100644 --- a/es5-shim.js +++ b/es5-shim.js @@ -1519,14 +1519,16 @@ defineProperties(StringPrototype, { // ES5 15.5.4.20 // whitespace from: http://es5.github.io/#x15.5.4.20 -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'; -var zeroWidth = '\u200b'; -var wsRegexChars = '[' + ws + ']'; -var trimBeginRegexp = new RegExp('^' + wsRegexChars + wsRegexChars + '*'); -var trimEndRegexp = new RegExp(wsRegexChars + wsRegexChars + '*$'); -var hasTrimWhitespaceBug = StringPrototype.trim && (ws.trim() || !zeroWidth.trim()); +var ws = '\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 nextLine = '\x85'; +var zeroWidth = '\u200B'; +var wsRegexChars = '[' + ws + '][' + ws + ']*'; +var trimBeginRegexp = new RegExp('^' + wsRegexChars); +var trimEndRegexp = new RegExp(wsRegexChars + '$'); +var hasTrimWhitespaceBug = 'trim' in StringPrototype && + typeof StringPrototype.trim === 'function' && + (ws.trim() || !nextLine.trim() || !zeroWidth.trim()); defineProperties(StringPrototype, { // http://blog.stevenlevithan.com/archives/faster-trim-javascript // http://perfectionkills.com/whitespace-deviations/ diff --git a/tests/spec/s-string.js b/tests/spec/s-string.js index de62924b..8543b5ef 100644 --- a/tests/spec/s-string.js +++ b/tests/spec/s-string.js @@ -4,16 +4,21 @@ describe('String', function () { 'use strict'; describe('trim', function () { - var test = '\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\uFEFFHello, World!\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'; + var test = '\t\n\v\f\r \xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFFHello, World!\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'; it('trims all ES5 whitespace', function () { expect(test.trim()).toEqual('Hello, World!'); expect(test.trim().length).toBe(13); }); + it('does not trim the next-line character', function () { + expect('\x85'.trim()).toBe('\x85'); + expect('\x85'.trim().length).toBe(1); + }); + it('does not trim the zero-width space', function () { - expect('\u200b'.trim()).toBe('\u200b'); - expect('\u200b'.trim().length).toBe(1); + expect('\u200B'.trim()).toBe('\u200B'); + expect('\u200B'.trim().length).toBe(1); }); });