diff --git a/lib/soupselect.js b/lib/soupselect.js index 1f1142b..2b23fae 100644 --- a/lib/soupselect.js +++ b/lib/soupselect.js @@ -55,12 +55,11 @@ htmlparser.DomUtil.* calls */ exports.select = function(dom, selector) { var currentContext = [dom]; - var found, tag, options; + var found, tag, options, recurse = true, childSelector = false; var tokens = selector.split(/\s+/); for ( var i = 0; i < tokens.length; i++ ) { - // Attribute selectors var match = attrSelectRe.exec(tokens[i]); if ( match ) { @@ -71,7 +70,7 @@ exports.select = function(dom, selector) { found = []; for (var j = 0; j < currentContext.length; j++ ) { - found = found.concat(domUtils.getElements(options, currentContext[j])); + found = found.concat(domUtils.getElements(options, currentContext[j], recurse)); }; if ( tag ) { @@ -96,9 +95,9 @@ exports.select = function(dom, selector) { // the document has no child elements but tags do so we search children to avoid // returning the current element via a false positive if ( typeof currentContext[k].children !== 'undefined' ) { - el = domUtils.getElementById(id_selector, currentContext[k].children); + el = domUtils.getElementById(id_selector, currentContext[k].children, recurse); } else { - el = domUtils.getElementById(id_selector, currentContext[k]); + el = domUtils.getElementById(id_selector, currentContext[k], recurse); } if ( el ) { @@ -137,7 +136,7 @@ exports.select = function(dom, selector) { // don't recurse in the case we have a tag or we get children we might not want found = found.concat(domUtils.getElements(options, context, false)); } else { - found = found.concat(domUtils.getElements(options, context)); + found = found.concat(domUtils.getElements(options, context, recurse)); } }; @@ -148,8 +147,10 @@ exports.select = function(dom, selector) { // Star selector else if ( tokens[i] === '*' ) { // nothing to do right? + } + else if ( tokens[i] == '>' ) { + childSelector = true; } - // Tag selector else { if (!tagRe.test(tokens[i])) { @@ -161,15 +162,17 @@ exports.select = function(dom, selector) { for ( var m = 0; m < currentContext.length; m++ ) { // htmlparsers document itself has no child property - only nodes do... if ( typeof currentContext[m].children !== 'undefined' ) { - found = found.concat(domUtils.getElementsByTagName(tokens[i], currentContext[m].children)); + found = found.concat(domUtils.getElementsByTagName(tokens[i], currentContext[m].children, recurse)); } else if (i === 0) { - found = found.concat(domUtils.getElementsByTagName(tokens[i], currentContext[m])); + found = found.concat(domUtils.getElementsByTagName(tokens[i], currentContext[m], recurse)); } }; currentContext = found; } + recurse = !childSelector; + childSelector = false; }; return currentContext;