Permalink
Browse files

safer quoting

  • Loading branch information...
1 parent 9ccdcd6 commit 8ab1648f91f77aa2b28489411fc05a2210005bc9 @vegashacker committed Nov 25, 2008
Showing with 11 additions and 2 deletions.
  1. +11 −2 js.scm
View
@@ -23,14 +23,23 @@
(string-join (hash-map scheme-hash (lambda (k v) (format "~A:~A" (js-quote k) v)))
", ")
"}"))
-
+
(define (js-quote thing)
(cond ((number? thing) (number->string thing))
- ((or (string? thing) (symbol? thing)) (format "'~A'" thing))
+ ((or (string? thing) (symbol? thing))
+ (format "\"~A\"" (careful-string-quote (if (string? thing)
+ thing
+ (symbol->string thing)))))
((eq? thing #t) "true")
((eq? thing #f) "false")
(else (e "Don't know how to js-quote ~A." thing))))
+(define (careful-string-quote scm-str)
+ (pregexp-replace-many scm-str
+ ("\n" => " ")
+ ("\r" => " ")
+ ("\"" => "'")))
+
(define (js-call-on-load fn-name #:quote-all (quote-all #f) . args)
(format "$(document).ready(function() { ~A })"
(apply js-call fn-name #:quote-all quote-all args)))

0 comments on commit 8ab1648

Please sign in to comment.