Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 40 lines (33 sloc) 1.189 kb
0d5d3bb @youngnh added html-selector project and post
authored
1 (ns twoguys.html-selector
2 (:use [clojure.java.io])
3 (:import [nu.validator.htmlparser.dom HtmlDocumentBuilder]
3685e03 @youngnh html-selector - element selectors
authored
4 [org.w3c.dom Document Node]
0d5d3bb @youngnh added html-selector project and post
authored
5 [org.xml.sax InputSource]))
6
7 (defn build-document [file-name]
8 (.parse (HtmlDocumentBuilder.) (InputSource. (reader file-name))))
9
10 (defn id-sel [document id]
11 (let [id (.substring id 1)]
12 [(.getElementById document id)]))
13
14 (defn nodelist-seq [node-list]
15 (letfn [(internal [i]
16 (lazy-seq
17 (when (< i (.getLength node-list))
18 (cons (.item node-list i) (internal (inc i))))))]
19 (internal 0)))
20
3685e03 @youngnh html-selector - element selectors
authored
21 (defmulti element-sel (fn [context elt-name]
22 (condp instance? context
23 Document Document
24 :default)))
25
26 (defmethod element-sel Document [document elt-name]
27 (nodelist-seq (.getElementsByTagName document elt-name)))
28
29 (defmethod element-sel :default [nodes elt-name]
30 (let [children (apply concat
31 (for [node nodes]
32 (nodelist-seq (.getChildNodes node))))]
33 (lazy-cat
34 (filter #(= elt-name (element-tagname %)) children)
35 (when-not (empty? children)
36 (element-sel children elt-name)))))
37
38 (defn element-tagname [elt]
39 (when (= Node/ELEMENT_NODE (.getNodeType elt))
40 (.getNodeName elt)))
Something went wrong with that request. Please try again.