Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 74 lines (57 sloc) 2.024 kB
0d5d3bb @youngnh added html-selector project and post
authored
1 (ns twoguys.html-selector
66ba707 @youngnh html-selector - class selectors
authored
2 (:use [clojure.string :only (split)]
3 [clojure.contrib.core :only (.?.)]
4 [clojure.java.io :only (reader)])
0d5d3bb @youngnh added html-selector project and post
authored
5 (:import [nu.validator.htmlparser.dom HtmlDocumentBuilder]
3685e03 @youngnh html-selector - element selectors
authored
6 [org.w3c.dom Document Node]
0d5d3bb @youngnh added html-selector project and post
authored
7 [org.xml.sax InputSource]))
8
9 (defn build-document [file-name]
10 (.parse (HtmlDocumentBuilder.) (InputSource. (reader file-name))))
11
12 (defn id-sel [document id]
13 (let [id (.substring id 1)]
bca48f2 @youngnh added text-selector, calls getTextContent
authored
14 (list (.getElementById document id))))
0d5d3bb @youngnh added html-selector project and post
authored
15
16 (defn nodelist-seq [node-list]
17 (letfn [(internal [i]
18 (lazy-seq
19 (when (< i (.getLength node-list))
20 (cons (.item node-list i) (internal (inc i))))))]
21 (internal 0)))
22
16a0c3c @youngnh selectors operate on single nodes
authored
23 (defn selector [node pred]
24 (let [children (nodelist-seq (.getChildNodes node))]
3685e03 @youngnh html-selector - element selectors
authored
25 (lazy-cat
2b9c600 @youngnh refactored to a unifying $ selector
authored
26 (filter pred children)
3685e03 @youngnh html-selector - element selectors
authored
27 (when-not (empty? children)
16a0c3c @youngnh selectors operate on single nodes
authored
28 (mapcat #(selector % pred) children)))))
3685e03 @youngnh html-selector - element selectors
authored
29
30 (defn element-tagname [elt]
31 (when (= Node/ELEMENT_NODE (.getNodeType elt))
66ba707 @youngnh html-selector - class selectors
authored
32 (.getNodeName elt)))
33
16a0c3c @youngnh selectors operate on single nodes
authored
34 (defmulti element-sel (fn [node elt-name]
35 (condp instance? node
2b9c600 @youngnh refactored to a unifying $ selector
authored
36 Document Document
16a0c3c @youngnh selectors operate on single nodes
authored
37 Node)))
2b9c600 @youngnh refactored to a unifying $ selector
authored
38
39 (defmethod element-sel Document [document elt-name]
40 (nodelist-seq (.getElementsByTagName document elt-name)))
41
16a0c3c @youngnh selectors operate on single nodes
authored
42 (defmethod element-sel Node [node elt-name]
43 (selector node #(= elt-name (element-tagname %))))
2b9c600 @youngnh refactored to a unifying $ selector
authored
44
66ba707 @youngnh html-selector - class selectors
authored
45 (defn get-attribute [elt attr]
46 (.?. elt getAttributes (getNamedItem attr) getValue))
47
48 (defn hasclass? [elt class]
49 (when-let [class-attr (get-attribute elt "class")]
50 (some #(= class %) (split class-attr #" "))))
51
16a0c3c @youngnh selectors operate on single nodes
authored
52 (defn class-sel [node class]
53 (selector node #(hasclass? % (.substring class 1))))
2b9c600 @youngnh refactored to a unifying $ selector
authored
54
bca48f2 @youngnh added text-selector, calls getTextContent
authored
55 (defmulti compile-selector type)
56
57 (defmethod compile-selector clojure.lang.IFn [f]
58 f)
59
60 (defmethod compile-selector String [s]
2b9c600 @youngnh refactored to a unifying $ selector
authored
61 (condp = (.charAt s 0)
62 \# #(id-sel % s)
63 \. #(class-sel % s)
64 #(element-sel % s)))
65
bca48f2 @youngnh added text-selector, calls getTextContent
authored
66 (defn text-sel [node]
67 (list (.getTextContent node)))
68
69 (defn flip [f]
70 (fn [& args]
71 (apply f (reverse args))))
72
16a0c3c @youngnh selectors operate on single nodes
authored
73 (defn $ [node & selectors]
bca48f2 @youngnh added text-selector, calls getTextContent
authored
74 (reduce (flip mapcat) [node] (map compile-selector selectors)))
Something went wrong with that request. Please try again.