Skip to content

Commit

Permalink
Fix skip children bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Kovacs committed Nov 3, 2016
1 parent 169922a commit 9f6625a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/traverse-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function traverse(
const shouldRecurseFn = result(shouldRecurse);

while (child) {
if (shouldRecurseFn(element) && element.children.length) {
if (shouldRecurseFn(child) && child.children.length) {
traverse(child, callback, shouldRecurse);
}

Expand All @@ -35,7 +35,7 @@ export function traverseNodes(
const shouldRecurseFn = result(shouldRecurse);

while (child) {
if (shouldRecurseFn(node) && node.childNodes.length) {
if (shouldRecurseFn(child) && child.childNodes.length) {
traverseNodes(child, callback, shouldRecurse);
}

Expand Down
26 changes: 23 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import $ from 'jquery';
import assert from 'assert';

import { pushNodeNamesTo } from './utils/callbacks';
import { pushNodeNamesTo, getNodeName } from './utils/callbacks';

import {
traverse,
Expand Down Expand Up @@ -70,18 +70,38 @@ describe('DOM traversal', () => {
traverse(deepHtml, pushNodeNamesTo(result), () => true);
assert.deepEqual(result, deepHtmlResult);
});

it('traverse should skip children of a specified element', () => {
const html = $('<div><p><i>foo</i></p><p class="skip"><i>bar</i></p><p><i>bat</i></p></div>').get(0);
// Adds a the .touched className to every <i> element.
const touchItalic = (child) => {
if (child.nodeName === 'I') {
child.classList.add('touched');
}
};

// Skip children of the element with the .skip className
traverse(html, touchItalic, child => !child.classList.contains('skip'));

assert.equal(html.querySelectorAll('.touched').length, 2);
});
});

describe('utilities', () => {
describe('map', () => {
it('should transform all top level nodes', () => {
const getNodeName = node => node.nodeName.toLowerCase();

assert.deepEqual(
traverseMap(simpleHtml, getNodeName),
['p', '#text', 'p']
);
});

it('should transform all nodes except the ones it should skip', () => {
assert.deepEqual(
traverseMap(simpleDeepHtml, getNodeName, child => getNodeName(child) !== 'span'),
['span', 'p', 'div']
);
});
});
describe('reduce', () => {});
});
Expand Down
6 changes: 3 additions & 3 deletions test/utils/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* @flow */
/* eslint-env node, mocha */

export function getNodeName(node: Node): string { return node.nodeName.toLowerCase(); }

export function pushNodeNamesTo(result: Array<string>): (node: Node) => void {
return (node) => { result.push(node.nodeName.toLowerCase()); };
return (node) => { result.push(getNodeName(node)); };
}

export function foo() {}

0 comments on commit 9f6625a

Please sign in to comment.