Skip to content

Commit

Permalink
try to fix broken UC browser RegExp#sticky
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 22, 2021
1 parent b68787a commit 13fca9a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core-js/internals/regexp-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var UPDATES_LAST_INDEX_WRONG = (function () {
return re1.lastIndex !== 0 || re2.lastIndex !== 0;
})();

var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y || stickyHelpers.BROKEN_CARET;
var UNSUPPORTED_Y = stickyHelpers.BROKEN_CARET;

// nonparticipating capturing group, copied from es5-shim's String#split patch.
var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
Expand Down
16 changes: 14 additions & 2 deletions packages/core-js/internals/regexp-sticky-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ var global = require('../internals/global');
// babel-minify and Closure Compiler transpiles RegExp('a', 'y') -> /a/y and it causes SyntaxError
var $RegExp = global.RegExp;

exports.UNSUPPORTED_Y = fails(function () {
var UNSUPPORTED_Y = fails(function () {
var re = $RegExp('a', 'y');
re.lastIndex = 2;
return re.exec('abcd') != null;
});

exports.BROKEN_CARET = fails(function () {
// UC Browser bug
// https://github.com/zloirock/core-js/issues/1008
var MISSED_STICKY = UNSUPPORTED_Y || fails(function () {
return !$RegExp('a', 'y').sticky;
});

var BROKEN_CARET = UNSUPPORTED_Y || fails(function () {
// https://bugzilla.mozilla.org/show_bug.cgi?id=773687
var re = $RegExp('^r', 'gy');
re.lastIndex = 2;
return re.exec('str') != null;
});

module.exports = {
BROKEN_CARET: BROKEN_CARET,
MISSED_STICKY: MISSED_STICKY,
UNSUPPORTED_Y: UNSUPPORTED_Y
};
7 changes: 4 additions & 3 deletions packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ var re2 = /a/g;
// "new" should create a new object, old webkit bug
var CORRECT_NEW = new NativeRegExp(re1) !== re1;

var MISSED_STICKY = stickyHelpers.MISSED_STICKY;
var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;

var BASE_FORCED = DESCRIPTORS &&
(!CORRECT_NEW || UNSUPPORTED_Y || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG || fails(function () {
(!CORRECT_NEW || MISSED_STICKY || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG || fails(function () {
re2[MATCH] = false;
// RegExp constructor can alter flags and IsRegExp works correct with @@match
return NativeRegExp(re1) != re1 || NativeRegExp(re2) == re2 || NativeRegExp(re1, 'i') != '/a/i';
Expand Down Expand Up @@ -146,9 +147,9 @@ if (isForced('RegExp', BASE_FORCED)) {

rawFlags = flags;

if (UNSUPPORTED_Y && 'sticky' in re1) {
if (MISSED_STICKY && 'sticky' in re1) {
sticky = !!flags && stringIndexOf(flags, 'y') > -1;
if (sticky) flags = replace(flags, /y/g, '');
if (sticky && UNSUPPORTED_Y) flags = replace(flags, /y/g, '');
}

if (UNSUPPORTED_NCG) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/es.regexp.sticky.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var global = require('../internals/global');
var DESCRIPTORS = require('../internals/descriptors');
var UNSUPPORTED_Y = require('../internals/regexp-sticky-helpers').UNSUPPORTED_Y;
var MISSED_STICKY = require('../internals/regexp-sticky-helpers').MISSED_STICKY;
var classof = require('../internals/classof-raw');
var defineProperty = require('../internals/object-define-property').f;
var getInternalState = require('../internals/internal-state').get;
Expand All @@ -10,7 +10,7 @@ var TypeError = global.TypeError;

// `RegExp.prototype.sticky` getter
// https://tc39.es/ecma262/#sec-get-regexp.prototype.sticky
if (DESCRIPTORS && UNSUPPORTED_Y) {
if (DESCRIPTORS && MISSED_STICKY) {
defineProperty(RegExpPrototype, 'sticky', {
configurable: true,
get: function () {
Expand Down

0 comments on commit 13fca9a

Please sign in to comment.