From 08b9f64890bdf55ab3d7c4362bb5adc4a1f4e9e7 Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Mon, 12 Dec 2016 14:21:42 -0500 Subject: [PATCH] escape non-range dashes --- lib/compilers.js | 4 ++++ test/wildmatch.js | 29 ++++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/compilers.js b/lib/compilers.js index 7bb8d93..fbf7fe8 100644 --- a/lib/compilers.js +++ b/lib/compilers.js @@ -60,6 +60,10 @@ module.exports = function(brackets) { return this.emit('^', node); } + if (/-/.test(inner) && !/(\d-\d|\w-\w)/.test(inner)) { + inner = inner.split('-').join('\\-'); + } + var isNegated = inner.charAt(0) === '^'; // add slashes to negated brackets, per spec if (isNegated && inner.indexOf('/') === -1) { diff --git a/test/wildmatch.js b/test/wildmatch.js index 1def356..21376c0 100644 --- a/test/wildmatch.js +++ b/test/wildmatch.js @@ -83,28 +83,34 @@ describe('original wildmatch', function() { }); it('should support Additional tests, including some malformed wildmats', function() { + assert(!match.isMatch('$', '[ --]')); assert(!match.isMatch('+', '[,-.]')); assert(!match.isMatch('-', '[!a-')); - assert(!match.isMatch('-', '[[-\\]]')); + assert(!match.isMatch('-', '[\\-_]')); assert(!match.isMatch('-', '[a-')); assert(!match.isMatch('-.]', '[,-.]')); assert(!match.isMatch('0', '[ --]')); + assert(!match.isMatch('2', '[\\1-\\3]')); assert(!match.isMatch('4', '[\\1-\\3]')); + assert(!match.isMatch('5', '[--A]')); assert(!match.isMatch('[', '[\\\\-^]')); assert(!match.isMatch('[', '[]-a]')); assert(!match.isMatch('\\', '[!\\\\]')); + assert(!match.isMatch('\\', '[[-\\]]')); assert(!match.isMatch('\\', '[\\]')); assert(!match.isMatch('\\', '[\\]]')); assert(!match.isMatch('\\]', '[\\]]')); - assert(!match.isMatch('^', '[!]-a]')); + assert(!match.isMatch(']', '[\\\\-^]')); + assert(!match.isMatch('^', '[]-a]')); assert(!match.isMatch('a[]b', 'a[]b')); assert(!match.isMatch('ab', '[!')); assert(!match.isMatch('ab', '[-')); assert(!match.isMatch('ab', 'a[]b')); assert(!match.isMatch('acrt', 'a[c-c]st')); + assert(!match.isMatch('G', '[A-\\\\]')); assert(!match.isMatch('j', '[a-e-n]')); assert(match.isMatch(' ', '[ --]')); - assert(match.isMatch('$', '[ --]')); + assert(match.isMatch(' ', '[-- ]')); assert(match.isMatch(',', '[,]')); assert(match.isMatch(',', '[\\\\,]')); assert(match.isMatch('-', '[ --]')); @@ -113,26 +119,21 @@ describe('original wildmatch', function() { assert(match.isMatch('-', '[---]')); assert(match.isMatch('-', '[--A]')); assert(match.isMatch('-', '[-]')); - assert(match.isMatch('-', '[\\-_]')); + assert(match.isMatch('-', '[[-\\]]')); assert(match.isMatch('-', '[a-e-n]')); assert(match.isMatch('-b]', '[a-]b]')); - assert(match.isMatch('2', '[\\1-\\3]')); assert(match.isMatch('3', '[\\1-\\3]')); - assert(match.isMatch('5', '[--A]')); assert(match.isMatch('[', '[!]-a]')); assert(match.isMatch('[', '[[-\\]]')); - assert(match.isMatch('\\', '[[-\\]]')); assert(match.isMatch('\\', '[\\\\,]')); assert(match.isMatch('\\', '[\\\\]')); assert(match.isMatch(']', '[[-\\]]')); - assert(match.isMatch(']', '[\\\\-^]')); assert(match.isMatch(']', '[\\]]')); - assert(match.isMatch('^', '[]-a]')); + assert(match.isMatch('^', '[!]-a]')); assert(match.isMatch('^', '[a^bc]')); assert(match.isMatch('a', '[!------]')); assert(match.isMatch('ab[', 'ab[')); assert(match.isMatch('acrt', 'a[c-c]rt')); - assert(match.isMatch('G', '[A-\\\\]')); }); it('should support Case-sensitivy features', function() { @@ -168,9 +169,11 @@ describe('original wildmatch', function() { }); it('should support Additional tests not found in the original wildmatch', function() { - assert(!match.isMatch('-', '[]-z]')); + assert(match.isMatch('-', '[]-z]')); assert(match.isMatch('-', '[[:space:]-\\]]')); - assert(match.isMatch('c', '[[:space:]-z]')); - assert(match.isMatch('c', '[]-z]')); + assert(match.isMatch(']', '[[:space:]-\\]]')); + assert(!match.isMatch('[', '[[:space:]-\\]]')); + assert(!match.isMatch('c', '[[:space:]-z]')); + assert(!match.isMatch('c', '[]-z]')); }); });