michael@0: // |reftest| fails michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var BUGNUMBER = 392378; michael@0: var summary = '15.5.4.11 - String.prototype.replace'; michael@0: var rex, f, a, i; michael@0: michael@0: reportCompare( michael@0: 2, michael@0: String.prototype.replace.length, michael@0: "Section 1" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "321", michael@0: String.prototype.replace.call(123, "123", "321"), michael@0: "Section 2" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "ok".replace(), michael@0: "Section 3" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "undefined**", michael@0: "***".replace("*"), michael@0: "Section 4" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xnullz", michael@0: "xyz".replace("y", null), michael@0: "Section 5" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x123", michael@0: "xyz".replace("yz", 123), michael@0: "Section 6" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "/x/g/x/g/x/g", michael@0: "xxx".replace(/x/g, /x/g), michael@0: "Section 7" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "undefined".replace(undefined, "ok"), michael@0: "Section 8" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "null".replace(null, "ok"), michael@0: "Section 9" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "123".replace(123, "ok"), michael@0: "Section 10" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xzyxyz", michael@0: "xyzxyz".replace("yz", "zy"), michael@0: "Section 11" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "(xyz)".replace("(xyz)", "ok"), michael@0: "Section 12" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "*$&yzxyz", michael@0: "xyzxyz".replace("x", "*$$&"), michael@0: "Section 13" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy*z*", michael@0: "xyz".replace("z", "*$&*"), michael@0: "Section 14" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xyxyzxyz", michael@0: "xyzxyzxyz".replace("zxy", "$`"), michael@0: "Section 15" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "zxyzxyzzxyz", michael@0: "xyzxyz".replace("xy", "$'xyz"), michael@0: "Section 16" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "$", michael@0: "xyzxyz".replace("xyzxyz", "$"), michael@0: "Section 17" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x$0$00xyz", michael@0: "xyzxyz".replace("yz", "$0$00"), michael@0: "Section 18" michael@0: ); michael@0: michael@0: // Result for $1/$01 .. $99 is implementation-defined if searchValue is no michael@0: // regular expression. $+ is a non-standard Mozilla extension. michael@0: michael@0: reportCompare( michael@0: "$!$\"$-1$*$#$.$xyz$$", michael@0: "xyzxyz$$".replace("xyz", "$!$\"$-1$*$#$.$"), michael@0: "Section 19" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "$$$&$$$&$&", michael@0: "$$$&".replace("$$", "$$$$$$&$&$$&"), michael@0: "Section 20" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "yxx", michael@0: "xxx".replace(/x/, "y"), michael@0: "Section 21" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "yyy", michael@0: "xxx".replace(/x/g, "y"), michael@0: "Section 22" michael@0: ); michael@0: michael@0: rex = /x/, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "yxx1", michael@0: "xxx".replace(rex, "y") + rex.lastIndex, michael@0: "Section 23" michael@0: ); michael@0: michael@0: rex = /x/g, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "yyy0", michael@0: "xxx".replace(rex, "y") + rex.lastIndex, michael@0: "Section 24" michael@0: ); michael@0: michael@0: rex = /y/, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "xxx1", michael@0: "xxx".replace(rex, "y") + rex.lastIndex, michael@0: "Section 25" michael@0: ); michael@0: michael@0: rex = /y/g, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "xxx0", michael@0: "xxx".replace(rex, "y") + rex.lastIndex, michael@0: "Section 26" michael@0: ); michael@0: michael@0: rex = /x?/, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "(x)xx1", michael@0: "xxx".replace(rex, "($&)") + rex.lastIndex, michael@0: "Section 27" michael@0: ); michael@0: michael@0: rex = /x?/g, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "(x)(x)(x)()0", michael@0: "xxx".replace(rex, "($&)") + rex.lastIndex, michael@0: "Section 28" michael@0: ); michael@0: michael@0: rex = /y?/, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "()xxx1", michael@0: "xxx".replace(rex, "($&)") + rex.lastIndex, michael@0: "Section 29" michael@0: ); michael@0: michael@0: rex = /y?/g, rex.lastIndex = 1; michael@0: reportCompare( michael@0: "()x()x()x()0", michael@0: "xxx".replace(rex, "($&)") + rex.lastIndex, michael@0: "Section 30" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy$0xy$zxy$zxyz$zxyz", michael@0: "xyzxyzxyz".replace(/zxy/, "$0$`$$$&$$$'$"), michael@0: "Section 31" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy$0xy$zxy$zxyz$$0xyzxy$zxy$z$z", michael@0: "xyzxyzxyz".replace(/zxy/g, "$0$`$$$&$$$'$"), michael@0: "Section 32" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xyxyxyzxyxyxyz", michael@0: "xyzxyz".replace(/(((x)(y)()()))()()()(z)/g, "$01$2$3$04$5$6$7$8$09$10"), michael@0: "Section 33" michael@0: ); michael@0: michael@0: rex = RegExp( michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()(y)"); michael@0: reportCompare( michael@0: "x(y)z", michael@0: "xyz".replace(rex, "($99)"), michael@0: "Section 34" michael@0: ); michael@0: michael@0: rex = RegExp( michael@0: "()()()()()()()()()(x)" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()(y)"); michael@0: reportCompare( michael@0: "(x0)z", michael@0: "xyz".replace(rex, "($100)"), michael@0: "Section 35" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xyz(XYZ)", michael@0: "xyzXYZ".replace(/XYZ/g, "($&)"), michael@0: "Section 36" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "(xyz)(XYZ)", michael@0: "xyzXYZ".replace(/xYz/gi, "($&)"), michael@0: "Section 37" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xyz\rxyz\n", michael@0: "xyz\rxyz\n".replace(/xyz$/g, "($&)"), michael@0: "Section 38" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "(xyz)\r(xyz)\n", michael@0: "xyz\rxyz\n".replace(/xyz$/gm, "($&)"), michael@0: "Section 39" michael@0: ); michael@0: michael@0: f = function () { return "failure" }; michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "ok".replace("x", f), michael@0: "Section 40" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "ok".replace(/(?=k)ok/, f), michael@0: "Section 41" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "ok".replace(/(?!)ok/, f), michael@0: "Section 42" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "ok", michael@0: "ok".replace(/ok(?!$)/, f), michael@0: "Section 43" michael@0: ); michael@0: michael@0: f = function (sub, offs, str) { michael@0: return ["", sub, typeof sub, offs, typeof offs, str, typeof str, ""] michael@0: .join("|"); michael@0: }; michael@0: michael@0: reportCompare( michael@0: "x|y|string|1|number|xyz|string|z", michael@0: "xyz".replace("y", f), michael@0: "Section 44" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x|(y)|string|1|number|x(y)z|string|z", michael@0: "x(y)z".replace("(y)", f), michael@0: "Section 45" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x|y*|string|1|number|xy*z|string|z", michael@0: "xy*z".replace("y*", f), michael@0: "Section 46" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "12|3|string|2|number|12345|string|45", michael@0: String.prototype.replace.call(1.2345e4, 3, f), michael@0: "Section 47" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "|x|string|0|number|xxx|string|xx", michael@0: "xxx".replace(/^x/g, f), michael@0: "Section 48" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xx|x|string|2|number|xxx|string|", michael@0: "xxx".replace(/x$/g, f), michael@0: "Section 49" michael@0: ); michael@0: michael@0: f = function (sub, paren, offs, str) { michael@0: return ["", sub, typeof sub, paren, typeof paren, offs, typeof offs, michael@0: str, typeof str, ""].join("|"); michael@0: }; michael@0: michael@0: reportCompare( michael@0: "xy|z|string|z|string|2|number|xyz|string|", michael@0: "xyz".replace(/(z)/g, f), michael@0: "Section 50" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xyz||string||string|3|number|xyz|string|", michael@0: "xyz".replace(/($)/g, f), michael@0: "Section 51" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "|xy|string|y|string|0|number|xyz|string|z", michael@0: "xyz".replace(/(?:x)(y)/g, f), michael@0: "Section 52" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "|x|string|x|string|0|number|xyz|string|yz", michael@0: "xyz".replace(/((?=xy)x)/g, f), michael@0: "Section 53" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "|x|string|x|string|0|number|xyz|string|yz", michael@0: "xyz".replace(/(x(?=y))/g, f), michael@0: "Section 54" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x|y|string|y|string|1|number|xyz|string|z", michael@0: "xyz".replace(/((?!x)y)/g, f), michael@0: "Section 55" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "|x|string|x|string|0|number|xyz|string|" + michael@0: "|y|string||undefined|1|number|xyz|string|z", michael@0: "xyz".replace(/y|(x)/g, f), michael@0: "Section 56" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy|z|string||string|2|number|xyz|string|", michael@0: "xyz".replace(/(z?)z/, f), michael@0: "Section 57" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy|z|string||undefined|2|number|xyz|string|", michael@0: "xyz".replace(/(z)?z/, f), michael@0: "Section 58" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy|z|string||undefined|2|number|xyz|string|", michael@0: "xyz".replace(/(z)?\1z/, f), michael@0: "Section 59" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy|z|string||undefined|2|number|xyz|string|", michael@0: "xyz".replace(/\1(z)?z/, f), michael@0: "Section 60" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xy|z|string||string|2|number|xyz|string|", michael@0: "xyz".replace(/(z?\1)z/, f), michael@0: "Section 61" michael@0: ); michael@0: michael@0: f = function (sub, paren1, paren2, offs, str) { michael@0: return ["", sub, typeof sub, paren1, typeof paren1, paren2, typeof paren2, michael@0: offs, typeof offs, str, typeof str, ""].join("|"); michael@0: }; michael@0: michael@0: reportCompare( michael@0: "x|y|string|y|string||undefined|1|number|xyz|string|z", michael@0: "xyz".replace(/(y)(\1)?/, f), michael@0: "Section 62" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x|yy|string|y|string|y|string|1|number|xyyz|string|z", michael@0: "xyyz".replace(/(y)(\1)?/g, f), michael@0: "Section 63" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x|y|string|y|string||undefined|1|number|xyyz|string|" + michael@0: "|y|string|y|string||undefined|2|number|xyyz|string|z", michael@0: "xyyz".replace(/(y)(\1)??/g, f), michael@0: "Section 64" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "x|y|string|y|string|y|string|1|number|xyz|string|z", michael@0: "xyz".replace(/(?=(y))(\1)?/, f), michael@0: "Section 65" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "xyy|z|string||undefined||string|3|number|xyyz|string|", michael@0: "xyyz".replace(/(?!(y)y)(\1)z/, f), michael@0: "Section 66" michael@0: ); michael@0: michael@0: rex = RegExp( michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()" + michael@0: "()()()()()()()()()()(z)?(y)"); michael@0: a = ["sub"]; michael@0: for (i = 1; i <= 102; ++i) michael@0: a[i] = "p" + i; michael@0: a[103] = "offs"; michael@0: a[104] = "str"; michael@0: a[105] = "return ['', sub, typeof sub, offs, typeof offs, str, typeof str, " + michael@0: "p100, typeof p100, p101, typeof p101, p102, typeof p102, ''].join('|');"; michael@0: f = Function.apply(null, a); michael@0: reportCompare( michael@0: "x|y|string|1|number|xyz|string||string||undefined|y|string|z", michael@0: "xyz".replace(rex, f), michael@0: "Section 67" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "undefined", michael@0: "".replace(/.*/g, function () {}), michael@0: "Section 68" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "nullxnullynullznull", michael@0: "xyz".replace(/.??/g, function () { return null; }), michael@0: "Section 69" michael@0: ); michael@0: michael@0: reportCompare( michael@0: "111", michael@0: "xyz".replace(/./g, function () { return 1; }), michael@0: "Section 70" michael@0: );