-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #lastIndex in IE8 and move NPCG check from #split to #exec
IE8 is not fully fixed yet
- Loading branch information
1 parent
62ade11
commit 5bd11fc
Showing
9 changed files
with
83 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
var regexpFlags = require('./regexp-flags'); | ||
|
||
var nativeExec = RegExp.prototype.exec; | ||
// This always refers to the native implementation, because the | ||
// String#replace polyfill uses ./fix-regexp-well-known-symbol-logic.js, | ||
// which loads this file before patching the method. | ||
var nativeReplace = String.prototype.replace; | ||
|
||
var patchedExec = nativeExec; | ||
|
||
var LAST_INDEX = 'lastIndex'; | ||
|
||
var UPDATES_LAST_INDEX_NON_GLOBAL = (function () { | ||
var re = /a/; | ||
nativeExec.call(re, 'a'); | ||
return re[LAST_INDEX] !== 0; | ||
})(); | ||
|
||
// nonparticipating capturing group, copied from es5-shim's String#split patch. | ||
var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined; | ||
|
||
var patch = UPDATES_LAST_INDEX_NON_GLOBAL || NPCG_INCLUDED; | ||
|
||
if (patch) { | ||
patchedExec = function exec(str) { | ||
var re = this; | ||
var lastIndex, reCopy, match, i; | ||
|
||
if (NPCG_INCLUDED) { | ||
reCopy = new RegExp('^' + re.source + '$(?!\\s)', regexpFlags.call(re)); | ||
} | ||
if (UPDATES_LAST_INDEX_NON_GLOBAL) lastIndex = this[LAST_INDEX]; | ||
|
||
match = nativeExec.call(this, str); | ||
|
||
if (UPDATES_LAST_INDEX_NON_GLOBAL && !this.global) this[LAST_INDEX] = lastIndex; | ||
if (NPCG_INCLUDED && match && match.length > 1) { | ||
// Fix browsers whose `exec` methods don't consistently return `undefined` | ||
// for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/ | ||
// eslint-disable-next-line no-loop-func | ||
nativeReplace.call(match[0], reCopy, function () { | ||
for (i = 1; i < arguments.length - 2; i++) { | ||
if (arguments[i] === undefined) match[i] = undefined; | ||
} | ||
}); | ||
} | ||
|
||
return match; | ||
}; | ||
} | ||
|
||
module.exports = { | ||
orig: nativeExec, | ||
impl: patchedExec, | ||
patched: patch | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var regexpExec = require('../internals/regexp-exec'); | ||
|
||
require('../internals/export')({ | ||
target: 'RegExp', | ||
proto: true, | ||
forced: regexpExec.patched | ||
}, { | ||
exec: regexpExec.impl | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters