unist-util-select
Advanced tools
+5
-0
@@ -6,6 +6,11 @@ 'use strict'; | ||
| var debug = require('debug')('unist-util-select'); | ||
| module.exports = function (ast, selector) { | ||
| debug('Selector: %j', selector); | ||
| selector = parseSelector(selector); | ||
| debug('AST: %s', | ||
| JSON.stringify(selector, null, 2).replace(/(^|\n)/g, '\n ')); | ||
| return selector ? select[selector.type](selector, ast) : []; | ||
| }; |
+18
-4
@@ -81,9 +81,10 @@ 'use strict'; | ||
| function matches (rule, node) { | ||
| var match = true; | ||
| // Match type. | ||
| if (rule.tagName && rule.tagName != '*' && node.type != rule.tagName) { | ||
| return false; | ||
| } | ||
| match = match && (!rule.tagName || rule.tagName == '*' || | ||
| rule.tagName == node.type); | ||
| // Match attributes. | ||
| return (rule.attrs || []).every(function (attr) { | ||
| match = match && (rule.attrs || []).every(function (attr) { | ||
| switch (attr.operator) { | ||
@@ -126,2 +127,15 @@ case undefined: | ||
| }); | ||
| // Match pseudo classes. | ||
| match = match && (rule.pseudos || []).every(function (pseudo) { | ||
| switch (pseudo.name) { | ||
| case 'not': | ||
| return !matches(pseudo.value.rule, node); | ||
| default: | ||
| throw Error('Undefined pseudo-class: ' + pseudo.name); | ||
| } | ||
| }); | ||
| return match; | ||
| } | ||
@@ -128,0 +142,0 @@ |
+1
-0
@@ -10,3 +10,4 @@ 'use strict'; | ||
| parser.registerAttrEqualityMods('^', '*', '$'); | ||
| parser.registerSelectorPseudos('not'); | ||
| return parser.parse.bind(parser); | ||
| }; |
+3
-2
| { | ||
| "name": "unist-util-select", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "Select unist nodes using css-like selectors", | ||
@@ -43,3 +43,4 @@ "author": "Eugene Sharygin <eush77@gmail.com>", | ||
| "dependencies": { | ||
| "css-selector-parser": "^1.1.0" | ||
| "css-selector-parser": "^1.1.0", | ||
| "debug": "^2.2.0" | ||
| }, | ||
@@ -46,0 +47,0 @@ "devDependencies": { |
+1
-0
@@ -62,2 +62,3 @@ [](https://npmjs.com/package/unist-util-select) | ||
| - [x] Ends with: `[value$="suffix"]` | ||
| - [x] Negation pseudo-class: `*:not(paragraph)` | ||
@@ -64,0 +65,0 @@ ## API |
8159
8.38%138
12.2%79
1.28%2
100%+ Added
+ Added
+ Added