M src/Stylish/Style/Index.hs => src/Stylish/Style/Index.hs +3 -3
@@ 1,11 1,12 @@
{-# LANGUAGE OverloadedStrings #-}
module Stylish.Style.Index (
- StyleIndex(..),
+ StyleIndex(..), styleIndex,
rulesForElement
) where
-- TODO do performance tests to decide beside between strict/lazy.
import Data.HashMap.Strict
+import Data.List (nub)
import Stylish.Parse
import Stylish.Element
@@ 48,11 49,10 @@ selectorKey (Property prop _ : _) = Property prop Exists
----
rulesForElement :: StyleIndex -> Element -> [StyleRule]
-rulesForElement self element = Prelude.foldr (++) [] rules
+rulesForElement self element = nub $ Prelude.foldr (++) [] rules
where
get key = lookup' key index
index = indexed self
- rules :: [[StyleRule]]
rules = unindexed self : Prelude.map get (testsForElement element)
testsForElement :: Element -> [SimpleSelector]
M test/Test.hs => test/Test.hs +39 -3
@@ 7,6 7,7 @@ import Data.CSS.Syntax.Tokens
import Stylish.Parse
import Stylish.Style.Index
+import Stylish.Element
main = hspec spec
@@ 68,8 69,43 @@ spec = do
parse emptyStyle "a + b {}" `shouldBe` TrivialStyleSheet [
StyleRule (Adjacent (Element [Tag "a"]) [Tag "b"]) []
]
+ describe "Style Index" $ do
+ it "Retrieves appropriate styles" $ do
+ let index = addRule styleIndex sampleRule
+ let element = ElementNode {
+ name = "a",
+ parent = Nothing,
+ previous = Nothing,
+ attributes = [
+ Attribute "class" "external",
+ Attribute "href" "https://adrian.geek.nz/",
+ Attribute "id" "mysite"
+ ]
+ }
+ let element2 = ElementNode {
+ name = "b",
+ parent = Just element,
+ previous = Just element, -- Invalid tree, oh well.
+ attributes = []
+ }
+ rulesForElement index element `shouldBe` [sampleRule]
+ rulesForElement index element2 `shouldBe` []
+
+ let rule = StyleRule (Element [Class "external"]) [("color", [Ident "green"])]
+ let index = addRule styleIndex rule
+ rulesForElement index element `shouldBe` [rule]
+ rulesForElement index element2 `shouldBe` []
+
+ let rule = StyleRule (Element [Id "mysite"]) [("color", [Ident "green"])]
+ let index = addRule styleIndex rule
+ rulesForElement index element `shouldBe` [rule]
+ rulesForElement index element2 `shouldBe` []
+
+ let rule = StyleRule (Element [Property "href" $ Prefix "https://"]) [("color", [Ident "green"])]
+ let index = addRule styleIndex rule
+ rulesForElement index element `shouldBe` [rule]
+ rulesForElement index element2 `shouldBe` []
emptyStyle = TrivialStyleSheet []
-linkStyle = TrivialStyleSheet [
- StyleRule (Element [Tag "a"]) [("color", [Ident "green"])]
- ]
+linkStyle = TrivialStyleSheet [sampleRule]
+sampleRule = StyleRule (Element [Tag "a"]) [("color", [Ident "green"])]