From a3d5ae5407b47e008d51490223faa19649bfd90f Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 8 Sep 2023 15:19:37 +1200 Subject: [PATCH] Add support for checkboxes, along with supporting infrastructure. --- app/Main.hs | 4 +-- bureaucromancy.cabal | 7 +++-- src/Text/HTML/Form.hs | 53 ++++++++++++++++++------------------ src/Text/HTML/Form/WebApp.hs | 30 +++++++++++++------- tpl/.keep | 0 5 files changed, 52 insertions(+), 42 deletions(-) delete mode 100644 tpl/.keep diff --git a/app/Main.hs b/app/Main.hs index 4c76ac8..77a980c 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -12,7 +12,7 @@ import Text.HTML.Form import Data.Text.Lazy.Encoding (encodeUtf8) import Data.Text.Lazy (fromStrict) import qualified Data.Text as Txt -import Text.XML (readFile, def) +import qualified Text.HTML.DOM as HTML import Data.Maybe (fromJust) main :: IO () @@ -22,7 +22,7 @@ main = do n:anchor:_ -> (n, anchor) [n] -> (n, "0") [] -> ("form.html", "0") - doc <- Text.XML.readFile def filename + doc <- HTML.readFile filename runEnv 2018 $ servePage $ fromJust $ parseDocument doc $ Txt.pack ident servePage :: Form -> Application diff --git a/bureaucromancy.cabal b/bureaucromancy.cabal index c7ab7ee..82121a4 100644 --- a/bureaucromancy.cabal +++ b/bureaucromancy.cabal @@ -62,7 +62,7 @@ library import: warnings -- Modules exported by the library. - exposed-modules: Text.HTML.Form, + exposed-modules: Text.HTML.Form, Text.HTML.Form.Query, Text.HTML.Form.WebApp, Text.HTML.Form.WebApp.Ginger -- Modules included in this library but not exported. @@ -73,7 +73,8 @@ library -- Other library packages from which modules are imported. build-depends: base ^>=4.16.4.0, ginger, file-embed-lzma, file-embed, mtl, - bytestring, text, xml-conduit, network-uri, regex-tdfa, containers + bytestring, text, xml-conduit, network-uri, regex-tdfa, containers, + filepath -- Directories containing source files. hs-source-dirs: src @@ -97,7 +98,7 @@ executable bureaucromancy -- Other library packages from which modules are imported. build-depends: base ^>=4.16.4.0, - bureaucromancy, warp, wai, http-types, text, xml-conduit + bureaucromancy, warp, wai, http-types, text, html-conduit -- Directories containing source files. hs-source-dirs: app diff --git a/src/Text/HTML/Form.hs b/src/Text/HTML/Form.hs index 4db6dc1..4eb0bd2 100644 --- a/src/Text/HTML/Form.hs +++ b/src/Text/HTML/Form.hs @@ -9,7 +9,6 @@ import Text.XML.Cursor import Text.XML (Document, Name(..), Node(..)) import Data.Maybe (fromMaybe, listToMaybe, mapMaybe) -import Data.Char (isDigit) import Data.List (singleton) import Text.Read (readMaybe) import Data.Function (on) @@ -33,7 +32,7 @@ data Form = Form { data Input = Input { -- Core attributes - label :: Node, + label :: Text, description :: Node, inputType :: Text, dirname :: Text, @@ -144,11 +143,9 @@ queryInputs form = (allInputs >=> inForm) form nestedInForm x = listToMaybe ((ancestor >=> laxElement "form") x) == Just form parseInput :: Cursor -> Maybe Input parseInput el | _:_ <- laxElement "input" el = Just Input { - label = fromMaybe (mkEl $ attr "name" el "") $ fmap node $ - listToMaybe ((ancestor >=> laxElement "label") el) *> - elByAttr "for" (attr "id" el ""), - description = fromMaybe (mkEl "") $ fmap node $ - elByID $ attr "aria-describedby" el "", + label = fromMaybe (attr "name" el "") $ fmap text label', + description = fromMaybe (mkEl $ attr "title" el "") $ fmap node $ + elByID (attr "aria-describedby" el "") `orElse` label', inputType = attr "type" el "text", value = attr "value" el "", inputAutocomplete = attr "autocomplete" el "on", @@ -192,12 +189,10 @@ parseInput el | _:_ <- laxElement "input" el = Just Input { } | _:_ <- laxElement "textarea" el = Just Input { inputType = "