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