Permalink
Browse files

Updated selectors to support function qualifiers

  • Loading branch information...
1 parent 3a6c082 commit fa083df99d3afab78f58a73dc0e3cd2d16e65bf1 @tautologistics committed May 7, 2010
Showing with 16 additions and 17 deletions.
  1. +13 −16 node-htmlparser.js
  2. +3 −1 utils_example.js
View
@@ -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);
View
@@ -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) {
@@ -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);

0 comments on commit fa083df

Please sign in to comment.