[utilities] move gnc:html-string-sanitize to utilities.scm

This commit is contained in:
Christopher Lam 2019-09-22 21:17:40 +08:00
parent 1a6314e108
commit a259ba4a3e
5 changed files with 52 additions and 49 deletions

View File

@ -870,18 +870,5 @@
"<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///~a\" />\n" "<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///~a\" />\n"
(gnc-path-find-localized-html-file file))) (gnc-path-find-localized-html-file file)))
;; function to sanitize strings prior to sending to html
(define (gnc:html-string-sanitize str)
(with-output-to-string
(lambda ()
(string-for-each
(lambda (c)
(display
(case c
((#\&) "&amp;")
((#\<) "&lt;")
((#\>) "&gt;")
(else c))))
str))))

View File

@ -122,7 +122,6 @@
(export gnc:html-make-options-link) (export gnc:html-make-options-link)
(export gnc:html-js-include) (export gnc:html-js-include)
(export gnc:html-css-include) (export gnc:html-css-include)
(export gnc:html-string-sanitize)
;; report.scm ;; report.scm
(export gnc:menuname-reports) (export gnc:menuname-reports)

View File

@ -12,44 +12,9 @@
(define (run-test) (define (run-test)
(test-runner-factory gnc:test-runner) (test-runner-factory gnc:test-runner)
(test-begin "test-html-utilities-srfi64.scm") (test-begin "test-html-utilities-srfi64.scm")
(test-gnc:html-string-sanitize)
(test-gnc:assign-colors) (test-gnc:assign-colors)
(test-end "test-html-utilities-srfi64.scm")) (test-end "test-html-utilities-srfi64.scm"))
(define (test-gnc:html-string-sanitize)
(test-begin "gnc:html-string-sanitize")
(test-equal "null test"
"abc"
(gnc:html-string-sanitize "abc"))
(test-equal "sanitize &copy;"
"&amp;copy;"
(gnc:html-string-sanitize "&copy;"))
(if (not (string=? (with-output-to-string (lambda () (display "🎃"))) "🎃"))
(test-skip 2))
(test-equal "emoji unchanged"
"🎃"
(gnc:html-string-sanitize "🎃"))
(test-equal "complex string"
"Smiley:\"🙂\" something"
(gnc:html-string-sanitize "Smiley:\"🙂\" something"))
(test-equal "sanitize <b>bold tags</b>"
"&lt;b&gt;bold tags&lt;/b&gt;"
(gnc:html-string-sanitize "<b>bold tags</b>"))
(test-equal "quotes are unchanged for html"
"\""
(gnc:html-string-sanitize "\""))
(test-equal "backslash is unchanged for html"
"\\"
(gnc:html-string-sanitize "\\"))
(test-end "gnc:html-string-sanitize"))
(define (test-gnc:assign-colors) (define (test-gnc:assign-colors)
(test-begin "test-gnc:assign-colors") (test-begin "test-gnc:assign-colors")
(test-equal "assign-colors can request many colors" (test-equal "assign-colors can request many colors"

View File

@ -10,6 +10,7 @@
(test-traverse-vec) (test-traverse-vec)
(test-substring-replace) (test-substring-replace)
(test-sort-and-delete-duplicates) (test-sort-and-delete-duplicates)
(test-gnc:html-string-sanitize)
(test-gnc:list-flatten) (test-gnc:list-flatten)
(test-begin "test-libgnucash-scm-utilities.scm")) (test-begin "test-libgnucash-scm-utilities.scm"))
@ -89,6 +90,40 @@
(sort-and-delete-duplicates '(3 1 2) <)) (sort-and-delete-duplicates '(3 1 2) <))
(test-end "sort-and-delete-duplicates")) (test-end "sort-and-delete-duplicates"))
(define (test-gnc:html-string-sanitize)
(test-begin "gnc:html-string-sanitize")
(test-equal "null test"
"abc"
(gnc:html-string-sanitize "abc"))
(test-equal "sanitize &copy;"
"&amp;copy;"
(gnc:html-string-sanitize "&copy;"))
(if (not (string=? (with-output-to-string (lambda () (display "🎃"))) "🎃"))
(test-skip 2))
(test-equal "emoji unchanged"
"🎃"
(gnc:html-string-sanitize "🎃"))
(test-equal "complex string"
"Smiley:\"🙂\" something"
(gnc:html-string-sanitize "Smiley:\"🙂\" something"))
(test-equal "sanitize <b>bold tags</b>"
"&lt;b&gt;bold tags&lt;/b&gt;"
(gnc:html-string-sanitize "<b>bold tags</b>"))
(test-equal "quotes are unchanged for html"
"\""
(gnc:html-string-sanitize "\""))
(test-equal "backslash is unchanged for html"
"\\"
(gnc:html-string-sanitize "\\"))
(test-end "gnc:html-string-sanitize"))
(define (test-gnc:list-flatten) (define (test-gnc:list-flatten)
(test-equal "gnc:list-flatten null" (test-equal "gnc:list-flatten null"
'() '()

View File

@ -172,6 +172,23 @@
s1 s2 s3 0 (string-length s1) (max 0 (1- start)) s1 s2 s3 0 (string-length s1) (max 0 (1- start))
(and (positive? end-after) (+ (max 0 (1- start)) (1- end-after))))) (and (positive? end-after) (+ (max 0 (1- start)) (1- end-after)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; function to sanitize strings. the resulting string can be safely
;; added to html.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-public (gnc:html-string-sanitize str)
(with-output-to-string
(lambda ()
(string-for-each
(lambda (c)
(display
(case c
((#\&) "&amp;")
((#\<) "&lt;")
((#\>) "&gt;")
(else c))))
str))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; avoid using strftime, still broken in guile-2.2. see explanation at ;; avoid using strftime, still broken in guile-2.2. see explanation at
;; https://lists.gnu.org/archive/html/bug-guile/2019-05/msg00003.html ;; https://lists.gnu.org/archive/html/bug-guile/2019-05/msg00003.html