dev-validator
Matthew Butterick 11 years ago
parent 08abd8e235
commit bdbfb89c7a

@ -35,7 +35,7 @@
(let ([bad-attrs (filter (λ(i) (not (txexpr-attr? i))) x)]) (let ([bad-attrs (filter (λ(i) (not (txexpr-attr? i))) x)])
(format "because ~a ~a" (string-join (map (λ(ba) (format "~v" ba)) bad-attrs) " and ") (if (> (length bad-attrs) 1) (format "because ~a ~a" (string-join (map (λ(ba) (format "~v" ba)) bad-attrs) " and ") (if (> (length bad-attrs) 1)
"are not valid attributes" "are not valid attributes"
"is not in the form (list symbol? string?)"))))) "is not in the form '(symbol \"string\")")))))
(match x (match x
[(list (? txexpr-attr?) ...) x] [(list (? txexpr-attr?) ...) x]
[else [else (error (string-append "validate-txexpr-attrs: " [else [else (error (string-append "validate-txexpr-attrs: "
@ -61,7 +61,7 @@
(valid-char? x) (cdata? x)) x] (valid-char? x) (cdata? x)) x]
[else (error (string-append "validate-txexpr-element: " [else (error (string-append "validate-txexpr-element: "
(if txexpr-context (format "in ~v, " txexpr-context) "") (if txexpr-context (format "in ~v, " txexpr-context) "")
(format "~v is not a valid element (expecting txexpr, string, symbol, XML char, or cdata)" x)))])) (format "~v is not a valid element (must be txexpr, string, symbol, XML char, or cdata)" x)))]))
(define+provide+safe (txexpr-element? x) (define+provide+safe (txexpr-element? x)
@ -82,7 +82,7 @@
(and (validate-txexpr-attrs-with-context attr-list) (and (validate-txexpr-attrs-with-context attr-list)
(andmap validate-txexpr-element-with-context rest))] (andmap validate-txexpr-element-with-context rest))]
[(list (? symbol? name) rest ...)(andmap validate-txexpr-element-with-context rest)] [(list (? symbol? name) rest ...)(andmap validate-txexpr-element-with-context rest)]
[else (error (format "validate-txexpr: first element is not a symbol in ~v" x))]) [else (error (format "validate-txexpr: ~v is not a list starting with a symbol" x))])
x)) x))
(define+provide+safe (txexpr? x) (define+provide+safe (txexpr? x)

@ -154,9 +154,12 @@ txexpr?]
Like @racket[txexpr?], but raises a descriptive error if @racket[_possible-txexpr] is invalid, and otherwise returns @racket[_possible-txexpr] itself. Like @racket[txexpr?], but raises a descriptive error if @racket[_possible-txexpr] is invalid, and otherwise returns @racket[_possible-txexpr] itself.
@examples[#:eval my-eval @examples[#:eval my-eval
(validate-txexpr '(root (mama.html son.html daughter.html) uncle.html)) (validate-txexpr 'root)
(validate-txexpr `(root (,+ son.html daughter.html) uncle.html)) (validate-txexpr '(root))
(validate-txexpr '(root (mama.html son.html son.html) mama.html)) (validate-txexpr '(root ((id "top")(class 42))))
(validate-txexpr '(root ((id "top")(class "42"))))
(validate-txexpr '(root ((id "top")(class "42")) ("hi")))
(validate-txexpr '(root ((id "top")(class "42")) "hi"))
] ]
@ -218,6 +221,19 @@ Like @racket[txexpr->values], but returns the three components in a list.
(txexpr->list '(div [[id "top"]] "Hello" (p "World"))) (txexpr->list '(div [[id "top"]] "Hello" (p "World")))
] ]
@defproc[
(txexpr->html
[tx txexpr?])
string?]
Convert @racket[_tx] to an HTML string. Better than @racket[xexpr->string] because consistent with the HTML spec, it will not escape text that appears within @code{script} or @code{style} blocks.
@examples[#:eval my-eval
(define tx '(root (script "3 > 2") "Why is 3 > 2?"))
(xexpr->string tx)
(txexpr->html tx)
]
@deftogether[( @deftogether[(
@defproc[ @defproc[
(get-tag (get-tag

@ -118,3 +118,5 @@
(check-equal? (call-with-values (λ() (splitf-txexpr split-this-tx (λ(x) (and (txexpr? x) (equal? 'meta (car x)))))) list) (check-equal? (call-with-values (λ() (splitf-txexpr split-this-tx (λ(x) (and (txexpr? x) (equal? 'meta (car x)))))) list)
(list '(root "hello" "world" (em "goodnight" "moon")) '((meta "foo" "bar") (meta "foo2" "bar2") (meta "foo3" "bar3")))) (list '(root "hello" "world" (em "goodnight" "moon")) '((meta "foo" "bar") (meta "foo2" "bar2") (meta "foo3" "bar3"))))
(check-equal? (txexpr->html '(root (script "3 > 2") "Why is 3 > 2?"))
"<root><script>3 > 2</script>Why is 3 &gt; 2?</root>")
Loading…
Cancel
Save