Permalink
Browse files

html-selector - element selectors

  • Loading branch information...
1 parent 0d5d3bb commit 3685e03d5f8735c95f2b2288313a8a5d85ab9070 @youngnh committed Nov 1, 2010
Showing with 46 additions and 2 deletions.
  1. +21 −2 html-selector/src/twoguys/html_selector.clj
  2. +25 −0 html-selector/test/twoguys/html_selector.clj
@@ -1,6 +1,7 @@
(ns twoguys.html-selector
(:use [clojure.java.io])
(:import [nu.validator.htmlparser.dom HtmlDocumentBuilder]
+ [org.w3c.dom Document Node]
[org.xml.sax InputSource]))
(defn build-document [file-name]
@@ -17,5 +18,23 @@
(cons (.item node-list i) (internal (inc i))))))]
(internal 0)))
-(defn element-sel [document elt-name]
- (nodelist-seq (.getElementsByTagName document elt-name)))
+(defmulti element-sel (fn [context elt-name]
+ (condp instance? context
+ Document Document
+ :default)))
+
+(defmethod element-sel Document [document elt-name]
+ (nodelist-seq (.getElementsByTagName document elt-name)))
+
+(defmethod element-sel :default [nodes elt-name]
+ (let [children (apply concat
+ (for [node nodes]
+ (nodelist-seq (.getChildNodes node))))]
+ (lazy-cat
+ (filter #(= elt-name (element-tagname %)) children)
+ (when-not (empty? children)
+ (element-sel children elt-name)))))
+
+(defn element-tagname [elt]
+ (when (= Node/ELEMENT_NODE (.getNodeType elt))
+ (.getNodeName elt)))
@@ -0,0 +1,25 @@
+(ns twoguys.html-selector-test
+ (:use [twoguys.html-selector] :reload-all)
+ (:use [clojure.test])
+ (:import [org.w3c.dom Document]))
+
+(deftest test-build-document
+ (is (instance? Document (build-document "data/matchup_mid1_mid2_week1.html"))))
+
+(deftest test-element-tagname
+ (let [document (build-document "data/matchup_mid1_mid2_week1.html")
+ table-elts (element-sel document "table")]
+ (is (= "table" (element-tagname (first table-elts))))))
+
+(deftest test-element-sel
+ (let [document (build-document "data/matchup_mid1_mid2_week1.html")
+ $matchup-summary (id-sel document "#matchup-summary")
+ $matchup (id-sel document "#matchup")]
+
+ (testing "given a Document, the context is the whole DOM"
+ (is (= 13 (count (element-sel document "table")))))
+
+ (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")))))))

0 comments on commit 3685e03

Please sign in to comment.