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 "<>")) "&lt;&gt;"))
    (is (= (str (html :<>)) "&lt;&gt;"))
    (is (= (str (html ^String (str "<>"))) "&lt;&gt;"))
    (is (= (str (html {} {"<a>" "<b>"})) "{&quot;&lt;a&gt;&quot; &quot;&lt;b&gt;&quot;}"))
    (is (= (str (html #{"<>"})) "#{&quot;&lt;&gt;&quot;}"))
    (is (= (str (html 1)) "1"))
    (is (= (str (html ^Number (+ 1 1))) "2")))
  (testing "non-literals"
    (is (= (str (html (list [:p "<foo>"] [:p "<bar>"])))
           "<p>&lt;foo&gt;</p><p>&lt;bar&gt;</p>"))
    (is (= (str (html ((constantly "<foo>")))) "&lt;foo&gt;"))
    (is (= (let [x "<foo>"] (str (html x))) "&lt;foo&gt;")))
  (testing "optimized forms"
    (is (= (str (html (if true :<foo> :<bar>))) "&lt;foo&gt;"))
    (is (= (str (html (for [x [:<foo>]] x))) "&lt;foo&gt;")))
  (testing "elements"
    (is (= (str (html [:p "<>"])) "<p>&lt;&gt;</p>"))
    (is (= (str (html [:p :<>])) "<p>&lt;&gt;</p>"))
    (is (= (str (html [:p {} {"<foo>" "<bar>"}]))
           "<p>{&quot;&lt;foo&gt;&quot; &quot;&lt;bar&gt;&quot;}</p>"))
    (is (= (str (html [:p {} #{"<foo>"}]))
           "<p>#{&quot;&lt;foo&gt;&quot;}</p>"))
    (is (= (str (html [:p {:class "<\">"}]))
           "<p class=\"&lt;&quot;&gt;\"></p>"))
    (is (= (str (html [:p {:class ["<\">"]}]))
           "<p class=\"&lt;&quot;&gt;\"></p>"))
    (is (= (str (html [:ul [:li "<foo>"]]))
           "<ul><li>&lt;foo&gt;</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>&lt;&gt;</p>"))
    (is (= (str (html [:ul (html [:li "<>"])])) "<ul><li>&lt;&gt;</li></ul>"))))

(deftest html-escaping
  (testing "precompilation"
    (is (= (str (html {:escape-strings? true}  [:p "<>"])) "<p>&lt;&gt;</p>"))
    (is (= (str (html {:escape-strings? false} [:p "<>"])) "<p><></p>")))
  (testing "dynamic generation"
    (let [x [:p "<>"]]
      (is (= (str (html {:escape-strings? true}  x)) "<p>&lt;&gt;</p>"))
      (is (= (str (html {:escape-strings? false} x)) "<p><></p>"))))
  (testing "attributes"
    (is (= (str (html {:escape-strings? true}  [:p {:class "<>"}]))
           "<p class=\"&lt;&gt;\"></p>"))
    (is (= (str (html {:escape-strings? false} [:p {:class "<>"}]))
           "<p class=\"&lt;&gt;\"></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 "<>")) "&lt;&gt;"))
    (is (= (str (html :<>)) "&lt;&gt;"))
    (is (= (str (html ^String (str "<>"))) "&lt;&gt;"))
    (is (= (str (html {} {"<a>" "<b>"})) "{&quot;&lt;a&gt;&quot; &quot;&lt;b&gt;&quot;}"))
    (is (= (str (html #{"<>"})) "#{&quot;&lt;&gt;&quot;}"))
    (is (= (str (html 1)) "1"))
    (is (= (str (html ^Number (+ 1 1))) "2")))
  (testing "non-literals"
    (is (= (str (html (list [:p "<foo>"] [:p "<bar>"])))
           "<p>&lt;foo&gt;</p><p>&lt;bar&gt;</p>"))
    (is (= (str (html ((constantly "<foo>")))) "&lt;foo&gt;"))
    (is (= (let [x "<foo>"] (str (html x))) "&lt;foo&gt;")))
  (testing "optimized forms"
    (is (= (str (html (if true :<foo> :<bar>))) "&lt;foo&gt;"))
    (is (= (str (html (for [x [:<foo>]] x))) "&lt;foo&gt;")))
  (testing "elements"
    (is (= (str (html [:p "<>"])) "<p>&lt;&gt;</p>"))
    (is (= (str (html [:p :<>])) "<p>&lt;&gt;</p>"))
    (is (= (str (html [:p {} {"<foo>" "<bar>"}]))
           "<p>{&quot;&lt;foo&gt;&quot; &quot;&lt;bar&gt;&quot;}</p>"))
    (is (= (str (html [:p {} #{"<foo>"}]))
           "<p>#{&quot;&lt;foo&gt;&quot;}</p>"))
    (is (= (str (html [:p {:class "<\">"}]))
           "<p class=\"&lt;&quot;&gt;\"></p>"))
    (is (= (str (html [:p {:class ["<\">"]}]))
           "<p class=\"&lt;&quot;&gt;\"></p>"))
    (is (= (str (html [:ul [:li "<foo>"]]))
           "<ul><li>&lt;foo&gt;</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>&lt;&gt;</p>"))
    (is (= (str (html [:ul (html [:li "<>"])])) "<ul><li>&lt;&gt;</li></ul>"))))

(deftest html-escaping
  (testing "precompilation"
    (is (= (str (html {:escape-strings? true}  [:p "<>"])) "<p>&lt;&gt;</p>"))
    (is (= (str (html {:escape-strings? false} [:p "<>"])) "<p><></p>")))
  (testing "dynamic generation"
    (let [x [:p "<>"]]
      (is (= (str (html {:escape-strings? true}  x)) "<p>&lt;&gt;</p>"))
      (is (= (str (html {:escape-strings? false} x)) "<p><></p>"))))
  (testing "attributes"
    (is (= (str (html {:escape-strings? true}  [:p {:class "<>"}]))
           "<p class=\"&lt;&gt;\"></p>"))
    (is (= (str (html {:escape-strings? false} [:p {:class "<>"}]))
           "<p class=\"&lt;&gt;\"></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 "<>")) "&lt;&gt;"))
    (is (= (str (html :<>)) "&lt;&gt;"))
    (is (= (str (html ^String (str "<>"))) "&lt;&gt;"))
    (is (= (str (html {} {"<a>" "<b>"})) "{&quot;&lt;a&gt;&quot; &quot;&lt;b&gt;&quot;}"))
    (is (= (str (html #{"<>"})) "#{&quot;&lt;&gt;&quot;}"))
    (is (= (str (html 1)) "1"))
    (is (= (str (html ^Number (+ 1 1))) "2")))
  (testing "non-literals"
    (is (= (str (html (list [:p "<foo>"] [:p "<bar>"])))
           "<p>&lt;foo&gt;</p><p>&lt;bar&gt;</p>"))
    (is (= (str (html ((constantly "<foo>")))) "&lt;foo&gt;"))
    (is (= (let [x "<foo>"] (str (html x))) "&lt;foo&gt;")))
  (testing "optimized forms"
    (is (= (str (html (if true :<foo> :<bar>))) "&lt;foo&gt;"))
    (is (= (str (html (for [x [:<foo>]] x))) "&lt;foo&gt;")))
  (testing "elements"
    (is (= (str (html [:p "<>"])) "<p>&lt;&gt;</p>"))
    (is (= (str (html [:p :<>])) "<p>&lt;&gt;</p>"))
    (is (= (str (html [:p {} {"<foo>" "<bar>"}]))
           "<p>{&quot;&lt;foo&gt;&quot; &quot;&lt;bar&gt;&quot;}</p>"))
    (is (= (str (html [:p {} #{"<foo>"}]))
           "<p>#{&quot;&lt;foo&gt;&quot;}</p>"))
    (is (= (str (html [:p {:class "<\">"}]))
           "<p class=\"&lt;&quot;&gt;\"></p>"))
    (is (= (str (html [:p {:class ["<\">"]}]))
           "<p class=\"&lt;&quot;&gt;\"></p>"))
    (is (= (str (html [:ul [:li "<foo>"]]))
           "<ul><li>&lt;foo&gt;</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>&lt;&gt;</p>"))
    (is (= (str (html [:ul (html [:li "<>"])])) "<ul><li>&lt;&gt;</li></ul>"))))

(deftest html-escaping
  (testing "precompilation"
    (is (= (str (html {:escape-strings? true}  [:p "<>"])) "<p>&lt;&gt;</p>"))
    (is (= (str (html {:escape-strings? false} [:p "<>"])) "<p><></p>")))
  (testing "dynamic generation"
    (let [x [:p "<>"]]
      (is (= (str (html {:escape-strings? true}  x)) "<p>&lt;&gt;</p>"))
      (is (= (str (html {:escape-strings? false} x)) "<p><></p>"))))
  (testing "attributes"
    (is (= (str (html {:escape-strings? true}  [:p {:class "<>"}]))
           "<p class=\"&lt;&gt;\"></p>"))
    (is (= (str (html {:escape-strings? false} [:p {:class "<>"}]))
           "<p class=\"&lt;&gt;\"></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>"))))