From 4ffd08f360c926e8ea66d5b48fdcf2c92b170fe0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 11 May 2021 13:19:20 +0200 Subject: [PATCH] [#835] Expose escaping functions from selmer.util --- feature-selmer/babashka/impl/selmer.clj | 48 +++++++++++++++++-- src/babashka/main.clj | 4 +- .../lib_tests/babashka/run_all_libtests.clj | 1 + test-resources/lib_tests/selmer/our_test.clj | 20 ++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 test-resources/lib_tests/selmer/our_test.clj diff --git a/feature-selmer/babashka/impl/selmer.clj b/feature-selmer/babashka/impl/selmer.clj index 8b2f2abb..eb9a7099 100644 --- a/feature-selmer/babashka/impl/selmer.clj +++ b/feature-selmer/babashka/impl/selmer.clj @@ -5,7 +5,7 @@ [selmer.filters :as filters] [selmer.parser] [selmer.tags :as tags] - [selmer.util :refer [*resource-fn*]])) + [selmer.util :as util])) (def spns (sci/create-ns 'selmer.parser nil)) @@ -28,16 +28,37 @@ (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 "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 last-modified on files. Uses classpath for filename-or-url path " [& args] - (binding [*resource-fn* resource] + (binding [util/*resource-fn* resource + util/*escape-variables* @escape-variables] (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 - (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)) @@ -56,3 +77,24 @@ (def selmer-filters-namespace {'add-filter! (sci/copy-var filters/add-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)}) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2aca0a19..83b499e8 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -390,7 +390,9 @@ Use bb run --help to show this help output. 'selmer.tags @(resolve 'babashka.impl.selmer/selmer-tags-namespace) '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 '{ArithmeticException java.lang.ArithmeticException diff --git a/test-resources/lib_tests/babashka/run_all_libtests.clj b/test-resources/lib_tests/babashka/run_all_libtests.clj index ee6aa171..5068837c 100644 --- a/test-resources/lib_tests/babashka/run_all_libtests.clj +++ b/test-resources/lib_tests/babashka/run_all_libtests.clj @@ -199,6 +199,7 @@ (test-namespaces 'helins.binf.test) (test-namespaces 'selmer.core-test) +(test-namespaces 'selmer.our-test) (test-namespaces 'jasentaa.position-test 'jasentaa.worked-example-1 diff --git a/test-resources/lib_tests/selmer/our_test.clj b/test-resources/lib_tests/selmer/our_test.clj new file mode 100644 index 00000000..4af2e85c --- /dev/null +++ b/test-resources/lib_tests/selmer/our_test.clj @@ -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})))))) +