diff src/mono_opt.sml @ 135:b1cfe49ce692

Proper escaping of cdata
author Adam Chlipala <adamc@hcoop.net>
date Thu, 17 Jul 2008 13:33:56 -0400
parents 6230bdd122e7
children c0ea24dcb86f
line wrap: on
line diff
--- a/src/mono_opt.sml	Thu Jul 17 13:12:16 2008 -0400
+++ b/src/mono_opt.sml	Thu Jul 17 13:33:56 2008 -0400
@@ -46,6 +46,7 @@
         Real.toString n
 
 val attrifyString = String.translate (fn #"\"" => "&quot;"
+                                       | #"&" => "&amp;"
                                        | ch => if Char.isPrint ch then
                                                    str ch
                                                else
@@ -54,6 +55,15 @@
 val urlifyInt = attrifyInt
 val urlifyFloat = attrifyFloat
 
+val htmlifyString = String.translate (fn ch => case ch of
+                                                   #"<" => "&lt;"
+                                                 | #"&" => "&amp;"
+                                                 | _ =>   
+                                                   if Char.isPrint ch orelse Char.isSpace ch then
+                                                       str ch
+                                                   else
+                                                       "&#" ^ Int.toString (ord ch) ^ ";")
+
 fun hexIt ch =
     let
         val s = Int.fmt StringCvt.HEX (ord ch)
@@ -122,6 +132,13 @@
         ESeq ((optExp (EWrite e1, loc), loc),
               (optExp (EWrite e2, loc), loc))
 
+      | EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]) =>
+        EPrim (Prim.String (htmlifyString s))
+      | EWrite (EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]), loc) =>
+        EWrite (EPrim (Prim.String (htmlifyString s)), loc)
+      | EWrite (EFfiApp ("Basis", "htmlifyString", [e]), _) =>
+        EFfiApp ("Basis", "htmlifyString_w", [e])
+
       | EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]) =>
         EPrim (Prim.String (attrifyInt n))
       | EWrite (EFfiApp ("Basis", "attrifyInt", [(EPrim (Prim.Int n), _)]), loc) =>