Home | History | Annotate | Download | only in third_party
      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 
     37 var trim            = String.prototype.trim;
     38 var trimLeft        = String.prototype.trimLeft;
     39 var trimRight       = String.prototype.trimRight;
     40 
     41 var testString      = 'foo bar';
     42 var trimString      = '';
     43 var leftTrimString  = '';
     44 var rightTrimString = '';
     45 var wsString        = '';
     46 
     47 var 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 
     72 for (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 
     79 trimString      = wsString   + testString + wsString;
     80 leftTrimString  = testString + wsString;  // Trimmed from the left.
     81 rightTrimString = wsString   + testString;  // Trimmed from the right.
     82 
     83 assertEquals(wsString.trim(),      '');
     84 assertEquals(wsString.trimLeft(),  '');
     85 assertEquals(wsString.trimRight(), '');
     86 
     87 assertEquals(trimString.trim(),      testString);
     88 assertEquals(trimString.trimLeft(),  leftTrimString);
     89 assertEquals(trimString.trimRight(), rightTrimString);
     90 
     91 assertEquals(leftTrimString.trim(),      testString);
     92 assertEquals(leftTrimString.trimLeft(),  leftTrimString);
     93 assertEquals(leftTrimString.trimRight(), testString);
     94 
     95 assertEquals(rightTrimString.trim(),      testString);
     96 assertEquals(rightTrimString.trimLeft(),  testString);
     97 assertEquals(rightTrimString.trimRight(), rightTrimString);
     98 
     99 var testValues = [0, Infinity, NaN, true, false, ({}), ['an','array'],
    100   ({toString:function(){return 'wibble'}})
    101 ];
    102 
    103 for (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