Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/huff2/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@
(defmulti emit
(fn [_append! form _opts] (:key form)))

(defmethod emit :default [_append! form _opts]
(throw (ex-info (cond
(keyword? (:value form))
(str "Got a bare keyword " (:value form)
" — wrap it in a vector to make an HTML element, e.g. ["
(:value form) "]")
:else
(str "Unexpected hiccup form: " (pr-str form)))
{:form form})))

(defmethod emit :primative [append! {:keys [value]} _opts]
(maybe-escape-html append! value))

Expand Down Expand Up @@ -258,7 +268,14 @@
(emit append! c opts)))

(defmethod emit :component-node [append! {{{:keys [view-fxn children]} :values} :value} {:keys [parser] :as opts}]
(emit append! (parser (apply view-fxn children)) opts))
(let [result (apply view-fxn children)
parsed (parser result)]
(if (= parsed :malli.core/invalid)
(throw (ex-info (str "Component " view-fxn " returned invalid hiccup")
{:component view-fxn
:args children
:returned result}))
(emit append! parsed opts))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Public api

Expand Down
13 changes: 12 additions & 1 deletion test/huff/core2_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns huff.core2-test
(:require [huff2.core :as h]
[clojure.string :as str]
[clojure.test :refer [deftest is]]))
[clojure.test :refer [deftest is testing]]))

(deftest class-list-equality-test
(is (= (h/html [:div]) (h/html [:div])))
Expand Down Expand Up @@ -180,3 +180,14 @@
(str (h/html [:div {:style {:width (-> 10 h/vmin)}}]))))
(is (= "<div style=\"width:10vmax;\"></div>"
(str (h/html [:div {:style {:width (-> 10 h/vmax)}}])))))

(deftest component-invalid-hiccup-error-test
(testing "component returning invalid hiccup gives clear error"
(let [bad-component (fn [] :div)]
(is (thrown-with-msg? Exception #"returned invalid hiccup"
(h/html [:div [bad-component]])))))
(testing "component returning bare keyword mentions the component"
(let [bad-component (fn [] :div)
ex-data' (try (h/html [:div [bad-component]])
(catch Exception e (ex-data e)))]
(is (= :div (:returned ex-data'))))))