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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 File Name: 15.1.2.4.js
24 ECMA Section: 15.1.2.4 Function properties of the global object
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
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.
38 When the escape function is called with one argument string, the
39 following steps are taken:
41 1. Call ToString(string).
42 2. Compute the number of characters in Result(1).
43 3. Let R be the empty string.
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).
56 12. Let S be a string containing three characters "%xy" where xy are two
57 hexadecimal digits encoding the value of Result(8).
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
65 Author: christine@netscape.com
69 var SECTION = "15.1.2.4";
70 var VERSION = "ECMA_1";
72 var TITLE = "escape(string)";
74 writeHeaderToLog( SECTION + " "+ TITLE);
76 var testcases = getTestCases();
80 function getTestCases() {
81 var array = new Array();
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") );
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 ) );
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)) );
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 ) );
106 var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
108 array[item++] = new TestCase( SECTION, "escape( " +ASCII_TEST_STRING+" )", ASCII_TEST_STRING, escape( ASCII_TEST_STRING ) );
110 // ASCII value less than
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)) );
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)) );
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)) );
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)) );
141 function ToUnicodeString( n ) {
142 var string = ToHexString(n);
144 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
145 string = "0" + string;
150 function ToHexString( n ) {
151 var hex = new Array();
153 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
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) );
162 hex[hex.length] = n % 16;
166 for ( var index = 0 ; index < hex.length ; index++ ) {
167 switch ( hex[index] ) {
187 string += hex[index];
191 if ( string.length == 1 ) {
192 string = "0" + string;
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 ";
205 return ( testcases );