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