Skip to content

Commit

Permalink
Add test for when a void element has a nil literal as a child
Browse files Browse the repository at this point in the history
  • Loading branch information
luontola committed Nov 30, 2023
1 parent 6fef3a6 commit 48ba3f7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions test/hiccup2/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,31 @@
(is (= (str (html {:escape-strings? false} [:p (raw "<>")]))
"<p><></p>"))))

(deftest runtime-attributes-vs-content-detection
(deftest runtime-attributes-vs-content-detection-for-void-elements
(testing "runtime attributes present"
(is (= (str (html {:mode :xhtml} [:br (identity {:id 1})])) "<br id=\"1\" />"))
(is (= (str (html {:mode :html} [:br (identity {:id 1})])) "<br id=\"1\">"))
(is (= (str (html {:mode :xml} [:br (identity {:id 1})])) "<br id=\"1\" />"))
(is (= (str (html {:mode :sgml} [:br (identity {:id 1})])) "<br id=\"1\">")))
(testing "runtime content present"
;; it's not valid HTML to have content inside void elements, but Hiccup should still obey what the user told it to do
(is (= (str (html {:mode :xhtml} [:br (identity "x")])) "<br>x</br>"))
(is (= (str (html {:mode :html} [:br (identity "x")])) "<br>x</br>"))
(is (= (str (html {:mode :xml} [:br (identity "x")])) "<br>x</br>"))
(is (= (str (html {:mode :sgml} [:br (identity "x")])) "<br>x</br>")))
(testing "runtime both absent"
;; TODO: this might not be desired behavior
;; TODO: this might not be desired behavior (use case: the user has a function which returns a map of attributes or nil)
(is (= (str (html {:mode :xhtml} [:br (identity nil)])) "<br></br>"))
(is (= (str (html {:mode :html} [:br (identity nil)])) "<br></br>"))
(is (= (str (html {:mode :xml} [:br (identity nil)])) "<br></br>"))
(is (= (str (html {:mode :sgml} [:br (identity nil)])) "<br></br>")))
(testing "compile-time both absent"
(testing "compile-time both absent: nil child"
;; TODO: same as above, but more of the user's fault to write a nil literal inside a void element
(is (= (str (html {:mode :xhtml} [:br nil])) "<br></br>"))
(is (= (str (html {:mode :html} [:br nil])) "<br></br>"))
(is (= (str (html {:mode :xml} [:br nil])) "<br></br>"))
(is (= (str (html {:mode :sgml} [:br nil])) "<br></br>")))
(testing "compile-time both absent: no children"
(is (= (str (html {:mode :xhtml} [:br])) "<br />"))
(is (= (str (html {:mode :html} [:br])) "<br>"))
(is (= (str (html {:mode :xml} [:br])) "<br />"))
Expand Down

0 comments on commit 48ba3f7

Please sign in to comment.