|
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-2.js |
|
9 ECMA Section: 15.1.2.5 Function properties of the global object |
|
10 unescape( string ) |
|
11 Description: |
|
12 |
|
13 This tests the cases where there are fewer than 4 characters following "%u", |
|
14 or fewer than 2 characters following "%" or "%u". |
|
15 |
|
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. |
|
19 |
|
20 When the unescape function is called with one argument string, the |
|
21 following steps are taken: |
|
22 |
|
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 */ |
|
54 |
|
55 var SECTION = "15.1.2.5-2"; |
|
56 var VERSION = "ECMA_1"; |
|
57 startTest(); |
|
58 var TITLE = "unescape(string)"; |
|
59 |
|
60 writeHeaderToLog( SECTION + " "+ TITLE); |
|
61 |
|
62 // since there is only one character following "%", no conversion should occur. |
|
63 |
|
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 } |
|
70 |
|
71 // since there is only one character following "%u", no conversion should occur. |
|
72 |
|
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 } |
|
79 |
|
80 |
|
81 // three char unicode string. no conversion should occur |
|
82 |
|
83 for ( var CHARCODE = 1024; CHARCODE < 65536; CHARCODE+= 1234 ) { |
|
84 new TestCase |
|
85 ( SECTION, |
|
86 "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3)+ " )", |
|
87 |
|
88 "%u"+(ToUnicodeString(CHARCODE)).substring(0,3), |
|
89 unescape( "%u"+(ToUnicodeString(CHARCODE)).substring(0,3) ) |
|
90 ); |
|
91 } |
|
92 |
|
93 test(); |
|
94 |
|
95 function ToUnicodeString( n ) { |
|
96 var string = ToHexString(n); |
|
97 |
|
98 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) { |
|
99 string = "0" + string; |
|
100 } |
|
101 |
|
102 return string; |
|
103 } |
|
104 function ToHexString( n ) { |
|
105 var hex = new Array(); |
|
106 |
|
107 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) { |
|
108 ; |
|
109 } |
|
110 |
|
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 } |
|
115 |
|
116 hex[hex.length] = n % 16; |
|
117 |
|
118 var string =""; |
|
119 |
|
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 } |
|
144 |
|
145 if ( string.length == 1 ) { |
|
146 string = "0" + string; |
|
147 } |
|
148 return string; |
|
149 } |