Allow Hiccup tags to be strings and symbols as well

This commit is contained in:
Dan Holmsand 2014-02-11 12:03:44 +01:00
parent ed4cf4e389
commit b041427e94
2 changed files with 27 additions and 4 deletions

View File

@ -17,6 +17,15 @@
(def dont-camel-case #{"aria" "data"})
(defn hiccup-tag? [x]
(or (keyword? x)
(symbol? x)
(string? x)))
(defn valid-tag? [x]
(or (hiccup-tag? x)
(ifn? x)))
(defn capitalize [s]
(if (< (count s) 2)
(string/upper-case s)
@ -195,7 +204,7 @@
wrapf))
(defn as-class [tag]
(if (keyword? tag)
(if (hiccup-tag? tag)
(cached-wrapper tag)
(do
(let [cached-class (.-cljsReactClass tag)]
@ -207,9 +216,7 @@
(defn vec-to-comp [v level]
(assert (pos? (count v)) "Hiccup form should not be empty")
(assert (let [tag (v 0)]
(or (keyword? tag)
(ifn? tag)))
(assert (valid-tag? (v 0))
(str "Invalid Hiccup form: " (pr-str v)))
(let [props (get v 1)
c (as-class (v 0))

View File

@ -284,3 +284,19 @@
(as-string [:div [comp :foo]])))
(is (re-find #"bardiv"
(as-string [:div [comp :bar]])))))
(deftest symbol-string-tag []
(is (re-find #"foobar"
(as-string ['div "foobar"])))
(is (re-find #"foobar"
(as-string ["div" "foobar"])))
(is (re-find #"id=.foo"
(as-string ['div#foo "x"])))
(is (re-find #"id=.foo"
(as-string ["div#foo" "x"])))
(is (re-find #"class=.foo bar"
(as-string ['div.foo {:class "bar"}])))
(is (re-find #"class=.foo bar"
(as-string ["div.foo.bar"])))
(is (re-find #"id=.foo"
(as-string ['div#foo.foo.bar]))))