Skip to content

Commit

Permalink
Merge pull request #207 from luontola/fix-accidental-escaping
Browse files Browse the repository at this point in the history
Fix literal child elements being escaped
  • Loading branch information
weavejester authored Jan 4, 2024
2 parents 67c3cfa + 2239b96 commit 4d7303c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/hiccup/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@
(defmethod compile-element ::default
[element]
`(render-element
[~(first element)
~@(for [x (rest element)]
(if (vector? x)
(compile-element x)
x))]))
[~(first element)
~@(for [x (rest element)]
(if (vector? x)
(util/raw-string (compile-element x))
x))]))

(defn- compile-seq
"Compile a sequence of data-structures into HTML."
Expand Down
35 changes: 21 additions & 14 deletions test/hiccup/compiler_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
(ns hiccup.compiler-test
(:require [clojure.test :refer :all]
[hiccup2.core :refer [html]]))
[clojure.walk :as walk]
[hiccup2.core :refer [html]])
(:import (hiccup.util RawString)))

(defn- extract-strings [code]
(->> (tree-seq coll? seq code)
(filter #(or (string? %)
(instance? RawString %)))
(map str)
(set)))

(deftest test-compile-element-literal-tag
;; `compile-element ::literal-tag` behavior varies based on the following
Expand Down Expand Up @@ -93,21 +102,19 @@
"<p>x</p>")))

(testing "runtime tag with child elements"
;; FIXME: this should return "<p><span>x</span></p>"
(is (= (str (html {:mode :xhtml} [(identity :p) [:span "x"]]))
"<p>&lt;span&gt;x&lt;/span&gt;</p>"))
(str (html {:mode :xhtml} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))
(is (= (str (html {:mode :html} [(identity :p) [:span "x"]]))
"<p>&lt;span&gt;x&lt;/span&gt;</p>"))
(str (html {:mode :html} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))
(is (= (str (html {:mode :xml} [(identity :p) [:span "x"]]))
"<p>&lt;span&gt;x&lt;/span&gt;</p>"))
(str (html {:mode :xml} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))
(is (= (str (html {:mode :sgml} [(identity :p) [:span "x"]]))
"<p>&lt;span&gt;x&lt;/span&gt;</p>"))
(str (html {:mode :sgml} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>")))

(is (= (str (html {:mode :xhtml} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))
(is (= (str (html {:mode :html} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))
(is (= (str (html {:mode :xml} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))
(is (= (str (html {:mode :sgml} [(identity :p) (identity [:span "x"])]))
"<p><span>x</span></p>"))))
(testing "compiles literal child elements"
(let [code (walk/macroexpand-all `(html [(identity :p) [:span "x"]]))]
(is (= (extract-strings code) #{"" "<span>x</span>"})))))

0 comments on commit 4d7303c

Please sign in to comment.