Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/soupselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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));
}

};
Expand All @@ -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])) {
Expand All @@ -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;
Expand Down