Skip to content

Commit

Permalink
Updated selectors to support function qualifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
tautologistics committed May 7, 2010
1 parent 3a6c082 commit fa083df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 13 additions & 16 deletions node-htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,39 +559,36 @@ function DefaultHandler (callback, options) {
if (element.type != "tag" && element.type != "script" && element.type != "style") {
return(false);
}
if (options["tag_name"] != element.name) {
return(false);
}
return(options["tag_name"](element.name));
} else if (key == "tag_type") {
if (element.type != options["tag_type"]) {
return(false);
}
return(options["tag_type"](element.type));
} else if (key == "tag_contains") {
if (element.type != "text" && element.type != "comment" && element.type != "directive") {
return(false);
}
if (!element.data) {
return(false);
}
if (element.data.indexOf(options["tag_contains"]) < 0) {
return(false);
}
return(options["tag_contains"](element.data));
} else {
if (!element.attribs || options[key] != element.attribs[key]) {
return(false);
}
return(element.attribs && options[key](element.attribs[key]));
}
}

return(true);
}

, getElements: function DomUtils$getElements (options, currentElement) {
if (!currentElement)
if (!currentElement) {
return([]);
}

var found = [];
var elementList;

function getTest (checkVal) {
return(((typeof options[key]) == "function") ? checkVal : function (value) { return(value == checkVal); });
}
for (var key in options) {
options[key] = getTest(options[key]);
}

if (DomUtils.testElement(options, currentElement)) {
found.push(currentElement);
Expand Down
4 changes: 3 additions & 1 deletion utils_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var sys = require("sys");
var htmlparser = require("./node-htmlparser");

var html = "<a>text a</a><b id='x'>text b</b><c class='y'>text c</c><d id='z' class='w'><e>text e</e></d>";
var html = "<a>text a</a><b id='x'>text b</b><c class='y'>text c</c><d id='z' class='w'><e>text e</e></d><g class='g h i'>hhh</g>";

var handler = new htmlparser.DefaultHandler(function(err, dom) {
if (err) {
Expand All @@ -15,6 +15,8 @@ var handler = new htmlparser.DefaultHandler(function(err, dom) {
sys.debug("id: " + sys.inspect(id, false, null));
var class = htmlparser.DomUtils.getElements({ class: "y" }, dom);
sys.debug("class: " + sys.inspect(class, false, null));
var multiclass = htmlparser.DomUtils.getElements({ class: function (value) { return(value && value.indexOf("h") > -1); } }, dom);
sys.debug("multiclass: " + sys.inspect(multiclass, false, null));
var name = htmlparser.DomUtils.getElementsByTagName("a", dom);
sys.debug("name: " + sys.inspect(name, false, null));
var text = htmlparser.DomUtils.getElementsByTagType("text", dom);
Expand Down

0 comments on commit fa083df

Please sign in to comment.