-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[added] sibling combinators and psuedos
- Loading branch information
Showing
9 changed files
with
262 additions
and
81 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
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
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,66 @@ | ||
import { | ||
isDomElement, isCompositeElement | ||
, isReactInstance, isDOMComponent, isCompositeComponent } from './utils'; | ||
|
||
function anyParent(test, element, node){ | ||
do { | ||
node = node.parentNode | ||
} while(node && !test(node.element, node)) | ||
|
||
return !!node | ||
} | ||
|
||
function directParent(test, _, node) { | ||
node = node.parentNode | ||
return !!(node && test(node.element, node)) | ||
} | ||
|
||
function anySibling(test, _, node) { | ||
do { | ||
node = node.prevSibling | ||
} while(node && !test(node.element, node)) | ||
|
||
return !!node | ||
} | ||
|
||
function directSibling(test, _, node) { | ||
node = node.prevSibling | ||
return !!(node && test(node.element, node)) | ||
} | ||
|
||
export default function(compiler) { | ||
compiler.registerPseudo('dom', ()=> isDomElement) | ||
compiler.registerPseudo('composite', ()=> isCompositeElement) | ||
|
||
compiler.registerPseudo('not', function(compiledSelector) { | ||
return (element, node) => { | ||
let matches = compiledSelector(element, node) | ||
return !matches | ||
} | ||
}) | ||
|
||
compiler.registerPseudo('first-child', () => | ||
(element, node) => { | ||
let parent = node.parentNode; | ||
return parent && parent.children.indexOf(node.instance || element) === 0 | ||
}) | ||
|
||
compiler.registerPseudo('last-child', () => | ||
(element, node) => { | ||
let parent = node.parentNode; | ||
let children = parent && parent.children | ||
return parent && children.indexOf(node.instance || element) === (children.length - 1) | ||
}) | ||
|
||
compiler.registerNesting('any', test => | ||
(element, node) => anyParent(test, element, node)) | ||
|
||
compiler.registerNesting('>', test => | ||
(element, node) => directParent(test, element, node)) | ||
|
||
compiler.registerNesting('~', test => | ||
(element, node) => anySibling(test, element, node)) | ||
|
||
compiler.registerNesting('+', test => | ||
(element, node) => directSibling(test, element, node)) | ||
} |
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
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
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
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,81 @@ | ||
import React from 'react'; | ||
import ReactInstanceMap from 'react/lib/ReactInstanceMap'; | ||
import isPlainObject from 'lodash/lang/isPlainObject'; | ||
import { create as createCompiler, parse } from './compiler'; | ||
import { | ||
anyParent, directParent | ||
, isDomElement, isCompositeElement | ||
, isReactInstance, isDOMComponent, isCompositeComponent } from './utils'; | ||
|
||
export function eachChild(subject, fn) { | ||
let inst, element, publicInst; | ||
|
||
if (!subject) return; | ||
|
||
if (React.isValidElement(subject)) | ||
return React.Children.forEach(subject.props.children, child => fn(child)) | ||
|
||
inst = subject | ||
element = inst._currentElement | ||
|
||
if (inst.getPublicInstance) | ||
publicInst = inst.getPublicInstance() | ||
|
||
if (isDOMComponent(publicInst)) { | ||
let renderedChildren = inst._renderedChildren || {} | ||
, child = element && element.props.children; | ||
|
||
if (child != null && !isPlainObject(child) && !Array.isArray(child) && !isReactInstance(child)) | ||
fn(child) | ||
|
||
Object.keys(renderedChildren).forEach( | ||
key => fn(renderedChildren[key]) | ||
); | ||
} | ||
else if (isCompositeComponent(publicInst)) { | ||
fn(inst._renderedComponent); | ||
} | ||
} | ||
|
||
|
||
export function createNode(subject, lastWrapper) { | ||
let element, inst; | ||
|
||
if (React.isValidElement(subject)) | ||
element = subject, inst = null; | ||
|
||
else if (isReactInstance(subject)) | ||
element = subject._currentElement, inst = subject; | ||
|
||
return Object.defineProperties({}, { | ||
element: { get: () => element, enumerable: true }, | ||
instance: { value: inst, enumerable: true }, | ||
parentNode: { value: lastWrapper, enumerable: true }, | ||
prevSibling: { | ||
enumerable: true, | ||
get(){ | ||
let children = lastWrapper ? lastWrapper.children : [] | ||
, idx = children.indexOf(inst || element) - 1 | ||
|
||
return idx < 0 ? null : createNode(children[idx], lastWrapper) | ||
} | ||
}, | ||
nextSibling: { | ||
enumerable: true, | ||
get(){ | ||
let children = lastWrapper ? lastWrapper.children : [] | ||
, idx = children.indexOf(inst || element) + 1 | ||
|
||
return idx >= children.length ? null : createNode(children[idx], lastWrapper) | ||
} | ||
}, | ||
children: { | ||
enumerable: true, | ||
get(){ | ||
let children = []; | ||
eachChild(inst || element, child => children.push(child)) | ||
return children | ||
} | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.