Skip to content

Commit

Permalink
Merge pull request #38 from RyanGlScott/master
Browse files Browse the repository at this point in the history
Escape single quote (') characters as '
  • Loading branch information
ndmitchell authored Sep 18, 2018
2 parents e185561 + 1c58c48 commit 175b780
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/Data/List/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,19 @@ line1 = second drop1 . break (== '\n')

-- | Escape a string such that it can be inserted into an HTML document or @\"@ attribute
-- without any special interpretation. This requires escaping the @<@, @>@, @&@ and @\"@ characters.
-- Note that it does /not/ escape @\'@, so will not work when placed in a @\'@ delimited attribute.
-- Also note that it will escape @\"@ even though that is not required in an HTML body (but is not harmful).
-- Note that it will escape @\"@ and @\'@ even though that is not required in an HTML body (but is not harmful).
--
-- > escapeHTML "this is a test" == "this is a test"
-- > escapeHTML "<b>\"g&t\"</n>" == "&lt;b&gt;&quot;g&amp;t&quot;&lt;/n&gt;"
-- > escapeHTML "don't" == "don't"
-- > escapeHTML "t'was another test" == "t&#39;was another test"
escapeHTML :: String -> String
escapeHTML = concatMap f
where
f '>' = "&gt;"
f '<' = "&lt;"
f '&' = "&amp;"
f '\"' = "&quot;"
f '\'' = "&#39;"
f x = [x]

-- | Invert of 'escapeHTML' (does not do general HTML unescaping)
Expand All @@ -309,6 +309,7 @@ unescapeHTML ('&':xs)
| Just xs <- stripPrefix "gt;" xs = '>' : unescapeHTML xs
| Just xs <- stripPrefix "amp;" xs = '&' : unescapeHTML xs
| Just xs <- stripPrefix "quot;" xs = '\"' : unescapeHTML xs
| Just xs <- stripPrefix "#39;" xs = '\'' : unescapeHTML xs
unescapeHTML (x:xs) = x : unescapeHTML xs
unescapeHTML [] = []

Expand Down
2 changes: 1 addition & 1 deletion test/TestGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ tests = do
testGen "line1 \"test\\nrest\\nmore\" == (\"test\",\"rest\\nmore\")" $ line1 "test\nrest\nmore" == ("test","rest\nmore")
testGen "escapeHTML \"this is a test\" == \"this is a test\"" $ escapeHTML "this is a test" == "this is a test"
testGen "escapeHTML \"<b>\\\"g&t\\\"</n>\" == \"&lt;b&gt;&quot;g&amp;t&quot;&lt;/n&gt;\"" $ escapeHTML "<b>\"g&t\"</n>" == "&lt;b&gt;&quot;g&amp;t&quot;&lt;/n&gt;"
testGen "escapeHTML \"don't\" == \"don't\"" $ escapeHTML "don't" == "don't"
testGen "escapeHTML \"t'was another test\" == \"t&#39;was another test\"" $ escapeHTML "t'was another test" == "t&#39;was another test"
testGen "\\xs -> unescapeHTML (escapeHTML xs) == xs" $ \xs -> unescapeHTML (escapeHTML xs) == xs
testGen "escapeJSON \"this is a test\" == \"this is a test\"" $ escapeJSON "this is a test" == "this is a test"
testGen "escapeJSON \"\\ttab\\nnewline\\\\\" == \"\\\\ttab\\\\nnewline\\\\\\\\\"" $ escapeJSON "\ttab\nnewline\\" == "\\ttab\\nnewline\\\\"
Expand Down

0 comments on commit 175b780

Please sign in to comment.