{-# LANGUAGE OverloadedStrings #-} module Text.HTML.Form.Query( renderQueryString, renderQueryString', renderQuery') where import Text.HTML.Form (Form(..), Input(..), OptionGroup(..), Option(..)) import Network.URI (escapeURIString, isUnescapedInURIComponent) import Data.List (intercalate) import Data.Text (unpack) renderQueryString :: Form -> String renderQueryString = renderQueryString' . renderQuery' renderQueryString' :: [(String, String)] -> String renderQueryString' query = intercalate "&" [ escape key ++ '=':escape val | (key, val) <- query ] renderQuery' :: Form -> [(String, String)] renderQuery' form = concatMap renderInput' $ inputs form renderInput' :: Input -> [(String, String)] renderInput' Input { checked = False, inputType = inputType' } | inputType' `elem` ["radio", "checkbox"] = [] renderInput' Input { inputType = "", inputName = k, list = opts, multiple = True } = [(unpack k, unpack $ optValue opt) | grp <- opts, opt <- subopts grp, optSelected opt] renderInput' Input { inputName = k, value = v } = [(unpack k, unpack v)] escape :: String -> String escape = escapeURIString isUnescapedInURIComponent