| 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 | gTestfile = '15.1.2.5-2.js'; |
| 7 | |
| 8 | /** |
| 9 | File Name: 15.1.2.5-2.js |
| 10 | ECMA Section: 15.1.2.5 Function properties of the global object |
| 11 | unescape( string ) |
| 12 | Description: |
| 13 | |
| 14 | This tests the cases where there are fewer than 4 characters following "%u", |
| 15 | or fewer than 2 characters following "%" or "%u". |
| 16 | |
| 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. |
| 20 | |
| 21 | When the unescape function is called with one argument string, the |
| 22 | following steps are taken: |
| 23 | |
| 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: [email protected] |
| 53 | Date: 28 october 1997 |
| 54 | */ |
| 55 | |
| 56 | var SECTION = "15.1.2.5-2"; |
| 57 | var VERSION = "ECMA_1"; |
| 58 | startTest(); |
| 59 | var TITLE = "unescape(string)"; |
| 60 | |
| 61 | writeHeaderToLog( SECTION + " "+ TITLE); |
| 62 | |
| 63 | // since there is only one character following "%", no conversion should occur. |
| 64 | |
| 65 | for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE += 16 ) { |
| 66 | new TestCase( SECTION, |
| 67 | "unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +" )", |
| 68 | "%"+(ToHexString(CHARCODE)).substring(0,1), |
| 69 | unescape( "%" + (ToHexString(CHARCODE)).substring(0,1) ) ); |
| 70 | } |
| 71 | |
| 72 | // since there is only one character following "%u", no conversion should occur. |
| 73 | |
| 74 | for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE +=16 ) { |
| 75 | new TestCase( SECTION, |
| 76 | "unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +" )", |
| 77 | "%u"+(ToHexString(CHARCODE)).substring(0,1), |
| 78 | unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1) ) ); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | // three char unicode string. no conversion should occur |
| 83 | |
| 84 | for ( var CHARCODE = 1024; CHARCODE < 65536; CHARCODE+= 1234 ) { |
| 85 | new TestCase |
| 86 | ( SECTION, |
| 87 | "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3)+ " )", |
| 88 | |
| 89 | "%u"+(ToUnicodeString(CHARCODE)).substring(0,3), |
| 90 | unescape( "%u"+(ToUnicodeString(CHARCODE)).substring(0,3) ) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | test(); |
| 95 | |
| 96 | function ToUnicodeString( n ) { |
| 97 | var string = ToHexString(n); |
| 98 | |
| 99 | for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) { |
| 100 | string = "0" + string; |
| 101 | } |
| 102 | |
| 103 | return string; |
| 104 | } |
| 105 | function ToHexString( n ) { |
| 106 | var hex = new Array(); |
| 107 | |
| 108 | for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) { |
| 109 | ; |
| 110 | } |
| 111 | |
| 112 | for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) { |
| 113 | hex[index] = Math.floor( n / Math.pow(16,mag) ); |
| 114 | n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) ); |
| 115 | } |
| 116 | |
| 117 | hex[hex.length] = n % 16; |
| 118 | |
| 119 | var string =""; |
| 120 | |
| 121 | for ( var index = 0 ; index < hex.length ; index++ ) { |
| 122 | switch ( hex[index] ) { |
| 123 | case 10: |
| 124 | string += "A"; |
| 125 | break; |
| 126 | case 11: |
| 127 | string += "B"; |
| 128 | break; |
| 129 | case 12: |
| 130 | string += "C"; |
| 131 | break; |
| 132 | case 13: |
| 133 | string += "D"; |
| 134 | break; |
| 135 | case 14: |
| 136 | string += "E"; |
| 137 | break; |
| 138 | case 15: |
| 139 | string += "F"; |
| 140 | break; |
| 141 | default: |
| 142 | string += hex[index]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( string.length == 1 ) { |
| 147 | string = "0" + string; |
| 148 | } |
| 149 | return string; |
| 150 | } |