Skip to content

Commit

Permalink
Fix #7: boolean HTML attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jun 3, 2024
1 parent e8da39a commit a9c6472
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

Unreleased changes are available via `io.github.borkdude/html {:git/sha "..."}` in `deps.edn`.

## Unreleased

- Fix [#7](https://github.com/borkdude/html/issues/7): boolean HTML attributes

## 0.1.0

Initial release
38 changes: 22 additions & 16 deletions src/borkdude/html.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
(replace "\"" """)
(replace "'" "'" #_(if (= *html-mode* :sgml) "'" "'")))))

(defn ->safe [x]
(defn ->safe
"Implementation, do not use"
[x]
(cond
(instance? Html x) (str x)
(string? x) (escape-html x)
Expand All @@ -31,23 +33,27 @@
m)))

(defn ->attrs
([m]
(str/join " "
(map (fn [[k v]]
(str (name k)
"=" (cond (string? v) (pr-str (escape-html v))
(keyword? v) (pr-str (name v))
(map? v) (pr-str (->css v))
:else (str v))))
m)))
([m base-map]
"Implementation, do not use"
([opts m]
(let [xml? (= :xml (:mode opts))]
(str/join " "
(map (fn [[k v]]
(if (and (true? v) (not xml?))
(name k)
(str (name k)
"=" (cond (string? v) (pr-str (escape-html v))
(keyword? v) (pr-str (name v))
(map? v) (pr-str (->css v))
:else (pr-str (str v))))))
m))))
([opts m base-map]
(let [m (merge base-map m)]
(->attrs m))))
(->attrs opts m))))

(defn- compile-attrs [m]
(defn- compile-attrs [opts m]
(if (contains? m :&)
`(->attrs ~(get m :&) ~(dissoc m :&))
(->attrs m)))
`(->attrs ~opts ~(get m :&) ~(dissoc m :&))
(->attrs opts m)))

#_(defmacro str* [& xs]
(loop [acc ""
Expand Down Expand Up @@ -79,7 +85,7 @@
children (if attrs? children (cons ?attrs children))
unsafe? (= "$" tag)
attrs (if attrs?
(let [a (compile-attrs ?attrs)]
(let [a (compile-attrs opts ?attrs)]
(if (string? a)
(str " " a)
a))
Expand Down
12 changes: 10 additions & 2 deletions test/borkdude/html_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,16 @@
(html [:div [:br]])

"<div><br></br></div>"
(xml [:div [:br]]))
)
(xml [:div [:br]])

"<input checked>"
(html [:input {:checked true}])

"<input checked=\"true\"></input>"
(xml [:input {:checked true}])

)
)

(comment
(ok)
Expand Down

0 comments on commit a9c6472

Please sign in to comment.