| 1 |
describe('When overridden, RegExp.prototype.exec()', function() { |
| 2 |
|
| 3 |
beforeEach(function() { |
| 4 |
XRegExp.install('natives'); |
| 5 |
}); |
| 6 |
|
| 7 |
// This is broken in old Firefox (tested in v2.0; it works in v8+), but not for any fault of |
| 8 |
// XRegExp. Uncomment this test if future XRegExp fixes it for old Firefox. |
| 9 |
/*it('should type convert an undefined argument to a string', function() { |
| 10 |
expect(/undefined/.exec()).toEqualMatch(['undefined']); |
| 11 |
});*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* NOTE: These specs should mirror those for RegExp.prototype.test, nonglobal |
| 15 |
* String.prototype.match, XRegExp.exec, and XRegExp.test as closely as possible. |
| 16 |
*/ |
| 17 |
|
| 18 |
// TODO: Copy/update specs from XRegExp.exec here |
| 19 |
|
| 20 |
/* |
| 21 |
* The following specs: |
| 22 |
* - Have no corresponding specs for XRegExp.exec and XRegExp.test. |
| 23 |
* - Are mirrored by RegExp.prototype.test and nonglobal String.prototype.match. |
| 24 |
*/ |
| 25 |
|
| 26 |
it('should ignore lastIndex and set the search start position at 0 for a nonglobal regex', function() { |
| 27 |
var regex = /x/; |
| 28 |
|
| 29 |
regex.lastIndex = 4; |
| 30 |
expect(regex.exec('123x5')).toEqualMatch(['x']); |
| 31 |
}); |
| 32 |
|
| 33 |
/* |
| 34 |
* The following specs: |
| 35 |
* - Have no corresponding specs for XRegExp.exec, XRegExp.test, and nonglobal |
| 36 |
* String.prototype.match. |
| 37 |
* - Are mirrored by RegExp.prototype.test. |
| 38 |
*/ |
| 39 |
|
| 40 |
it('should use lastIndex to set the search start position for a global regex', function() { |
| 41 |
var regex = /x/g; |
| 42 |
|
| 43 |
regex.lastIndex = 4; |
| 44 |
expect(regex.exec('123x5')).toBeNull(); |
| 45 |
|
| 46 |
regex.lastIndex = 2; |
| 47 |
expect(regex.exec('123x5')).toEqualMatch(['x']); |
| 48 |
}); |
| 49 |
|
| 50 |
it('should type convert lastIndex when setting the search start position', function() { |
| 51 |
var regex = /x/g; |
| 52 |
|
| 53 |
regex.lastIndex = '3'; |
| 54 |
expect(regex.exec('123x5')).toEqualMatch(['x']); |
| 55 |
|
| 56 |
regex.lastIndex = '4'; |
| 57 |
expect(regex.exec('123x5')).toBeNull(); |
| 58 |
}); |
| 59 |
|
| 60 |
/* |
| 61 |
* The following specs: |
| 62 |
* - Have no corresponding specs for RegExp.prototype.test and XRegExp.test. |
| 63 |
* - Are mirrored by XRegExp.exec and nonglobal String.prototype.match. |
| 64 |
*/ |
| 65 |
|
| 66 |
xdescribe('provides extensions:', function() { |
| 67 |
// TODO: Copy/update named capture specs from XRegExp.exec here |
| 68 |
}); |
| 69 |
|
| 70 |
}); |
| 71 |
|
| 72 |
describe('When overridden, RegExp.prototype.test()', function() { |
| 73 |
|
| 74 |
beforeEach(function() { |
| 75 |
XRegExp.install('natives'); |
| 76 |
}); |
| 77 |
|
| 78 |
/* |
| 79 |
* NOTE: These specs should mirror those for RegExp.prototype.exec, nonglobal |
| 80 |
* String.prototype.match, XRegExp.test, and XRegExp.exec as closely as possible. |
| 81 |
*/ |
| 82 |
|
| 83 |
// TODO: Copy/update specs from XRegExp.test here |
| 84 |
|
| 85 |
/* |
| 86 |
* The following specs: |
| 87 |
* - Have no corresponding specs for XRegExp.test and XRegExp.exec. |
| 88 |
* - Are mirrored by RegExp.prototype.exec and nonglobal String.prototype.match. |
| 89 |
*/ |
| 90 |
|
| 91 |
it('should ignore lastIndex and set the search start position at 0 for a nonglobal regex', function() { |
| 92 |
var regex = /x/; |
| 93 |
|
| 94 |
regex.lastIndex = 4; |
| 95 |
expect(regex.test('123x5')).toBe(true); |
| 96 |
}); |
| 97 |
|
| 98 |
/* |
| 99 |
* The following specs: |
| 100 |
* - Have no corresponding specs for XRegExp.test, XRegExp.exec, and nonglobal |
| 101 |
* String.prototype.match. |
| 102 |
* - Are mirrored by RegExp.prototype.exec. |
| 103 |
*/ |
| 104 |
|
| 105 |
it('should use lastIndex to set the search start position for a global regex', function() { |
| 106 |
var regex = /x/g; |
| 107 |
|
| 108 |
regex.lastIndex = 4; |
| 109 |
expect(regex.test('123x5')).toBe(false); |
| 110 |
|
| 111 |
regex.lastIndex = 2; |
| 112 |
expect(regex.test('123x5')).toBe(true); |
| 113 |
}); |
| 114 |
|
| 115 |
it('should type convert lastIndex when setting the search start position', function() { |
| 116 |
var regex = /x/g; |
| 117 |
|
| 118 |
regex.lastIndex = '3'; |
| 119 |
expect(regex.test('123x5')).toBe(true); |
| 120 |
|
| 121 |
regex.lastIndex = '4'; |
| 122 |
expect(regex.test('123x5')).toBe(false); |
| 123 |
}); |
| 124 |
|
| 125 |
// NOTE: The fixed RegExp.prototype.test does not provide any extensions to native handling |
| 126 |
|
| 127 |
}); |
| 128 |
|
| 129 |
describe('When overridden, String.prototype.match()', function() { |
| 130 |
|
| 131 |
beforeEach(function() { |
| 132 |
XRegExp.install('natives'); |
| 133 |
}); |
| 134 |
|
| 135 |
describe('with a global regex', function() { |
| 136 |
|
| 137 |
it('should return an array with all matches', function() { |
| 138 |
expect('a bc'.match(/(\w)/g)).toEqualMatch(['a', 'b', 'c']); |
| 139 |
}); |
| 140 |
|
| 141 |
it('should return null if no match is found', function() { |
| 142 |
expect('a bc'.match(/x/g)).toBeNull(); |
| 143 |
}); |
| 144 |
|
| 145 |
it('should reset lastIndex to 0 when a match is found', function() { |
| 146 |
var regex = /x/g; |
| 147 |
regex.lastIndex = 1; |
| 148 |
'123x5'.match(regex); |
| 149 |
|
| 150 |
expect(regex.lastIndex).toBe(0); |
| 151 |
}); |
| 152 |
|
| 153 |
it('should reset lastIndex to 0 when no match is found', function() { |
| 154 |
var regex = /x/g; |
| 155 |
regex.lastIndex = 1; |
| 156 |
'123'.match(regex); |
| 157 |
|
| 158 |
expect(regex.lastIndex).toBe(0); |
| 159 |
}); |
| 160 |
|
| 161 |
it('should start the search at the beginning of the string, ignoring lastIndex', function() { |
| 162 |
var regex = /x/g; |
| 163 |
regex.lastIndex = 4; |
| 164 |
|
| 165 |
expect('123x5'.match(regex)).toBeTruthy(); |
| 166 |
}); |
| 167 |
|
| 168 |
it('should convert any nonstring context to a string (except null and undefined)', function() { |
| 169 |
expect(String.prototype.match.call(11, /1/g)).toEqualMatch(['1', '1']); |
| 170 |
}); |
| 171 |
|
| 172 |
it('should throw an exception when called on null or undefined context, if strict mode is supported', function() { |
| 173 |
[null, undefined].forEach(function(value) { |
| 174 |
// This doesn't work the same when strict mode isn't supported, because the match |
| 175 |
// method will be called with the global object (window) as its context, rather |
| 176 |
// than null or undefined |
| 177 |
if (hasStrictMode) { |
| 178 |
expect(function() {String.prototype.match.call(value, /x/g);}).toThrowError(TypeError); |
| 179 |
} else { |
| 180 |
// Keep the assertion count consistent cross-browser |
| 181 |
expect(hasStrictMode).toBe(false); |
| 182 |
} |
| 183 |
}); |
| 184 |
}); |
| 185 |
|
| 186 |
}); |
| 187 |
|
| 188 |
describe('with a nonglobal regex', function() { |
| 189 |
|
| 190 |
/* |
| 191 |
* NOTE: These specs should mirror those for RegExp.prototype.exec, RegExp.prototype.test, |
| 192 |
* XRegExp.exec, and XRegExp.test as closely as possible. |
| 193 |
*/ |
| 194 |
|
| 195 |
// TODO: Copy/update specs from RegExp.prototype.exec here |
| 196 |
|
| 197 |
/* |
| 198 |
* The following specs: |
| 199 |
* - Have no corresponding specs for RegExp.prototype.exec, RegExp.prototype.test, |
| 200 |
* XRegExp.test, and XRegExp.exec. |
| 201 |
*/ |
| 202 |
|
| 203 |
it('should convert any provided non RegExp object to a RegExp', function() { |
| 204 |
// These don't error because, per the spec, the values are passed through new RegExp() |
| 205 |
// before being used as the context object for the (fixed) RegExp.prototype.exec |
| 206 |
var tests = [ |
| 207 |
{str: '12', regex: '^(1)', result: ['1', '1']}, |
| 208 |
// This would throw if the string was converted to an XRegExp rather than RegExp |
| 209 |
{str: '\x01', regex: '\\1', result: ['\x01']}, |
| 210 |
// The converted value '[object Object]' creates a character class |
| 211 |
{str: '[obj]', regex: {}, result: ['o']}, |
| 212 |
{str: 'null', regex: null, result: ['null']} |
| 213 |
]; |
| 214 |
|
| 215 |
tests.forEach(function(test) { |
| 216 |
expect(test.str.match(test.regex)).toEqualMatch(test.result); |
| 217 |
}); |
| 218 |
|
| 219 |
// The native RegExp with an explicit undefined pattern incorrectly creates /undefined/ |
| 220 |
// in Firefox 3 and 3.6 (but not v4) |
| 221 |
//expect('undefined'.match()).toEqualMatch(['']); |
| 222 |
//expect('undefined'.match(undefined)).toEqualMatch(['']); |
| 223 |
}); |
| 224 |
|
| 225 |
it('should throw an exception when called on null or undefined context, if strict mode is supported', function() { |
| 226 |
[null, undefined].forEach(function(value) { |
| 227 |
// This doesn't work the same when strict mode isn't supported, because the match |
| 228 |
// method will be called with the global object (window) as its context, rather |
| 229 |
// than null or undefined |
| 230 |
if (hasStrictMode) { |
| 231 |
expect(function() {String.prototype.match.call(value, /x/);}).toThrowError(TypeError); |
| 232 |
} else { |
| 233 |
// Keep the assertion count consistent cross-browser |
| 234 |
expect(hasStrictMode).toBe(false); |
| 235 |
} |
| 236 |
}); |
| 237 |
}); |
| 238 |
|
| 239 |
/* |
| 240 |
* The following specs: |
| 241 |
* - Have no corresponding specs for RegExp.prototype.test and XRegExp.test. |
| 242 |
* - Are mirrored by RegExp.prototype.exec and XRegExp.exec. |
| 243 |
*/ |
| 244 |
|
| 245 |
xdescribe('provides extensions:', function() { |
| 246 |
// TODO: Copy/update specs from RegExp.prototype.exec here |
| 247 |
}); |
| 248 |
|
| 249 |
}); |
| 250 |
|
| 251 |
}); |
| 252 |
|
| 253 |
describe('When overridden, String.prototype.replace()', function() { |
| 254 |
|
| 255 |
beforeEach(function() { |
| 256 |
XRegExp.install('natives'); |
| 257 |
}); |
| 258 |
|
| 259 |
/* |
| 260 |
* NOTE: These specs should mirror those for XRegExp.replace as closely as possible. |
| 261 |
*/ |
| 262 |
|
| 263 |
it('should replace the first match only when given a nonglobal regex', function() { |
| 264 |
expect('aaa'.replace(/a/, 'b')).toBe('baa'); |
| 265 |
}); |
| 266 |
|
| 267 |
it('should replace all matches when given a global regex', function() { |
| 268 |
expect('aaa'.replace(/a/g, 'b')).toBe('bbb'); |
| 269 |
}); |
| 270 |
|
| 271 |
it('should replace the first match only when given a string as the search pattern', function() { |
| 272 |
expect('aaa'.replace('a', 'b')).toBe('baa'); |
| 273 |
}); |
| 274 |
|
| 275 |
it('should not type convert a string search pattern to a regex', function() { |
| 276 |
expect('aaa'.replace('a(a)', 'b')).toBe('aaa'); |
| 277 |
expect('a(a)a'.replace('a(a)', 'b')).toBe('ba'); |
| 278 |
}); |
| 279 |
|
| 280 |
it('should handle single-digit backreference $1 in the replacement string', function() { |
| 281 |
expect('aaa'.replace(/a(a)/, '$1b')).toBe('aba'); |
| 282 |
|
| 283 |
// Backreference to a nonparticipating capturing group |
| 284 |
expect('test'.replace(/t|(e)/g, '$1')).toBe('es'); |
| 285 |
}); |
| 286 |
|
| 287 |
it('should handle double-digit backreferences $01, $10, and $99 in the replacement string', function() { |
| 288 |
expect('aaa'.replace(/a(a)/, '$01b')).toBe('aba'); |
| 289 |
expect('aaa'.replace(new RegExp('a' + repeat('()', 9) + '(a)'), '$10b')).toBe('aba'); |
| 290 |
expect('aaa'.replace(new RegExp('a' + repeat('()', 98) + '(a)'), '$99b')).toBe('aba'); |
| 291 |
}); |
| 292 |
|
| 293 |
it('should end backreferences in the replacement string after two digits', function() { |
| 294 |
expect('aaa'.replace(new RegExp('a' + repeat('()', 99) + '(a)'), '$100b')).toBe('0ba'); |
| 295 |
}); |
| 296 |
|
| 297 |
// NOTE: IE < 9 incorrectly treats all occurrences of $ as literal text when performing a |
| 298 |
// replacement based on a search value that is not a regex. XRegExp restores the special |
| 299 |
// meaning of $$, $&, etc. for all replacements. |
| 300 |
|
| 301 |
it('should handle backreference $& in the replacement string', function() { |
| 302 |
expect('aaa'.replace(/aa/, '$&b')).toBe('aaba'); |
| 303 |
expect('aaa'.replace('aa', '$&b')).toBe('aaba'); |
| 304 |
}); |
| 305 |
|
| 306 |
it("should handle right context token $' in the replacement string", function() { |
| 307 |
expect('aaa'.replace(/aa/, "$'b")).toBe('aba'); |
| 308 |
expect('aaa'.replace('aa', "$'b")).toBe('aba'); |
| 309 |
}); |
| 310 |
|
| 311 |
it('should handle left context token $` in the replacement string', function() { |
| 312 |
expect('xaaa'.replace(/aa/, '$`b')).toBe('xxba'); |
| 313 |
expect('xaaa'.replace('aa', '$`b')).toBe('xxba'); |
| 314 |
}); |
| 315 |
|
| 316 |
it('should handle token $$ in the replacement string', function() { |
| 317 |
expect('aaa'.replace(/aa/, '$$b')).toBe('$ba'); |
| 318 |
expect('aaa'.replace('aa', '$$b')).toBe('$ba'); |
| 319 |
}); |
| 320 |
|
| 321 |
it('should allow a function to generate the replacement', function() { |
| 322 |
expect('aaa'.replace(/a/, function() {return 'b';})).toBe('baa'); |
| 323 |
expect('aaa'.replace(/a/g, function() {return 'b';})).toBe('bbb'); |
| 324 |
expect('aaa'.replace('a', function() {return 'b';})).toBe('baa'); |
| 325 |
}); |
| 326 |
|
| 327 |
it('should allow using backreferences with replacement functions', function() { |
| 328 |
expect('aaa'.replace(/aa/, function($0) {return $0 + 'b';})).toBe('aaba'); |
| 329 |
expect('aaa'.replace(/a(a)/, function($0, $1) {return $1 + 'b';})).toBe('aba'); |
| 330 |
expect('aaa'.replace('aa', function($0) {return $0 + 'b';})).toBe('aaba'); |
| 331 |
}); |
| 332 |
|
| 333 |
// Firefox (last tested v14.0.1) provides empty strings instead of undefined, but not for any |
| 334 |
// fault of XRegExp |
| 335 |
/*it('should provide undefined within replacement functions for nonparticipating capturing groups', function() { |
| 336 |
expect('y'.replace(/(x)?y/, function($0, $1) {return String($1);})).toBe('undefined'); |
| 337 |
expect('y'.replace(/(x)?y/, function($0, $1) {return $1;})).toBe('undefined'); |
| 338 |
});*/ |
| 339 |
|
| 340 |
it('should not substitute tokens returned by replacement functions', function() { |
| 341 |
// Regex search... |
| 342 |
expect('aaa'.replace(/a(a)/, function($0, $1) {return '$1';})).toBe('$1a'); |
| 343 |
expect('aaa'.replace(/a/, function() {return '$&';})).toBe('$&aa'); |
| 344 |
|
| 345 |
// String search... |
| 346 |
// This is broken in Safari (tested in Safari 5.1.2/7534.52.7), but not for any fault of |
| 347 |
// XRegExp. Uncomment this test if future XRegExp fixes it for Safari. |
| 348 |
//expect('aaa'.replace('a', function() {return '$&';})).toBe('$&aa'); |
| 349 |
}); |
| 350 |
|
| 351 |
it('should allow using the match position within replacement functions', function() { |
| 352 |
expect('xaaa'.replace(/a/, function($0, pos) {return '' + pos;})).toBe('x1aa'); |
| 353 |
expect('xaaa'.replace(/a/g, function($0, pos) {return '' + pos;})).toBe('x123'); |
| 354 |
expect('xaaa'.replace(/(a)/g, function($0, $1, pos) {return '' + pos;})).toBe('x123'); |
| 355 |
expect('xaaa'.replace('a', function($0, pos) {return '' + pos;})).toBe('x1aa'); |
| 356 |
}); |
| 357 |
|
| 358 |
it('should allow using the source string within replacement functions', function() { |
| 359 |
expect('xaaa'.replace(/a/, function($0, pos, str) {return str;})).toBe('xxaaaaa'); |
| 360 |
expect('xaaa'.replace(/(a)/, function($0, $1, pos, str) {return str;})).toBe('xxaaaaa'); |
| 361 |
expect('xaaa'.replace('a', function($0, pos, str) {return str;})).toBe('xxaaaaa'); |
| 362 |
}); |
| 363 |
|
| 364 |
it('should return string as the typeof the last argument in replacement functions', function() { |
| 365 |
// NOTE: This tests a fix for IE < 9, which doesn't get this correct natively |
| 366 |
|
| 367 |
expect('100'.replace(/0/, function($0, pos, str) {return typeof str;})).toBe('1string0'); |
| 368 |
expect(new String('100').replace(/0/, function($0, pos, str) {return typeof str;})).toBe('1string0'); |
| 369 |
expect(String.prototype.replace.call(100, /0/, function($0, pos, str) {return typeof str;})).toBe('1string0'); |
| 370 |
}); |
| 371 |
|
| 372 |
it('should handle nonstring context when using a replacement text token that references the subject text', function() { |
| 373 |
expect(String.prototype.replace.call(0, /^/, '$`')).toBe('0'); |
| 374 |
}); |
| 375 |
|
| 376 |
it('should not modify the lastIndex of a nonglobal regex', function() { |
| 377 |
var regex = /x/; |
| 378 |
|
| 379 |
'123x567'.replace(regex, '_'); |
| 380 |
expect(regex.lastIndex).toBe(0); |
| 381 |
|
| 382 |
regex.lastIndex = 1; |
| 383 |
'123x567'.replace(regex, '_'); |
| 384 |
expect(regex.lastIndex).toBe(1); |
| 385 |
|
| 386 |
'nomatch'.replace(regex, '_'); |
| 387 |
expect(regex.lastIndex).toBe(1); |
| 388 |
}); |
| 389 |
|
| 390 |
it('should reset the lastIndex of a global regex to 0', function() { |
| 391 |
var regex = /x/g; |
| 392 |
|
| 393 |
regex.lastIndex = 1; |
| 394 |
'123x567'.replace(regex, '_'); |
| 395 |
expect(regex.lastIndex).toBe(0); |
| 396 |
|
| 397 |
regex.lastIndex = 1; |
| 398 |
'nomatch'.replace(regex, '_'); |
| 399 |
expect(regex.lastIndex).toBe(0); |
| 400 |
}); |
| 401 |
|
| 402 |
it('should ignore lastIndex when setting the search start position', function() { |
| 403 |
[/x/, /x/g].forEach(function(regex) { |
| 404 |
regex.lastIndex = 5; |
| 405 |
expect('123x567'.replace(regex, '_')).toBe('123_567'); |
| 406 |
}); |
| 407 |
}); |
| 408 |
|
| 409 |
it('should update lastIndex during replacement iterations', function() { |
| 410 |
var regex = /x/g; |
| 411 |
var interimLastIndex = 0; |
| 412 |
'1x2'.replace(regex, function() { |
| 413 |
interimLastIndex = regex.lastIndex; |
| 414 |
}); |
| 415 |
|
| 416 |
expect(interimLastIndex).toBe(2); |
| 417 |
}); |
| 418 |
|
| 419 |
it('should convert any provided nonstring search to a string', function() { |
| 420 |
var values = [ |
| 421 |
{target: '10x10', search: 10, replacement: 'x', expected: 'xx10'}, |
| 422 |
{target: 'xaaa,ba,b', search: ['a','b'], replacement: 'x', expected: 'xaaxa,b'}, |
| 423 |
{target: 'undefined', search: undefined, replacement: 'x', expected: 'x'} |
| 424 |
]; |
| 425 |
|
| 426 |
values.forEach(function(value) { |
| 427 |
expect(value.target.replace(value.search, value.replacement)).toBe(value.expected); |
| 428 |
}); |
| 429 |
|
| 430 |
// Implicit undefined search and replacement |
| 431 |
expect('undefined'.replace()).toBe('undefined'); |
| 432 |
}); |
| 433 |
|
| 434 |
it('should convert any provided nonstring/nonfunction replacement to a string', function() { |
| 435 |
var values = [ |
| 436 |
{target: 'xaaa', search: /a/g, replacement: 1.1, expected: 'x1.11.11.1'}, |
| 437 |
{target: 'xaaa', search: /a/g, replacement: ['a','b'], expected: 'xa,ba,ba,b'}, |
| 438 |
{target: 'x', search: /x/, replacement: /x/, expected: '/x/'}, |
| 439 |
{target: 'xaaa', search: /a/, replacement: undefined, expected: 'xundefinedaa'} |
| 440 |
]; |
| 441 |
|
| 442 |
values.forEach(function(value) { |
| 443 |
expect(value.target.replace(value.search, value.replacement)).toBe(value.expected); |
| 444 |
}); |
| 445 |
|
| 446 |
// Implicit undefined replacement |
| 447 |
expect('xaaa'.replace(/a/)).toBe('xundefinedaa'); |
| 448 |
}); |
| 449 |
|
| 450 |
it('should convert any nonstring context to a string (except null and undefined)', function() { |
| 451 |
var values = [ |
| 452 |
100, |
| 453 |
{}, |
| 454 |
true, |
| 455 |
false, |
| 456 |
NaN, |
| 457 |
['a'] |
| 458 |
]; |
| 459 |
|
| 460 |
values.forEach(function(value) { |
| 461 |
expect(String.prototype.replace.call(value, /^/, 'x')).toBe('x' + value); |
| 462 |
}); |
| 463 |
}); |
| 464 |
|
| 465 |
it('should throw an exception when called on null or undefined context, if strict mode is supported', function() { |
| 466 |
[null, undefined].forEach(function(value) { |
| 467 |
// This doesn't work the same when strict mode isn't supported, because the replace |
| 468 |
// method will be called with the global object (window) as its context, rather |
| 469 |
// than null or undefined |
| 470 |
if (hasStrictMode) { |
| 471 |
expect(function() {String.prototype.replace.call(value, /^/, '');}).toThrowError(TypeError); |
| 472 |
} else { |
| 473 |
// Keep the assertion count consistent cross-browser |
| 474 |
expect(hasStrictMode).toBe(false); |
| 475 |
} |
| 476 |
}); |
| 477 |
}); |
| 478 |
|
| 479 |
describe('provides extensions:', function() { |
| 480 |
|
| 481 |
it('should allow accessing named backreferences in a callback function as properties of the first argument', function() { |
| 482 |
expect('abc'.replace(XRegExp('(?<name>.).'), function(match) { |
| 483 |
return ':' + match.name + ':'; |
| 484 |
})).toBe(':a:c'); |
| 485 |
}); |
| 486 |
|
| 487 |
describe('supports new replacement text syntax:', function() { |
| 488 |
|
| 489 |
describe('backreference $0', function() { |
| 490 |
|
| 491 |
it('should work like $&', function() { |
| 492 |
expect('xaaa'.replace(/aa/, '$0b')).toBe('xaaba'); |
| 493 |
expect('xaaa'.replace('aa', '$0b')).toBe('xaaba'); |
| 494 |
}); |
| 495 |
|
| 496 |
it('should be allowed as $00', function() { |
| 497 |
expect('xaaa'.replace(/aa/, '$00b')).toBe('xaaba'); |
| 498 |
}); |
| 499 |
|
| 500 |
it('should end after two zeros', function() { |
| 501 |
expect('xaaa'.replace(/aa/, '$000b')).toBe('xaa0ba'); |
| 502 |
expect('xaaa'.replace(/aa/, '$001b')).toBe('xaa1ba'); |
| 503 |
}); |
| 504 |
|
| 505 |
}); |
| 506 |
|
| 507 |
describe('named backreferences', function() { |
| 508 |
|
| 509 |
it('should return the named backreference', function() { |
| 510 |
expect('test'.replace(XRegExp('(?<test>t)', 'g'), ':${test}:')).toBe(':t:es:t:'); |
| 511 |
|
| 512 |
// Backreference to a nonparticipating capturing group |
| 513 |
expect('test'.replace(XRegExp('t|(?<test>t)', 'g'), ':${test}:')).toBe('::es::'); |
| 514 |
}); |
| 515 |
|
| 516 |
it('should throw an exception for backreferences to unknown group names', function() { |
| 517 |
expect(function() {'test'.replace(XRegExp('(?<test>t)', 'g'), ':${x}:');}).toThrowError(SyntaxError); |
| 518 |
}); |
| 519 |
|
| 520 |
}); |
| 521 |
|
| 522 |
describe('explicit numbered backreferences', function() { |
| 523 |
|
| 524 |
it('should return the numbered backreference', function() { |
| 525 |
expect('test'.replace(/(.)./g, '${1}')).toBe('ts'); |
| 526 |
|
| 527 |
// Backreference to a nonparticipating capturing group |
| 528 |
expect('test'.replace(/t|(e)/g, '${1}')).toBe('es'); |
| 529 |
}); |
| 530 |
|
| 531 |
it('should allow leading zeros', function() { |
| 532 |
expect('test'.replace(/(.)./g, '${01}')).toBe('ts'); |
| 533 |
expect('test'.replace(/(.)./g, '${001}')).toBe('ts'); |
| 534 |
}); |
| 535 |
|
| 536 |
it('should return named backreferences by number', function() { |
| 537 |
expect('test'.replace(XRegExp('(?<name>.).', 'g'), '${1}')).toBe('ts'); |
| 538 |
}); |
| 539 |
|
| 540 |
it('should separate numbered backreferences from following literal digits', function() { |
| 541 |
expect('test'.replace(new RegExp('(.).', 'g'), '${1}0')).toBe('t0s0'); |
| 542 |
expect('test'.replace(new RegExp('(.).' + repeat('()', 9), 'g'), '${1}0')).toBe('t0s0'); |
| 543 |
}); |
| 544 |
|
| 545 |
it('should throw an exception for backreferences to unknown group numbers', function() { |
| 546 |
expect(function() {'test'.replace(/t/, '${1}');}).toThrowError(SyntaxError); |
| 547 |
expect(function() {'test'.replace(/(t)/, '${2}');}).toThrowError(SyntaxError); |
| 548 |
}); |
| 549 |
|
| 550 |
it('should allow ${0} to refer to the entire match', function() { |
| 551 |
expect('test'.replace(/../g, '${0}:')).toBe('te:st:'); |
| 552 |
expect('test'.replace(/../g, '${00}:')).toBe('te:st:'); |
| 553 |
expect('test'.replace(/../g, '${000}:')).toBe('te:st:'); |
| 554 |
}); |
| 555 |
|
| 556 |
it('should support backreferences 100 and greater, if the browser does natively', function() { |
| 557 |
// IE < 9 doesn't allow backreferences greater than \99 *within* a regex, but |
| 558 |
// XRegExp still allows backreferences to groups 100+ within replacement text |
| 559 |
try { |
| 560 |
// Regex with 1,000 capturing groups. This fails in Firefox 4-6 (but not v3.6 |
| 561 |
// or v7+) with `InternalError: regular expression too complex` |
| 562 |
var lottaGroups = new RegExp([ |
| 563 |
'^(a)\\1', repeat('()', 8), |
| 564 |
'(b)\\10', repeat('()', 89), |
| 565 |
'(c)', repeat('()', 899), |
| 566 |
'(d)$' |
| 567 |
].join('')); |
| 568 |
|
| 569 |
expect('aabbcd'.replace(lottaGroups, '${0} ${01} ${001} ${0001} ${1} ${10} ${100} ${1000}')).toBe('aabbcd a a a a b c d'); |
| 570 |
// For comparison... |
| 571 |
expect('aabbcd'.replace(lottaGroups, '$0 $01 $001 $0001 $1 $10 $100 $1000')).toBe('aabbcd a aabbcd1 aabbcd01 a b b0 b00'); |
| 572 |
} catch (err) { |
| 573 |
// Keep the assertion count consistent cross-browser |
| 574 |
expect(true).toBe(true); |
| 575 |
expect(true).toBe(true); |
| 576 |
} |
| 577 |
}); |
| 578 |
|
| 579 |
}); |
| 580 |
|
| 581 |
describe('strict error handling', function() { |
| 582 |
|
| 583 |
it('should throw an exception for backreferences to unknown group numbers', function() { |
| 584 |
expect(function() {'xaaa'.replace(/aa/, '$1b');}).toThrowError(SyntaxError); |
| 585 |
expect(function() {'xaaa'.replace(/aa/, '$01b');}).toThrowError(SyntaxError); |
| 586 |
expect(function() {'xaaa'.replace(/a(a)/, '$2b');}).toThrowError(SyntaxError); |
| 587 |
expect(function() {'xa(a)a'.replace('a(a)', '$1b');}).toThrowError(SyntaxError); |
| 588 |
}); |
| 589 |
|
| 590 |
}); |
| 591 |
|
| 592 |
}); |
| 593 |
|
| 594 |
}); |
| 595 |
|
| 596 |
}); |
| 597 |
|
| 598 |
describe('When overridden, String.prototype.split()', function() { |
| 599 |
|
| 600 |
beforeEach(function() { |
| 601 |
XRegExp.install('natives'); |
| 602 |
}); |
| 603 |
|
| 604 |
/* |
| 605 |
* NOTE: These specs should mirror those for XRegExp.split as closely as possible. |
| 606 |
*/ |
| 607 |
|
| 608 |
// TODO: Copy/update specs from XRegExp.split here |
| 609 |
|
| 610 |
// NOTE: The fixed String.prototype.split does not provide any extensions to native handling |
| 611 |
|
| 612 |
}); |