Skip to content

Commit

Permalink
Merge pull request #6 from featurist/case-insensitive-tags
Browse files Browse the repository at this point in the history
Case-insensitive element names
  • Loading branch information
chrisdickinson committed May 7, 2016
2 parents ed8ac6f + bda120d commit f110680
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module.exports = language

var tokenizer = require('./tokenizer')

function language(lookups) {
function language(lookups, matchComparison) {
return function(selector) {
return parse(selector, remap(lookups))
return parse(selector, remap(lookups),
matchComparison || caseSensitiveComparison)
}
}

Expand All @@ -23,7 +24,7 @@ function opt_okay(opts, key) {
return opts.hasOwnProperty(key) && typeof opts[key] === 'string'
}

function parse(selector, options) {
function parse(selector, options, matchComparison) {
var stream = tokenizer()
, default_subj = true
, selectors = [[]]
Expand Down Expand Up @@ -74,7 +75,7 @@ function parse(selector, options) {
token.type === 'attr' ? attr(token) :
token.type === ':' || token.type === '::' ? pseudo(token) :
token.type === '*' ? Boolean :
matches(token.type, token.data)
matches(token.type, token.data, matchComparison)
)
}

Expand Down Expand Up @@ -183,9 +184,9 @@ function parse(selector, options) {
valid_attr(options.attr, token.data)
}

function matches(type, data) {
function matches(type, data, matchComparison) {
return function(node) {
return options[type](node) == data
return matchComparison(type, options[type](node), data);
}
}

Expand Down Expand Up @@ -243,7 +244,7 @@ function parse(selector, options) {
}

function pseudo(token) {
return valid_pseudo(options, token.data)
return valid_pseudo(options, token.data, matchComparison)
}

}
Expand All @@ -252,7 +253,7 @@ function entry(node, next, subj) {
return next(node, subj) ? node : null
}

function valid_pseudo(options, match) {
function valid_pseudo(options, match, matchComparison) {
switch(match) {
case 'empty': return valid_empty(options)
case 'first-child': return valid_first_child(options)
Expand All @@ -265,11 +266,11 @@ function valid_pseudo(options, match) {
}

if(match.indexOf('any') === 0) {
return valid_any_match(options, match.slice(4, -1))
return valid_any_match(options, match.slice(4, -1), matchComparison)
}

if(match.indexOf('not') === 0) {
return valid_not_match(options, match.slice(4, -1))
return valid_not_match(options, match.slice(4, -1), matchComparison)
}

if(match.indexOf('nth-child') === 0) {
Expand All @@ -281,8 +282,8 @@ function valid_pseudo(options, match) {
}
}

function valid_not_match(options, selector) {
var fn = parse(selector, options)
function valid_not_match(options, selector, matchComparison) {
var fn = parse(selector, options, matchComparison)

return not_function

Expand All @@ -291,8 +292,8 @@ function valid_not_match(options, selector) {
}
}

function valid_any_match(options, selector) {
var fn = parse(selector, options)
function valid_any_match(options, selector, matchComparison) {
var fn = parse(selector, options, matchComparison)

return fn
}
Expand Down Expand Up @@ -436,3 +437,7 @@ function check_spc(l, r) {
function check_dsh(l, r) {
return l.split('-').indexOf(r) > -1
}

function caseSensitiveComparison(type, pattern, data) {
return pattern === data;
}
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ language = cssauron({
, parent: 'parent'
, children: 'children'
, contents: 'contents || ""'
}, function(type, pattern, data) {
if (type == 'tag') {
return pattern.toLowerCase() == data.toLowerCase();
} else {
return pattern == data;
}
})

test('select single', test_select_single)
Expand All @@ -26,6 +32,8 @@ function test_select_single(assert) {
assert.ok(!language('.one-other-class')(data))
assert.ok(language('one-tag')(data))
assert.ok(!language('two-tag')(data))
assert.ok(language('ONE-TAG')(data))
assert.ok(!language('TWO-TAG')(data))
assert.end()
}

Expand Down Expand Up @@ -76,6 +84,7 @@ function test_select_multiple(assert) {
assert.ok(language('.one-class + .two-class')(data2))
assert.ok(!language('.one-class + #one-id')(data))
assert.ok(language('one-tag ~ #three-id')(data3))
assert.ok(language('ONE-TAG ~ #three-id')(data3))
assert.ok(language('one-tag:first-child')(data))
assert.ok(language('one-tag:empty')(data))
assert.ok(!language('#parent-id:empty')(parent))
Expand Down

0 comments on commit f110680

Please sign in to comment.