[#835] Expose escaping functions from selmer.util

This commit is contained in:
Michiel Borkent 2021-05-11 13:19:20 +02:00
parent e3413600c6
commit 4ffd08f360
4 changed files with 69 additions and 4 deletions

View file

@ -5,7 +5,7 @@
[selmer.filters :as filters] [selmer.filters :as filters]
[selmer.parser] [selmer.parser]
[selmer.tags :as tags] [selmer.tags :as tags]
[selmer.util :refer [*resource-fn*]])) [selmer.util :as util]))
(def spns (sci/create-ns 'selmer.parser nil)) (def spns (sci/create-ns 'selmer.parser nil))
@ -28,16 +28,37 @@
(def selmer-parser-ns (make-ns 'selmer.parser spns)) (def selmer-parser-ns (make-ns 'selmer.parser spns))
(def suns (sci/create-ns 'selmer.util nil))
(def escape-variables
(sci/new-dynamic-var '*escape-variables* util/*escape-variables* {:ns suns}))
(defn render-file (defn render-file
"Parses files if there isn't a memoized post-parse vector ready to go, "Parses files if there isn't a memoized post-parse vector ready to go,
renders post-parse vector with passed context-map regardless. Double-checks renders post-parse vector with passed context-map regardless. Double-checks
last-modified on files. Uses classpath for filename-or-url path " last-modified on files. Uses classpath for filename-or-url path "
[& args] [& args]
(binding [*resource-fn* resource] (binding [util/*resource-fn* resource
util/*escape-variables* @escape-variables]
(apply selmer.parser/render-file args))) (apply selmer.parser/render-file args)))
(defn render
" render takes the string, the context-map and possibly also opts. "
[& args]
(binding [util/*escape-variables* @escape-variables]
(apply selmer.parser/render args)))
(defn render-template
" vector of ^selmer.node.INodes and a context map."
[template context-map]
(binding [util/*escape-variables* @escape-variables]
(selmer.parser/render-template template context-map)))
(def selmer-parser-namespace (def selmer-parser-namespace
(assoc selmer-parser-ns 'render-file (sci/copy-var render-file spns))) (-> selmer-parser-ns
(assoc 'render-file (sci/copy-var render-file spns)
'render (sci/copy-var render spns)
'render-template (sci/copy-var render-template spns))))
(def stns (sci/create-ns 'selmer.tags nil)) (def stns (sci/create-ns 'selmer.tags nil))
@ -56,3 +77,24 @@
(def selmer-filters-namespace (def selmer-filters-namespace
{'add-filter! (sci/copy-var filters/add-filter! sfns) {'add-filter! (sci/copy-var filters/add-filter! sfns)
'remove-filter! (sci/copy-var filters/remove-filter! sfns)}) 'remove-filter! (sci/copy-var filters/remove-filter! sfns)})
(defn turn-off-escaping! []
(sci/alter-var-root escape-variables (constantly false)))
(defn turn-on-escaping! []
(sci/alter-var-root escape-variables (constantly true)))
(defmacro with-escaping [& body]
`(binding [selmer.util/*escape-variables* true]
~@body))
(defmacro without-escaping [& body]
`(binding [selmer.util/*escape-variables* false]
~@body))
(def selmer-util-namespace
{'turn-off-escaping! (sci/copy-var turn-off-escaping! suns)
'turn-on-escaping! (sci/copy-var turn-on-escaping! suns)
'*escape-variables* escape-variables
'with-escaping (sci/copy-var with-escaping suns)
'without-escaping (sci/copy-var without-escaping suns)})

View file

@ -390,7 +390,9 @@ Use bb run --help to show this help output.
'selmer.tags 'selmer.tags
@(resolve 'babashka.impl.selmer/selmer-tags-namespace) @(resolve 'babashka.impl.selmer/selmer-tags-namespace)
'selmer.filters 'selmer.filters
@(resolve 'babashka.impl.selmer/selmer-filters-namespace)))) @(resolve 'babashka.impl.selmer/selmer-filters-namespace)
'selmer.util
@(resolve 'babashka.impl.selmer/selmer-util-namespace))))
(def imports (def imports
'{ArithmeticException java.lang.ArithmeticException '{ArithmeticException java.lang.ArithmeticException

View file

@ -199,6 +199,7 @@
(test-namespaces 'helins.binf.test) (test-namespaces 'helins.binf.test)
(test-namespaces 'selmer.core-test) (test-namespaces 'selmer.core-test)
(test-namespaces 'selmer.our-test)
(test-namespaces 'jasentaa.position-test (test-namespaces 'jasentaa.position-test
'jasentaa.worked-example-1 'jasentaa.worked-example-1

View file

@ -0,0 +1,20 @@
(ns selmer.our-test
"Some additional tests we added ourselves"
(:require [clojure.test :as t :refer [deftest is testing]]
[selmer.parser :as selmer]
[selmer.util :as util]))
(deftest escaping-test
(testing "escaping by default"
(is (= "&foo" (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))
(testing "can be disabled"
(util/turn-off-escaping!)
(is (= "&foo" (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))
(testing "can be re-enabled"
(util/turn-on-escaping!)
(prn util/*escape-variables*)
(is (= "&foo" (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))
(testing "macros"
(is (= "&foo" (util/without-escaping (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))
(is (= "&foo" (util/with-escaping (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))))