add xml-attribute-encode function

This commit is contained in:
John Clements 2016-09-14 09:34:10 -07:00
parent 62f5b2c4e4
commit 63d0f79847
3 changed files with 12 additions and 1 deletions

View File

@ -540,6 +540,10 @@ END
(test-suite
"xml->xexpr"
(test-equal? "xml-attribute-encode"
(xml-attribute-encode "ab\"cd?e;;<i> %&quot;f")
"ab&quot;cd?e;;&lt;i&gt; %&amp;quot;f")
(test-xml->xexpr
"<doc><bold>hi</bold> there!</doc>"
'(doc () (bold () "hi") " there!"))

View File

@ -166,7 +166,7 @@
[(#\") "&quot;"]
[else c]))
;; escape : String -> String
;; escape : String Regexp -> String
(define (escape x table)
(regexp-replace* table x replace-escaped))

View File

@ -101,6 +101,7 @@
[xml->xexpr (content/c . -> . xexpr/c)]
[xexpr->xml (xexpr/c . -> . content/c)]
[xexpr-drop-empty-attributes (parameter/c boolean?)]
[xml-attribute-encode (string? . -> . string?)]
[write-xexpr (->* (xexpr/c)
(output-port?
#:insert-newlines? any/c)
@ -168,3 +169,9 @@
[(p-i? x)
(write-xml-p-i x 0 void out)]))
(void))
;; given a string, encode it in the style required for attributes. Specifically,
;; double-quote must be encoded as well as <, >, and &, because the double-quote
;; would otherwise end the attribute.
(define (xml-attribute-encode str)
(escape str escape-attribute-table))