This repository has been archived by the owner on Jun 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dom.cljs
63 lines (53 loc) · 1.51 KB
/
dom.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(ns hipo.dom)
(defn create-element [namespace-uri tag]
(if namespace-uri
(.createElementNS js/document namespace-uri tag)
(.createElement js/document tag)))
(defn- node? [o] (instance? js/Node o))
(defn- element? [el] (if el (identical? 1 (.-nodeType el))))
(defn- text-element? [el] (if el (identical? 3 (.-nodeType el))))
(defn child
[el i]
{:pre [(node? el) (not (neg? i))]}
(aget (.-childNodes el) i))
(defn children
([el] (children el 0))
([el i]
{:pre [(node? el)
(not (neg? i))]}
(let [fel (.-firstChild el)]
(if fel
(loop [cel fel
acc [cel]]
(let [nel (.-nextSibling cel)]
(if (and (not (zero? (- (count acc) (inc i)))) nel)
(recur nel (conj acc nel))
acc)))))))
(defn replace!
[el nel]
{:pre [(node? el) (node? nel)
(not (nil? (.-parentElement el)))]}
(.replaceChild (.-parentElement el) nel el))
(defn replace-text!
[el s]
{:pre [(node? el) (string? s)]}
(if (text-element? el)
(set! (.-textContent el) s)
(replace! el (.createTextNode js/document s))))
(defn clear!
[el]
{:pre [(element? el)]}
(set! (.-innerHTML el) ""))
(defn remove-trailing-children!
[el n]
{:pre [(node? el) (not (neg? n))]}
(dotimes [_ n]
(.removeChild el (.-lastChild el))))
(defn insert-child!
[el i nel]
{:pre [(node? el) (not (neg? i)) (node? nel)]}
(.insertBefore el nel (child el i)))
(defn remove-child!
[el i]
{:pre [(node? el) (not (neg? i))]}
(.removeChild el (child el i)))