~alcinnz/haskell-stylist

e6802ca7550a08a38890dc0c976f78e5aab10589 — Adrian Cochrane 4 years ago 62083a7
Add support for range types to media query interpretor.

This allows browsers to be more vague when fetching @imports.
1 files changed, 15 insertions(+), 1 deletions(-)

M src/Data/CSS/Preprocessor/Conditions/Expr.hs
M src/Data/CSS/Preprocessor/Conditions/Expr.hs => src/Data/CSS/Preprocessor/Conditions/Expr.hs +15 -1
@@ 72,7 72,7 @@ pushOp toks op b ops = parse' toks ((op, b):ops)
---- Shunting Yard Evaluator
--------
-- | Dynamic types for evaluating media queries.
data Datum = B Bool | N Float | Ratio Float Float deriving Eq
data Datum = B Bool | N Float | R {minv :: Float, maxv :: Float} | Ratio Float Float deriving Eq

-- | Evaluates a media query with the given functions for evaluating vars & tokens.
eval :: (Text -> Datum) -> (Token -> Datum) -> Expr -> Bool


@@ 89,10 89,24 @@ eval' stack v t (Tok (Ident name):ops) = eval' (v name:stack) v t ops
-- TODO: How should I handle ratios?
eval' (N y:N x:stack) v t (MkRatio:ops) = eval' (Ratio x y:stack) v t ops
eval' (N y:N x:stack) v t (Less:ops) = eval' (B (x < y):stack) v t ops
eval' (R _ y:N x:stack) v t (Less:ops) = eval' (B (x < y):stack) v t ops
eval' (N y: R x _:stack) v t (Less:ops) = eval' (B (x < y):stack) v t ops
eval' (R _ y:R x _:stack) v t (Less:ops) = eval' (B (x < y):stack) v t ops
eval' (N y:N x:stack) v t (LessEq:ops) = eval' (B (x <= y):stack) v t ops
eval' (R _ y:N x:stack) v t (LessEq:ops) = eval' (B (x <= y):stack) v t ops
eval' (N y: R x _:stack) v t (LessEq:ops) = eval' (B (x <= y):stack) v t ops
eval' (R _ y:R x _:stack) v t (LessEq:ops) = eval' (B (x <= y):stack) v t ops
eval' (R yMin yMax:R xMin xMax:stack) v t (Equal:ops) =
    eval' (B (xMax >= yMin && xMin <= yMax) : stack) v t ops
eval' (y:x:stack) v t (Equal:ops) = eval' (B (x == y):stack) v t ops
eval' (N y:N x:stack) v t (Greater:ops) = eval' (B (x > y):stack) v t ops
eval' (R y _:N x:stack) v t (Greater:ops) = eval' (B (x > y):stack) v t ops
eval' (N y:R _ x:stack) v t (Greater:ops) = eval' (B (x > y):stack) v t ops
eval' (R y _:R _ x:stack) v t (Greater:ops) = eval' (B (x > y):stack) v t ops
eval' (N y:N x:stack) v t (GreaterEq:ops) = eval' (B (x >= y):stack) v t ops
eval' (R y _:N x:stack) v t (GreaterEq:ops) = eval' (B (x >= y):stack) v t ops
eval' (N y:R _ x:stack) v t (GreaterEq:ops) = eval' (B (x >= y):stack) v t ops
eval' (R y _:R _ x:stack) v t (GreaterEq:ops) = eval' (B (x >= y):stack) v t ops
eval' (B ret:_) _ _ [] = ret
eval' [] _ _ [] = True -- Special case
eval' _ _ _ _ = False -- Error handling fallback.