• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions
5// are met:
6//
7// 1. Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9//
10// 2. Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following
12// disclaimer in the documentation and/or other materials provided
13// with the distribution.
14//
15// 3. Neither the name of the copyright holder(s) nor the names of any
16// contributors may be used to endorse or promote products derived
17// from this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30// OF THE POSSIBILITY OF SUCH DAMAGE.
31
32// Based on LayoutTests/fast/js/script-tests/string-trim.js
33
34// References to trim(), trimLeft() and trimRight() functions for
35// testing Function's *.call() and *.apply() methods.
36
37var trim            = String.prototype.trim;
38var trimLeft        = String.prototype.trimLeft;
39var trimRight       = String.prototype.trimRight;
40
41var testString      = 'foo bar';
42var trimString      = '';
43var leftTrimString  = '';
44var rightTrimString = '';
45var wsString        = '';
46
47var whitespace      = [
48  {s : '\u0009', t : 'HORIZONTAL TAB'},
49  {s : '\u000A', t : 'LINE FEED OR NEW LINE'},
50  {s : '\u000B', t : 'VERTICAL TAB'},
51  {s : '\u000C', t : 'FORMFEED'},
52  {s : '\u000D', t : 'CARRIAGE RETURN'},
53  {s : '\u0020', t : 'SPACE'},
54  {s : '\u00A0', t : 'NO-BREAK SPACE'},
55  {s : '\u2000', t : 'EN QUAD'},
56  {s : '\u2001', t : 'EM QUAD'},
57  {s : '\u2002', t : 'EN SPACE'},
58  {s : '\u2003', t : 'EM SPACE'},
59  {s : '\u2004', t : 'THREE-PER-EM SPACE'},
60  {s : '\u2005', t : 'FOUR-PER-EM SPACE'},
61  {s : '\u2006', t : 'SIX-PER-EM SPACE'},
62  {s : '\u2007', t : 'FIGURE SPACE'},
63  {s : '\u2008', t : 'PUNCTUATION SPACE'},
64  {s : '\u2009', t : 'THIN SPACE'},
65  {s : '\u200A', t : 'HAIR SPACE'},
66  {s : '\u3000', t : 'IDEOGRAPHIC SPACE'},
67  {s : '\u2028', t : 'LINE SEPARATOR'},
68  {s : '\u2029', t : 'PARAGRAPH SEPARATOR'},
69  {s : '\u200B', t : 'ZERO WIDTH SPACE (category Cf)'}
70];
71
72for (var i = 0; i < whitespace.length; i++) {
73  assertEquals(whitespace[i].s.trim(), '');
74  assertEquals(whitespace[i].s.trimLeft(), '');
75  assertEquals(whitespace[i].s.trimRight(), '');
76  wsString += whitespace[i].s;
77}
78
79trimString      = wsString   + testString + wsString;
80leftTrimString  = testString + wsString;  // Trimmed from the left.
81rightTrimString = wsString   + testString;  // Trimmed from the right.
82
83assertEquals(wsString.trim(),      '');
84assertEquals(wsString.trimLeft(),  '');
85assertEquals(wsString.trimRight(), '');
86
87assertEquals(trimString.trim(),      testString);
88assertEquals(trimString.trimLeft(),  leftTrimString);
89assertEquals(trimString.trimRight(), rightTrimString);
90
91assertEquals(leftTrimString.trim(),      testString);
92assertEquals(leftTrimString.trimLeft(),  leftTrimString);
93assertEquals(leftTrimString.trimRight(), testString);
94
95assertEquals(rightTrimString.trim(),      testString);
96assertEquals(rightTrimString.trimLeft(),  testString);
97assertEquals(rightTrimString.trimRight(), rightTrimString);
98
99var testValues = [0, Infinity, NaN, true, false, ({}), ['an','array'],
100  ({toString:function(){return 'wibble'}})
101];
102
103for (var i = 0; i < testValues.length; i++) {
104  assertEquals(trim.call(testValues[i]), String(testValues[i]));
105  assertEquals(trimLeft.call(testValues[i]), String(testValues[i]));
106  assertEquals(trimRight.call(testValues[i]), String(testValues[i]));
107}
108