Merge pull request #1622 from jezen/master

Add jsAttributesHandler
This commit is contained in:
Michael Snoyman 2019-08-26 16:25:46 +03:00 committed by GitHub
commit 50a3242507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View File

@ -1,5 +1,10 @@
# ChangeLog for yesod-core # ChangeLog for yesod-core
## 1.6.16
* Add `jsAttributesHandler` to run arbitrary Handler code before building the
attributes map for the script tag generated by `widgetFile` [#1622](https://github.com/yesodweb/yesod/pull/1622)
## 1.6.15 ## 1.6.15
* Move `redirectToPost` JavaScript form submission from HTML element to * Move `redirectToPost` JavaScript form submission from HTML element to

View File

@ -249,6 +249,16 @@ class RenderRoute site => Yesod site where
jsAttributes :: site -> [(Text, Text)] jsAttributes :: site -> [(Text, Text)]
jsAttributes _ = [] jsAttributes _ = []
-- | Same as @jsAttributes@ but allows you to run arbitrary Handler code
--
-- This is useful if you need to add a randomised nonce value to the script
-- tag generated by @widgetFile@. If this function is overridden then
-- @jsAttributes@ is ignored.
--
-- @since 1.6.16
jsAttributesHandler :: HandlerFor site [(Text, Text)]
jsAttributesHandler = jsAttributes <$> getYesod
-- | Create a session backend. Returning 'Nothing' disables -- | Create a session backend. Returning 'Nothing' disables
-- sessions. If you'd like to change the way that the session -- sessions. If you'd like to change the way that the session
-- cookies are created, take a look at -- cookies are created, take a look at
@ -520,7 +530,9 @@ defaultCsrfMiddleware = defaultCsrfSetCookieMiddleware . defaultCsrfCheckMiddlew
widgetToPageContent :: Yesod site widgetToPageContent :: Yesod site
=> WidgetFor site () => WidgetFor site ()
-> HandlerFor site (PageContent (Route site)) -> HandlerFor site (PageContent (Route site))
widgetToPageContent w = HandlerFor $ \hd -> do widgetToPageContent w = do
jsAttrs <- jsAttributesHandler
HandlerFor $ \hd -> do
master <- unHandlerFor getYesod hd master <- unHandlerFor getYesod hd
ref <- newIORef mempty ref <- newIORef mempty
unWidgetFor w WidgetData unWidgetFor w WidgetData
@ -564,7 +576,7 @@ widgetToPageContent w = HandlerFor $ \hd -> do
^{mkScriptTag s} ^{mkScriptTag s}
$maybe j <- jscript $maybe j <- jscript
$maybe s <- jsLoc $maybe s <- jsLoc
<script src="#{s}" *{jsAttributes master}> <script src="#{s}" *{jsAttrs}>
$nothing $nothing
<script>^{jelper j} <script>^{jelper j}
|] |]
@ -627,7 +639,7 @@ defaultErrorHandler NotFound = selectRep $ do
let path' = TE.decodeUtf8With TEE.lenientDecode $ W.rawPathInfo r let path' = TE.decodeUtf8With TEE.lenientDecode $ W.rawPathInfo r
defaultMessageWidget "Not Found" [hamlet|<p>#{path'}|] defaultMessageWidget "Not Found" [hamlet|<p>#{path'}|]
provideRep $ return $ object ["message" .= ("Not Found" :: Text)] provideRep $ return $ object ["message" .= ("Not Found" :: Text)]
provideRep $ return $ ("Not Found" :: Text) provideRep $ return ("Not Found" :: Text)
-- For API requests. -- For API requests.
-- For a user with a browser, -- For a user with a browser,
@ -651,7 +663,7 @@ defaultErrorHandler NotAuthenticated = selectRep $ do
let apair u = ["authentication_url" .= rend u] let apair u = ["authentication_url" .= rend u]
content = maybe [] apair (authRoute site) content = maybe [] apair (authRoute site)
return $ object $ ("message" .= ("Not logged in"::Text)):content return $ object $ ("message" .= ("Not logged in"::Text)):content
provideRep $ return $ ("Not logged in" :: Text) provideRep $ return ("Not logged in" :: Text)
defaultErrorHandler (PermissionDenied msg) = selectRep $ do defaultErrorHandler (PermissionDenied msg) = selectRep $ do
provideRep $ defaultLayout $ defaultMessageWidget provideRep $ defaultLayout $ defaultMessageWidget
@ -670,7 +682,7 @@ defaultErrorHandler (InvalidArgs ia) = selectRep $ do
<li>#{msg} <li>#{msg}
|] |]
provideRep $ return $ object ["message" .= ("Invalid Arguments" :: Text), "errors" .= ia] provideRep $ return $ object ["message" .= ("Invalid Arguments" :: Text), "errors" .= ia]
provideRep $ return $ ("Invalid Arguments: " <> T.intercalate " " ia) provideRep $ return ("Invalid Arguments: " <> T.intercalate " " ia)
defaultErrorHandler (InternalError e) = do defaultErrorHandler (InternalError e) = do
$logErrorS "yesod-core" e $logErrorS "yesod-core" e

View File

@ -1,5 +1,5 @@
name: yesod-core name: yesod-core
version: 1.6.15 version: 1.6.16
license: MIT license: MIT
license-file: LICENSE license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com> author: Michael Snoyman <michael@snoyman.com>