1/* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
5 *
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
10 *
11 * The Original Code is Mozilla Communicator client code, released March
12 * 31, 1998.
13 *
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22/**
23    File Name:          15.1.2.4.js
24    ECMA Section:       15.1.2.4  Function properties of the global object
25                        escape( string )
26
27    Description:
28    The escape function computes a new version of a string value in which
29    certain characters have been replaced by a hexadecimal escape sequence.
30    The result thus contains no special characters that might have special
31    meaning within a URL.
32
33    For characters whose Unicode encoding is 0xFF or less, a two-digit
34    escape sequence of the form %xx is used in accordance with RFC1738.
35    For characters whose Unicode encoding is greater than 0xFF, a four-
36    digit escape sequence of the form %uxxxx is used.
37
38    When the escape function is called with one argument string, the
39    following steps are taken:
40
41    1.  Call ToString(string).
42    2.  Compute the number of characters in Result(1).
43    3.  Let R be the empty string.
44    4.  Let k be 0.
45    5.  If k equals Result(2), return R.
46    6.  Get the character at position k within Result(1).
47    7.  If Result(6) is one of the 69 nonblank ASCII characters
48        ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
49        0123456789 @*_+-./, go to step 14.
50    8.  Compute the 16-bit unsigned integer that is the Unicode character
51        encoding of Result(6).
52    9.  If Result(8), is less than 256, go to step 12.
53   10.  Let S be a string containing six characters "%uwxyz" where wxyz are
54        four hexadecimal digits encoding the value of Result(8).
55   11.  Go to step 15.
56   12.  Let S be a string containing three characters "%xy" where xy are two
57        hexadecimal digits encoding the value of Result(8).
58   13.  Go to step 15.
59   14.  Let S be a string containing the single character Result(6).
60   15.  Let R be a new string value computed by concatenating the previous value
61        of R and S.
62   16.  Increase k by 1.
63   17.  Go to step 5.
64
65    Author:             christine@netscape.com
66    Date:               28 october 1997
67
68*/
69    var SECTION = "15.1.2.4";
70    var VERSION = "ECMA_1";
71    startTest();
72    var TITLE   = "escape(string)";
73
74    writeHeaderToLog( SECTION + " "+ TITLE);
75
76    var testcases = getTestCases();
77
78    test();
79
80function getTestCases() {
81    var array = new Array();
82    var item = 0;
83
84    array[item++] = new TestCase( SECTION, "escape.length",         1,          escape.length );
85    array[item++] = new TestCase( SECTION, "escape.length = null; escape.length",   1,  eval("escape.length = null; escape.length") );
86    array[item++] = new TestCase( SECTION, "delete escape.length",                  false,  delete escape.length );
87    array[item++] = new TestCase( SECTION, "delete escape.length; escape.length",   1,      eval("delete escape.length; escape.length") );
88    array[item++] = new TestCase( SECTION, "var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS",    "",    eval("var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS") );
89
90    array[item++] = new TestCase( SECTION, "escape()",              "undefined",    escape() );
91    array[item++] = new TestCase( SECTION, "escape('')",            "",             escape('') );
92    array[item++] = new TestCase( SECTION, "escape( null )",        "null",         escape(null) );
93    array[item++] = new TestCase( SECTION, "escape( void 0 )",      "undefined",    escape(void 0) );
94    array[item++] = new TestCase( SECTION, "escape( true )",        "true",         escape( true ) );
95    array[item++] = new TestCase( SECTION, "escape( false )",       "false",        escape( false ) );
96
97    array[item++] = new TestCase( SECTION, "escape( new Boolean(true) )",   "true", escape(new Boolean(true)) );
98    array[item++] = new TestCase( SECTION, "escape( new Boolean(false) )",  "false",    escape(new Boolean(false)) );
99
100    array[item++] = new TestCase( SECTION, "escape( Number.NaN  )",                 "NaN",      escape(Number.NaN) );
101    array[item++] = new TestCase( SECTION, "escape( -0 )",                          "0",        escape( -0 ) );
102    array[item++] = new TestCase( SECTION, "escape( 'Infinity' )",                  "Infinity", escape( "Infinity" ) );
103    array[item++] = new TestCase( SECTION, "escape( Number.POSITIVE_INFINITY )",    "Infinity", escape( Number.POSITIVE_INFINITY ) );
104    array[item++] = new TestCase( SECTION, "escape( Number.NEGATIVE_INFINITY )",    "-Infinity", escape( Number.NEGATIVE_INFINITY ) );
105
106    var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
107
108    array[item++] = new TestCase( SECTION, "escape( " +ASCII_TEST_STRING+" )",    ASCII_TEST_STRING,  escape( ASCII_TEST_STRING ) );
109
110    // ASCII value less than
111
112    for ( var CHARCODE = 0; CHARCODE < 32; CHARCODE++ ) {
113        array[item++] = new TestCase( SECTION,
114                            "escape(String.fromCharCode("+CHARCODE+"))",
115                            "%"+ToHexString(CHARCODE),
116                            escape(String.fromCharCode(CHARCODE))  );
117    }
118    for ( var CHARCODE = 128; CHARCODE < 256; CHARCODE++ ) {
119        array[item++] = new TestCase( SECTION,
120                            "escape(String.fromCharCode("+CHARCODE+"))",
121                            "%"+ToHexString(CHARCODE),
122                            escape(String.fromCharCode(CHARCODE))  );
123    }
124
125    for ( var CHARCODE = 256; CHARCODE < 1024; CHARCODE++ ) {
126        array[item++] = new TestCase( SECTION,
127                            "escape(String.fromCharCode("+CHARCODE+"))",
128                            "%u"+ ToUnicodeString(CHARCODE),
129                            escape(String.fromCharCode(CHARCODE))  );
130    }
131    for ( var CHARCODE = 65500; CHARCODE < 65536; CHARCODE++ ) {
132        array[item++] = new TestCase( SECTION,
133                            "escape(String.fromCharCode("+CHARCODE+"))",
134                            "%u"+ ToUnicodeString(CHARCODE),
135                            escape(String.fromCharCode(CHARCODE))  );
136    }
137
138    return ( array );
139}
140
141function ToUnicodeString( n ) {
142    var string = ToHexString(n);
143
144    for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
145        string = "0" + string;
146    }
147
148    return string;
149}
150function ToHexString( n ) {
151    var hex = new Array();
152
153    for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
154        ;
155    }
156
157    for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
158        hex[index] = Math.floor( n / Math.pow(16,mag) );
159        n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
160    }
161
162    hex[hex.length] = n % 16;
163
164    var string ="";
165
166    for ( var index = 0 ; index < hex.length ; index++ ) {
167        switch ( hex[index] ) {
168            case 10:
169                string += "A";
170                break;
171            case 11:
172                string += "B";
173                break;
174            case 12:
175                string += "C";
176                break;
177            case 13:
178                string += "D";
179                break;
180            case 14:
181                string += "E";
182                break;
183            case 15:
184                string += "F";
185                break;
186            default:
187                string += hex[index];
188        }
189    }
190
191    if ( string.length == 1 ) {
192        string = "0" + string;
193    }
194    return string;
195}
196function test() {
197    for ( tc=0; tc < testcases.length; tc++ ) {
198        testcases[tc].passed = writeTestCaseResult(
199                            testcases[tc].expect,
200                            testcases[tc].actual,
201                            testcases[tc].description +" = "+ testcases[tc].actual );
202        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
203    }
204    stopTest();
205    return ( testcases );
206}
207