~alcinnz/haskell-stylist

ref: df2761aad16333724b3fe15635d4d99e1663f3c5 haskell-stylist/src/Stylish/Style/Interpret.hs -rw-r--r-- 1.3 KiB
df2761aa — Adrian Cochrane Compile 'complex' selectors (ones with multiple tests but no tree traversal). 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{-# LANGUAGE OverloadedStrings #-}
module Stylish.Style.Interpret(
        compile, SelectorFunc(..)
    ) where

import Stylish.Parse.Selector

import Data.Text.Internal (Text(..))
import Data.List

data SelectorFunc = TestTag Text SelectorFunc | TestAttrs AttrsFunc SelectorFunc | Matched
data AttrsFunc = TestAttr Text PropertyTest AttrsFunc | MatchedAttrs

compile :: Selector -> SelectorFunc
compile (Element selector) = compileInner selector

compileInner selector = compileInner' $ lowerInner selector
compileInner' (Just tag, attributes) = TestTag tag $ TestAttrs (compileAttrs $ sortAttrs attributes) Matched
compileInner' (Nothing, attributes) = TestAttrs (compileAttrs $ sortAttrs attributes) Matched
compileAttrs ((name, test):attrs) = TestAttr name test $ compileAttrs attrs
compileAttrs [] = MatchedAttrs

lowerInner :: [SimpleSelector] -> (Maybe Text, [(Text, PropertyTest)])
lowerInner (Tag tag:selector) = (Just tag, snd $ lowerInner selector)
lowerInner (Id id:s) = (tag, ("id", Include id):tail) where (tag, tail) = lowerInner s
lowerInner (Class c:s) = (tag, ("class", Include c):tail) where (tag, tail) = lowerInner s
lowerInner (Property name test:s) = (tag, (name, test):tail) where (tag, tail) = lowerInner s
lowerInner [] = (Nothing, [])

sortAttrs = sortBy compareAttrs where compareAttrs x y = fst x `compare` fst y