Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rasshofer committed Oct 25, 2016
0 parents commit 3c10a63
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.nyc_output
coverage
9 changes: 9 additions & 0 deletions .travis.yml
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"
22 changes: 22 additions & 0 deletions LICENSE
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.
46 changes: 46 additions & 0 deletions README.md
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.
61 changes: 61 additions & 0 deletions css-selector-tree.js
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
};
34 changes: 34 additions & 0 deletions package.json
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"
}
}
71 changes: 71 additions & 0 deletions tests.js
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'
}]);

0 comments on commit 3c10a63

Please sign in to comment.