summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2025-09-20 20:25:34 +0300
committerAsko Nõmm <asko@nmm.ee>2025-09-20 20:25:34 +0300
commit5f163f2b62613368714d8a1e8b342daaac0f89d5 (patch)
tree1e7a557002c24a9d78e1dd575ec3c03d0958459a
parent9f31cc6a5aae83f7986e733603e62e91eae31196 (diff)
Fix issue with node name capture, add another test case.
-rw-r--r--src/dompa/coordinates.cljc2
-rw-r--r--src/dompa/html.cljc10
-rw-r--r--test/dompa/coordinates_test.cljc74
3 files changed, 57 insertions, 29 deletions
diff --git a/src/dompa/coordinates.cljc b/src/dompa/coordinates.cljc
index 3584dc9..abaf233 100644
--- a/src/dompa/coordinates.cljc
+++ b/src/dompa/coordinates.cljc
@@ -149,7 +149,7 @@
(if (str/starts-with? html "<")
(->> (subs html 1)
(take-while #(not (contains? #{\space \>} %)))
- (reduce str)
+ (apply str)
keyword)
:dompa/text))
diff --git a/src/dompa/html.cljc b/src/dompa/html.cljc
index 6268fa0..cea7b69 100644
--- a/src/dompa/html.cljc
+++ b/src/dompa/html.cljc
@@ -6,12 +6,14 @@
"Transform a `html` string into a vector of coordinates
indicating where an HTML node ends and begins."
[html]
- (->> coordinates/compose
- (coordinates/unify html)))
+ (->> html
+ coordinates/compose
+ coordinates/unify))
(defn ->nodes
"Transform a `html` string into a tree of nodes,
each representing one HTML node and its children."
[html]
- (->> (->coordinates html)
- (coordinates/->nodes html))) \ No newline at end of file
+ (-> html
+ ->coordinates
+ coordinates/->nodes)) \ No newline at end of file
diff --git a/test/dompa/coordinates_test.cljc b/test/dompa/coordinates_test.cljc
index eb118d2..d6528a0 100644
--- a/test/dompa/coordinates_test.cljc
+++ b/test/dompa/coordinates_test.cljc
@@ -5,101 +5,101 @@
(deftest compose-test
(testing "Create coordinates"
(let [html "<div>hello</div>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 9] [10 15]]}
(coordinates/compose html)))))
(testing "Create coordinates with invalid HTML"
(let [html "<div>hello"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 9]]}
(coordinates/compose html))))
(let [html "<div>hello</span>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 9] [10 16]]}
(coordinates/compose html))))
(let [html "<div"]
- (is (= {:html html
+ (is (= {:html html
:coordinates []}
(coordinates/compose html))))
(let [html "div>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 3]]}
(coordinates/compose html))))
(let [html "<>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 1]]}
(coordinates/compose html)))))
(testing "Create coordinates with just text"
(let [html "hello"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4]]}
(coordinates/compose html)))))
(testing "Create coordinates with text starting"
(let [html "hello<div></div>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 9] [10 15]]}
(coordinates/compose html)))))
(testing "Create coordinates with text ending"
(let [html "<div></div>hello"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 10] [11 15]]}
(coordinates/compose html))))))
(deftest unify-test
(testing "Unify coordinates"
(let [html "<div>hello</div>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 15] [5 9]]}
(-> (coordinates/compose html)
coordinates/unify)))))
(testing "Unify coordinates with invalid HTML"
(let [html "<div>hello"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 9]]}
(-> (coordinates/compose html)
coordinates/unify))))
(let [html "<div>hello</span>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 9]]}
(-> (coordinates/compose html)
coordinates/unify)))))
(testing "Unify coordinates with just text"
(let [html "hello"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4]]}
(-> (coordinates/compose html)
coordinates/unify)))))
(testing "Unify coordinates with text starting"
(let [html "hello<div></div>"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 4] [5 15]]}
(-> (coordinates/compose html)
coordinates/unify)))))
(testing "Unify coordinates with text ending"
(let [html "<div></div>hello"]
- (is (= {:html html
+ (is (= {:html html
:coordinates [[0 10] [11 15]]}
(-> (coordinates/compose html)
coordinates/unify))))))
(deftest nodes-test
(testing "Create nodes"
- (is (= [{:name :div
- :attrs {}
- :children [{:name :dompa/text
+ (is (= [{:name :div
+ :attrs {}
+ :children [{:name :dompa/text
:value "hello"}]}]
(-> "<div>hello</div>"
coordinates/compose
@@ -107,13 +107,39 @@
coordinates/->nodes))))
(testing "Create nodes with attributes"
- (is (= [{:attrs {:class "some test classes"
- :data-attr "something"
- :checked true}
- :children [{:name :dompa/text
- :value "hello"}]
- :name :div}]
+ (is (= [{:name :div
+ :attrs {:class "some test classes"
+ :data-attr "something"
+ :checked true}
+ :children [{:name :dompa/text
+ :value "hello"}]}]
(-> "<div class=\"some test classes\" data-attr=\"something\" checked>hello</div>"
coordinates/compose
coordinates/unify
+ coordinates/->nodes))))
+
+ (testing "Create nested nodes"
+ (is (= [{:name :b
+ :attrs {}
+ :children [{:name :dompa/text
+ :value "bold"}]}
+ {:name :img
+ :attrs {:src "img.png"}}
+ {:name :dompa/text
+ :value "Hello, "}
+ {:name :span
+ :attrs {}
+ :children [{:name :dompa/text
+ :value "wor"}
+ {:name :i
+ :attrs {}
+ :children [{:name :dompa/text
+ :value "l"}
+ {:name :b
+ :attrs {}
+ :children [{:name :dompa/text
+ :value "d"}]}]}]}]
+ (-> "<b>bold</b><img src=\"img.png\" />Hello, <span>wor<i>l<b>d</b></i></span>"
+ coordinates/compose
+ coordinates/unify
coordinates/->nodes))))) \ No newline at end of file