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.5-3.js
9 ECMA Section: 15.1.2.5 Function properties of the global object
10 unescape( string )
12 Description:
13 This tests the cases where one of the four characters following "%u" is
14 not a hexidecimal character, or one of the two characters following "%"
15 or "%u" is not a hexidecimal character.
17 The unescape function computes a new version of a string value in which
18 each escape sequences of the sort that might be introduced by the escape
19 function is replaced with the character that it represents.
21 When the unescape function is called with one argument string, the
22 following steps are taken:
24 1. Call ToString(string).
25 2. Compute the number of characters in Result(1).
26 3. Let R be the empty string.
27 4. Let k be 0.
28 5. If k equals Result(2), return R.
29 6. Let c be the character at position k within Result(1).
30 7. If c is not %, go to step 18.
31 8. If k is greater than Result(2)-6, go to step 14.
32 9. If the character at position k+1 within result(1) is not u, go to step
33 14.
34 10. If the four characters at positions k+2, k+3, k+4, and k+5 within
35 Result(1) are not all hexadecimal digits, go to step 14.
36 11. Let c be the character whose Unicode encoding is the integer represented
37 by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
38 within Result(1).
39 12. Increase k by 5.
40 13. Go to step 18.
41 14. If k is greater than Result(2)-3, go to step 18.
42 15. If the two characters at positions k+1 and k+2 within Result(1) are not
43 both hexadecimal digits, go to step 18.
44 16. Let c be the character whose Unicode encoding is the integer represented
45 by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
46 within Result(1).
47 17. Increase k by 2.
48 18. Let R be a new string value computed by concatenating the previous value
49 of R and c.
50 19. Increase k by 1.
51 20. Go to step 5.
52 Author: christine@netscape.com
53 Date: 28 october 1997
54 */
57 var SECTION = "15.1.2.5-3";
58 var VERSION = "ECMA_1";
59 startTest();
60 var TITLE = "unescape(string)";
62 writeHeaderToLog( SECTION + " "+ TITLE);
64 for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
65 NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
67 new TestCase( SECTION,
68 "unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +
69 String.fromCharCode( NONHEXCHARCODE ) +" )" +
70 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
71 "%"+(ToHexString(CHARCODE)).substring(0,1)+
72 String.fromCharCode( NONHEXCHARCODE ),
73 unescape( "%" + (ToHexString(CHARCODE)).substring(0,1)+
74 String.fromCharCode( NONHEXCHARCODE ) ) );
75 }
76 for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
77 NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
79 new TestCase( SECTION,
80 "unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +
81 String.fromCharCode( NONHEXCHARCODE ) +" )" +
82 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
83 "%u"+(ToHexString(CHARCODE)).substring(0,1)+
84 String.fromCharCode( NONHEXCHARCODE ),
85 unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1)+
86 String.fromCharCode( NONHEXCHARCODE ) ) );
87 }
89 for ( var CHARCODE = 0, NONHEXCHARCODE = 0 ; CHARCODE < 65536; CHARCODE+= 54321, NONHEXCHARCODE++ ) {
90 NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
92 new TestCase( SECTION,
93 "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3) +
94 String.fromCharCode( NONHEXCHARCODE ) +" )" +
95 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
97 String.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE)).substring(0,2))) +
98 (ToUnicodeString(CHARCODE)).substring(2,3) +
99 String.fromCharCode( NONHEXCHARCODE ),
101 unescape( "%" + (ToUnicodeString(CHARCODE)).substring(0,3)+
102 String.fromCharCode( NONHEXCHARCODE ) ) );
103 }
105 test();
107 function getNextNonHexCharCode( n ) {
108 for ( ; n < Math.pow(2,16); n++ ) {
109 if ( ( n == 43 || n == 45 || n == 46 || n == 47 ||
110 (n >= 71 && n <= 90) || (n >= 103 && n <= 122) ||
111 n == 64 || n == 95 ) ) {
112 break;
113 } else {
114 n = ( n > 122 ) ? 0 : n;
115 }
116 }
117 return n;
118 }
119 function ToUnicodeString( n ) {
120 var string = ToHexString(n);
122 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
123 string = "0" + string;
124 }
126 return string;
127 }
128 function ToHexString( n ) {
129 var hex = new Array();
131 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
132 ;
133 }
135 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
136 hex[index] = Math.floor( n / Math.pow(16,mag) );
137 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
138 }
140 hex[hex.length] = n % 16;
142 var string ="";
144 for ( var index = 0 ; index < hex.length ; index++ ) {
145 switch ( hex[index] ) {
146 case 10:
147 string += "A";
148 break;
149 case 11:
150 string += "B";
151 break;
152 case 12:
153 string += "C";
154 break;
155 case 13:
156 string += "D";
157 break;
158 case 14:
159 string += "E";
160 break;
161 case 15:
162 string += "F";
163 break;
164 default:
165 string += hex[index];
166 }
167 }
169 if ( string.length == 1 ) {
170 string = "0" + string;
171 }
172 return string;
173 }