Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 File Name: 15.1.2.4.js
9 ECMA Section: 15.1.2.4 Function properties of the global object
10 escape( string )
12 Description:
13 The escape function computes a new version of a string value in which
14 certain characters have been replaced by a hexadecimal escape sequence.
15 The result thus contains no special characters that might have special
16 meaning within a URL.
18 For characters whose Unicode encoding is 0xFF or less, a two-digit
19 escape sequence of the form %xx is used in accordance with RFC1738.
20 For characters whose Unicode encoding is greater than 0xFF, a four-
21 digit escape sequence of the form %uxxxx is used.
23 When the escape function is called with one argument string, the
24 following steps are taken:
26 1. Call ToString(string).
27 2. Compute the number of characters in Result(1).
28 3. Let R be the empty string.
29 4. Let k be 0.
30 5. If k equals Result(2), return R.
31 6. Get the character at position k within Result(1).
32 7. If Result(6) is one of the 69 nonblank ASCII characters
33 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
34 0123456789 @*_+-./, go to step 14.
35 8. Compute the 16-bit unsigned integer that is the Unicode character
36 encoding of Result(6).
37 9. If Result(8), is less than 256, go to step 12.
38 10. Let S be a string containing six characters "%uwxyz" where wxyz are
39 four hexadecimal digits encoding the value of Result(8).
40 11. Go to step 15.
41 12. Let S be a string containing three characters "%xy" where xy are two
42 hexadecimal digits encoding the value of Result(8).
43 13. Go to step 15.
44 14. Let S be a string containing the single character Result(6).
45 15. Let R be a new string value computed by concatenating the previous value
46 of R and S.
47 16. Increase k by 1.
48 17. Go to step 5.
50 Author: christine@netscape.com
51 Date: 28 october 1997
53 */
54 var SECTION = "15.1.2.4";
55 var VERSION = "ECMA_1";
56 startTest();
57 var TITLE = "escape(string)";
59 writeHeaderToLog( SECTION + " "+ TITLE);
61 new TestCase( SECTION, "escape.length", 1, escape.length );
62 new TestCase( SECTION, "escape.length = null; escape.length", 1, eval("escape.length = null; escape.length") );
63 new TestCase( SECTION, "delete escape.length", false, delete escape.length );
64 new TestCase( SECTION, "delete escape.length; escape.length", 1, eval("delete escape.length; escape.length") );
65 new TestCase( SECTION, "var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS", "", eval("var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS") );
67 new TestCase( SECTION, "escape()", "undefined", escape() );
68 new TestCase( SECTION, "escape('')", "", escape('') );
69 new TestCase( SECTION, "escape( null )", "null", escape(null) );
70 new TestCase( SECTION, "escape( void 0 )", "undefined", escape(void 0) );
71 new TestCase( SECTION, "escape( true )", "true", escape( true ) );
72 new TestCase( SECTION, "escape( false )", "false", escape( false ) );
74 new TestCase( SECTION, "escape( new Boolean(true) )", "true", escape(new Boolean(true)) );
75 new TestCase( SECTION, "escape( new Boolean(false) )", "false", escape(new Boolean(false)) );
77 new TestCase( SECTION, "escape( Number.NaN )", "NaN", escape(Number.NaN) );
78 new TestCase( SECTION, "escape( -0 )", "0", escape( -0 ) );
79 new TestCase( SECTION, "escape( 'Infinity' )", "Infinity", escape( "Infinity" ) );
80 new TestCase( SECTION, "escape( Number.POSITIVE_INFINITY )", "Infinity", escape( Number.POSITIVE_INFINITY ) );
81 new TestCase( SECTION, "escape( Number.NEGATIVE_INFINITY )", "-Infinity", escape( Number.NEGATIVE_INFINITY ) );
83 var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
85 new TestCase( SECTION, "escape( " +ASCII_TEST_STRING+" )", ASCII_TEST_STRING, escape( ASCII_TEST_STRING ) );
87 // ASCII value less than
89 for ( var CHARCODE = 0; CHARCODE < 32; CHARCODE++ ) {
90 new TestCase( SECTION,
91 "escape(String.fromCharCode("+CHARCODE+"))",
92 "%"+ToHexString(CHARCODE),
93 escape(String.fromCharCode(CHARCODE)) );
94 }
95 for ( var CHARCODE = 128; CHARCODE < 256; CHARCODE++ ) {
96 new TestCase( SECTION,
97 "escape(String.fromCharCode("+CHARCODE+"))",
98 "%"+ToHexString(CHARCODE),
99 escape(String.fromCharCode(CHARCODE)) );
100 }
102 for ( var CHARCODE = 256; CHARCODE < 1024; CHARCODE++ ) {
103 new TestCase( SECTION,
104 "escape(String.fromCharCode("+CHARCODE+"))",
105 "%u"+ ToUnicodeString(CHARCODE),
106 escape(String.fromCharCode(CHARCODE)) );
107 }
108 for ( var CHARCODE = 65500; CHARCODE < 65536; CHARCODE++ ) {
109 new TestCase( SECTION,
110 "escape(String.fromCharCode("+CHARCODE+"))",
111 "%u"+ ToUnicodeString(CHARCODE),
112 escape(String.fromCharCode(CHARCODE)) );
113 }
115 test();
117 function ToUnicodeString( n ) {
118 var string = ToHexString(n);
120 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
121 string = "0" + string;
122 }
124 return string;
125 }
126 function ToHexString( n ) {
127 var hex = new Array();
129 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
130 ;
131 }
133 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
134 hex[index] = Math.floor( n / Math.pow(16,mag) );
135 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
136 }
138 hex[hex.length] = n % 16;
140 var string ="";
142 for ( var index = 0 ; index < hex.length ; index++ ) {
143 switch ( hex[index] ) {
144 case 10:
145 string += "A";
146 break;
147 case 11:
148 string += "B";
149 break;
150 case 12:
151 string += "C";
152 break;
153 case 13:
154 string += "D";
155 break;
156 case 14:
157 string += "E";
158 break;
159 case 15:
160 string += "F";
161 break;
162 default:
163 string += hex[index];
164 }
165 }
167 if ( string.length == 1 ) {
168 string = "0" + string;
169 }
170 return string;
171 }