84 lines
3.1 KiB
JavaScript
84 lines
3.1 KiB
JavaScript
var DESCRIPTORS = require('../internals/descriptors');
|
|
var global = require('../internals/global');
|
|
var isForced = require('../internals/is-forced');
|
|
var inheritIfRequired = require('../internals/inherit-if-required');
|
|
var defineProperty = require('../internals/object-define-property').f;
|
|
var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
|
|
var isRegExp = require('../internals/is-regexp');
|
|
var getFlags = require('../internals/regexp-flags');
|
|
var stickyHelpers = require('../internals/regexp-sticky-helpers');
|
|
var redefine = require('../internals/redefine');
|
|
var fails = require('../internals/fails');
|
|
var setInternalState = require('../internals/internal-state').set;
|
|
var setSpecies = require('../internals/set-species');
|
|
var wellKnownSymbol = require('../internals/well-known-symbol');
|
|
|
|
var MATCH = wellKnownSymbol('match');
|
|
var NativeRegExp = global.RegExp;
|
|
var RegExpPrototype = NativeRegExp.prototype;
|
|
var re1 = /a/g;
|
|
var re2 = /a/g;
|
|
|
|
// "new" should create a new object, old webkit bug
|
|
var CORRECT_NEW = new NativeRegExp(re1) !== re1;
|
|
|
|
var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
|
|
|
|
var FORCED = DESCRIPTORS && isForced('RegExp', (!CORRECT_NEW || UNSUPPORTED_Y || 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';
|
|
})));
|
|
|
|
// `RegExp` constructor
|
|
// https://tc39.github.io/ecma262/#sec-regexp-constructor
|
|
if (FORCED) {
|
|
var RegExpWrapper = function RegExp(pattern, flags) {
|
|
var thisIsRegExp = this instanceof RegExpWrapper;
|
|
var patternIsRegExp = isRegExp(pattern);
|
|
var flagsAreUndefined = flags === undefined;
|
|
var sticky;
|
|
|
|
if (!thisIsRegExp && patternIsRegExp && pattern.constructor === RegExpWrapper && flagsAreUndefined) {
|
|
return pattern;
|
|
}
|
|
|
|
if (CORRECT_NEW) {
|
|
if (patternIsRegExp && !flagsAreUndefined) pattern = pattern.source;
|
|
} else if (pattern instanceof RegExpWrapper) {
|
|
if (flagsAreUndefined) flags = getFlags.call(pattern);
|
|
pattern = pattern.source;
|
|
}
|
|
|
|
if (UNSUPPORTED_Y) {
|
|
sticky = !!flags && flags.indexOf('y') > -1;
|
|
if (sticky) flags = flags.replace(/y/g, '');
|
|
}
|
|
|
|
var result = inheritIfRequired(
|
|
CORRECT_NEW ? new NativeRegExp(pattern, flags) : NativeRegExp(pattern, flags),
|
|
thisIsRegExp ? this : RegExpPrototype,
|
|
RegExpWrapper
|
|
);
|
|
|
|
if (UNSUPPORTED_Y && sticky) setInternalState(result, { sticky: sticky });
|
|
|
|
return result;
|
|
};
|
|
var proxy = function (key) {
|
|
key in RegExpWrapper || defineProperty(RegExpWrapper, key, {
|
|
configurable: true,
|
|
get: function () { return NativeRegExp[key]; },
|
|
set: function (it) { NativeRegExp[key] = it; }
|
|
});
|
|
};
|
|
var keys = getOwnPropertyNames(NativeRegExp);
|
|
var index = 0;
|
|
while (keys.length > index) proxy(keys[index++]);
|
|
RegExpPrototype.constructor = RegExpWrapper;
|
|
RegExpWrapper.prototype = RegExpPrototype;
|
|
redefine(global, 'RegExp', RegExpWrapper);
|
|
}
|
|
|
|
// https://tc39.github.io/ecma262/#sec-get-regexp-@@species
|
|
setSpecies('RegExp');
|