Skip to content

Commit

Permalink
c.c.prxml: Fix bad refers from lazy-xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuart Sierra committed Feb 11, 2010
1 parent c8be496 commit 7a0e744
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/clojure/clojure/contrib/prxml.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
:doc "Compact syntax for generating XML. See the documentation of \"prxml\" :doc "Compact syntax for generating XML. See the documentation of \"prxml\"
for details."} for details."}
clojure.contrib.prxml clojure.contrib.prxml
(:use [clojure.contrib.lazy-xml :only (escape-xml)])) (:use [clojure.contrib.string :only (escape)]
[clojure.contrib.java :only (as-str)]))


(def (def
#^{:doc "If true, empty tags will have a space before the closing />"} #^{:doc "If true, empty tags will have a space before the closing />"}
Expand All @@ -38,16 +39,20 @@ for details."}
and no extra line-breaks."} and no extra line-breaks."}
*prxml-indent* nil) *prxml-indent* nil)


(defn- namestr [x]
(if (or (symbol? x) (keyword? x)) (name x) (str x)))

(def #^{:private true} *prxml-tag-depth* 0) (def #^{:private true} *prxml-tag-depth* 0)


(def #^{:private true} print-xml) ; forward declaration (def #^{:private true} print-xml) ; forward declaration


(defn- escape-xml [s]
(escape {\< "&lt;"
\> "&gt;"
\& "&amp;"
\' "&apos;"
\" "&quot;"} s))

(defn- prxml-attribute [name value] (defn- prxml-attribute [name value]
(print " ") (print " ")
(print (namestr name)) (print (as-str name))
(print "=\"") (print "=\"")
(print (escape-xml (str value))) (print (escape-xml (str value)))
(print "\"")) (print "\""))
Expand Down Expand Up @@ -88,7 +93,7 @@ for details."}
(print ">")) (print ">"))


(defmethod print-xml-tag :default [tag attrs contents] (defmethod print-xml-tag :default [tag attrs contents]
(let [tag-name (namestr tag)] (let [tag-name (as-str tag)]
(when *prxml-indent* (when *prxml-indent*
(newline) (newline)
(dotimes [n (* *prxml-tag-depth* *prxml-indent*)] (print " "))) (dotimes [n (* *prxml-tag-depth* *prxml-indent*)] (print " ")))
Expand Down
10 changes: 10 additions & 0 deletions src/test/clojure/clojure/contrib/test_prxml.clj
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns clojure.contrib.test-prxml
(:use clojure.test clojure.contrib.prxml))

(deftest prxml-basic
(is (= "<p>Hello, World!</p>"
(with-out-str (prxml [:p "Hello, World!"])))))

(deftest prxml-escaping
(is (= "<a href=\"foo&amp;bar\">foo&lt;bar</a>"
(with-out-str (prxml [:a {:href "foo&bar"} "foo<bar"])))))

0 comments on commit 7a0e744

Please sign in to comment.