Back
raw-string (clj)
(source)function
(raw-string & xs)
Converts one or more strings into an object that will not be escaped when
used with the [[hiccup2.core/html]] macro.
Examples
hiccup
(ns hiccup2.core_test
(:require [clojure.test :refer :all]
[hiccup2.core :refer :all]
[hiccup.util :as util]))
(deftest return-types
(testing "html returns a RawString"
(is (util/raw-string? (html [:div]))))
(testing "converting to string"
(= (str (html [:div])) "<div></div>")))
(deftest auto-escaping
(testing "literals"
(is (= (str (html "<>")) "<>"))
(is (= (str (html :<>)) "<>"))
(is (= (str (html ^String (str "<>"))) "<>"))
(is (= (str (html {} {"<a>" "<b>"})) "{"<a>" "<b>"}"))
(is (= (str (html #{"<>"})) "#{"<>"}"))
(is (= (str (html 1)) "1"))
(is (= (str (html ^Number (+ 1 1))) "2")))
(testing "non-literals"
(is (= (str (html (list [:p "<foo>"] [:p "<bar>"])))
"<p><foo></p><p><bar></p>"))
(is (= (str (html ((constantly "<foo>")))) "<foo>"))
(is (= (let [x "<foo>"] (str (html x))) "<foo>")))
(testing "optimized forms"
(is (= (str (html (if true :<foo> :<bar>))) "<foo>"))
(is (= (str (html (for [x [:<foo>]] x))) "<foo>")))
(testing "elements"
(is (= (str (html [:p "<>"])) "<p><></p>"))
(is (= (str (html [:p :<>])) "<p><></p>"))
(is (= (str (html [:p {} {"<foo>" "<bar>"}]))
"<p>{"<foo>" "<bar>"}</p>"))
(is (= (str (html [:p {} #{"<foo>"}]))
"<p>#{"<foo>"}</p>"))
(is (= (str (html [:p {:class "<\">"}]))
"<p class=\"<">\"></p>"))
(is (= (str (html [:p {:class ["<\">"]}]))
"<p class=\"<">\"></p>"))
(is (= (str (html [:ul [:li "<foo>"]]))
"<ul><li><foo></li></ul>")))
(testing "raw strings"
(is (= (str (html (util/raw-string "<foo>"))) "<foo>"))
(is (= (str (html [:p (util/raw-string "<foo>")])) "<p><foo></p>"))
(is (= (str (html (html [:p "<>"]))) "<p><></p>"))
(is (= (str (html [:ul (html [:li "<>"])])) "<ul><li><></li></ul>"))))
(deftest html-escaping
(testing "precompilation"
(is (= (str (html {:escape-strings? true} [:p "<>"])) "<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p "<>"])) "<p><></p>")))
(testing "dynamic generation"
(let [x [:p "<>"]]
(is (= (str (html {:escape-strings? true} x)) "<p><></p>"))
(is (= (str (html {:escape-strings? false} x)) "<p><></p>"))))
(testing "attributes"
(is (= (str (html {:escape-strings? true} [:p {:class "<>"}]))
"<p class=\"<>\"></p>"))
(is (= (str (html {:escape-strings? false} [:p {:class "<>"}]))
"<p class=\"<>\"></p>")))
(testing "raw strings"
(is (= (str (html {:escape-strings? true} [:p (util/raw-string "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p (util/raw-string "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? true} [:p (raw "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p (raw "<>")]))
"<p><></p>"))))
babashka/babashka
(ns hiccup2.core-test
(:require [clojure.test :refer :all]
[hiccup2.core :refer :all]
[hiccup.util :as util]))
(deftest return-types
#_(testing "html returns a RawString"
(is (util/raw-string? (html [:div]))))
(testing "converting to string"
(= (str (html [:div])) "<div></div>")))
(deftest auto-escaping
(testing "literals"
(is (= (str (html "<>")) "<>"))
(is (= (str (html :<>)) "<>"))
(is (= (str (html ^String (str "<>"))) "<>"))
(is (= (str (html {} {"<a>" "<b>"})) "{"<a>" "<b>"}"))
(is (= (str (html #{"<>"})) "#{"<>"}"))
(is (= (str (html 1)) "1"))
(is (= (str (html ^Number (+ 1 1))) "2")))
(testing "non-literals"
(is (= (str (html (list [:p "<foo>"] [:p "<bar>"])))
"<p><foo></p><p><bar></p>"))
(is (= (str (html ((constantly "<foo>")))) "<foo>"))
(is (= (let [x "<foo>"] (str (html x))) "<foo>")))
(testing "optimized forms"
(is (= (str (html (if true :<foo> :<bar>))) "<foo>"))
(is (= (str (html (for [x [:<foo>]] x))) "<foo>")))
(testing "elements"
(is (= (str (html [:p "<>"])) "<p><></p>"))
(is (= (str (html [:p :<>])) "<p><></p>"))
(is (= (str (html [:p {} {"<foo>" "<bar>"}]))
"<p>{"<foo>" "<bar>"}</p>"))
(is (= (str (html [:p {} #{"<foo>"}]))
"<p>#{"<foo>"}</p>"))
(is (= (str (html [:p {:class "<\">"}]))
"<p class=\"<">\"></p>"))
(is (= (str (html [:p {:class ["<\">"]}]))
"<p class=\"<">\"></p>"))
(is (= (str (html [:ul [:li "<foo>"]]))
"<ul><li><foo></li></ul>")))
(testing "raw strings"
#_(is (= (str (html (util/raw-string "<foo>"))) "<foo>"))
(is (= (str (html [:p (util/raw-string "<foo>")])) "<p><foo></p>"))
(is (= (str (html (html [:p "<>"]))) "<p><></p>"))
(is (= (str (html [:ul (html [:li "<>"])])) "<ul><li><></li></ul>"))))
(deftest html-escaping
(testing "precompilation"
(is (= (str (html {:escape-strings? true} [:p "<>"])) "<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p "<>"])) "<p><></p>")))
(testing "dynamic generation"
(let [x [:p "<>"]]
(is (= (str (html {:escape-strings? true} x)) "<p><></p>"))
(is (= (str (html {:escape-strings? false} x)) "<p><></p>"))))
(testing "attributes"
(is (= (str (html {:escape-strings? true} [:p {:class "<>"}]))
"<p class=\"<>\"></p>"))
(is (= (str (html {:escape-strings? false} [:p {:class "<>"}]))
"<p class=\"<>\"></p>")))
(testing "raw strings"
(is (= (str (html {:escape-strings? true} [:p (util/raw-string "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p (util/raw-string "<>")]))
"<p><></p>"))
#_(is (= (str (html {:escape-strings? true} [:p (raw "<>")]))
"<p><></p>"))
#_(is (= (str (html {:escape-strings? false} [:p (raw "<>")]))
"<p><></p>"))))
weavejester/hiccup
(ns hiccup2.core
"Library for rendering a tree of vectors into HTML. Pre-compiles where
possible for performance. Strings are automatically escaped."
{:added "2.0"}
(:require [hiccup.compiler :as compiler]
[hiccup.util :as util]))
`:escape-strings?`
: True if strings should be escaped (defaults to true)."
{:added "2.0"}
[options & content]
(if (map? options)
(let [mode (:mode options :xhtml)
escape-strings? (:escape-strings? options true)]
`(binding [util/*html-mode* ~mode
util/*escape-strings?* ~escape-strings?]
(util/raw-string ~(apply compiler/compile-html-with-bindings content))))
`(util/raw-string ~(apply compiler/compile-html-with-bindings options content))))
(def ^{:added "2.0"} raw
"Short alias for [[hiccup.util/raw-string]]."
util/raw-string)
weavejester/hiccup
(ns hiccup2.core_test
(:require [clojure.test :refer :all]
[hiccup2.core :refer :all]
[hiccup.util :as util]))
(deftest return-types
(testing "html returns a RawString"
(is (util/raw-string? (html [:div]))))
(testing "converting to string"
(= (str (html [:div])) "<div></div>")))
(deftest auto-escaping
(testing "literals"
(is (= (str (html "<>")) "<>"))
(is (= (str (html :<>)) "<>"))
(is (= (str (html ^String (str "<>"))) "<>"))
(is (= (str (html {} {"<a>" "<b>"})) "{"<a>" "<b>"}"))
(is (= (str (html #{"<>"})) "#{"<>"}"))
(is (= (str (html 1)) "1"))
(is (= (str (html ^Number (+ 1 1))) "2")))
(testing "non-literals"
(is (= (str (html (list [:p "<foo>"] [:p "<bar>"])))
"<p><foo></p><p><bar></p>"))
(is (= (str (html ((constantly "<foo>")))) "<foo>"))
(is (= (let [x "<foo>"] (str (html x))) "<foo>")))
(testing "optimized forms"
(is (= (str (html (if true :<foo> :<bar>))) "<foo>"))
(is (= (str (html (for [x [:<foo>]] x))) "<foo>")))
(testing "elements"
(is (= (str (html [:p "<>"])) "<p><></p>"))
(is (= (str (html [:p :<>])) "<p><></p>"))
(is (= (str (html [:p {} {"<foo>" "<bar>"}]))
"<p>{"<foo>" "<bar>"}</p>"))
(is (= (str (html [:p {} #{"<foo>"}]))
"<p>#{"<foo>"}</p>"))
(is (= (str (html [:p {:class "<\">"}]))
"<p class=\"<">\"></p>"))
(is (= (str (html [:p {:class ["<\">"]}]))
"<p class=\"<">\"></p>"))
(is (= (str (html [:ul [:li "<foo>"]]))
"<ul><li><foo></li></ul>")))
(testing "raw strings"
(is (= (str (html (util/raw-string "<foo>"))) "<foo>"))
(is (= (str (html [:p (util/raw-string "<foo>")])) "<p><foo></p>"))
(is (= (str (html (html [:p "<>"]))) "<p><></p>"))
(is (= (str (html [:ul (html [:li "<>"])])) "<ul><li><></li></ul>"))))
(deftest html-escaping
(testing "precompilation"
(is (= (str (html {:escape-strings? true} [:p "<>"])) "<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p "<>"])) "<p><></p>")))
(testing "dynamic generation"
(let [x [:p "<>"]]
(is (= (str (html {:escape-strings? true} x)) "<p><></p>"))
(is (= (str (html {:escape-strings? false} x)) "<p><></p>"))))
(testing "attributes"
(is (= (str (html {:escape-strings? true} [:p {:class "<>"}]))
"<p class=\"<>\"></p>"))
(is (= (str (html {:escape-strings? false} [:p {:class "<>"}]))
"<p class=\"<>\"></p>")))
(testing "raw strings"
(is (= (str (html {:escape-strings? true} [:p (util/raw-string "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p (util/raw-string "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? true} [:p (raw "<>")]))
"<p><></p>"))
(is (= (str (html {:escape-strings? false} [:p (raw "<>")]))
"<p><></p>"))))