special handling of CDATA string

pull/5/head
Matthew Butterick 7 years ago
parent dbe6b10b5f
commit b58e2d5ead

@ -214,7 +214,7 @@
(define+provide+safe (->txexpr-attr-value x) (define+provide+safe (->txexpr-attr-value x)
(can-be-txexpr-attr-value? . -> . txexpr-attr-value?) (can-be-txexpr-attr-value? . -> . txexpr-attr-value?)
(unless (can-be-txexpr-attr-value? x) (unless (can-be-txexpr-attr-value? x)
(raise-argument-error '->txexpr-attr-value "can-be-txexpr-attr-value?" x)) (raise-argument-error '->txexpr-attr-value "can-be-txexpr-attr-value?" x))
(->string x)) (->string x))
@ -352,16 +352,21 @@
(cdata #f #f x) (cdata #f #f x)
x)) x))
;; but treat CDATA strings correctly anyhow, because that's friendly
(define (cdata-string? x)
(and (string? x) (regexp-match #rx"^<!\\[CDATA\\[.*\\]\\]>$" x) #t))
(define+provide+safe (xexpr->html x) (define+provide+safe (xexpr->html x)
(xexpr? . -> . string?) (xexpr? . -> . string?)
(xexpr->string (xexpr->string
(let loop ([x x]) (let loop ([x x])
(if (txexpr? x) (cond
(let*-values ([(tag attrs elements) (txexpr->values x)] [(txexpr? x)
[(proc) (if (memq tag '(script style)) (let*-values ([(tag attrs elements) (txexpr->values x)]
->cdata [(proc) (if (memq tag '(script style))
loop)]) ->cdata
;; a little faster than `txexpr` since we know the pieces are valid loop)])
(txexpr-unsafe tag attrs (map proc elements))) ;; a little faster than `txexpr` since we know the pieces are valid
x)))) (txexpr-unsafe tag attrs (map proc elements)))]
[(cdata-string? x) (->cdata x)]
[else x]))))

@ -220,4 +220,7 @@
(check-false (findf-txexpr split-this-tx false-pred)) (check-false (findf-txexpr split-this-tx false-pred))
(check-equal? (xexpr->html '(root (script "3 > 2") "Why is 3 > 2?")) (check-equal? (xexpr->html '(root (script "3 > 2") "Why is 3 > 2?"))
"<root><script>3 > 2</script>Why is 3 &gt; 2?</root>")) "<root><script>3 > 2</script>Why is 3 &gt; 2?</root>")
(check-equal? (xexpr->html '(root (div "<![CDATA[3 > 2]]>") "Why is 3 > 2?"))
"<root><div><![CDATA[3 > 2]]></div>Why is 3 &gt; 2?</root>"))
Loading…
Cancel
Save