tree-selector
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -16,3 +16,3 @@ "use strict"; | ||
} | ||
var classes = opts.className(node); | ||
var classes = opts.className(node).split(' '); | ||
for (var i = 0; i < classList.length; i++) { | ||
@@ -33,17 +33,22 @@ if (classes.indexOf(classList[i]) === -1) { | ||
} | ||
if (t === 'startsWith' && !attr.startsWith(v)) { | ||
return false; | ||
else if (t !== 'exact') { | ||
if (typeof v !== 'string') { | ||
throw new Error('All non-string values have to be an exact match'); | ||
} | ||
if (t === 'startsWith' && !attr.startsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'endsWith' && !attr.endsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'contains' && attr.indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'whitespace' && attr.split(' ').indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'dash' && attr.split('-').indexOf(v) === -1) { | ||
return false; | ||
} | ||
} | ||
if (t === 'endsWith' && !attr.endsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'contains' && attr.indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'whitespace' && attr.split(' ').indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'dash' && attr.split('-').indexOf(v) === -1) { | ||
return false; | ||
} | ||
} | ||
@@ -86,3 +91,2 @@ for (var i = 0; i < pseudos.length; i++) { | ||
if (factor && | ||
parseResult[1] && | ||
parseResult[2] === 'n' && | ||
@@ -98,4 +102,4 @@ index % factor !== add) { | ||
} | ||
else if (!parseResult[2] && | ||
index !== parseInt(parseResult[0] + parseResult[1])) { | ||
else if (!parseResult[2] && factor && | ||
index !== factor - 1) { | ||
return false; | ||
@@ -102,0 +106,0 @@ } |
import { Selector } from './selectorParser'; | ||
import { Options } from './types'; | ||
export declare function createQuerySelector<T>(options: Options<T>): (sel: string | Selector, node: T) => T[]; | ||
export declare function createQuerySelector<T>(options: Options<T>, matches?: (sel: string | Selector, node: T) => T | boolean): (sel: string | Selector, node: T) => T[]; |
@@ -5,6 +5,7 @@ "use strict"; | ||
var matches_1 = require("./matches"); | ||
function createQuerySelector(options) { | ||
var matches = matches_1.createMatches(options); | ||
function createQuerySelector(options, matches) { | ||
var _matches = matches || matches_1.createMatches(options); | ||
function findSubtree(selector, depth, node) { | ||
var matched = matches(selector, node) ? [node] : []; | ||
var n = _matches(selector, node); | ||
var matched = n ? (typeof n === 'object' ? [n] : [node]) : []; | ||
if (depth === 0) { | ||
@@ -30,4 +31,10 @@ return matched; | ||
} | ||
if (matches(selector, siblings[i])) { | ||
results.push(siblings[i]); | ||
var n = _matches(selector, siblings[i]); | ||
if (n) { | ||
if (typeof n === 'object') { | ||
results.push(n); | ||
} | ||
else { | ||
results.push(siblings[i]); | ||
} | ||
} | ||
@@ -34,0 +41,0 @@ if (next) { |
@@ -11,3 +11,3 @@ export interface Selector { | ||
export interface Attributes { | ||
[attr: string]: [AttributeMatch, string]; | ||
[attr: string]: [AttributeMatch, string | number | boolean]; | ||
} | ||
@@ -14,0 +14,0 @@ export declare type AttributeMatch = 'exact' | 'truthy' | 'startsWith' | 'endsWith' | 'contains' | 'whitespace' | 'dash'; |
@@ -13,7 +13,7 @@ "use strict"; | ||
var SPACE = '[ \t]*'; | ||
var STRING = "\"[^\"]*\""; | ||
var VALUE = "[^\\]]+"; | ||
var CLASS = "(?:\\." + IDENT + ")"; | ||
var ID = "(?:#" + IDENT + ")"; | ||
var OP = "(?:=|\\$=|\\^=|\\*=|~=|\\|=)"; | ||
var ATTR = "(?:\\[" + SPACE + IDENT + SPACE + "(?:" + OP + SPACE + STRING + SPACE + ")?\\])"; | ||
var ATTR = "(?:\\[" + SPACE + IDENT + SPACE + "(?:" + OP + SPACE + VALUE + SPACE + ")?\\])"; | ||
var SUBTREE = "(?:[ \t]+)"; | ||
@@ -24,3 +24,3 @@ var CHILD = "(?:" + SPACE + "(>)" + SPACE + ")"; | ||
var COMBINATOR = "(?:" + SUBTREE + "|" + CHILD + "|" + NEXT_SIBLING + "|" + SIBLING + ")"; | ||
var CONTAINS = "contains\\([^\\)]*\\)"; | ||
var CONTAINS = "contains\\(\"[^\"]*\"\\)"; | ||
var FORMULA = "(?:even|odd|\\d*(?:-?n(?:\\+\\d+)?)?)"; | ||
@@ -74,3 +74,3 @@ var NTH_CHILD = "nth-child\\(" + FORMULA + "\\)"; | ||
} | ||
var postprocessRegex = new RegExp("(" + IDENT + ")" + SPACE + "(" + OP + ")?" + SPACE + "(" + STRING + ")?"); | ||
var postprocessRegex = new RegExp("(" + IDENT + ")" + SPACE + "(" + OP + ")?" + SPACE + "(" + VALUE + ")?"); | ||
var attrs = matches | ||
@@ -82,3 +82,3 @@ .filter(function (s) { return s.startsWith('['); }) | ||
return (_b = {}, | ||
_b[attr] = [getOp(op), val ? val.slice(1, -1) : val], | ||
_b[attr] = [getOp(op), val ? parseAttrValue(val) : val], | ||
_b); | ||
@@ -101,2 +101,14 @@ var _b; | ||
exports.parseSelector = parseSelector; | ||
function parseAttrValue(v) { | ||
if (v.startsWith('"')) { | ||
return v.slice(1, -1); | ||
} | ||
if (v === "true") { | ||
return true; | ||
} | ||
if (v === "false") { | ||
return false; | ||
} | ||
return parseFloat(v); | ||
} | ||
function postProcessPseudos(sel) { | ||
@@ -110,3 +122,3 @@ if (sel === 'first-child' || | ||
if (sel.startsWith('contains')) { | ||
var text = sel.slice(9, -1); | ||
var text = sel.slice(10, -2); | ||
return ['contains', text]; | ||
@@ -113,0 +125,0 @@ } |
@@ -14,3 +14,3 @@ import { parseSelector } from './selectorParser'; | ||
} | ||
var classes = opts.className(node); | ||
var classes = opts.className(node).split(' '); | ||
for (var i = 0; i < classList.length; i++) { | ||
@@ -31,17 +31,22 @@ if (classes.indexOf(classList[i]) === -1) { | ||
} | ||
if (t === 'startsWith' && !attr.startsWith(v)) { | ||
return false; | ||
else if (t !== 'exact') { | ||
if (typeof v !== 'string') { | ||
throw new Error('All non-string values have to be an exact match'); | ||
} | ||
if (t === 'startsWith' && !attr.startsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'endsWith' && !attr.endsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'contains' && attr.indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'whitespace' && attr.split(' ').indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'dash' && attr.split('-').indexOf(v) === -1) { | ||
return false; | ||
} | ||
} | ||
if (t === 'endsWith' && !attr.endsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'contains' && attr.indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'whitespace' && attr.split(' ').indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'dash' && attr.split('-').indexOf(v) === -1) { | ||
return false; | ||
} | ||
} | ||
@@ -84,3 +89,2 @@ for (var i = 0; i < pseudos.length; i++) { | ||
if (factor && | ||
parseResult[1] && | ||
parseResult[2] === 'n' && | ||
@@ -96,4 +100,4 @@ index % factor !== add) { | ||
} | ||
else if (!parseResult[2] && | ||
index !== parseInt(parseResult[0] + parseResult[1])) { | ||
else if (!parseResult[2] && factor && | ||
index !== factor - 1) { | ||
return false; | ||
@@ -100,0 +104,0 @@ } |
import { Selector } from './selectorParser'; | ||
import { Options } from './types'; | ||
export declare function createQuerySelector<T>(options: Options<T>): (sel: string | Selector, node: T) => T[]; | ||
export declare function createQuerySelector<T>(options: Options<T>, matches?: (sel: string | Selector, node: T) => T | boolean): (sel: string | Selector, node: T) => T[]; |
import { parseSelector } from './selectorParser'; | ||
import { createMatches } from './matches'; | ||
export function createQuerySelector(options) { | ||
var matches = createMatches(options); | ||
export function createQuerySelector(options, matches) { | ||
var _matches = matches || createMatches(options); | ||
function findSubtree(selector, depth, node) { | ||
var matched = matches(selector, node) ? [node] : []; | ||
var n = _matches(selector, node); | ||
var matched = n ? (typeof n === 'object' ? [n] : [node]) : []; | ||
if (depth === 0) { | ||
@@ -27,4 +28,10 @@ return matched; | ||
} | ||
if (matches(selector, siblings[i])) { | ||
results.push(siblings[i]); | ||
var n = _matches(selector, siblings[i]); | ||
if (n) { | ||
if (typeof n === 'object') { | ||
results.push(n); | ||
} | ||
else { | ||
results.push(siblings[i]); | ||
} | ||
} | ||
@@ -31,0 +38,0 @@ if (next) { |
@@ -11,3 +11,3 @@ export interface Selector { | ||
export interface Attributes { | ||
[attr: string]: [AttributeMatch, string]; | ||
[attr: string]: [AttributeMatch, string | number | boolean]; | ||
} | ||
@@ -14,0 +14,0 @@ export declare type AttributeMatch = 'exact' | 'truthy' | 'startsWith' | 'endsWith' | 'contains' | 'whitespace' | 'dash'; |
@@ -11,7 +11,7 @@ var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
var SPACE = '[ \t]*'; | ||
var STRING = "\"[^\"]*\""; | ||
var VALUE = "[^\\]]+"; | ||
var CLASS = "(?:\\." + IDENT + ")"; | ||
var ID = "(?:#" + IDENT + ")"; | ||
var OP = "(?:=|\\$=|\\^=|\\*=|~=|\\|=)"; | ||
var ATTR = "(?:\\[" + SPACE + IDENT + SPACE + "(?:" + OP + SPACE + STRING + SPACE + ")?\\])"; | ||
var ATTR = "(?:\\[" + SPACE + IDENT + SPACE + "(?:" + OP + SPACE + VALUE + SPACE + ")?\\])"; | ||
var SUBTREE = "(?:[ \t]+)"; | ||
@@ -22,3 +22,3 @@ var CHILD = "(?:" + SPACE + "(>)" + SPACE + ")"; | ||
var COMBINATOR = "(?:" + SUBTREE + "|" + CHILD + "|" + NEXT_SIBLING + "|" + SIBLING + ")"; | ||
var CONTAINS = "contains\\([^\\)]*\\)"; | ||
var CONTAINS = "contains\\(\"[^\"]*\"\\)"; | ||
var FORMULA = "(?:even|odd|\\d*(?:-?n(?:\\+\\d+)?)?)"; | ||
@@ -72,3 +72,3 @@ var NTH_CHILD = "nth-child\\(" + FORMULA + "\\)"; | ||
} | ||
var postprocessRegex = new RegExp("(" + IDENT + ")" + SPACE + "(" + OP + ")?" + SPACE + "(" + STRING + ")?"); | ||
var postprocessRegex = new RegExp("(" + IDENT + ")" + SPACE + "(" + OP + ")?" + SPACE + "(" + VALUE + ")?"); | ||
var attrs = matches | ||
@@ -80,3 +80,3 @@ .filter(function (s) { return s.startsWith('['); }) | ||
return (_b = {}, | ||
_b[attr] = [getOp(op), val ? val.slice(1, -1) : val], | ||
_b[attr] = [getOp(op), val ? parseAttrValue(val) : val], | ||
_b); | ||
@@ -98,2 +98,14 @@ var _b; | ||
} | ||
function parseAttrValue(v) { | ||
if (v.startsWith('"')) { | ||
return v.slice(1, -1); | ||
} | ||
if (v === "true") { | ||
return true; | ||
} | ||
if (v === "false") { | ||
return false; | ||
} | ||
return parseFloat(v); | ||
} | ||
function postProcessPseudos(sel) { | ||
@@ -107,3 +119,3 @@ if (sel === 'first-child' || | ||
if (sel.startsWith('contains')) { | ||
var text = sel.slice(9, -1); | ||
var text = sel.slice(10, -2); | ||
return ['contains', text]; | ||
@@ -110,0 +122,0 @@ } |
{ | ||
"name": "tree-selector", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Use CSS selectors to match nodes in a custom object tree", | ||
@@ -5,0 +5,0 @@ "main": "lib/cjs/index.js", |
@@ -23,3 +23,3 @@ import { Options } from './types'; | ||
} | ||
const classes = opts.className(node); | ||
const classes = opts.className(node).split(' '); | ||
for (let i = 0; i < classList.length; i++) { | ||
@@ -41,18 +41,22 @@ if (classes.indexOf(classList[i]) === -1) { | ||
return false; | ||
} else if(t !== 'exact') { | ||
if(typeof v !== 'string') { | ||
throw new Error('All non-string values have to be an exact match'); | ||
} | ||
if (t === 'startsWith' && !attr.startsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'endsWith' && !attr.endsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'contains' && attr.indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'whitespace' && attr.split(' ').indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'dash' && attr.split('-').indexOf(v) === -1) { | ||
return false; | ||
} | ||
} | ||
if (t === 'startsWith' && !attr.startsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'endsWith' && !attr.endsWith(v)) { | ||
return false; | ||
} | ||
if (t === 'contains' && attr.indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'whitespace' && attr.split(' ').indexOf(v) === -1) { | ||
return false; | ||
} | ||
if (t === 'dash' && attr.split('-').indexOf(v) === -1) { | ||
return false; | ||
} | ||
} | ||
@@ -103,3 +107,2 @@ | ||
factor && | ||
parseResult[1] && | ||
parseResult[2] === 'n' && | ||
@@ -117,4 +120,4 @@ index % factor !== add | ||
} else if ( | ||
!parseResult[2] && | ||
index !== parseInt(parseResult[0] + parseResult[1]) | ||
!parseResult[2] && factor && | ||
index !== factor - 1 | ||
) { | ||
@@ -121,0 +124,0 @@ return false; |
@@ -6,8 +6,10 @@ import { Selector, parseSelector, Combinator } from './selectorParser'; | ||
export function createQuerySelector<T>( | ||
options: Options<T> | ||
options: Options<T>, | ||
matches?: (sel: string | Selector, node: T) => T | boolean | ||
): (sel: string | Selector, node: T) => T[] { | ||
const matches = createMatches(options); | ||
const _matches = matches || createMatches(options); | ||
function findSubtree(selector: Selector, depth: number, node: T): T[] { | ||
const matched = matches(selector, node) ? [node] : []; | ||
const n = _matches(selector, node); | ||
const matched = n ? (typeof n === 'object' ? [n] : [node]) : []; | ||
if (depth === 0) { | ||
@@ -37,4 +39,9 @@ return matched; | ||
} | ||
if (matches(selector, siblings[i] as T)) { | ||
results.push(siblings[i] as T); | ||
const n = _matches(selector, siblings[i] as T); | ||
if (n) { | ||
if(typeof n === 'object') { | ||
results.push(n); | ||
} else { | ||
results.push(siblings[i] as T); | ||
} | ||
} | ||
@@ -41,0 +48,0 @@ |
@@ -19,3 +19,3 @@ export interface Selector { | ||
export interface Attributes { | ||
[attr: string]: [AttributeMatch, string]; | ||
[attr: string]: [AttributeMatch, string | number | boolean]; | ||
} | ||
@@ -36,3 +36,3 @@ | ||
const SPACE = '[ \t]*'; | ||
const STRING = `"[^"]*"`; | ||
const VALUE = `[^\\]]+`; | ||
@@ -43,3 +43,3 @@ const CLASS = `(?:\\.${IDENT})`; | ||
const OP = `(?:=|\\$=|\\^=|\\*=|~=|\\|=)`; | ||
const ATTR = `(?:\\[${SPACE}${IDENT}${SPACE}(?:${OP}${SPACE}${STRING}${SPACE})?\\])`; | ||
const ATTR = `(?:\\[${SPACE}${IDENT}${SPACE}(?:${OP}${SPACE}${VALUE}${SPACE})?\\])`; | ||
@@ -53,3 +53,3 @@ const SUBTREE = `(?:[ \t]+)`; | ||
const CONTAINS = `contains\\([^\\)]*\\)`; | ||
const CONTAINS = `contains\\("[^"]*"\\)`; | ||
const FORMULA = `(?:even|odd|\\d*(?:-?n(?:\\+\\d+)?)?)`; | ||
@@ -114,3 +114,3 @@ const NTH_CHILD = `nth-child\\(${FORMULA}\\)`; | ||
const postprocessRegex = new RegExp( | ||
`(${IDENT})${SPACE}(${OP})?${SPACE}(${STRING})?` | ||
`(${IDENT})${SPACE}(${OP})?${SPACE}(${VALUE})?` | ||
); | ||
@@ -121,3 +121,3 @@ const attrs = matches | ||
.map(([attr, op, val]) => ({ | ||
[attr]: [getOp(op), val ? val.slice(1, -1) : val] | ||
[attr]: [getOp(op), val ? parseAttrValue(val) : val] | ||
})) | ||
@@ -140,2 +140,15 @@ .reduce((acc, curr) => ({ ...acc, ...curr }), {}) as Attributes; | ||
function parseAttrValue(v: string): string | boolean | number { | ||
if(v.startsWith('"')) { | ||
return v.slice(1, -1); | ||
} | ||
if(v === "true") { | ||
return true; | ||
} | ||
if(v === "false") { | ||
return false; | ||
} | ||
return parseFloat(v); | ||
} | ||
function postProcessPseudos(sel: string): Pseudo { | ||
@@ -151,3 +164,3 @@ if ( | ||
if (sel.startsWith('contains')) { | ||
const text = sel.slice(9, -1); | ||
const text = sel.slice(10, -2); | ||
return ['contains', text]; | ||
@@ -154,0 +167,0 @@ } |
@@ -29,2 +29,11 @@ import { createMatches } from '../src/index'; | ||
it('should not match when class names overlap', () => { | ||
const testElement = { | ||
tag: 'div', | ||
className: 'nomatches' | ||
}; | ||
assert.equal(matches('.matches', testElement), false); | ||
}); | ||
it('should match against an attribute selector', () => { | ||
@@ -61,2 +70,16 @@ const testElement = { | ||
it('should match against an attribute selector with number', () => { | ||
const testElement = { | ||
tag: 'div', | ||
className: 'foo bar baz', | ||
attributes: { | ||
test: 'foo', | ||
bar: 'buzjjjjjjj', | ||
zuz: 1 | ||
} | ||
}; | ||
assert.equal(matches('div[zuz=1]', testElement), true); | ||
}); | ||
it('should not match non-existant attributes', () => { | ||
@@ -116,3 +139,3 @@ const testElement = { | ||
assert(matches('div:root', vtree)); | ||
assert(matches('div:contains(blabla)', vtree.children[1])); | ||
assert(matches('div:contains("blabla")', vtree.children[1])); | ||
@@ -127,3 +150,3 @@ assert.strictEqual( | ||
assert.strictEqual( | ||
matches('div:contains(blabla)', vtree.children[0]), | ||
matches('div:contains("blabla")', vtree.children[0]), | ||
false | ||
@@ -151,3 +174,3 @@ ); | ||
assert(matches('div:nth-child(odd)', vtree.children[1])); | ||
assert(matches('div:nth-child(3)', vtree.children[3])); | ||
assert(matches('div:nth-child(3)', vtree.children[2])); | ||
assert(matches('div:nth-child(n+2)', vtree.children[4])); | ||
@@ -154,0 +177,0 @@ assert(matches('div:nth-child(-n+2)', vtree.children[1])); |
@@ -151,3 +151,7 @@ import { parseSelector } from '../src/index'; | ||
if (v !== undefined) { | ||
selector += `(${v})`; | ||
if(k === 'contains') { | ||
selector += `("${v}")`; | ||
} else { | ||
selector += `(${v})`; | ||
} | ||
} | ||
@@ -154,0 +158,0 @@ const result = parseSelector(selector); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
135465
1663