Skip to content

Commit

Permalink
[changed] added isSelector option to registerPseduo to allow non se…
Browse files Browse the repository at this point in the history
…lctor pseudo calls
  • Loading branch information
jquense committed Dec 26, 2015
1 parent 497260c commit a03390d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,26 @@ let found = findAll(elements, function (node) {
})
```

### registerPseudo(pseudoSelector, testFunction)
### registerPseudo(pseudoSelector, isSelector=true, testFunction)

Registers a new pseudo selector with the compiler. The second parameter is a function that will be called
with the compiled inner selector of the pseudo selector (if it exists) and should return a __new__ function that
tests an element or node.
tests an element or node. The second `isSelector` argument indicates that the inner text of the pseudoSelector
(as in `'foo'` in `:has(.foo)`) is itself a selector that needs to be compiled into a function.

```js
// A simple `:disabled` pseudo selector
bill.registerPseudo('disabled', function() {
bill.registerPseudo('disabled', false, function() {
return (node) => node.nodeType !== NODE_TYPES.TEXT
&& node.element.props.disabled === true
})

// We want to test if an element has a sibling that matches
// a selector e.g. :nextSibling('.foo')
bill.registerPseudo('nextSibling', function (test) {
// a selector e.g. :nextSibling(.foo)
bill.registerPseudo('nextSibling', function (compiledInnerSelector) {
return function (node) {
node = node.nextSibling
return !!node && test(node)
return !!node && compiledInnerSelector(node)
}
})

Expand Down
2 changes: 1 addition & 1 deletion lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function createNode(subject, lastWrapper) {
var children = undefined,
type = undefined;

if (element) {
if (element != null && element !== false) {
if (_utils.isCompositeElement(element)) type = NODE_TYPES.COMPOSITE;else if (_utils.isDomElement(element)) type = NODE_TYPES.DOM;else if (_utils.isTextElement(element)) type = NODE_TYPES.TEXT;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bill",
"version": "2.0.2",
"version": "2.0.3",
"description": "css selectors for React Elements",
"main": "index.js",
"repository": {
Expand Down
11 changes: 8 additions & 3 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ export function create(options = {}) {
NESTING[name] = fn
},

registerPseudo(name, fn){
parser.registerSelectorPseudos(name)
registerPseudo(name, containsSelector, fn) {
if (typeof containsSelector === 'function')
fn = containsSelector, containsSelector = true;

if (containsSelector)
parser.registerSelectorPseudos(name)

PSEUDOS[name] = fn
}
}
Expand Down Expand Up @@ -115,7 +120,7 @@ export function create(options = {}) {

let pseudoCompiled = pseudo.valueType === 'selector'
? compile(pseudo.value, values)
: pseudo
: pseudo.value

return PSEUDOS[pseudo.name](pseudoCompiled, values, options)
})
Expand Down
12 changes: 11 additions & 1 deletion test/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Main exports', () => {
])
})

it('should registerPseudo', ()=> {
it('should registerPseudo selector', ()=> {
bill.registerPseudo('nextSibling', test => (node) => {
node = node.nextSibling
return !!node && test(node)
Expand All @@ -61,6 +61,16 @@ describe('Main exports', () => {
matches[0].element.props.children.should.equal('2')
})

it('should registerPseudo', ()=> {
bill.registerPseudo('test', false, text => (node) => {
expect(text).to.equal('foo')
})

let matches = bill.match('li:test(foo)',
<li className='foo'>foo</li>
)
})

it('should registerNesting', ()=> {
bill.registerNesting('!', test => node => {
node = node.nextSibling
Expand Down

0 comments on commit a03390d

Please sign in to comment.