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-2.js
9 ECMA Section: 15.1.2.5 Function properties of the global object
10 unescape( string )
11 Description:
13 This tests the cases where there are fewer than 4 characters following "%u",
14 or fewer than 2 characters following "%" or "%u".
16 The unescape function computes a new version of a string value in which
17 each escape sequences of the sort that might be introduced by the escape
18 function is replaced with the character that it represents.
20 When the unescape function is called with one argument string, the
21 following steps are taken:
23 1. Call ToString(string).
24 2. Compute the number of characters in Result(1).
25 3. Let R be the empty string.
26 4. Let k be 0.
27 5. If k equals Result(2), return R.
28 6. Let c be the character at position k within Result(1).
29 7. If c is not %, go to step 18.
30 8. If k is greater than Result(2)-6, go to step 14.
31 9. If the character at position k+1 within result(1) is not u, go to step
32 14.
33 10. If the four characters at positions k+2, k+3, k+4, and k+5 within
34 Result(1) are not all hexadecimal digits, go to step 14.
35 11. Let c be the character whose Unicode encoding is the integer represented
36 by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
37 within Result(1).
38 12. Increase k by 5.
39 13. Go to step 18.
40 14. If k is greater than Result(2)-3, go to step 18.
41 15. If the two characters at positions k+1 and k+2 within Result(1) are not
42 both hexadecimal digits, go to step 18.
43 16. Let c be the character whose Unicode encoding is the integer represented
44 by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
45 within Result(1).
46 17. Increase k by 2.
47 18. Let R be a new string value computed by concatenating the previous value
48 of R and c.
49 19. Increase k by 1.
50 20. Go to step 5.
51 Author: christine@netscape.com
52 Date: 28 october 1997
53 */
55 var SECTION = "15.1.2.5-2";
56 var VERSION = "ECMA_1";
57 startTest();
58 var TITLE = "unescape(string)";
60 writeHeaderToLog( SECTION + " "+ TITLE);
62 // since there is only one character following "%", no conversion should occur.
64 for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE += 16 ) {
65 new TestCase( SECTION,
66 "unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +" )",
67 "%"+(ToHexString(CHARCODE)).substring(0,1),
68 unescape( "%" + (ToHexString(CHARCODE)).substring(0,1) ) );
69 }
71 // since there is only one character following "%u", no conversion should occur.
73 for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE +=16 ) {
74 new TestCase( SECTION,
75 "unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +" )",
76 "%u"+(ToHexString(CHARCODE)).substring(0,1),
77 unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1) ) );
78 }
81 // three char unicode string. no conversion should occur
83 for ( var CHARCODE = 1024; CHARCODE < 65536; CHARCODE+= 1234 ) {
84 new TestCase
85 ( SECTION,
86 "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3)+ " )",
88 "%u"+(ToUnicodeString(CHARCODE)).substring(0,3),
89 unescape( "%u"+(ToUnicodeString(CHARCODE)).substring(0,3) )
90 );
91 }
93 test();
95 function ToUnicodeString( n ) {
96 var string = ToHexString(n);
98 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
99 string = "0" + string;
100 }
102 return string;
103 }
104 function ToHexString( n ) {
105 var hex = new Array();
107 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
108 ;
109 }
111 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
112 hex[index] = Math.floor( n / Math.pow(16,mag) );
113 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
114 }
116 hex[hex.length] = n % 16;
118 var string ="";
120 for ( var index = 0 ; index < hex.length ; index++ ) {
121 switch ( hex[index] ) {
122 case 10:
123 string += "A";
124 break;
125 case 11:
126 string += "B";
127 break;
128 case 12:
129 string += "C";
130 break;
131 case 13:
132 string += "D";
133 break;
134 case 14:
135 string += "E";
136 break;
137 case 15:
138 string += "F";
139 break;
140 default:
141 string += hex[index];
142 }
143 }
145 if ( string.length == 1 ) {
146 string = "0" + string;
147 }
148 return string;
149 }