Merge pull request #1460 from jbclements/add-xml-attribute-encode

add xml-attribute-encode function
This commit is contained in:
Jay McCarthy 2016-09-14 21:17:55 -04:00 committed by GitHub
commit 903afe2240
4 changed files with 29 additions and 1 deletions

View File

@ -300,6 +300,23 @@ Converts an @tech{X-expression} into a string containing XML.}
Converts XML represented with a string into an @tech{X-expression}.}
@defproc[(xml-encode-attribute [str string?]) string?]{
Escapes a string as required for XML attributes.
The escaping performed for attribute strings is slightly
different from that performed for body strings, in that
double-quotes must be escaped, as they would otherwise
terminate the enclosing string.
Note that this conversion is performed automatically in attribute
positions by @racket[xexpr->string], and you are therefore unlikely to
need this function unless you are using @racket[include-template] to
insert strings directly into attribute positions of HTML.
@history[#:added "6.6.0.7"]
}
@defproc[((eliminate-whitespace [tags (listof symbol?) empty]
[choose (boolean? . -> . boolean?) (λ (x) x)])
[elem element?])

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))