| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
var BUGNUMBER = 392378; |
| 6 |
var summary = '15.5.4.11 - String.prototype.replace'; |
| 7 |
var rex, f, a, i; |
| 8 |
|
| 9 |
reportCompare( |
| 10 |
2, |
| 11 |
String.prototype.replace.length, |
| 12 |
"Section 1" |
| 13 |
); |
| 14 |
|
| 15 |
reportCompare( |
| 16 |
"321", |
| 17 |
String.prototype.replace.call(123, "123", "321"), |
| 18 |
"Section 2" |
| 19 |
); |
| 20 |
|
| 21 |
reportCompare( |
| 22 |
"ok", |
| 23 |
"ok".replace(), |
| 24 |
"Section 3" |
| 25 |
); |
| 26 |
|
| 27 |
reportCompare( |
| 28 |
"undefined**", |
| 29 |
"***".replace("*"), |
| 30 |
"Section 4" |
| 31 |
); |
| 32 |
|
| 33 |
reportCompare( |
| 34 |
"xnullz", |
| 35 |
"xyz".replace("y", null), |
| 36 |
"Section 5" |
| 37 |
); |
| 38 |
|
| 39 |
reportCompare( |
| 40 |
"x123", |
| 41 |
"xyz".replace("yz", 123), |
| 42 |
"Section 6" |
| 43 |
); |
| 44 |
|
| 45 |
reportCompare( |
| 46 |
"/x/g/x/g/x/g", |
| 47 |
"xxx".replace(/x/g, /x/g), |
| 48 |
"Section 7" |
| 49 |
); |
| 50 |
|
| 51 |
reportCompare( |
| 52 |
"ok", |
| 53 |
"undefined".replace(undefined, "ok"), |
| 54 |
"Section 8" |
| 55 |
); |
| 56 |
|
| 57 |
reportCompare( |
| 58 |
"ok", |
| 59 |
"null".replace(null, "ok"), |
| 60 |
"Section 9" |
| 61 |
); |
| 62 |
|
| 63 |
reportCompare( |
| 64 |
"ok", |
| 65 |
"123".replace(123, "ok"), |
| 66 |
"Section 10" |
| 67 |
); |
| 68 |
|
| 69 |
reportCompare( |
| 70 |
"xzyxyz", |
| 71 |
"xyzxyz".replace("yz", "zy"), |
| 72 |
"Section 11" |
| 73 |
); |
| 74 |
|
| 75 |
reportCompare( |
| 76 |
"ok", |
| 77 |
"(xyz)".replace("(xyz)", "ok"), |
| 78 |
"Section 12" |
| 79 |
); |
| 80 |
|
| 81 |
reportCompare( |
| 82 |
"*$&yzxyz", |
| 83 |
"xyzxyz".replace("x", "*$$&"), |
| 84 |
"Section 13" |
| 85 |
); |
| 86 |
|
| 87 |
reportCompare( |
| 88 |
"xy*z*", |
| 89 |
"xyz".replace("z", "*$&*"), |
| 90 |
"Section 14" |
| 91 |
); |
| 92 |
|
| 93 |
reportCompare( |
| 94 |
"xyxyzxyz", |
| 95 |
"xyzxyzxyz".replace("zxy", "$`"), |
| 96 |
"Section 15" |
| 97 |
); |
| 98 |
|
| 99 |
reportCompare( |
| 100 |
"zxyzxyzzxyz", |
| 101 |
"xyzxyz".replace("xy", "$'xyz"), |
| 102 |
"Section 16" |
| 103 |
); |
| 104 |
|
| 105 |
reportCompare( |
| 106 |
"$", |
| 107 |
"xyzxyz".replace("xyzxyz", "$"), |
| 108 |
"Section 17" |
| 109 |
); |
| 110 |
|
| 111 |
reportCompare( |
| 112 |
"x$0$00xyz", |
| 113 |
"xyzxyz".replace("yz", "$0$00"), |
| 114 |
"Section 18" |
| 115 |
); |
| 116 |
|
| 117 |
// Result for $1/$01 .. $99 is implementation-defined if searchValue is no |
| 118 |
// regular expression. $+ is a non-standard Mozilla extension. |
| 119 |
|
| 120 |
reportCompare( |
| 121 |
"$!$\"$-1$*$#$.$xyz$$", |
| 122 |
"xyzxyz$$".replace("xyz", "$!$\"$-1$*$#$.$"), |
| 123 |
"Section 19" |
| 124 |
); |
| 125 |
|
| 126 |
reportCompare( |
| 127 |
"$$$&$$$&$&", |
| 128 |
"$$$&".replace("$$", "$$$$$$&$&$$&"), |
| 129 |
"Section 20" |
| 130 |
); |
| 131 |
|
| 132 |
reportCompare( |
| 133 |
"yxx", |
| 134 |
"xxx".replace(/x/, "y"), |
| 135 |
"Section 21" |
| 136 |
); |
| 137 |
|
| 138 |
reportCompare( |
| 139 |
"yyy", |
| 140 |
"xxx".replace(/x/g, "y"), |
| 141 |
"Section 22" |
| 142 |
); |
| 143 |
|
| 144 |
rex = /x/, rex.lastIndex = 1; |
| 145 |
reportCompare( |
| 146 |
"yxx1", |
| 147 |
"xxx".replace(rex, "y") + rex.lastIndex, |
| 148 |
"Section 23" |
| 149 |
); |
| 150 |
|
| 151 |
rex = /x/g, rex.lastIndex = 1; |
| 152 |
reportCompare( |
| 153 |
"yyy0", |
| 154 |
"xxx".replace(rex, "y") + rex.lastIndex, |
| 155 |
"Section 24" |
| 156 |
); |
| 157 |
|
| 158 |
rex = /y/, rex.lastIndex = 1; |
| 159 |
reportCompare( |
| 160 |
"xxx1", |
| 161 |
"xxx".replace(rex, "y") + rex.lastIndex, |
| 162 |
"Section 25" |
| 163 |
); |
| 164 |
|
| 165 |
rex = /y/g, rex.lastIndex = 1; |
| 166 |
reportCompare( |
| 167 |
"xxx0", |
| 168 |
"xxx".replace(rex, "y") + rex.lastIndex, |
| 169 |
"Section 26" |
| 170 |
); |
| 171 |
|
| 172 |
rex = /x?/, rex.lastIndex = 1; |
| 173 |
reportCompare( |
| 174 |
"(x)xx1", |
| 175 |
"xxx".replace(rex, "($&)") + rex.lastIndex, |
| 176 |
"Section 27" |
| 177 |
); |
| 178 |
|
| 179 |
rex = /x?/g, rex.lastIndex = 1; |
| 180 |
reportCompare( |
| 181 |
"(x)(x)(x)()0", |
| 182 |
"xxx".replace(rex, "($&)") + rex.lastIndex, |
| 183 |
"Section 28" |
| 184 |
); |
| 185 |
|
| 186 |
rex = /y?/, rex.lastIndex = 1; |
| 187 |
reportCompare( |
| 188 |
"()xxx1", |
| 189 |
"xxx".replace(rex, "($&)") + rex.lastIndex, |
| 190 |
"Section 29" |
| 191 |
); |
| 192 |
|
| 193 |
rex = /y?/g, rex.lastIndex = 1; |
| 194 |
reportCompare( |
| 195 |
"()x()x()x()0", |
| 196 |
"xxx".replace(rex, "($&)") + rex.lastIndex, |
| 197 |
"Section 30" |
| 198 |
); |
| 199 |
|
| 200 |
reportCompare( |
| 201 |
"xy$0xy$zxy$zxyz$zxyz", |
| 202 |
"xyzxyzxyz".replace(/zxy/, "$0$`$$$&$$$'$"), |
| 203 |
"Section 31" |
| 204 |
); |
| 205 |
|
| 206 |
reportCompare( |
| 207 |
"xy$0xy$zxy$zxyz$$0xyzxy$zxy$z$z", |
| 208 |
"xyzxyzxyz".replace(/zxy/g, "$0$`$$$&$$$'$"), |
| 209 |
"Section 32" |
| 210 |
); |
| 211 |
|
| 212 |
reportCompare( |
| 213 |
"xyxyxyzxyxyxyz", |
| 214 |
"xyzxyz".replace(/(((x)(y)()()))()()()(z)/g, "$01$2$3$04$5$6$7$8$09$10"), |
| 215 |
"Section 33" |
| 216 |
); |
| 217 |
|
| 218 |
rex = RegExp( |
| 219 |
"()()()()()()()()()()" + |
| 220 |
"()()()()()()()()()()" + |
| 221 |
"()()()()()()()()()()" + |
| 222 |
"()()()()()()()()()()" + |
| 223 |
"()()()()()()()()()()" + |
| 224 |
"()()()()()()()()()()" + |
| 225 |
"()()()()()()()()()()" + |
| 226 |
"()()()()()()()()()()" + |
| 227 |
"()()()()()()()()()()" + |
| 228 |
"()()()()()()()()(y)"); |
| 229 |
reportCompare( |
| 230 |
"x(y)z", |
| 231 |
"xyz".replace(rex, "($99)"), |
| 232 |
"Section 34" |
| 233 |
); |
| 234 |
|
| 235 |
rex = RegExp( |
| 236 |
"()()()()()()()()()(x)" + |
| 237 |
"()()()()()()()()()()" + |
| 238 |
"()()()()()()()()()()" + |
| 239 |
"()()()()()()()()()()" + |
| 240 |
"()()()()()()()()()()" + |
| 241 |
"()()()()()()()()()()" + |
| 242 |
"()()()()()()()()()()" + |
| 243 |
"()()()()()()()()()()" + |
| 244 |
"()()()()()()()()()()" + |
| 245 |
"()()()()()()()()()(y)"); |
| 246 |
reportCompare( |
| 247 |
"(x0)z", |
| 248 |
"xyz".replace(rex, "($100)"), |
| 249 |
"Section 35" |
| 250 |
); |
| 251 |
|
| 252 |
reportCompare( |
| 253 |
"xyz(XYZ)", |
| 254 |
"xyzXYZ".replace(/XYZ/g, "($&)"), |
| 255 |
"Section 36" |
| 256 |
); |
| 257 |
|
| 258 |
reportCompare( |
| 259 |
"(xyz)(XYZ)", |
| 260 |
"xyzXYZ".replace(/xYz/gi, "($&)"), |
| 261 |
"Section 37" |
| 262 |
); |
| 263 |
|
| 264 |
reportCompare( |
| 265 |
"xyz\rxyz\n", |
| 266 |
"xyz\rxyz\n".replace(/xyz$/g, "($&)"), |
| 267 |
"Section 38" |
| 268 |
); |
| 269 |
|
| 270 |
reportCompare( |
| 271 |
"(xyz)\r(xyz)\n", |
| 272 |
"xyz\rxyz\n".replace(/xyz$/gm, "($&)"), |
| 273 |
"Section 39" |
| 274 |
); |
| 275 |
|
| 276 |
f = function () { return "failure" }; |
| 277 |
|
| 278 |
reportCompare( |
| 279 |
"ok", |
| 280 |
"ok".replace("x", f), |
| 281 |
"Section 40" |
| 282 |
); |
| 283 |
|
| 284 |
reportCompare( |
| 285 |
"ok", |
| 286 |
"ok".replace(/(?=k)ok/, f), |
| 287 |
"Section 41" |
| 288 |
); |
| 289 |
|
| 290 |
reportCompare( |
| 291 |
"ok", |
| 292 |
"ok".replace(/(?!)ok/, f), |
| 293 |
"Section 42" |
| 294 |
); |
| 295 |
|
| 296 |
reportCompare( |
| 297 |
"ok", |
| 298 |
"ok".replace(/ok(?!$)/, f), |
| 299 |
"Section 43" |
| 300 |
); |
| 301 |
|
| 302 |
f = function (sub, offs, str) { |
| 303 |
return ["", sub, typeof sub, offs, typeof offs, str, typeof str, ""] |
| 304 |
.join("|"); |
| 305 |
}; |
| 306 |
|
| 307 |
reportCompare( |
| 308 |
"x|y|string|1|number|xyz|string|z", |
| 309 |
"xyz".replace("y", f), |
| 310 |
"Section 44" |
| 311 |
); |
| 312 |
|
| 313 |
reportCompare( |
| 314 |
"x|(y)|string|1|number|x(y)z|string|z", |
| 315 |
"x(y)z".replace("(y)", f), |
| 316 |
"Section 45" |
| 317 |
); |
| 318 |
|
| 319 |
reportCompare( |
| 320 |
"x|y*|string|1|number|xy*z|string|z", |
| 321 |
"xy*z".replace("y*", f), |
| 322 |
"Section 46" |
| 323 |
); |
| 324 |
|
| 325 |
reportCompare( |
| 326 |
"12|3|string|2|number|12345|string|45", |
| 327 |
String.prototype.replace.call(1.2345e4, 3, f), |
| 328 |
"Section 47" |
| 329 |
); |
| 330 |
|
| 331 |
reportCompare( |
| 332 |
"|x|string|0|number|xxx|string|xx", |
| 333 |
"xxx".replace(/^x/g, f), |
| 334 |
"Section 48" |
| 335 |
); |
| 336 |
|
| 337 |
reportCompare( |
| 338 |
"xx|x|string|2|number|xxx|string|", |
| 339 |
"xxx".replace(/x$/g, f), |
| 340 |
"Section 49" |
| 341 |
); |
| 342 |
|
| 343 |
f = function (sub, paren, offs, str) { |
| 344 |
return ["", sub, typeof sub, paren, typeof paren, offs, typeof offs, |
| 345 |
str, typeof str, ""].join("|"); |
| 346 |
}; |
| 347 |
|
| 348 |
reportCompare( |
| 349 |
"xy|z|string|z|string|2|number|xyz|string|", |
| 350 |
"xyz".replace(/(z)/g, f), |
| 351 |
"Section 50" |
| 352 |
); |
| 353 |
|
| 354 |
reportCompare( |
| 355 |
"xyz||string||string|3|number|xyz|string|", |
| 356 |
"xyz".replace(/($)/g, f), |
| 357 |
"Section 51" |
| 358 |
); |
| 359 |
|
| 360 |
reportCompare( |
| 361 |
"|xy|string|y|string|0|number|xyz|string|z", |
| 362 |
"xyz".replace(/(?:x)(y)/g, f), |
| 363 |
"Section 52" |
| 364 |
); |
| 365 |
|
| 366 |
reportCompare( |
| 367 |
"|x|string|x|string|0|number|xyz|string|yz", |
| 368 |
"xyz".replace(/((?=xy)x)/g, f), |
| 369 |
"Section 53" |
| 370 |
); |
| 371 |
|
| 372 |
reportCompare( |
| 373 |
"|x|string|x|string|0|number|xyz|string|yz", |
| 374 |
"xyz".replace(/(x(?=y))/g, f), |
| 375 |
"Section 54" |
| 376 |
); |
| 377 |
|
| 378 |
reportCompare( |
| 379 |
"x|y|string|y|string|1|number|xyz|string|z", |
| 380 |
"xyz".replace(/((?!x)y)/g, f), |
| 381 |
"Section 55" |
| 382 |
); |
| 383 |
|
| 384 |
reportCompare( |
| 385 |
"|x|string|x|string|0|number|xyz|string|" + |
| 386 |
"|y|string||undefined|1|number|xyz|string|z", |
| 387 |
"xyz".replace(/y|(x)/g, f), |
| 388 |
"Section 56" |
| 389 |
); |
| 390 |
|
| 391 |
reportCompare( |
| 392 |
"xy|z|string||string|2|number|xyz|string|", |
| 393 |
"xyz".replace(/(z?)z/, f), |
| 394 |
"Section 57" |
| 395 |
); |
| 396 |
|
| 397 |
reportCompare( |
| 398 |
"xy|z|string||undefined|2|number|xyz|string|", |
| 399 |
"xyz".replace(/(z)?z/, f), |
| 400 |
"Section 58" |
| 401 |
); |
| 402 |
|
| 403 |
reportCompare( |
| 404 |
"xy|z|string||undefined|2|number|xyz|string|", |
| 405 |
"xyz".replace(/(z)?\1z/, f), |
| 406 |
"Section 59" |
| 407 |
); |
| 408 |
|
| 409 |
reportCompare( |
| 410 |
"xy|z|string||undefined|2|number|xyz|string|", |
| 411 |
"xyz".replace(/\1(z)?z/, f), |
| 412 |
"Section 60" |
| 413 |
); |
| 414 |
|
| 415 |
reportCompare( |
| 416 |
"xy|z|string||string|2|number|xyz|string|", |
| 417 |
"xyz".replace(/(z?\1)z/, f), |
| 418 |
"Section 61" |
| 419 |
); |
| 420 |
|
| 421 |
f = function (sub, paren1, paren2, offs, str) { |
| 422 |
return ["", sub, typeof sub, paren1, typeof paren1, paren2, typeof paren2, |
| 423 |
offs, typeof offs, str, typeof str, ""].join("|"); |
| 424 |
}; |
| 425 |
|
| 426 |
reportCompare( |
| 427 |
"x|y|string|y|string||undefined|1|number|xyz|string|z", |
| 428 |
"xyz".replace(/(y)(\1)?/, f), |
| 429 |
"Section 62" |
| 430 |
); |
| 431 |
|
| 432 |
reportCompare( |
| 433 |
"x|yy|string|y|string|y|string|1|number|xyyz|string|z", |
| 434 |
"xyyz".replace(/(y)(\1)?/g, f), |
| 435 |
"Section 63" |
| 436 |
); |
| 437 |
|
| 438 |
reportCompare( |
| 439 |
"x|y|string|y|string||undefined|1|number|xyyz|string|" + |
| 440 |
"|y|string|y|string||undefined|2|number|xyyz|string|z", |
| 441 |
"xyyz".replace(/(y)(\1)??/g, f), |
| 442 |
"Section 64" |
| 443 |
); |
| 444 |
|
| 445 |
reportCompare( |
| 446 |
"x|y|string|y|string|y|string|1|number|xyz|string|z", |
| 447 |
"xyz".replace(/(?=(y))(\1)?/, f), |
| 448 |
"Section 65" |
| 449 |
); |
| 450 |
|
| 451 |
reportCompare( |
| 452 |
"xyy|z|string||undefined||string|3|number|xyyz|string|", |
| 453 |
"xyyz".replace(/(?!(y)y)(\1)z/, f), |
| 454 |
"Section 66" |
| 455 |
); |
| 456 |
|
| 457 |
rex = RegExp( |
| 458 |
"()()()()()()()()()()" + |
| 459 |
"()()()()()()()()()()" + |
| 460 |
"()()()()()()()()()()" + |
| 461 |
"()()()()()()()()()()" + |
| 462 |
"()()()()()()()()()()" + |
| 463 |
"()()()()()()()()()()" + |
| 464 |
"()()()()()()()()()()" + |
| 465 |
"()()()()()()()()()()" + |
| 466 |
"()()()()()()()()()()" + |
| 467 |
"()()()()()()()()()()(z)?(y)"); |
| 468 |
a = ["sub"]; |
| 469 |
for (i = 1; i <= 102; ++i) |
| 470 |
a[i] = "p" + i; |
| 471 |
a[103] = "offs"; |
| 472 |
a[104] = "str"; |
| 473 |
a[105] = "return ['', sub, typeof sub, offs, typeof offs, str, typeof str, " + |
| 474 |
"p100, typeof p100, p101, typeof p101, p102, typeof p102, ''].join('|');"; |
| 475 |
f = Function.apply(null, a); |
| 476 |
reportCompare( |
| 477 |
"x|y|string|1|number|xyz|string||string||undefined|y|string|z", |
| 478 |
"xyz".replace(rex, f), |
| 479 |
"Section 67" |
| 480 |
); |
| 481 |
|
| 482 |
reportCompare( |
| 483 |
"undefined", |
| 484 |
"".replace(/.*/g, function () {}), |
| 485 |
"Section 68" |
| 486 |
); |
| 487 |
|
| 488 |
reportCompare( |
| 489 |
"nullxnullynullznull", |
| 490 |
"xyz".replace(/.??/g, function () { return null; }), |
| 491 |
"Section 69" |
| 492 |
); |
| 493 |
|
| 494 |
reportCompare( |
| 495 |
"111", |
| 496 |
"xyz".replace(/./g, function () { return 1; }), |
| 497 |
"Section 70" |
| 498 |
); |