-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3c10a63
Showing
7 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.nyc_output | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
- "0.12" | ||
- "4" | ||
- "5" | ||
- "6" | ||
- "7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2016 Thomas Rasshofer <hello@thomasrasshofer.com> | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# CSS Selector Tree | ||
|
||
> A small helper to split CSS selectors into subjects and conditions | ||
[![Build Status](https://travis-ci.org/rasshofer/cssSelectorTree.svg)](https://travis-ci.org/rasshofer/css-selector-tree) | ||
[![Coverage Status](https://coveralls.io/repos/github/rasshofer/css-selector-tree/badge.svg)](https://coveralls.io/github/rasshofer/css-selector-tree) | ||
[![Dependency Status](https://david-dm.org/rasshofer/css-selector-tree/status.svg)](https://david-dm.org/rasshofer/css-selector-tree) | ||
[![Dependency Status](https://david-dm.org/rasshofer/css-selector-tree/dev-status.svg)](https://david-dm.org/rasshofer/css-selector-tree) | ||
|
||
## Installation | ||
|
||
```shell | ||
npm install css-selector-tree --save-dev | ||
``` | ||
|
||
## API | ||
|
||
### cssSelectorTree.tree(selector) | ||
|
||
Accepts a CSS selector and returns its tree (subject and conditions). | ||
|
||
In case no valid CSS selector is provided, `false` is returned. | ||
|
||
### cssSelectorTree.subject(selector) | ||
|
||
Accepts a CSS selector and returns its subject. | ||
|
||
In case no valid CSS selector is provided, `false` is returned. | ||
|
||
### cssSelectorTree.conditions(selector) | ||
|
||
Accepts a CSS selector and returns its conditions. | ||
|
||
In case no valid CSS selector is provided, `false` is returned. | ||
|
||
## Changelog | ||
|
||
* 0.0.1 | ||
* Initial version | ||
|
||
## License | ||
|
||
Copyright (c) 2016 [Thomas Rasshofer](http://thomasrasshofer.com/) | ||
Licensed under the MIT license. | ||
|
||
See LICENSE for more info. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var tokenizer = require('css-selector-tokenizer'); | ||
|
||
function generateSelectorTree (selector) { | ||
|
||
if (!selector) { | ||
return false; | ||
} | ||
|
||
selector = selector.split(/,/)[0]; | ||
|
||
var parsed = tokenizer.parse(selector); | ||
|
||
var filtered = parsed.nodes.filter(function (node) { | ||
return node.type === 'selector' && node.nodes && node.nodes.length; | ||
}); | ||
|
||
if (filtered.length === 0) { | ||
return false; | ||
} | ||
|
||
var conditions = filtered.shift().nodes.filter(function (subnode) { | ||
if (subnode.type === 'spacing') { | ||
subnode.type = 'child'; | ||
delete subnode.value; | ||
} | ||
return subnode; | ||
}); | ||
|
||
return conditions.reverse(); | ||
|
||
} | ||
|
||
function extractSelectorSubject (selector) { | ||
|
||
var tree = generateSelectorTree(selector); | ||
|
||
if (tree === false) { | ||
return false; | ||
} | ||
|
||
return tree[0]; | ||
|
||
} | ||
|
||
function extractSelectorConditions (selector) { | ||
|
||
var tree = generateSelectorTree(selector); | ||
|
||
if (tree === false) { | ||
return false; | ||
} | ||
|
||
return tree.slice(1); | ||
|
||
} | ||
|
||
module.exports = { | ||
tree: generateSelectorTree, | ||
subject: extractSelectorSubject, | ||
conditions: extractSelectorConditions | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "css-selector-tree", | ||
"version": "0.0.1", | ||
"description": "A small helper to split CSS selectors into subjects and conditions", | ||
"author": { | ||
"name": "Thomas Rasshofer", | ||
"email": "hello@thomasrasshofer.com", | ||
"url": "http://thomasrasshofer.com/" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/rasshofer/css-selector-tree.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/rasshofer/css-selector-tree/issues" | ||
}, | ||
"main": "css-selector-tree.js", | ||
"keywords": [ | ||
"css-selector-tree", | ||
"css", | ||
"complexity" | ||
], | ||
"devDependencies": { | ||
"jshint": "^2.9.3", | ||
"tap": "^7.1.1" | ||
}, | ||
"dependencies": { | ||
"css-selector-tokenizer": "^0.7.0" | ||
}, | ||
"scripts": { | ||
"test": "jshint css-selector-tree.js && tap tests.js --cov" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var tap = require('tap'); | ||
var cssSelectorTree = require('./css-selector-tree'); | ||
|
||
// API | ||
|
||
tap.equal(typeof cssSelectorTree, 'object'); | ||
tap.equal(typeof cssSelectorTree.tree, 'function'); | ||
tap.equal(typeof cssSelectorTree.subject, 'function'); | ||
tap.equal(typeof cssSelectorTree.conditions, 'function'); | ||
|
||
// Tree API | ||
|
||
tap.equal(cssSelectorTree.tree(null), false); | ||
tap.equal(cssSelectorTree.tree(), false); | ||
tap.equal(cssSelectorTree.tree(''), false); | ||
tap.equal(cssSelectorTree.tree(new Array(3).join(' ')), false); | ||
tap.deepEqual(cssSelectorTree.tree('.grid-homepage .teaser .article-title .headline'), [{ | ||
type: 'class', | ||
name: 'headline' | ||
}, { | ||
type: 'child' | ||
}, { | ||
type: 'class', | ||
name: 'article-title' | ||
}, { | ||
type: 'child' | ||
}, { | ||
type: 'class', | ||
name: 'teaser' | ||
}, { | ||
type: 'child' | ||
}, { | ||
type: 'class', | ||
name: 'grid-homepage' | ||
}]); | ||
|
||
// Subject API | ||
|
||
tap.equal(cssSelectorTree.subject(null), false); | ||
tap.equal(cssSelectorTree.subject(), false); | ||
tap.equal(cssSelectorTree.subject(''), false); | ||
tap.equal(cssSelectorTree.subject(new Array(3).join(' ')), false); | ||
tap.deepEqual(cssSelectorTree.subject('.grid-homepage .teaser .article-title .headline'), { | ||
type: 'class', | ||
name: 'headline' | ||
}); | ||
|
||
// Conditions API | ||
|
||
tap.equal(cssSelectorTree.conditions(null), false); | ||
tap.equal(cssSelectorTree.conditions(), false); | ||
tap.equal(cssSelectorTree.conditions(''), false); | ||
tap.equal(cssSelectorTree.conditions(new Array(3).join(' ')), false); | ||
tap.deepEqual(cssSelectorTree.conditions('.grid-homepage .teaser .article-title .headline'), [{ | ||
type: 'child' | ||
}, { | ||
type: 'class', | ||
name: 'article-title' | ||
}, { | ||
type: 'child' | ||
}, { | ||
type: 'class', | ||
name: 'teaser' | ||
}, { | ||
type: 'child' | ||
}, { | ||
type: 'class', | ||
name: 'grid-homepage' | ||
}]); |