Skip to content

Commit

Permalink
pseudos: implement :icontains, case-insensitive version of :contains
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshv committed Nov 22, 2015
1 parent bc3ece1 commit dda1d4e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ _As defined by CSS 4 and / or jQuery._
* Pseudos:
* `:not`
* `:contains` *
* `:icontains` * (case-insensitive version of `:contains`)
* `:has` *
* `:root`
* `:empty`
Expand Down
7 changes: 7 additions & 0 deletions lib/pseudos.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ var filters = {
return next(elem) && getText(elem).indexOf(text) >= 0;
};
},
icontains: function(next, text){
var itext = text.toLowerCase();
return function icontains(elem){
return next(elem) &&
getText(elem).toLowerCase().indexOf(itext) >= 0;
};
},

//location specific methods
"nth-child": function(next, rule){
Expand Down
86 changes: 86 additions & 0 deletions test/icontains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var CSSselect = require("../"),
makeDom = require("htmlparser2").parseDOM,
assert = require("assert");

var dom = makeDom("<div><p>In the end, it doesn't really Matter.</p><div>Indeed-that's a delicate matter.</div>");

describe("icontains", function(){
describe("ignore case", function(){
it("should match full string", function(){
var matches = CSSselect.selectAll(":icontains(indeed-that's a delicate matter.)", dom);
assert.equal(matches.length, 2);
assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
matches = CSSselect.selectAll(":icontains(inDeeD-THAT's a DELICATE matteR.)", dom);
assert.equal(matches.length, 2);
assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
});

it("should match substring", function(){
var matches = CSSselect.selectAll(":icontains(indeed)", dom);
assert.equal(matches.length, 2);
assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
matches = CSSselect.selectAll(":icontains(inDeeD)", dom);
assert.equal(matches.length, 2);
assert.deepEqual(matches, [dom[0], dom[0].children[1]]);
});

it("should match specific element", function(){
var matches = CSSselect.selectAll("p:icontains(matter)", dom);
assert.equal(matches.length, 1);
assert.deepEqual(matches, [dom[0].children[0]]);
matches = CSSselect.selectAll("p:icontains(mATter)", dom);
assert.equal(matches.length, 1);
assert.deepEqual(matches, [dom[0].children[0]]);
});

it("should match multiple elements", function(){
var matches = CSSselect.selectAll(":icontains(matter)", dom);
assert.equal(matches.length, 3);
assert.deepEqual(matches, [dom[0], dom[0].children[0],
dom[0].children[1]]);
matches = CSSselect.selectAll(":icontains(mATter)", dom);
assert.equal(matches.length, 3);
assert.deepEqual(matches, [dom[0], dom[0].children[0],
dom[0].children[1]]);
});

it("should match empty string", function(){
var matches = CSSselect.selectAll(":icontains()", dom);
assert.equal(matches.length, 3);
assert.deepEqual(matches, [dom[0], dom[0].children[0],
dom[0].children[1]]);
});

it("should match quoted string", function(){
var matches = CSSselect.selectAll(":icontains('')", dom);
assert.equal(matches.length, 3);
assert.deepEqual(matches, [dom[0], dom[0].children[0],
dom[0].children[1]]);
matches = CSSselect.selectAll("p:icontains('matter')", dom);
assert.equal(matches.length, 1);
assert.deepEqual(matches, [dom[0].children[0]]);
matches = CSSselect.selectAll("p:icontains(\"matter\")", dom);
assert.equal(matches.length, 1);
assert.deepEqual(matches, [dom[0].children[0]]);
});

it("should match whitespace", function(){
var matches = CSSselect.selectAll(":icontains( matter)", dom);
assert.equal(matches.length, 3);
assert.deepEqual(matches, [dom[0], dom[0].children[0],
dom[0].children[1]]);
matches = CSSselect.selectAll(":icontains( mATter)", dom);
assert.equal(matches.length, 3);
assert.deepEqual(matches, [dom[0], dom[0].children[0],
dom[0].children[1]]);
});
});

describe("no matches", function(){
it("should not match", function(){
var matches = CSSselect.selectAll("p:icontains(indeed)", dom);
assert.equal(matches.length, 0);
});

});
});

0 comments on commit dda1d4e

Please sign in to comment.