1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
\r
2 /* ***** BEGIN LICENSE BLOCK *****
\r
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
\r
5 * The contents of this file are subject to the Mozilla Public License Version
\r
6 * 1.1 (the "License"); you may not use this file except in compliance with
\r
7 * the License. You may obtain a copy of the License at
\r
8 * http://www.mozilla.org/MPL/
\r
10 * Software distributed under the License is distributed on an "AS IS" basis,
\r
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
\r
12 * for the specific language governing rights and limitations under the
\r
15 * The Original Code is Mozilla Communicator client code, released
\r
18 * The Initial Developer of the Original Code is
\r
19 * Netscape Communications Corporation.
\r
20 * Portions created by the Initial Developer are Copyright (C) 1998
\r
21 * the Initial Developer. All Rights Reserved.
\r
25 * Alternatively, the contents of this file may be used under the terms of
\r
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
\r
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
\r
28 * in which case the provisions of the GPL or the LGPL are applicable instead
\r
29 * of those above. If you wish to allow use of your version of this file only
\r
30 * under the terms of either the GPL or the LGPL, and not to allow others to
\r
31 * use your version of this file under the terms of the MPL, indicate your
\r
32 * decision by deleting the provisions above and replace them with the notice
\r
33 * and other provisions required by the GPL or the LGPL. If you do not delete
\r
34 * the provisions above, a recipient may use your version of this file under
\r
35 * the terms of any one of the MPL, the GPL or the LGPL.
\r
37 * ***** END LICENSE BLOCK ***** */
\r
40 File Name: 15.1.2.4.js
\r
41 ECMA Section: 15.1.2.4 Function properties of the global object
\r
45 The escape function computes a new version of a string value in which
\r
46 certain characters have been replaced by a hexadecimal escape sequence.
\r
47 The result thus contains no special characters that might have special
\r
48 meaning within a URL.
\r
50 For characters whose Unicode encoding is 0xFF or less, a two-digit
\r
51 escape sequence of the form %xx is used in accordance with RFC1738.
\r
52 For characters whose Unicode encoding is greater than 0xFF, a four-
\r
53 digit escape sequence of the form %uxxxx is used.
\r
55 When the escape function is called with one argument string, the
\r
56 following steps are taken:
\r
58 1. Call ToString(string).
\r
59 2. Compute the number of characters in Result(1).
\r
60 3. Let R be the empty string.
\r
62 5. If k equals Result(2), return R.
\r
63 6. Get the character at position k within Result(1).
\r
64 7. If Result(6) is one of the 69 nonblank ASCII characters
\r
65 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
\r
66 0123456789 @*_+-./, go to step 14.
\r
67 8. Compute the 16-bit unsigned integer that is the Unicode character
\r
68 encoding of Result(6).
\r
69 9. If Result(8), is less than 256, go to step 12.
\r
70 10. Let S be a string containing six characters "%uwxyz" where wxyz are
\r
71 four hexadecimal digits encoding the value of Result(8).
\r
73 12. Let S be a string containing three characters "%xy" where xy are two
\r
74 hexadecimal digits encoding the value of Result(8).
\r
76 14. Let S be a string containing the single character Result(6).
\r
77 15. Let R be a new string value computed by concatenating the previous value
\r
79 16. Increase k by 1.
\r
82 Author: christine@netscape.com
\r
83 Date: 28 october 1997
\r
86 var SECTION = "15.1.2.4";
\r
87 var VERSION = "ECMA_1";
\r
89 var TITLE = "escape(string)";
\r
91 writeHeaderToLog( SECTION + " "+ TITLE);
\r
93 new TestCase( SECTION, "escape.length", 1, escape.length );
\r
94 new TestCase( SECTION, "escape.length = null; escape.length", 1, eval("escape.length = null; escape.length") );
\r
95 new TestCase( SECTION, "delete escape.length", false, delete escape.length );
\r
96 new TestCase( SECTION, "delete escape.length; escape.length", 1, eval("delete escape.length; escape.length") );
\r
97 new TestCase( SECTION, "var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS", "", eval("var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS") );
\r
99 new TestCase( SECTION, "escape()", "undefined", escape() );
\r
100 new TestCase( SECTION, "escape('')", "", escape('') );
\r
101 new TestCase( SECTION, "escape( null )", "null", escape(null) );
\r
102 new TestCase( SECTION, "escape( void 0 )", "undefined", escape(void 0) );
\r
103 new TestCase( SECTION, "escape( true )", "true", escape( true ) );
\r
104 new TestCase( SECTION, "escape( false )", "false", escape( false ) );
\r
106 new TestCase( SECTION, "escape( new Boolean(true) )", "true", escape(new Boolean(true)) );
\r
107 new TestCase( SECTION, "escape( new Boolean(false) )", "false", escape(new Boolean(false)) );
\r
109 new TestCase( SECTION, "escape( Number.NaN )", "NaN", escape(Number.NaN) );
\r
110 new TestCase( SECTION, "escape( -0 )", "0", escape( -0 ) );
\r
111 new TestCase( SECTION, "escape( 'Infinity' )", "Infinity", escape( "Infinity" ) );
\r
112 new TestCase( SECTION, "escape( Number.POSITIVE_INFINITY )", "Infinity", escape( Number.POSITIVE_INFINITY ) );
\r
113 new TestCase( SECTION, "escape( Number.NEGATIVE_INFINITY )", "-Infinity", escape( Number.NEGATIVE_INFINITY ) );
\r
115 var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
\r
117 new TestCase( SECTION, "escape( " +ASCII_TEST_STRING+" )", ASCII_TEST_STRING, escape( ASCII_TEST_STRING ) );
\r
119 // ASCII value less than
\r
121 for ( var CHARCODE = 0; CHARCODE < 32; CHARCODE++ ) {
\r
122 new TestCase( SECTION,
\r
123 "escape(String.fromCharCode("+CHARCODE+"))",
\r
124 "%"+ToHexString(CHARCODE),
\r
125 escape(String.fromCharCode(CHARCODE)) );
\r
127 for ( var CHARCODE = 128; CHARCODE < 256; CHARCODE++ ) {
\r
128 new TestCase( SECTION,
\r
129 "escape(String.fromCharCode("+CHARCODE+"))",
\r
130 "%"+ToHexString(CHARCODE),
\r
131 escape(String.fromCharCode(CHARCODE)) );
\r
134 for ( var CHARCODE = 256; CHARCODE < 1024; CHARCODE++ ) {
\r
135 new TestCase( SECTION,
\r
136 "escape(String.fromCharCode("+CHARCODE+"))",
\r
137 "%u"+ ToUnicodeString(CHARCODE),
\r
138 escape(String.fromCharCode(CHARCODE)) );
\r
140 for ( var CHARCODE = 65500; CHARCODE < 65536; CHARCODE++ ) {
\r
141 new TestCase( SECTION,
\r
142 "escape(String.fromCharCode("+CHARCODE+"))",
\r
143 "%u"+ ToUnicodeString(CHARCODE),
\r
144 escape(String.fromCharCode(CHARCODE)) );
\r
149 function ToUnicodeString( n ) {
\r
150 var string = ToHexString(n);
\r
152 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
\r
153 string = "0" + string;
\r
158 function ToHexString( n ) {
\r
159 var hex = new Array();
\r
161 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
\r
165 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
\r
166 hex[index] = Math.floor( n / Math.pow(16,mag) );
\r
167 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
\r
170 hex[hex.length] = n % 16;
\r
174 for ( var index = 0 ; index < hex.length ; index++ ) {
\r
175 switch ( hex[index] ) {
\r
195 string += hex[index];
\r
199 if ( string.length == 1 ) {
\r
200 string = "0" + string;
\r