Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Unreleased changes are available via `io.github.borkdude/html {:git/sha "..."}`
## Unreleased

- Fix shorthand classes being dropped when the attribute map has no `:class`: `[:div.card {:id "a"}]` rendered `<div id="a">`. The same path interpolated a symbol's _name_ when `:class` was bound to an expression, so `(let [x "big"] [:div.card {:class x}])` rendered `class="card x"`.
- Omit an attribute whose value is `nil` or `false`. `[:input {:disabled false}]` rendered `disabled="false"`, which — HTML boolean attributes being presence-based — disabled the element. An attribute map that now renders to nothing no longer leaves a stray space (`<div >`), which also fixes `[:div {}]`.
- Render `aria-*` booleans as the strings `"true"` and `"false"`. ARIA attributes are string-valued enumerations in which absence is a distinct state, so `[:button {:aria-expanded false}]` must render `aria-expanded="false"` rather than being omitted, and `true` must render `aria-expanded="true"` rather than a bare attribute (whose empty value is invalid, and falls back to the attribute's default). `nil` still omits.

## 0.2.6

Expand Down
34 changes: 22 additions & 12 deletions src/borkdude/html.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,29 @@
([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))
(and (map? v) (= "style" (name k))) (pr-str (escape-html (->css v)))
:else (pr-str (escape-html (str v)))))))
m))))
(keep (fn [[k v]]
(let [k (name k)
aria? (str/starts-with? k "aria-")]
(cond
(nil? v) nil
(and (false? v) (not aria?)) nil
(and (true? v) (not xml?) (not aria?)) k
:else
(str k
"=" (cond (string? v) (pr-str (escape-html v))
(keyword? v) (pr-str (name v))
(and (map? v) (= "style" k)) (pr-str (escape-html (->css v)))
:else (pr-str (escape-html (str v))))))))
m))))
([opts m base-map]
(let [m (merge base-map m)]
(->attrs opts m))))

(defn attrs-prefix
"Implementation, do not use"
[s]
(if (= "" s) "" (str " " s)))

(defn constant? [v]
(not (or (seq? v) (symbol? v))))

Expand Down Expand Up @@ -128,16 +138,16 @@
attrs (if ?attrs
(let [a (compile-attrs opts ?attrs)]
(if (string? a)
(str " " a)
a))
(attrs-prefix a)
`(attrs-prefix ~a)))
"")]
(if unsafe?
`(->Html (str ~(first children)))
`(->Html (str ~@(if omit-tag?
nil
(if (string? attrs)
[(str "<" tag attrs ">")]
["<" tag " " attrs ">"]))
["<" tag attrs ">"]))
~@(map #(list (if xml?
`xml
`html) %) children)
Expand Down
41 changes: 41 additions & 0 deletions test/borkdude/html_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@
"<input checked=\"true\"></input>"
(xml [:input {:checked true}])

"<input>"
(html [:input {:disabled false}])

"<input>"
(html [:input {:disabled nil}])

"<input></input>"
(xml [:input {:disabled nil}])

"<input type=\"text\">"
(html [:input {:type "text" :disabled false}])

"<input type=\"text\">"
(let [d false]
(html [:input {:type "text" :disabled d}]))

"<button aria-expanded=\"true\"></button>"
(html [:button {:aria-expanded true}])

"<button aria-expanded=\"false\"></button>"
(html [:button {:aria-expanded false}])

"<button aria-expanded=\"false\"></button>"
(let [b false]
(html [:button {:aria-expanded b}]))

"<button aria-expanded=\"true\"></button>"
(xml [:button {:aria-expanded true}])

"<span></span>"
(html [:span {:aria-label nil}])

"<div role=\"checkbox\" aria-checked=\"mixed\"></div>"
(html [:div {:role "checkbox" :aria-checked "mixed"}])

"<div></div>"
(html [:div {:data-open false}])

"<a href=\"http://dude\"></a>"
(html [:a {:href (str "http://" "dude")}])

Expand All @@ -100,6 +138,9 @@
"<div class=\"card\"></div>"
(html [:div.card {}])

"<div></div>"
(html [:div {}])

"<div class=\"card big\"></div>"
(let [x "big"]
(html [:div.card {:class x}]))
Expand Down
Loading