-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix RegExp methods in old browsers #453
Merged
zloirock
merged 9 commits into
zloirock:master
from
nicolo-ribaudo:fix-regexs-on-old-browsers
Nov 17, 2018
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aeacd0d
Fix RegExp methods on Chrome 50 & 51
nicolo-ribaudo 02a4e07
Reduce repetition
nicolo-ribaudo 37c85d5
Rename internals/regexp-exec -> internals/regexp-exec-abstract
nicolo-ribaudo 62ade11
Add RegExp#exec tests (they are failing in IE8)
nicolo-ribaudo 5bd11fc
Fix #lastIndex in IE8 and move NPCG check from #split to #exec
nicolo-ribaudo b8f73b1
Fix String#split and another RegExp#lastIndex case in IE8
nicolo-ribaudo 823756a
Fix String#replace bug with custon exec in IE8
nicolo-ribaudo d2aac81
Fix bug in IE7 (only tested using IE8 in compatibility mode)
nicolo-ribaudo d3fa95e
Fix bug in Safari 9
nicolo-ribaudo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var classof = require('./classof-raw'); | ||
var regexpExec = require('./regexp-exec'); | ||
|
||
// `RegExpExec` abstract operation | ||
// https://tc39.github.io/ecma262/#sec-regexpexec | ||
module.exports = function (R, S) { | ||
var exec = R.exec; | ||
if (typeof exec === 'function') { | ||
var result = exec.call(R, S); | ||
if (typeof result !== 'object') { | ||
throw new TypeError('RegExp exec method returned something other than an Object or null'); | ||
} | ||
return result; | ||
} | ||
|
||
if (classof(R) !== 'RegExp') { | ||
throw new TypeError('RegExp#exec called on incompatible receiver'); | ||
} | ||
|
||
return regexpExec.impl.call(R, S); | ||
}; | ||
|
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 |
---|---|---|
@@ -1,22 +1,63 @@ | ||
var classof = require('../internals/classof-raw'); | ||
var builtinExec = RegExp.prototype.exec; | ||
|
||
// `RegExpExec` abstract operation | ||
// https://tc39.github.io/ecma262/#sec-regexpexec | ||
module.exports = function (R, S) { | ||
var exec = R.exec; | ||
if (typeof exec === 'function') { | ||
var result = exec.call(R, S); | ||
if (typeof result !== 'object') { | ||
throw new TypeError('RegExp exec method returned something other than an Object or null'); | ||
'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 LENGTH = 'length'; | ||
|
||
var UPDATES_LAST_INDEX_WRONG = (function () { | ||
var re1 = /a/, | ||
re2 = /b*/g; | ||
nativeExec.call(re1, 'a'); | ||
nativeExec.call(re2, 'a'); | ||
return re1[LAST_INDEX] !== 0 || re2[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_WRONG || 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)); | ||
} | ||
return result; | ||
} | ||
if (UPDATES_LAST_INDEX_WRONG) lastIndex = re[LAST_INDEX]; | ||
|
||
if (classof(R) !== 'RegExp') { | ||
throw new TypeError('RegExp#exec called on incompatible receiver'); | ||
} | ||
match = nativeExec.call(re, str); | ||
|
||
return builtinExec.call(R, S); | ||
}; | ||
if (UPDATES_LAST_INDEX_WRONG && match) { | ||
re[LAST_INDEX] = re.global ? match.index + match[0][LENGTH] : 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,41 +3,45 @@ | |
var anObject = require('../internals/an-object'); | ||
var toLength = require('../internals/to-length'); | ||
var advanceStringIndex = require('../internals/advance-string-index'); | ||
var regExpExec = require('../internals/regexp-exec'); | ||
var nativeExec = RegExp.prototype.exec; | ||
var regExpExec = require('../internals/regexp-exec-abstract'); | ||
|
||
// @@match logic | ||
require('../internals/fix-regexp-well-known-symbol-logic')('match', 1, function (defined, MATCH, nativeMatch) { | ||
return [ | ||
// `String.prototype.match` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.match | ||
function match(regexp) { | ||
var O = defined(this); | ||
var matcher = regexp == undefined ? undefined : regexp[MATCH]; | ||
return matcher !== undefined ? matcher.call(regexp, O) : new RegExp(regexp)[MATCH](String(O)); | ||
}, | ||
// `RegExp.prototype[@@match]` method | ||
// https://tc39.github.io/ecma262/#sec-regexp.prototype-@@match | ||
function (regexp) { | ||
if (regexp.exec === nativeExec) return nativeMatch.call(this, regexp); | ||
require('../internals/fix-regexp-well-known-symbol-logic')( | ||
'match', | ||
1, | ||
function (defined, MATCH, nativeMatch, maybeCallNative) { | ||
return [ | ||
// `String.prototype.match` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.match | ||
function match(regexp) { | ||
var O = defined(this); | ||
var matcher = regexp == undefined ? undefined : regexp[MATCH]; | ||
return matcher !== undefined ? matcher.call(regexp, O) : new RegExp(regexp)[MATCH](String(O)); | ||
}, | ||
// `RegExp.prototype[@@match]` method | ||
// https://tc39.github.io/ecma262/#sec-regexp.prototype-@@match | ||
function (regexp) { | ||
var res = maybeCallNative(nativeMatch, regexp, this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All those "maybeCallNative" calls are to fix the infinite recursion in Chrome 50 |
||
if (res.done) return res.value; | ||
|
||
var rx = anObject(regexp); | ||
var S = String(this); | ||
var rx = anObject(regexp); | ||
var S = String(this); | ||
|
||
if (!rx.global) return regExpExec(rx, S); | ||
if (!rx.global) return regExpExec(rx, S); | ||
|
||
var fullUnicode = rx.unicode; | ||
rx.lastIndex = 0; | ||
var A = []; | ||
var n = 0; | ||
var result; | ||
while ((result = regExpExec(rx, S)) !== null) { | ||
var matchStr = String(result[0]); | ||
A[n] = matchStr; | ||
if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode); | ||
n++; | ||
var fullUnicode = rx.unicode; | ||
rx.lastIndex = 0; | ||
var A = []; | ||
var n = 0; | ||
var result; | ||
while ((result = regExpExec(rx, S)) !== null) { | ||
var matchStr = String(result[0]); | ||
A[n] = matchStr; | ||
if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode); | ||
n++; | ||
} | ||
return n === 0 ? null : A; | ||
} | ||
return n === 0 ? null : A; | ||
} | ||
]; | ||
}); | ||
]; | ||
} | ||
); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the fix for Chrome 51.