Skip to content

Commit

Permalink
[fixed] missed single text children
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Oct 10, 2015
1 parent b0ee5ef commit 4645192
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 23 deletions.
16 changes: 12 additions & 4 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var _lodashUtilityUniqueId = require('lodash/utility/uniqueId');

var _lodashUtilityUniqueId2 = _interopRequireDefault(_lodashUtilityUniqueId);

var _utils = require('./utils');

var _cssSelectorParser = require('css-selector-parser');

var parser = new _cssSelectorParser.CssSelectorParser();
Expand All @@ -31,6 +33,12 @@ var prim = function prim(value) {
return value === null || ['string', 'number'].indexOf(typ) !== -1;
};

function failText(fn) {
return function () {
return _utils.isTextElement(arguments[0]) ? false : fn.apply(undefined, arguments);
};
}

function parse(selector) {
var ast = typeof selector === 'string' ? parser.parse(selector) : selector;

Expand Down Expand Up @@ -107,17 +115,17 @@ function create() {
var fns = [];
var rule = rules.shift();

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

if (rule.attrs) fns.push(getPropComparer(rule, values));
if (rule.attrs) fns.push(failText(getPropComparer(rule, values)));

if (rule.classNames) fns.push(function (_ref) {
if (rule.classNames) fns.push(failText(function (_ref) {
var className = _ref.props.className;

return rule.classNames.every(function (clsName) {
return className && className.indexOf(clsName) !== -1;
});
});
}));

if (rule.pseudos) {
fns = fns.concat(rule.pseudos.map(function (pseudo) {
Expand Down
8 changes: 5 additions & 3 deletions lib/element-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports.__esModule = true;
exports.match = match;
exports.findAll = findAll;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

Expand Down Expand Up @@ -57,11 +58,12 @@ function findAll(root, test, includeSelf) {
return { parent: null };
} : arguments[3];

var found = [];
var found = [],
children = [];

if (!_react2['default'].isValidElement(root)) return found;
if (root == null || root === false) return found;

var children = root.props.children;
if (_react2['default'].isValidElement(root)) children = root.props.children;

if (includeSelf && test(root, getParent)) found.push(root);

Expand Down
22 changes: 12 additions & 10 deletions lib/instance-selector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

exports.__esModule = true;
exports.findAll = findAll;
exports.match = match;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
Expand Down Expand Up @@ -50,34 +51,35 @@ compiler.registerNesting('>', function (test) {
};
});

function findAll(inst, test) {
var excludeSelf = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
function findAll(inst, test, includeSelf) {
var getParent = arguments.length <= 3 || arguments[3] === undefined ? function () {
return { parent: null };
} : arguments[3];

var found = [];
var found = [],
publicInst = undefined;

if (!inst || !inst.getPublicInstance) return found;
if (!inst) return found;

var publicInst = inst.getPublicInstance(),
element = inst._currentElement,
if (inst.getPublicInstance) publicInst = inst.getPublicInstance();

var element = inst._currentElement,
parent = function parent() {
return { parent: element, getParent: getParent };
};

if (!excludeSelf && test(element, inst, getParent)) found = found.concat(inst);
if (includeSelf && test(element, inst, getParent)) found = found.concat(inst);

if (isDOMComponent(publicInst)) {
(function () {
var renderedChildren = inst._renderedChildren || {};

Object.keys(renderedChildren).forEach(function (key) {
found = found.concat(findAll(renderedChildren[key], test, false, parent));
found = found.concat(findAll(renderedChildren[key], test, true, parent));
});
})();
} else if (isCompositeComponent(publicInst)) {
found = found.concat(findAll(inst._renderedComponent, test, false, parent));
found = found.concat(findAll(inst._renderedComponent, test, true, parent));
}

return found;
Expand All @@ -95,7 +97,7 @@ function match(selector, inst) {
: inst._reactInternalComponent //is a DOM node
? inst._reactInternalComponent : _reactLibReactInstanceMap2['default'].get(inst);

return findAll(tree, compiler.compile(selector), !includeSelf);
return findAll(tree, compiler.compile(selector), includeSelf);
}

var compile = compiler.compile;
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bill",
"version": "1.2.1",
"version": "1.3.0",
"description": "css selectors for React Elements",
"main": "index.js",
"repository": {
Expand Down
9 changes: 7 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
exports.__esModule = true;
exports.anyParent = anyParent;
exports.directParent = directParent;
var isTextElement = function isTextElement(element) {
return typeof element === 'string';
};

exports.isTextElement = isTextElement;
var isDomElement = function isDomElement(element) {
return typeof element.type === 'string' && element.type.toLowerCase() === element.type;
return !isTextElement(element) && typeof element.type === 'string' && element.type.toLowerCase() === element.type;
};

exports.isDomElement = isDomElement;
var isCompositeElement = function isCompositeElement(element) {
return typeof element.type === 'function';
return !isTextElement(element) && typeof element.type === 'function';
};

exports.isCompositeElement = isCompositeElement;
Expand Down
7 changes: 6 additions & 1 deletion src/instance-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export function findAll(inst, test, includeSelf, getParent = ()=> ({ parent: nul
found = found.concat(inst)

if (isDOMComponent(publicInst)) {
let renderedChildren = inst._renderedChildren || {};
let renderedChildren = inst._renderedChildren || {}
, child = inst._currentElement.props.children;

if (typeof child === 'string' && test(child, null, parent) ) {
found = found.concat(child)
}

Object.keys(renderedChildren).forEach(key => {
found = found.concat(
Expand Down
4 changes: 2 additions & 2 deletions test/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ describe('Selecting', ()=> {
it('should match nested', ()=>{
match('div a.foo',
<div>
Hello there
<span>Hello there</span>
<span>
{'More text Nodes'}
<a className='foo'/>
<a className='foo'>single text node</a>
</span>
<a className='foo'/>
</div>
Expand Down

0 comments on commit 4645192

Please sign in to comment.