Skip to content

Commit

Permalink
[added] Node object abstraction used by the compiler
Browse files Browse the repository at this point in the history
Adds a light abstraction over element and instances for a common
traversal API
  • Loading branch information
jquense committed Dec 20, 2015
1 parent 2885893 commit f353cde
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 216 deletions.
66 changes: 0 additions & 66 deletions src/common.js

This file was deleted.

40 changes: 23 additions & 17 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import React from 'react';
import transform from 'lodash/object/transform';
import has from 'lodash/object/has';
import uid from 'lodash/utility/uniqueId';
import { createNode, NODE_TYPES } from './node';
import { isTextElement, legacySelector } from './utils';
import { CssSelectorParser } from 'css-selector-parser';
import fnName from 'fn-name';

let parser = new CssSelectorParser();
let name = type => type.displayName || fnName(type) || ''

let name = type => typeof type === 'string'
? type : type.displayName || fnName(type) || ''

let prim = value => {
let typ = typeof value;
return value === null || ['string', 'number'].indexOf(typ) !== -1
}

function failText(fn){
return (...args) => isTextElement(args[0]) ? false : fn(...args)
return (...args) => args[0].nodeType === NODE_TYPES.TEXT ? false : fn(...args)
}

export function parse(selector){
Expand Down Expand Up @@ -88,9 +91,7 @@ export function create(options = {}) {
let rule = rules.shift();

if (rule.tagName)
fns.push(
failText(getTagComparer(rule, values))
)
fns.push(getTagComparer(rule, values))

if (rule.attrs)
fns.push(
Expand All @@ -99,7 +100,7 @@ export function create(options = {}) {

if (rule.classNames)
fns.push(
failText(({ props }) => {
failText(({ element: { props } }) => {
let className = props && props.className
return rule.classNames.every(clsName =>
className && className.indexOf(clsName) !== -1)
Expand Down Expand Up @@ -131,9 +132,11 @@ export function create(options = {}) {
fns.push(NESTING[operator](nestedCompiled))
}

return fns.reduce((current, next = ()=> true)=> {
let compiledRule = fns.reduce((current, next = ()=> true)=> {
return (...args)=> current(...args) && next(...args)
})

return (element, ...args)=> compiledRule(createNode(element), ...args)
}

function selector(strings, ...values){
Expand Down Expand Up @@ -161,24 +164,27 @@ export function create(options = {}) {
}

function getTagComparer(rule, values) {
let isStr = t => typeof t === 'string'
let tagName = values[rule.tagName] || rule.tagName;
let tagName = values[rule.tagName] || rule.tagName
, test;

if (rule.tagName === '*')
return ()=> true
test = ()=> true

else {
if (typeof tagName !== 'string')
test = root => root.element.type === tagName
else {
test = root => name(root.element.type).toUpperCase() === tagName.toUpperCase();
}

if (isStr(tagName)){
tagName = tagName.toUpperCase();
return root => isStr(root.type)
? root.type.toUpperCase() === tagName
: name(root.type).toUpperCase() === tagName;
test = failText(test)
}

return root => root.type === tagName
return test
}

function getPropComparer(rule, values) {
return ({ props }) => rule.attrs.every(attr => {
return ({ element: { props } }) => rule.attrs.every(attr => {
if (!has(attr, 'value'))
return !!props[attr.name]

Expand Down
42 changes: 0 additions & 42 deletions src/element-selector.js

This file was deleted.

24 changes: 17 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { isValidElement } from 'react';
import * as elements from './element-selector';
import * as instance from './instance-selector';
import { NODE_TYPE } from './node';
import { findAll, createNode } from './node';
import { create as createCompiler, parse } from './compiler';
import selectors from './selectors';

function match(selector, element){
if (isValidElement(element))
return elements.match(selector, element)
let compiler = createCompiler()

return instance.match(selector, element)
selectors(compiler);

function match(selector, tree, includeSelf = true) {
return createNode(tree).findAll(compiler.compile(selector), includeSelf)
}

function matchKind(selector, element, includeSelf) {
return match(selector, element, includeSelf)
.map(node => node.instance || node.element)
}

module.exports = {
match,
selector: elements.compiler.selector
matchKind,
findAll,
isNode: el => el && el.$$typeof === NODE_TYPE,
}
67 changes: 0 additions & 67 deletions src/instance-selector.js

This file was deleted.

Loading

0 comments on commit f353cde

Please sign in to comment.