Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape single quote (') characters as ' #38

Merged
merged 1 commit into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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