Fix anaylze for HostWildcardCondition involving trimAsterisk. Fix #158.

This commit is contained in:
FelisCatus 2015-01-04 14:27:05 +08:00
parent f8fd10fca8
commit 590c3baed9
3 changed files with 14 additions and 5 deletions

View File

@ -218,8 +218,8 @@ module.exports = exports =
if pattern.indexOf('**.') == 0
shExp2RegExp pattern.substring(1), trimAsterisk: true
else if pattern.indexOf('*.') == 0
shExp2RegExp(pattern.substring(2), trimAsterisk: true)
.replace(/./, '(?:^|\\.)')
shExp2RegExp(pattern.substring(2), trimAsterisk: false)
.replace(/./, '(?:^|\\.)').replace(/\.\*\$$/, '')
else
shExp2RegExp pattern, trimAsterisk: true
@safeRegex parts.join('|')

View File

@ -113,6 +113,15 @@ describe 'Conditions', ->
it 'should also match hostname without the optional level', ->
# https://github.com/FelisCatus/SwitchyOmega/wiki/Host-wildcard-condition
testCond(cond, 'http://example.com/', 'match')
it 'should process patterns like *.*example.com correctly', ->
# https://github.com/FelisCatus/SwitchyOmega/issues/158
con =
conditionType: 'HostWildcardCondition'
pattern: '*.*example.com'
testCond(con, 'http://example.com/', 'match')
testCond(con, 'http://www.example.com/', 'match')
testCond(con, 'http://www.some-example.com/', 'match')
testCond(con, 'http://xample.com/', not 'match')
it 'should allow override of the magical behavior', ->
con =
conditionType: 'HostWildcardCondition'

View File

@ -131,16 +131,16 @@ describe 'RuleList', ->
result.should.have.length(0)
it 'should parse wildcard rules', ->
list = compose 'Wildcard': [
'*://example.com/*'
'*://example.com/abc/*'
]
result = parse(list, 'match', 'notmatch')
result.should.have.length(1)
result[0].should.eql(
source: '*://example.com/*'
source: '*://example.com/abc/*'
profileName: 'match'
condition:
conditionType: 'UrlWildcardCondition'
pattern: '*://example.com/*'
pattern: '*://example.com/abc/*'
)
it 'should parse RegExp rules', ->
list = compose 'RegExp': [