Javascript in Head (#380)

This commit is contained in:
Michael Snoyman 2012-07-04 17:53:24 +03:00
parent dbf1961613
commit 7c4d6b5e47
2 changed files with 13 additions and 1 deletions

View File

@ -153,7 +153,7 @@ instance render ~ RY master => ToWidgetHead sub master (render -> Html) where
instance render ~ RY master => ToWidgetHead sub master (render -> Css) where
toWidgetHead = toWidget
instance render ~ RY master => ToWidgetHead sub master (render -> Javascript) where
toWidgetHead = toWidget
toWidgetHead j = toWidgetHead $ \r -> H.script $ preEscapedLazyText $ renderJavascriptUrl r j
instance ToWidgetHead sub master Html where
toWidgetHead = toWidgetHead . const

View File

@ -26,6 +26,7 @@ mkYesod "Y" [parseRoutes|
/whamlet WhamletR GET
/towidget TowidgetR GET
/auto AutoR GET
/jshead JSHeadR GET
|]
instance Yesod Y where
@ -82,12 +83,16 @@ $newline never
where
someHtml = [shamlet|somehtml|]
getJSHeadR :: Handler RepHtml
getJSHeadR = defaultLayout $ toWidgetHead [julius|alert("hello");|]
widgetTest :: Spec
widgetTest = describe "Test.Widget"
[ it "addJuliusBody" case_addJuliusBody
, it "whamlet" case_whamlet
, it "two letter lang codes" case_two_letter_lang
, it "automatically applies toWidget" case_auto
, it "toWidgetHead puts JS in head" case_jshead
]
runner :: Session () -> IO ()
@ -121,3 +126,10 @@ case_auto = runner $ do
, requestHeaders = [("Accept-Language", "es")]
}
assertBody "<!DOCTYPE html>\n<html><head><title></title></head><body>somehtml</body></html>" res
case_jshead :: IO ()
case_jshead = runner $ do
res <- request defaultRequest
{ pathInfo = ["jshead"]
}
assertBody "<!DOCTYPE html>\n<html><head><title></title><script>alert(\"hello\");</script></head><body></body></html>" res