Skip to content
Browse files

Merge pull request #1 from thorstadt/master

Locally toggled XML escaping
  • Loading branch information...
2 parents 50f1f32 + 4ea4c7d commit 3849d94ef6fd8936a293735c5d7fde96a5c50491 @philjackson committed Sep 11, 2011
Showing with 21 additions and 11 deletions.
  1. +18 −11 xmlgen.el
  2. +3 −0 xmlgen.etest
View
29 xmlgen.el
@@ -84,17 +84,24 @@ elements content.")
(let ((el (car xml)))
(unless (symbolp el)
(error "Element must be a symbol (got '%S')." el))
- (setq el (symbol-name el))
- (concat "<" el (xmlgen-attr-to-string attrs)
- (if (> (length xml) 1)
- (concat ">" (mapconcat
- (lambda (s) (xmlgen s el (1+ level)))
- (if xmlgen-escape-elm-vals
- (mapcar 'xmlgen-string-escape (cdr xml))
- (cdr xml))
- "")
- "</" el ">")
- "/>"))))))))
+ (if (member el '(!unescape !escape))
+ (let ((xmlgen-escape-elm-vals (if (equal '!escape el) t nil)))
+ (mapconcat
+ (lambda (s) (xmlgen s in-elm (1+ level)))
+ (cdr xml)
+ ""))
+ (progn
+ (setq el (symbol-name el))
+ (concat "<" el (xmlgen-attr-to-string attrs)
+ (if (> (length xml) 1)
+ (concat ">" (mapconcat
+ (lambda (s) (xmlgen s el (1+ level)))
+ (if xmlgen-escape-elm-vals
+ (mapcar 'xmlgen-string-escape (cdr xml))
+ (cdr xml))
+ "")
+ "</" el ">")
+ "/>"))))))))))
(defun xmlgen-string-escape (string)
"Escape STRING for inclusion in some XML."
View
3 xmlgen.etest
@@ -23,6 +23,9 @@
(equal (xmlgen '(h1 "Title")) "<h1>Title</h1>")
(equal (xmlgen '(h1 :class "something" "hi"))
"<h1 class=\"something\">hi</h1>")
+ (equal (xmlgen '(div (p "Escaped: &") (!unescape (p "Unescaped: &") (!escape
+ (p "& escaped")))))
+ "<div><p>Escaped: &amp;</p><p>Unescaped: &</p><p>&amp; escaped</p></div>")
(equal (xmlgen '(p "hello" "again")) "<p>helloagain</p>")
("more complex"
(equal (xmlgen

0 comments on commit 3849d94

Please sign in to comment.
Something went wrong with that request. Please try again.