From a9c6472eb2311f1a7e121185e94a4a0156eee0a4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 3 Jun 2024 10:53:45 +0200 Subject: [PATCH] Fix #7: boolean HTML attribute --- CHANGELOG.md | 6 ++++++ src/borkdude/html.cljc | 38 +++++++++++++++++++++--------------- test/borkdude/html_test.cljc | 12 ++++++++++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29e891a..f49371f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/borkdude/html.cljc b/src/borkdude/html.cljc index 8ba8756..5815e3c 100644 --- a/src/borkdude/html.cljc +++ b/src/borkdude/html.cljc @@ -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) @@ -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 "" @@ -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)) diff --git a/test/borkdude/html_test.cljc b/test/borkdude/html_test.cljc index f66392b..4005bcb 100644 --- a/test/borkdude/html_test.cljc +++ b/test/borkdude/html_test.cljc @@ -66,8 +66,16 @@ (html [:div [:br]]) "


" - (xml [:div [:br]])) -) + (xml [:div [:br]]) + + "" + (html [:input {:checked true}]) + + "" + (xml [:input {:checked true}]) + + ) + ) (comment (ok)