Permalink
Browse files

html-selector - class selectors

  • Loading branch information...
1 parent 3685e03 commit 66ba707455e6a14a8d2601b86a16eb25a5fb7c7d @youngnh committed Nov 1, 2010
Showing with 31 additions and 3 deletions.
  1. +21 −2 html-selector/src/twoguys/html_selector.clj
  2. +10 −1 html-selector/test/twoguys/html_selector.clj
@@ -1,5 +1,7 @@
(ns twoguys.html-selector
- (:use [clojure.java.io])
+ (:use [clojure.string :only (split)]
+ [clojure.contrib.core :only (.?.)]
+ [clojure.java.io :only (reader)])
(:import [nu.validator.htmlparser.dom HtmlDocumentBuilder]
[org.w3c.dom Document Node]
[org.xml.sax InputSource]))
@@ -37,4 +39,21 @@
(defn element-tagname [elt]
(when (= Node/ELEMENT_NODE (.getNodeType elt))
- (.getNodeName elt)))
+ (.getNodeName elt)))
+
+(defn get-attribute [elt attr]
+ (.?. elt getAttributes (getNamedItem attr) getValue))
+
+(defn hasclass? [elt class]
+ (when-let [class-attr (get-attribute elt "class")]
+ (some #(= class %) (split class-attr #" "))))
+
+(defn class-sel [nodes class]
+ (let [class-name (.substring class 1)
+ children (apply concat
+ (for [node nodes]
+ (nodelist-seq (.getChildNodes node))))]
+ (lazy-cat
+ (filter #(hasclass? % class-name) children)
+ (when-not (empty? children)
+ (class-sel children class)))))
@@ -22,4 +22,13 @@
(testing "given a seq of nodes, they are the context"
(is (= 1 (count (element-sel $matchup-summary "table"))))
(is (= 4 (count (element-sel $matchup "table"))))
- (is (= 5 (count (element-sel (concat $matchup-summary $matchup) "table")))))))
+ (is (= 5 (count (element-sel (concat $matchup-summary $matchup) "table")))))))
+
+(deftest test-class-sel
+ (let [document (build-document "data/matchup_mid1_mid2_week1.html")
+ $statTable1 (id-sel document "#statTable1")
+ $rows (-> $statTable1
+ (element-sel "tbody")
+ (element-sel "tr"))]
+ (is (= ["C" "1B" "2B" "3B" "SS" "LF" "CF" "RF" "Util" "BN" "BN" "BN" "BN" "BN" "BN" "--" "--" "--"]
+ (map #(.getTextContent %) (class-sel $rows ".pos"))))))

0 comments on commit 66ba707

Please sign in to comment.