Minor yesod-test fixes

This commit is contained in:
Michael Snoyman 2012-08-03 11:31:35 +03:00
parent f80a810df9
commit bc8e4b0588
2 changed files with 8 additions and 13 deletions

View File

@ -89,6 +89,7 @@ import Data.Monoid (mappend)
import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Encoding (encodeUtf8, decodeUtf8)
import Text.XML.Cursor hiding (element)
import qualified Text.XML.Cursor as C
import qualified Text.HTML.DOM as HD
-- | The state used in 'describe' to build a list of specs
@ -165,8 +166,8 @@ withResponse f = maybe err f =<< fmap readResponse ST.get
-- | Use HXT to parse a value from an html tag.
-- Check for usage examples in this module's source.
parseHTML :: Html -> (Cursor -> [a]) -> [a]
parseHTML html p = p $ fromDocument $ HD.parseLBS html
parseHTML :: Html -> Cursor
parseHTML html = fromDocument $ HD.parseLBS html
-- | Query the last response using css selectors, returns a list of matched fragments
htmlQuery :: HoldsResponse a => Query -> ST.StateT a IO [Html]
@ -285,10 +286,9 @@ nameFromLabel :: T.Text -> RequestBuilder T.Text
nameFromLabel label = withResponse $ \ res -> do
let
body = simpleBody res
escaped = escapeHtmlEntities label
mfor = parseHTML body $ \c -> c
$// attributeIs "name" "label"
>=> contentContains escaped
mfor = parseHTML body
$// C.element "label"
>=> contentContains label
>=> attribute "for"
contentContains x c
@ -297,7 +297,7 @@ nameFromLabel label = withResponse $ \ res -> do
case mfor of
for:[] -> do
let mname = parseHTML body $ \c -> c
let mname = parseHTML body
$// attributeIs "id" for
>=> attribute "name"
case mname of
@ -346,7 +346,7 @@ addNonce_ scope = do
matches <- htmlQuery $ scope `mappend` "input[name=_token][type=hidden][value]"
case matches of
[] -> failure $ "No nonce found in the current page"
element:[] -> byName "_token" $ head $ parseHTML element $ attribute "value"
element:[] -> byName "_token" $ head $ attribute "value" $ parseHTML element
_ -> failure $ "More than one nonce found in the page"
-- | For responses that display a single form, just lookup the only nonce available.

View File

@ -46,13 +46,8 @@ import Text.XML
import Text.XML.Cursor
import qualified Data.ByteString.Lazy as L
import qualified Text.HTML.DOM as HD
#if MIN_VERSION_blaze_html(0, 5, 0)
import Text.Blaze.Html (toHtml)
import Text.Blaze.Html.Renderer.String (renderHtml)
#else
import Text.Blaze (toHtml)
import Text.Blaze.Renderer.String (renderHtml)
#endif
type Query = T.Text
type Html = L.ByteString