Skip to content

Commit

Permalink
Merge 1033d7b into 8894cd9
Browse files Browse the repository at this point in the history
  • Loading branch information
ekilah authored Aug 25, 2020
2 parents 8894cd9 + 1033d7b commit a9b4231
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 1.16.0 (2020-08-25)

- Fixes `flags(regexp)` to work in older browsers (Edge versions 15-18), which fixes crashes on startup in those old clients

## version 1.15.0 (2020-07-27)

- Adds support for the `s` (`dotAll`) flag in `Parsimmon.regexp`
Expand Down
13 changes: 11 additions & 2 deletions src/parsimmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,17 @@ function formatError(input, error) {
}

function flags(re) {
var s = "" + re;
return s.slice(s.lastIndexOf("/") + 1);
if (re.flags !== undefined) {
return re.flags;
}
// legacy browser support
return [
re.global ? "g" : "",
re.ignoreCase ? "i" : "",
re.multiline ? "m" : "",
re.unicode ? "u" : "",
re.sticky ? "y" : ""
].join("");
}

function anchoredRegexp(re) {
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"Parsimmon": true,
"assert": true,
"testSetScenario": true
},
"rules": {
"no-invalid-regexp": ["error", {"allowConstructorFlags": ["u", "y"]}]
}
}
72 changes: 72 additions & 0 deletions test/core/flags.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";

testSetScenario(function() {
describe("flags()", function() {
it("works in modern browsers", function() {
assert.throws(function() {
Parsimmon.regexp(new RegExp("a", "g"));
});
assert.doesNotThrow(function() {
Parsimmon.regexp(new RegExp("a", "i"));
});
assert.doesNotThrow(function() {
Parsimmon.regexp(new RegExp("a", "m"));
});
assert.doesNotThrow(function() {
Parsimmon.regexp(new RegExp("a", "u"));
});
assert.throws(function() {
Parsimmon.regexp(new RegExp("a", "y"));
});
});

it("works on legacy browsers without Regexp.flags property with flags", function() {
var oldRegExpG = new RegExp("a", "g");
var oldRegExpI = new RegExp("a", "i");
var oldRegExpM = new RegExp("a", "m");
var oldRegExpU = new RegExp("a", "u");
var oldRegExpY = new RegExp("a", "y");
var oldRegExps = [
oldRegExpG,
oldRegExpI,
oldRegExpM,
oldRegExpU,
oldRegExpY
];

// Simulate old RegExp without the flags property
oldRegExps.map(function(r) {
Object.defineProperty(r, "flags", { value: undefined });
assert.strictEqual(r.flags, undefined);
});

assert.throws(function() {
Parsimmon.regexp(oldRegExpG);
});
assert.doesNotThrow(function() {
Parsimmon.regexp(oldRegExpI);
});
assert.doesNotThrow(function() {
Parsimmon.regexp(oldRegExpM);
});
assert.doesNotThrow(function() {
Parsimmon.regexp(oldRegExpU);
});
assert.throws(function() {
Parsimmon.regexp(oldRegExpY);
});
});

it("works on legacy browsers without Regexp.flags property without flags", function() {
var oldRegExp = new RegExp("a", "");

// Simulate old RegExp without the flags property
Object.defineProperty(oldRegExp, "flags", { value: undefined });
assert.strictEqual(oldRegExp.flags, undefined);

assert.doesNotThrow(function() {
Parsimmon.regexp(oldRegExp);
});
});
});
});

0 comments on commit a9b4231

Please sign in to comment.