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