~alcinnz/haskell-stylist

524ca9ccb53650f86854618ae35bcddba0546fab — Adrian Cochrane 5 years ago 813c306
Compute selector specificity.
2 files changed, 22 insertions(+), 0 deletions(-)

A src/Stylish/Style/Selector/Specificity.hs
M test/Test.hs
A src/Stylish/Style/Selector/Specificity.hs => src/Stylish/Style/Selector/Specificity.hs +21 -0
@@ 0,0 1,21 @@
module Stylish.Style.Selector.Specificity(
        computeSpecificity
    ) where

import Stylish.Parse.Selector

computeSpecificity :: Selector -> (Int, Int, Int)
computeSpecificity (Element selector) = computeSpecificity' selector
computeSpecificity (Child upSel sel) = computeSpecificity upSel `add` computeSpecificity' sel
computeSpecificity (Descendant upSel sel) = computeSpecificity upSel `add` computeSpecificity' sel
computeSpecificity (Adjacent upSel sel) = computeSpecificity upSel `add` computeSpecificity' sel
computeSpecificity (Sibling upSel sel) = computeSpecificity upSel `add` computeSpecificity' sel

computeSpecificity' (Tag _:sel) = computeSpecificity' sel `add` (0, 0, 1)
computeSpecificity' (Class _:sel) = computeSpecificity' sel `add` (0, 1, 0)
computeSpecificity' (Property _ _:sel) = computeSpecificity' sel `add` (0, 1, 0)
computeSpecificity' (Id _:sel) = computeSpecificity' sel `add` (1, 0, 0)
computeSpecificity' [] = (0, 0, 0)

add :: (Int, Int, Int) -> (Int, Int, Int) -> (Int, Int, Int)
add (a, b, c) (x, y, z) = (a + x, b + y, c + z)

M test/Test.hs => test/Test.hs +1 -0
@@ 9,6 9,7 @@ import Stylish.Parse
import Stylish.Style.Selector.Index
import Stylish.Element
import Stylish.Style.Selector.Interpret
import Stylish.Style.Selector.Specificity

main = hspec spec