Skip to content

Commit aa47d8d

Browse files
committed
web: fix add form completions (fixes #1156)
It seems like show (toJSON "a") used to give "\"a\"" instead of "String \"a\"". I haven't found the root cause, it's possible that this fix won't work if built with older libs.
1 parent c5537ce commit aa47d8d

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

hledger-web/Hledger/Web/Widget/AddForm.hs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Hledger.Web.Widget.AddForm
1313

1414
import Control.Monad.State.Strict (evalStateT)
1515
import Data.Bifunctor (first)
16-
import Data.List (dropWhileEnd, nub, sort, unfoldr)
16+
import Data.List (dropWhileEnd, intercalate, nub, sort, unfoldr)
1717
import Data.Maybe (isJust)
1818
#if !(MIN_VERSION_base(4,13,0))
1919
import Data.Semigroup ((<>))
@@ -73,10 +73,6 @@ addForm j today = identifyForm "add" $ \extra -> do
7373
-- bindings used in add-form.hamlet
7474
let descriptions = sort $ nub $ tdescription <$> jtxns j
7575
journals = fst <$> jfiles j
76-
listToJsonArray :: [Text] -> Markup
77-
listToJsonArray = preEscapedString . escapeJSSpecialChars . show . toJSON
78-
where
79-
escapeJSSpecialChars = regexReplaceCI "</script>" "<\\/script>" -- #236
8076

8177
pure (validateTransaction dateRes descRes postRes, $(widgetFile "add-form"))
8278

@@ -98,6 +94,28 @@ addForm j today = identifyForm "add" $ \extra -> do
9894
, fieldEnctype = UrlEncoded
9995
}
10096

97+
-- Used in add-form.hamlet
98+
toBloodhoundJson :: [Text] -> Markup
99+
toBloodhoundJson ts =
100+
-- This used to work, but since 1.16, it seems like something changed.
101+
-- toJSON ("a"::Text) gives String "a" instead of "a", etc.
102+
-- preEscapedString . escapeJSSpecialChars . show . toJSON
103+
preEscapedString $ concat [
104+
"[",
105+
intercalate "," $ map (
106+
("{\"value\":" ++).
107+
(++"}").
108+
escapeJSSpecialChars .
109+
drop 7 . -- "String "
110+
show .
111+
toJSON
112+
) ts,
113+
"]"
114+
]
115+
where
116+
-- avoid https://github.com/simonmichael/hledger/issues/236
117+
escapeJSSpecialChars = regexReplaceCI "</script>" "<\\/script>"
118+
101119
validateTransaction ::
102120
FormResult Day
103121
-> FormResult Text

hledger-web/templates/add-form.hamlet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<script>
22
jQuery(document).ready(function() {
33
descriptionsSuggester = new Bloodhound({
4-
local:#{listToJsonArray descriptions},
4+
local:#{toBloodhoundJson descriptions},
55
limit:100,
66
datumTokenizer: function(d) { return [d.value]; },
77
queryTokenizer: function(q) { return [q]; }
88
});
99
descriptionsSuggester.initialize();
1010

1111
accountsSuggester = new Bloodhound({
12-
local:#{listToJsonArray (journalAccountNamesDeclaredOrImplied j)},
12+
local:#{toBloodhoundJson (journalAccountNamesDeclaredOrImplied j)},
1313
limit:100,
1414
datumTokenizer: function(d) { return [d.value]; },
1515
queryTokenizer: function(q) { return [q]; }

0 commit comments

Comments
 (0)