Skip to content

Commit 0119d18

Browse files
committed
Initial commit
1 parent b494f29 commit 0119d18

File tree

5 files changed

+437
-0
lines changed

5 files changed

+437
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Nik Coughlin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

index.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
function isTag(elem){
2+
return elem.nodeType === 1;
3+
}
4+
function getChildren(elem){
5+
return Array.prototype.slice.call(elem.childNodes, 0);
6+
}
7+
function getParent(elem){
8+
return elem.parentElement;
9+
}
10+
function removeSubsets(nodes) {
11+
var idx = nodes.length, node, ancestor, replace;
12+
13+
// Check if each node (or one of its ancestors) is already contained in the
14+
// array.
15+
while(--idx > -1) {
16+
node = ancestor = nodes[idx];
17+
18+
// Temporarily remove the node under consideration
19+
nodes[idx] = null;
20+
replace = true;
21+
22+
while(ancestor) {
23+
if(nodes.indexOf(ancestor) > -1) {
24+
replace = false;
25+
nodes.splice(idx, 1);
26+
break;
27+
}
28+
ancestor = getParent(ancestor)
29+
}
30+
31+
// If the node has been found to be unique, re-insert it.
32+
if(replace) {
33+
nodes[idx] = node;
34+
}
35+
}
36+
37+
return nodes;
38+
}
39+
40+
var adapter = {
41+
isTag: isTag,
42+
existsOne: function(test, elems){
43+
return elems.some(function(elem){
44+
return isTag(elem) ?
45+
test(elem) || adapter.existsOne(test, getChildren(elem)) :
46+
false;
47+
});
48+
},
49+
getSiblings: function(elem){
50+
var parent = getParent(elem);
51+
return parent && getChildren(parent);
52+
},
53+
getChildren: getChildren,
54+
getParent: getParent,
55+
getAttributeValue: function(elem, name){
56+
if(elem.attributes && elem.attributes[name]){
57+
return elem.attributes[name].value;
58+
}
59+
},
60+
hasAttrib: function(elem, name){
61+
return name in elem.attributes;
62+
},
63+
removeSubsets: removeSubsets,
64+
getName: function(elem){
65+
return elem.tagName.toLowerCase();
66+
},
67+
findOne: function findOne(test, arr){
68+
var elem = null;
69+
70+
for(var i = 0, l = arr.length; i < l && !elem; i++){
71+
if(test(arr[i])){
72+
elem = arr[i];
73+
} else {
74+
var childs = getChildren(arr[i]);
75+
if(childs && childs.length > 0){
76+
elem = findOne(test, childs);
77+
}
78+
}
79+
}
80+
81+
return elem;
82+
},
83+
findAll: function findAll(test, elems){
84+
var result = [];
85+
for(var i = 0, j = elems.length; i < j; i++){
86+
if(!isTag(elems[i])) continue;
87+
if(test(elems[i])) result.push(elems[i]);
88+
var childs = getChildren(elems[i]);
89+
if(childs) result = result.concat(findAll(test, childs));
90+
}
91+
return result;
92+
},
93+
getText: function getText(elem) {
94+
if(Array.isArray(elem)) return elem.map(getText).join("");
95+
96+
if(isTag(elem)) return getText(getChildren(elem));
97+
98+
if(elem.nodeType === 3) return elem.nodeValue;
99+
100+
return "";
101+
}
102+
};
103+
104+
module.exports = adapter;

package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "css-select-browser-adapter",
3+
"version": "0.1.0",
4+
"description": "Browser adapter for css-select",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/nrkn/css-select-browser-adapter.git"
15+
},
16+
"keywords": [
17+
"css",
18+
"select",
19+
"browser",
20+
"adapter"
21+
],
22+
"author": "Nik Coughlin <nrkn.com@gmail.com>",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/nrkn/css-select-browser-adapter/issues"
26+
},
27+
"homepage": "https://github.com/nrkn/css-select-browser-adapter#readme",
28+
"devDependencies": {
29+
"css-select": "^1.2.0",
30+
"jsdom": "^9.8.0",
31+
"mocha": "^3.1.2"
32+
}
33+
}

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# css-select-browser-adapter
2+
3+
An adapter for `css-select` that allows you to query the browser DOM
4+
5+
For more information see [css-select](https://github.com/fb55/css-select)

0 commit comments

Comments
 (0)