Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix literal child elements being escaped #207

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>"})))))