allow strings as formlet parameters

It allows programmer to ommit unnecessary double string<->bytes
conversion when repeating input from the user.

Signed-off-by: Jan Dvořák <mordae@anilinux.org>
This commit is contained in:
Jan Dvořák 2013-09-11 10:02:10 +02:00 committed by Jay McCarthy
parent 650e9980cb
commit b97e3e9b9f
2 changed files with 48 additions and 44 deletions

View File

@ -295,7 +295,7 @@ These @tech{formlet}s are the main combinators for form input.
}
@defproc[(input [#:type type string?]
[#:value value (or/c false/c bytes?) #f]
[#:value value (or/c false/c bytes? string?) #f]
[#:size size (or/c false/c exact-nonnegative-integer?) #f]
[#:max-length max-length (or/c false/c exact-nonnegative-integer?) #f]
[#:read-only? read-only? boolean? #f]
@ -305,7 +305,7 @@ These @tech{formlet}s are the main combinators for form input.
and arguments.
}
@defproc[(text-input [#:value value (or/c false/c bytes?) #f]
@defproc[(text-input [#:value value (or/c false/c bytes? string?) #f]
[#:size size (or/c false/c exact-nonnegative-integer?) #f]
[#:max-length max-length (or/c false/c exact-nonnegative-integer?) #f]
[#:read-only? read-only? boolean? #f]
@ -315,7 +315,7 @@ These @tech{formlet}s are the main combinators for form input.
and the attributes given in the arguments.
}
@defproc[(password-input [#:value value (or/c false/c bytes?) #f]
@defproc[(password-input [#:value value (or/c false/c bytes? string?) #f]
[#:size size (or/c false/c exact-nonnegative-integer?) #f]
[#:max-length max-length (or/c false/c exact-nonnegative-integer?) #f]
[#:read-only? read-only? boolean? #f]
@ -325,7 +325,7 @@ These @tech{formlet}s are the main combinators for form input.
type and the attributes given in the arguments.
}
@defproc[(textarea-input [#:value value (or/c false/c bytes?) #f]
@defproc[(textarea-input [#:value value (or/c false/c bytes? string?) #f]
[#:rows rows (or/c false/c number?) #f]
[#:cols cols (or/c false/c number?) #f]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
@ -334,7 +334,7 @@ These @tech{formlet}s are the main combinators for form input.
given in the arguments.
}
@defproc[(checkbox [value bytes?]
@defproc[(checkbox [value (or/c bytes? string?)]
[checked? boolean?]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
@ -342,7 +342,7 @@ These @tech{formlet}s are the main combinators for form input.
type and the attributes given in the arguments.
}
@defproc[(radio [value bytes?]
@defproc[(radio [value (or/c bytes? string?)]
[checked? boolean?]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
@ -377,14 +377,14 @@ results of @racket[display]. The result of processing this formlet is
a list of elements of the sequence.
}
@defproc[(submit [value bytes?]
@defproc[(submit [value (or/c bytes? string?)]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
This @tech{formlet} renders using an INPUT element with the SUBMIT
type and the attributes given in the arguments.
}
@defproc[(reset [value bytes?]
@defproc[(reset [value (or/c bytes? string?)]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
This @tech{formlet} renders using an INPUT element with the RESET type
@ -397,17 +397,17 @@ a list of elements of the sequence.
and the attributes given in the arguments.
}
@defproc[(hidden [value bytes?] [#:attributes attrs (listof (list/c symbol? string?)) empty])
@defproc[(hidden [value (or/c bytes? string?)] [#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
This @tech{formlet} renders using an INPUT element with HIDDEN type
and the attributes given in the arguments.
}
@defproc[(img [alt bytes?]
[src bytes?]
@defproc[(img [alt (or/c bytes? string?)]
[src (or/c bytes? string?)]
[#:height height (or/c false/c exact-nonnegative-integer?) #f]
[#:longdesc ldesc (or/c false/c bytes?) #f]
[#:usemap map (or/c false/c bytes?) #f]
[#:longdesc ldesc (or/c false/c bytes? string?) #f]
[#:usemap map (or/c false/c bytes? string?) #f]
[#:width width (or/c false/c exact-nonnegative-integer?) #f]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
@ -415,10 +415,10 @@ a list of elements of the sequence.
given in the arguments.
}
@defproc[(button [type bytes?]
[button-text bytes?]
@defproc[(button [type (or/c bytes? string?)]
[button-text (or/c bytes? string?)]
[#:disabled disabled boolean? #f]
[#:value value (or/c false/c bytes?) #f]
[#:value value (or/c false/c bytes? string?) #f]
[#:attributes attrs (listof (list/c symbol? string?)) empty])
(formlet/c (or/c false/c binding?))]{
This @tech{formlet} renders using a BUTTON element with the attributes
@ -457,7 +457,7 @@ a list of elements of the sequence.
or errors.
}
@defproc[(default
@defproc[(default
[def bytes?]
[f (formlet/c (or/c false/c binding?))])
(formlet/c bytes?)]{

View File

@ -8,6 +8,12 @@
pure
cross))
;; Convert UTF-8 bytes to string when needed.
(define (coerce-string/utf-8 bstr-or-str)
(if (bytes? bstr-or-str)
(bytes->string/utf-8 bstr-or-str)
bstr-or-str))
; Low-level
(define (next-name i)
(values (format "input_~a" i) (add1 i)))
@ -70,7 +76,7 @@
(list 'type type)
(append
(filter list?
(list (and value (list 'value (bytes->string/utf-8 value)))
(list (and value (list 'value (coerce-string/utf-8 value)))
(and size (list 'size (number->string size)))
(and max-length (list 'maxlength (number->string max-length)))
(and read-only? (list 'readonly "true"))))
@ -217,13 +223,13 @@
(λ (n)
(list 'button
(list* (list 'name n)
(list 'type (bytes->string/utf-8 type))
(list 'type (coerce-string/utf-8 type))
(append
(filter list?
(list (and disabled (list 'disabled (if disabled "true" "false")))
(and value (list 'value (bytes->string/utf-8 value)))))
(and value (list 'value (coerce-string/utf-8 value)))))
attrs))
(bytes->string/utf-8 text)))))
(coerce-string/utf-8 text)))))
(define (img alt src
#:height [height #f]
@ -235,13 +241,13 @@
(λ (n)
(list 'img
(list* (list 'name n)
(list 'src (bytes->string/utf-8 src))
(list 'alt (bytes->string/utf-8 alt))
(list 'src (coerce-string/utf-8 src))
(list 'alt (coerce-string/utf-8 alt))
(append
(filter list?
(list (and height (list 'height (number->string height)))
(and ldesc (list 'longdesc (bytes->string/utf-8 ldesc)))
(and map (list 'usemap (bytes->string/utf-8 map)))
(and ldesc (list 'longdesc (coerce-string/utf-8 ldesc)))
(and map (list 'usemap (coerce-string/utf-8 map)))
(and width (list 'width (number->string width)))))
attrs))))))
@ -308,21 +314,19 @@
(list (and rows (list 'rows (number->string rows)))
(and cols (list 'cols (number->string cols)))))
attrs))
(if value
(bytes->string/utf-8 value)
"")))))
(if value (coerce-string/utf-8 value) "")))))
(provide/contract
[input (()
(#:type string?
#:value (or/c false/c bytes?)
#:value (or/c false/c bytes? string?)
#:max-length (or/c false/c exact-nonnegative-integer?)
#:read-only? boolean?
#:attributes (listof (list/c symbol? string?))
#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?))))]
(formlet/c (or/c false/c binding?)))]
[text-input (()
(#:value (or/c false/c bytes?)
(#:value (or/c false/c bytes? string?)
#:size (or/c false/c exact-nonnegative-integer?)
#:max-length (or/c false/c exact-nonnegative-integer?)
#:read-only? boolean?
@ -330,18 +334,18 @@
. ->* .
(formlet/c (or/c false/c binding?)))]
[password-input (()
(#:value (or/c false/c bytes?)
(#:value (or/c false/c bytes? string?)
#:size (or/c false/c exact-nonnegative-integer?)
#:max-length (or/c false/c exact-nonnegative-integer?)
#:read-only? boolean?
#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
[checkbox ((bytes? boolean?)
[checkbox (((or/c bytes? string?) boolean?)
(#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
[radio ((bytes? boolean?)
[radio (((or/c bytes? string?) boolean?)
(#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
@ -359,11 +363,11 @@
#:display (any/c . -> . pretty-xexpr/c))
. ->* .
(formlet/c (listof any/c)))]
[submit ((bytes?)
[submit (((or/c bytes? string?))
(#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
[reset ((bytes?)
[reset (((or/c bytes? string?))
(#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
@ -371,22 +375,22 @@
(#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
[hidden ((bytes?)
[hidden (((or/c bytes? string?))
(#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
[img ((bytes? bytes?)
[img (((or/c bytes? string?) (or/c bytes? string?))
(#:height (or/c false/c exact-nonnegative-integer?)
#:longdesc (or/c false/c bytes?)
#:usemap (or/c false/c bytes?)
#:longdesc (or/c false/c (or/c bytes? string?))
#:usemap (or/c false/c (or/c bytes? string?))
#:width (or/c false/c exact-nonnegative-integer?)
#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
[button ((bytes? bytes?)
[button (((or/c bytes? string?) (or/c bytes? string?))
(#:disabled
boolean?
#:value (or/c false/c bytes?)
#:value (or/c false/c (or/c bytes? string?))
#:attributes (listof (list/c symbol? string?)))
. ->* .
(formlet/c (or/c false/c binding?)))]
@ -408,7 +412,7 @@
[textarea-input (()
(#:attributes
(listof (list/c symbol? string?))
#:value (or/c false/c bytes?)
#:value (or/c false/c (or/c bytes? string?))
#:rows number?
#:cols number?)
. ->* .