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 ***** */
39 gTestfile = '15.1.2.5-3.js';
42 File Name: 15.1.2.5-3.js
43 ECMA Section: 15.1.2.5 Function properties of the global object
47 This tests the cases where one of the four characters following "%u" is
48 not a hexidecimal character, or one of the two characters following "%"
49 or "%u" is not a hexidecimal character.
51 The unescape function computes a new version of a string value in which
52 each escape sequences of the sort that might be introduced by the escape
53 function is replaced with the character that it represents.
55 When the unescape 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. Let c be the character at position k within Result(1).
64 7. If c is not %, go to step 18.
65 8. If k is greater than Result(2)-6, go to step 14.
66 9. If the character at position k+1 within result(1) is not u, go to step
68 10. If the four characters at positions k+2, k+3, k+4, and k+5 within
69 Result(1) are not all hexadecimal digits, go to step 14.
70 11. Let c be the character whose Unicode encoding is the integer represented
71 by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
75 14. If k is greater than Result(2)-3, go to step 18.
76 15. If the two characters at positions k+1 and k+2 within Result(1) are not
77 both hexadecimal digits, go to step 18.
78 16. Let c be the character whose Unicode encoding is the integer represented
79 by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
82 18. Let R be a new string value computed by concatenating the previous value
86 Author: christine@netscape.com
91 var SECTION = "15.1.2.5-3";
92 var VERSION = "ECMA_1";
94 var TITLE = "unescape(string)";
96 writeHeaderToLog( SECTION + " "+ TITLE);
98 for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
99 NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
101 new TestCase( SECTION,
102 "unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +
103 String.fromCharCode( NONHEXCHARCODE ) +" )" +
104 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
105 "%"+(ToHexString(CHARCODE)).substring(0,1)+
106 String.fromCharCode( NONHEXCHARCODE ),
107 unescape( "%" + (ToHexString(CHARCODE)).substring(0,1)+
108 String.fromCharCode( NONHEXCHARCODE ) ) );
110 for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
111 NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
113 new TestCase( SECTION,
114 "unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +
115 String.fromCharCode( NONHEXCHARCODE ) +" )" +
116 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
117 "%u"+(ToHexString(CHARCODE)).substring(0,1)+
118 String.fromCharCode( NONHEXCHARCODE ),
119 unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1)+
120 String.fromCharCode( NONHEXCHARCODE ) ) );
123 for ( var CHARCODE = 0, NONHEXCHARCODE = 0 ; CHARCODE < 65536; CHARCODE+= 54321, NONHEXCHARCODE++ ) {
124 NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
126 new TestCase( SECTION,
127 "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3) +
128 String.fromCharCode( NONHEXCHARCODE ) +" )" +
129 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
131 String.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE)).substring(0,2))) +
132 (ToUnicodeString(CHARCODE)).substring(2,3) +
133 String.fromCharCode( NONHEXCHARCODE ),
135 unescape( "%" + (ToUnicodeString(CHARCODE)).substring(0,3)+
136 String.fromCharCode( NONHEXCHARCODE ) ) );
141 function getNextNonHexCharCode( n ) {
142 for ( ; n < Math.pow(2,16); n++ ) {
143 if ( ( n == 43 || n == 45 || n == 46 || n == 47 ||
144 (n >= 71 && n <= 90) || (n >= 103 && n <= 122) ||
145 n == 64 || n == 95 ) ) {
148 n = ( n > 122 ) ? 0 : n;
153 function ToUnicodeString( n ) {
154 var string = ToHexString(n);
156 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
157 string = "0" + string;
162 function ToHexString( n ) {
163 var hex = new Array();
165 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
169 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
170 hex[index] = Math.floor( n / Math.pow(16,mag) );
171 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
174 hex[hex.length] = n % 16;
178 for ( var index = 0 ; index < hex.length ; index++ ) {
179 switch ( hex[index] ) {
199 string += hex[index];
203 if ( string.length == 1 ) {
204 string = "0" + string;