The strangler foundation for migrating interactive SSR components from Hiccup to
Selmer (plain-HTML Alpine/HTMX attributes instead of mixed keyword/string encodings).
- project.clj: add [selmer "1.12.61"].
- auto-ap.ssr.selmer: render / render-str (selmer/render-file + string), hiccup->html
(Hiccup -> string for {{ frag|safe }}), raw (wrap a rendered fragment for embedding
in a Hiccup tree without double-escaping), render->hiccup.
- resources/templates/interop-smoke.html: proves render-file from the classpath and
that plain-HTML alpine attrs (x-model, @keydown, tippy?.show()) pass through verbatim.
- selmer_test: 4 tests / 8 assertions covering both interop directions; all green.
Proven via REPL + tests: a Hiccup component renders inside a Selmer template, and a
Selmer fragment renders inside a Hiccup tree. Both valid during the transition.
37 lines
1.7 KiB
Clojure
37 lines
1.7 KiB
Clojure
(ns auto-ap.ssr.selmer-test
|
|
(:require
|
|
[auto-ap.ssr.selmer :as sut]
|
|
[clojure.string :as str]
|
|
[clojure.test :refer [deftest is testing]]
|
|
[hiccup2.core :as h2]))
|
|
|
|
(deftest hiccup->html
|
|
(testing "renders a Hiccup form to an HTML string"
|
|
(is (= "<span class=\"label\">A & B</span>"
|
|
(sut/hiccup->html [:span.label "A & B"])))))
|
|
|
|
(deftest selmer-embeds-hiccup
|
|
(testing "a Hiccup component renders inside a Selmer template via |safe"
|
|
(let [frag (sut/hiccup->html [:span.badge "from hiccup"])
|
|
out (sut/render-str "<div>{{frag|safe}}</div>" {:frag frag})]
|
|
(is (str/includes? out "<span class=\"badge\">from hiccup</span>"))
|
|
;; without |safe the markup would be escaped; |safe keeps it verbatim
|
|
(is (not (str/includes? out "<span"))))))
|
|
|
|
(deftest selmer-fragment-inside-hiccup
|
|
(testing "a Selmer fragment renders inside a Hiccup tree without double-escaping"
|
|
(let [sel (sut/render-str "<a href=\"{{url}}\">{{label}}</a>" {:url "/x" :label "Go"})
|
|
out (str (h2/html {} [:div (sut/raw sel)]))]
|
|
(is (= "<div><a href=\"/x\">Go</a></div>" out)))))
|
|
|
|
(deftest render-file-from-classpath
|
|
(testing "render-file resolves a template under resources/templates and keeps plain-HTML Alpine/HTMX attrs"
|
|
(let [out (sut/render "templates/interop-smoke.html"
|
|
{:title "Interop OK"
|
|
:hiccup_frag (sut/hiccup->html [:span.badge "from hiccup"])})]
|
|
(is (str/includes? out "Interop OK"))
|
|
(is (str/includes? out "from hiccup"))
|
|
;; plain-HTML attributes (the whole point of Selmer) survive unambiguously
|
|
(is (str/includes? out "x-model=\"value.value\""))
|
|
(is (str/includes? out "tippy?.show()")))))
|