Skip to content

Commit

Permalink
Add getChildren methods that get all childs of given gom tag type
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jun 1, 2015
1 parent 33fb93c commit f5b6044
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 21 deletions.
31 changes: 27 additions & 4 deletions browser/gom.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,53 @@ module.exports = function($) {
_hasClass = function(node, name) {
return node.attributes["class"].indexOf(name) !== -1;
};
$.getChildren = function(node, childTagNames) {
$.getFirstDescendant = function(node, childTagNames) {
var child, foundChild, j, k, l, len, len1, len2, ref, ref1, tagName;
if (node.children instanceof Array) {
if ((node != null) && node.children instanceof Array) {
for (j = 0, len = childTagNames.length; j < len; j++) {
tagName = childTagNames[j];
ref = node.children;
for (k = 0, len1 = ref.length; k < len1; k++) {
child = ref[k];
if (child.tag === tagName) {
if ((child != null) && child.tag === tagName) {
return child;
}
}
}
ref1 = node.children;
for (l = 0, len2 = ref1.length; l < len2; l++) {
child = ref1[l];
foundChild = $.getChildren(child, childTagNames);
foundChild = $.getFirstDescendant(child, childTagNames);
if (foundChild) {
return foundChild;
}
}
}
return null;
};
$.getChildren = function(node, childTagNames, isChild) {
var child, childResult, foundChild, j, len, ref, ref1;
if (isChild == null) {
isChild = false;
}
foundChild = [];
if (isChild && (ref = node != null ? node.tag : void 0, indexOf.call(childTagNames, ref) >= 0)) {
console.log("found child " + node.tag);
foundChild.push(node);
}
if ((node != null ? node.children : void 0) != null) {
console.log("looking for child of " + node.tag);
ref1 = node.children;
for (j = 0, len = ref1.length; j < len; j++) {
child = ref1[j];
childResult = $.getChildren(child, childTagNames, true);
if ((childResult != null ? childResult.length : void 0) > 0) {
Array.prototype.push.apply(foundChild, childResult);
}
}
}
return foundChild;
};
$.setAttribute = function(node, key, val) {
if (!isNode(node)) {
return node;
Expand Down
25 changes: 18 additions & 7 deletions browser/spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,35 @@ describe("Helpers", function() {
return "hello";
}, "hello");
});
return describe('getChildren', function() {
describe('getFirstDescendant', function() {
var imgNode, node;
imgNode = $('img', {});
node = $('div', {}, [$('section', {}, [imgNode]), $('article', {}, [$('p', {}, "hello")])]);
it('getChild', function() {
it('getFirstDescendant', function() {
var foundNode;
foundNode = $.getChildren(node, ['img']);
foundNode = $.getFirstDescendant(node, ['img']);
return expect(foundNode).to.equal(imgNode);
});
it('with two search', function() {
it('with two child tag names', function() {
var foundNode;
foundNode = $.getChildren(node, ['cover', 'img']);
foundNode = $.getFirstDescendant(node, ['cover', 'img']);
return expect(foundNode).to.equal(imgNode);
});
return it('with two present search', function() {
return it('with two present child tag names', function() {
var foundNode;
foundNode = $.getChildren(node, ['img', 'p']);
foundNode = $.getFirstDescendant(node, ['img', 'p']);
return expect(foundNode).to.equal(imgNode);
});
});
return describe('getChildren', function() {
var imgNode, imgNode2, node;
imgNode = $('img', {});
imgNode2 = $('img', {});
node = $('div', {}, [$('section', {}, [imgNode]), $('article', {}, [$('div', {}, [$('div', {}, [imgNode2])])])]);
return it('getChildren', function() {
var foundNodes;
foundNodes = $.getChildren(node, ['img']);
return expect(foundNodes != null ? foundNodes.length : void 0).to.equal(2);
});
});
});
21 changes: 18 additions & 3 deletions lib/mixins/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,33 @@ module.exports = ($) ->
_hasClass = (node,name) ->
return node.attributes.class.indexOf(name) isnt -1

$.getChildren = (node, childTagNames) ->
$.getFirstDescendant = (node, childTagNames) ->
if node? and node.children instanceof Array
for tagName in childTagNames
for tagName in childTagNames #order are prioritize.
for child in node.children
return child if child? and child.tag is tagName

for child in node.children
foundChild = $.getChildren(child, childTagNames)
foundChild = $.getFirstDescendant(child, childTagNames)
return foundChild if foundChild

return null

$.getChildren = (node,childTagNames,isChild=false) ->
foundChild = []

if isChild and node?.tag in childTagNames
console.log "found child " + node.tag
foundChild.push node

if node?.children?
console.log "looking for child of " + node.tag
for child in node.children
childResult = $.getChildren(child, childTagNames,true)
Array.prototype.push.apply(foundChild,childResult) if childResult?.length > 0

return foundChild

$.setAttribute = (node,key,val) ->
return node unless isNode(node)
node.attributes = {} unless node.attributes?
Expand Down
32 changes: 25 additions & 7 deletions spec/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe "Helpers", ->
"hello"
, "hello"

describe 'getChildren', ->
describe 'getFirstDescendant', ->

imgNode = $ 'img', {}
node = $ 'div', {}, [
Expand All @@ -176,14 +176,32 @@ describe "Helpers", ->
]
]

it 'getChild', ->
foundNode = $.getChildren node, ['img']
it 'getFirstDescendant', ->
foundNode = $.getFirstDescendant node, ['img']
expect(foundNode).to.equal imgNode

it 'with two search', ->
foundNode = $.getChildren node, ['cover','img']
it 'with two child tag names', ->
foundNode = $.getFirstDescendant node, ['cover','img']
expect(foundNode).to.equal imgNode

it 'with two present search', ->
foundNode = $.getChildren node, ['img','p']
it 'with two present child tag names', ->
foundNode = $.getFirstDescendant node, ['img','p']
expect(foundNode).to.equal imgNode

describe 'getChildren', ->

imgNode = $ 'img', {}
imgNode2 = $ 'img', {}

node = $ 'div', {}, [
$ 'section', {}, [imgNode]
$ 'article', {}, [
$ 'div', {}, [
$ 'div', {}, [imgNode2]
]
]
]

it 'getChildren', ->
foundNodes = $.getChildren node, ['img']
expect(foundNodes?.length).to.equal 2

0 comments on commit f5b6044

Please sign in to comment.