packages feed

hamlet 1.1.6.3 → 1.1.6.4

raw patch · 5 files changed

+38/−4 lines, 5 files

Files

Text/Hamlet.hs view
@@ -205,7 +205,7 @@   where     go :: (Deref, [Doc]) -> Q Exp     go (d, docs) = do-        let d' = derefToExp scope d+        let d' = derefToExp ((specialOrIdent, VarE 'or):scope) d         docs' <- docsToExp env hr scope docs         return $ TupE [d', docs'] docToExp env hr scope (DocCase deref cases) = do
Text/Hamlet/Parse.hs view
@@ -12,6 +12,7 @@     , CloseStyle (..)     , Binding (..)     , NewlineStyle (..)+    , specialOrIdent     )     where @@ -24,7 +25,7 @@ import Text.ParserCombinators.Parsec hiding (Line) import Data.Set (Set) import qualified Data.Set as Set-import Data.Maybe (mapMaybe, fromMaybe)+import Data.Maybe (mapMaybe, fromMaybe, isNothing)  data Result v = Error String | Ok v     deriving (Show, Eq, Read, Data, Typeable)@@ -464,7 +465,7 @@     let attrs' =             case clazzes of               [] -> map attrFix noclass-              _ -> (Nothing, "class", map (second Just) clazzes)+              _ -> (testIncludeClazzes clazzes, "class", map (second Just) clazzes)                        : map attrFix noclass     let closeStyle =             if not (null content) || not (null inside)@@ -660,3 +661,24 @@  spaceTabs :: Parser String spaceTabs = many $ oneOf " \t"++-- | When using conditional classes, it will often be a single class, e.g.:+--+-- > <div :isHome:.homepage>+--+-- If isHome is False, we do not want any class attribute to be present.+-- However, due to combining multiple classes together, the most obvious+-- implementation would produce a class="". The purpose of this function is to+-- work around that. It does so by checking if all the classes on this tag are+-- optional. If so, it will only include the class attribute if at least one+-- conditional is true.+testIncludeClazzes :: [(Maybe Deref, [Content])] -> Maybe Deref+testIncludeClazzes cs+    | any (isNothing . fst) cs = Nothing+    | otherwise = Just $ DerefBranch (DerefIdent specialOrIdent) $ DerefList $ mapMaybe fst cs++-- | This funny hack is to allow us to refer to the 'or' function without+-- requiring the user to have it in scope. See how this function is used in+-- Text.Hamlet.+specialOrIdent :: Ident+specialOrIdent = Ident "__or__hamlet__special"
Text/Hamlet/RT.hs view
@@ -101,6 +101,11 @@         els' <- maybe (return []) (mapM convert) els         return $ SDCond conds' els'       where+        -- | See the comments in Text.Hamlet.Parse.testIncludeClazzes. The conditional+        -- added there doesn't work for runtime Hamlet, so we remove it here.+        go (DerefBranch (DerefIdent x) _, docs') | x == specialOrIdent = do+            docs'' <- mapM convert docs'+            return (["True"], docs'')         go (deref, docs') = do             deref' <- flattenDeref' x deref             docs'' <- mapM convert docs'@@ -176,6 +181,7 @@             => [String] -> [String] -> HamletMap url -> m (HamletData url)     lookup' orig k m =         case lookup k m of+            Nothing | k == ["True"] -> return $ HDBool True             Nothing -> fa $ showName orig ++ ": not found"             Just x -> return x 
hamlet.cabal view
@@ -1,5 +1,5 @@ name:            hamlet-version:         1.1.6.3+version:         1.1.6.4 license:         MIT license-file:    LICENSE author:          Michael Snoyman <michael@snoyman.com>
test/HamletTest.hs view
@@ -439,6 +439,12 @@     $of _
         <p>Many
 |]
+    it "optional and missing classes" $
+        helper "<i>foo</i>\n" [hamlet|<i :False:.not-present>foo|]
+    it "multiple optional and missing classes" $
+        helper "<i>foo</i>\n" [hamlet|<i :False:.not-present :False:.also-not-here>foo|]
+    it "optional and present classes" $
+        helper "<i class=\"present\">foo</i>\n" [hamlet|<i :False:.not-present :True:.present>foo|]
 
 data Pair = Pair String Int