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