Skip to content

Commit

Permalink
safer quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
rob7hunter committed Nov 25, 2008
1 parent 9ccdcd6 commit 8ab1648
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions js.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 8ab1648

Please sign in to comment.