Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't escape text contents of script tags!

This is a workaround for what seems to me wrong behavior in
tagsoup, which escapes contents of script tags, even if
they're wrapped in CDATA.
  • Loading branch information...
commit fb438c9c9b46f46220e0e5e904e94d54f751ec0d 1 parent 8d612f3
John MacFarlane authored
Showing with 21 additions and 1 deletion.
  1. +21 −1 standalone-html.hs
22 standalone-html.hs
View
@@ -10,6 +10,7 @@ import Data.ByteString (ByteString)
import Data.ByteString.UTF8 (toString, fromString)
import System.FilePath (takeExtension, dropExtension)
import Data.Char (toLower, isAscii, isAlphaNum)
+import Data.Monoid
import Codec.Compression.GZip as Gzip
import qualified Data.ByteString.Lazy as L
@@ -198,10 +199,29 @@ getRaw t src = do
raw <- getItem src'
return (decompress raw, mime)
+renderNormal :: [Tag ByteString] -> ByteString
+renderNormal = renderTagsOptions renderOptions{ optMinimize = (\t -> t == "br" || t == "img" || t == "meta" || t == "link" ) }
+
+-- Workaround problem in tagsoup; text inside <script> should not be escaped.
+renderScriptTag :: Tag ByteString -> ByteString
+renderScriptTag (TagText s) =
+ renderTagsOptions renderOptions{ optEscape = id } [TagText s]
+renderScriptTag x = renderNormal [x]
+
+render :: [Tag ByteString] -> ByteString
+render (t:ts) | isTagOpenName "script" t =
+ renderNormal [t] `mappend`
+ mconcat (map renderScriptTag contents) `mappend`
+ render rest
+ where (contents, rest) = break (isTagCloseName "script") ts
+render (t:ts) = renderNormal nonscripts `mappend` render rest
+ where (nonscripts, rest) = break (isTagOpenName "script") (t:ts)
+render [] = mempty
+
main :: IO ()
main = do
inp <- B.getContents
let tags = parseTags inp
out <- mapM convertTag tags
- B.putStr $ renderTagsOptions renderOptions{ optMinimize = (\t -> t == "br" || t == "img" || t == "meta" || t == "link" ) } out
+ B.putStr $ render out
Please sign in to comment.
Something went wrong with that request. Please try again.