Skip to content

Commit

Permalink
Fix literal child elements being escaped
Browse files Browse the repository at this point in the history
Fixes `(html [(identity :p) [:span "x"]])` producing
`"<p>&lt;span&gt;x&lt;/span&gt;</p>"` instead of
`"<p><span>x</span></p>"`
  • Loading branch information
luontola committed Dec 8, 2023
1 parent 19c7531 commit 27faeb6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/hiccup/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@
`(when-not (map? ~attrs-sym)
~(str "</" tag ">")))))))

(defmethod compile-element :default
(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 27faeb6

Please sign in to comment.