Back

raw (clj)

(source)

variable

Short alias for [[hiccup.util/raw-string]].

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>"))))
coast-framework/coast
(ns coast
  (:require [coast.potemkin.namespaces :as namespaces]
            [hiccup2.core]
            [coast.db]
            [coast.db.connection]
            [coast.theta]
            [coast.env]
            [coast.time2]
            [coast.components]
            [coast.responses]
            [coast.utils]
            [coast.error]
            [coast.router]
            [coast.validation])
  (:refer-clojure :exclude [update]))

  [hiccup2.core
   raw
   html])