Projekt

Obecné

Profil

Stáhnout (3.25 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2
require('./es6.regexp.exec');
3
var redefine = require('./_redefine');
4
var hide = require('./_hide');
5
var fails = require('./_fails');
6
var defined = require('./_defined');
7
var wks = require('./_wks');
8
var regexpExec = require('./_regexp-exec');
9

    
10
var SPECIES = wks('species');
11

    
12
var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
13
  // #replace needs built-in support for named groups.
14
  // #match works fine because it just return the exec results, even if it has
15
  // a "grops" property.
16
  var re = /./;
17
  re.exec = function () {
18
    var result = [];
19
    result.groups = { a: '7' };
20
    return result;
21
  };
22
  return ''.replace(re, '$<a>') !== '7';
23
});
24

    
25
var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = (function () {
26
  // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
27
  var re = /(?:)/;
28
  var originalExec = re.exec;
29
  re.exec = function () { return originalExec.apply(this, arguments); };
30
  var result = 'ab'.split(re);
31
  return result.length === 2 && result[0] === 'a' && result[1] === 'b';
32
})();
33

    
34
module.exports = function (KEY, length, exec) {
35
  var SYMBOL = wks(KEY);
36

    
37
  var DELEGATES_TO_SYMBOL = !fails(function () {
38
    // String methods call symbol-named RegEp methods
39
    var O = {};
40
    O[SYMBOL] = function () { return 7; };
41
    return ''[KEY](O) != 7;
42
  });
43

    
44
  var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL ? !fails(function () {
45
    // Symbol-named RegExp methods call .exec
46
    var execCalled = false;
47
    var re = /a/;
48
    re.exec = function () { execCalled = true; return null; };
49
    if (KEY === 'split') {
50
      // RegExp[@@split] doesn't call the regex's exec method, but first creates
51
      // a new one. We need to return the patched regex when creating the new one.
52
      re.constructor = {};
53
      re.constructor[SPECIES] = function () { return re; };
54
    }
55
    re[SYMBOL]('');
56
    return !execCalled;
57
  }) : undefined;
58

    
59
  if (
60
    !DELEGATES_TO_SYMBOL ||
61
    !DELEGATES_TO_EXEC ||
62
    (KEY === 'replace' && !REPLACE_SUPPORTS_NAMED_GROUPS) ||
63
    (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
64
  ) {
65
    var nativeRegExpMethod = /./[SYMBOL];
66
    var fns = exec(
67
      defined,
68
      SYMBOL,
69
      ''[KEY],
70
      function maybeCallNative(nativeMethod, regexp, str, arg2, forceStringMethod) {
71
        if (regexp.exec === regexpExec) {
72
          if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
73
            // The native String method already delegates to @@method (this
74
            // polyfilled function), leasing to infinite recursion.
75
            // We avoid it by directly calling the native @@method method.
76
            return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
77
          }
78
          return { done: true, value: nativeMethod.call(str, regexp, arg2) };
79
        }
80
        return { done: false };
81
      }
82
    );
83
    var strfn = fns[0];
84
    var rxfn = fns[1];
85

    
86
    redefine(String.prototype, KEY, strfn);
87
    hide(RegExp.prototype, SYMBOL, length == 2
88
      // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
89
      // 21.2.5.11 RegExp.prototype[@@split](string, limit)
90
      ? function (string, arg) { return rxfn.call(string, this, arg); }
91
      // 21.2.5.6 RegExp.prototype[@@match](string)
92
      // 21.2.5.9 RegExp.prototype[@@search](string)
93
      : function (string) { return rxfn.call(string, this); }
94
    );
95
  }
96
};
(36-36/303)