Skip to content

Commit da7318e

Browse files
authored
Use options to pass through adapters (#101)
Breaking change: Pseudos now take the adapter as the second argument, and arguments as the third. Filters aren’t checked anymore if they have the required number of arguments
1 parent 05b09dd commit da7318e

16 files changed

Lines changed: 3054 additions & 1406 deletions

File tree

index.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,36 @@
22

33
module.exports = CSSselect;
44

5-
var DomUtils = require("domutils"),
6-
falseFunc = require("boolbase").falseFunc,
7-
compileFactory = require("./lib/compile.js"),
8-
defaultCompile = compileFactory(DomUtils);
5+
var DomUtils = require("domutils");
6+
var falseFunc = require("boolbase").falseFunc;
7+
var compileRaw = require("./lib/compile.js");
98

10-
function adapterCompile(adapter){
11-
return adapter === DomUtils ? defaultCompile : compileFactory(adapter);
9+
function wrapCompile(func){
10+
return function addAdapter(selector, options, context){
11+
options = options || {};
12+
options.adapter = options.adapter || DomUtils;
13+
14+
return func(selector, options, context);
15+
};
1216
}
1317

18+
var compile = wrapCompile(compileRaw);
19+
var compileUnsafe = wrapCompile(compileRaw.compileUnsafe);
20+
1421
function getSelectorFunc(searchFunc){
1522
return function select(query, elems, options){
16-
options = options || {}
23+
options = options || {};
1724
options.adapter = options.adapter || DomUtils;
18-
var compile = adapterCompile(options.adapter);
1925

20-
if(typeof query !== "function") query = compile.compileUnsafe(query, options, elems);
21-
if(query.shouldTestNextSiblings) elems = appendNextSiblings((options && options.context) || elems, options.adapter);
26+
if(typeof query !== "function"){
27+
query = compileUnsafe(query, options, elems);
28+
}
29+
if(query.shouldTestNextSiblings){
30+
elems = appendNextSiblings(
31+
(options && options.context) || elems,
32+
options.adapter
33+
);
34+
}
2235
if(!Array.isArray(elems)) elems = options.adapter.getChildren(elems);
2336
else elems = options.adapter.removeSubsets(elems);
2437
return searchFunc(query, elems, options);
@@ -46,17 +59,20 @@ function appendNextSiblings(elems, adapter){
4659
}
4760

4861
var selectAll = getSelectorFunc(function selectAll(query, elems, options){
49-
return (query === falseFunc || !elems || elems.length === 0) ? [] : options.adapter.findAll(query, elems);
62+
return query === falseFunc || !elems || elems.length === 0
63+
? []
64+
: options.adapter.findAll(query, elems);
5065
});
5166

5267
var selectOne = getSelectorFunc(function selectOne(query, elems, options){
53-
return (query === falseFunc || !elems || elems.length === 0) ? null : options.adapter.findOne(query, elems);
68+
return query === falseFunc || !elems || elems.length === 0
69+
? null
70+
: options.adapter.findOne(query, elems);
5471
});
5572

5673
function is(elem, query, options){
57-
options = options || {}
74+
options = options || {};
5875
options.adapter = options.adapter || DomUtils;
59-
var compile = adapterCompile(options.adapter);
6076
return (typeof query === "function" ? query : compile(query, options))(elem);
6177
}
6278

@@ -67,19 +83,19 @@ function CSSselect(query, elems, options){
6783
return selectAll(query, elems, options);
6884
}
6985

70-
CSSselect.compile = defaultCompile;
71-
CSSselect.filters = defaultCompile.Pseudos.filters;
72-
CSSselect.pseudos = defaultCompile.Pseudos.pseudos;
86+
CSSselect.compile = compile;
87+
CSSselect.filters = compileRaw.Pseudos.filters;
88+
CSSselect.pseudos = compileRaw.Pseudos.pseudos;
7389

7490
CSSselect.selectAll = selectAll;
7591
CSSselect.selectOne = selectOne;
7692

7793
CSSselect.is = is;
7894

7995
//legacy methods (might be removed)
80-
CSSselect.parse = defaultCompile;
96+
CSSselect.parse = compile;
8197
CSSselect.iterate = selectAll;
8298

8399
//hooks
84-
CSSselect._compileUnsafe = defaultCompile.compileUnsafe;
85-
CSSselect._compileToken = defaultCompile.compileToken;
100+
CSSselect._compileUnsafe = compileUnsafe;
101+
CSSselect._compileToken = compileRaw.compileToken;

0 commit comments

Comments
 (0)