Skip to content

Commit

Permalink
[New] Detect and patch RegExp#toString in IE 8, which returns flags…
Browse files Browse the repository at this point in the history
… in the wrong order.

Closes es-shims#364.
  • Loading branch information
ljharb committed Dec 9, 2015
1 parent 9ae8165 commit a827b75
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"max-statements": [1, 30],
"new-cap": [2, { "capIsNewExceptions": ["ToInteger", "ToObject", "ToPrimitive", "ToUint32"] }],
"no-constant-condition": [1],
"no-extend-native": [2, {"exceptions": ["Date", "Error"]}],
"no-extend-native": [2, {"exceptions": ["Date", "Error", "RegExp"]}],
"no-extra-parens": [0],
"no-extra-semi": [1],
"no-func-assign": [1],
Expand Down
18 changes: 18 additions & 0 deletions es5-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1768,4 +1768,22 @@ if (supportsDescriptors) {
ensureNonEnumerable(Error.prototype, 'name');
}

if (String(/a/mig) !== '/a/gim') {
var regexToString = function toString() {
var str = '/' + this.source + '/';
if (this.global) {
str += 'g';
}
if (this.ignoreCase) {
str += 'i';
}
if (this.multiline) {
str += 'm';
}
return str;
};
// can't use defineProperties here because of toString enumeration issue in IE <= 8
RegExp.prototype.toString = regexToString;
}

}));
21 changes: 11 additions & 10 deletions tests/spec/s-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ describe('RegExp', function () {
describe('#toString()', function () {
describe('literals', function () {
it('should return correct flags and in correct order', function () {
expect(/pattern/.toString()).toBe('/pattern/');
expect(/pattern/i.toString()).toBe('/pattern/i');
expect(/pattern/mi.toString()).toBe('/pattern/im');
expect(/pattern/im.toString()).toBe('/pattern/im');
expect(/pattern/mgi.toString()).toBe('/pattern/gim');
expect(String(/pattern/)).toBe('/pattern/');
expect(String(/pattern/i)).toBe('/pattern/i');
expect(String(/pattern/mi)).toBe('/pattern/im');
expect(String(/pattern/im)).toBe('/pattern/im');
expect(String(/pattern/mgi)).toBe('/pattern/gim');
});
});

describe('objects', function () {
it('should return correct flags and in correct order', function () {
expect(new RegExp('pattern').toString()).toBe('/pattern/');
expect(new RegExp('pattern', 'i').toString()).toBe('/pattern/i');
expect(new RegExp('pattern', 'mi').toString()).toBe('/pattern/im');
expect(new RegExp('pattern', 'im').toString()).toBe('/pattern/im');
expect(new RegExp('pattern', 'mgi').toString()).toBe('/pattern/gim');
expect(String(new RegExp('pattern'))).toBe('/pattern/');
expect(String(new RegExp('pattern', 'i'))).toBe('/pattern/i');
expect(String(new RegExp('pattern', 'mi'))).toBe('/pattern/im');
expect(String(new RegExp('pattern', 'im'))).toBe('/pattern/im');
expect(String(new RegExp('pattern', 'mgi'))).toBe('/pattern/gim');
});
});
});
Expand Down

0 comments on commit a827b75

Please sign in to comment.