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
File Name: 15.1.2.5-3.js
40
ECMA Section: 15.1.2.5 Function properties of the global object
44
This tests the cases where one of the four characters following "%u" is
45
not a hexidecimal character, or one of the two characters following "%"
46
or "%u" is not a hexidecimal character.
48
The unescape function computes a new version of a string value in which
49
each escape sequences of the sort that might be introduced by the escape
50
function is replaced with the character that it represents.
52
When the unescape function is called with one argument string, the
53
following steps are taken:
55
1. Call ToString(string).
56
2. Compute the number of characters in Result(1).
57
3. Let R be the empty string.
59
5. If k equals Result(2), return R.
60
6. Let c be the character at position k within Result(1).
61
7. If c is not %, go to step 18.
62
8. If k is greater than Result(2)-6, go to step 14.
63
9. If the character at position k+1 within result(1) is not u, go to step
65
10. If the four characters at positions k+2, k+3, k+4, and k+5 within
66
Result(1) are not all hexadecimal digits, go to step 14.
67
11. Let c be the character whose Unicode encoding is the integer represented
68
by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
72
14. If k is greater than Result(2)-3, go to step 18.
73
15. If the two characters at positions k+1 and k+2 within Result(1) are not
74
both hexadecimal digits, go to step 18.
75
16. Let c be the character whose Unicode encoding is the integer represented
76
by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
79
18. Let R be a new string value computed by concatenating the previous value
83
Author: christine@netscape.com
88
var SECTION = "15.1.2.5-3";
89
var VERSION = "ECMA_1";
91
var TITLE = "unescape(string)";
93
writeHeaderToLog( SECTION + " "+ TITLE);
95
for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
96
NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
98
new TestCase( SECTION,
99
"unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +
100
String.fromCharCode( NONHEXCHARCODE ) +" )" +
101
"[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
102
"%"+(ToHexString(CHARCODE)).substring(0,1)+
103
String.fromCharCode( NONHEXCHARCODE ),
104
unescape( "%" + (ToHexString(CHARCODE)).substring(0,1)+
105
String.fromCharCode( NONHEXCHARCODE ) ) );
107
for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
108
NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
110
new TestCase( SECTION,
111
"unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +
112
String.fromCharCode( NONHEXCHARCODE ) +" )" +
113
"[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
114
"%u"+(ToHexString(CHARCODE)).substring(0,1)+
115
String.fromCharCode( NONHEXCHARCODE ),
116
unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1)+
117
String.fromCharCode( NONHEXCHARCODE ) ) );
120
for ( var CHARCODE = 0, NONHEXCHARCODE = 0 ; CHARCODE < 65536; CHARCODE+= 54321, NONHEXCHARCODE++ ) {
121
NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
123
new TestCase( SECTION,
124
"unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3) +
125
String.fromCharCode( NONHEXCHARCODE ) +" )" +
126
"[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
128
String.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE)).substring(0,2))) +
129
(ToUnicodeString(CHARCODE)).substring(2,3) +
130
String.fromCharCode( NONHEXCHARCODE ),
132
unescape( "%" + (ToUnicodeString(CHARCODE)).substring(0,3)+
133
String.fromCharCode( NONHEXCHARCODE ) ) );
138
function getNextNonHexCharCode( n ) {
139
for ( ; n < Math.pow(2,16); n++ ) {
140
if ( ( n == 43 || n == 45 || n == 46 || n == 47 ||
141
(n >= 71 && n <= 90) || (n >= 103 && n <= 122) ||
142
n == 64 || n == 95 ) ) {
145
n = ( n > 122 ) ? 0 : n;
150
function ToUnicodeString( n ) {
151
var string = ToHexString(n);
153
for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
154
string = "0" + string;
159
function ToHexString( n ) {
160
var hex = new Array();
162
for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
166
for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
167
hex[index] = Math.floor( n / Math.pow(16,mag) );
168
n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
171
hex[hex.length] = n % 16;
175
for ( var index = 0 ; index < hex.length ; index++ ) {
176
switch ( hex[index] ) {
196
string += hex[index];
200
if ( string.length == 1 ) {
201
string = "0" + string;