Skip to content

Commit f07c1cb

Browse files
committed
Add more selector combinators
1 parent edc2458 commit f07c1cb

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/CSS.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import CSS.Gradient (Extend, Radial, Ramp, circle, circular, closestCorner, clos
1616
import CSS.Property (class Val, Key(..), Literal(..), Prefixed(..), Value(..), cast, noCommas, plain, quote, value, (!)) as X
1717
import CSS.Render (Inline(..), Rendered, Sheet(..), collect, collect', face, feature, frame, getInline, getSheet, imp, kframe, mediaQuery, mediaType, merger, nel, predicate, properties, putInline, putStyleSheet, query', render, renderedInline, renderedSheet, rule', rules, selector, selector', selector'', sepWith) as X
1818
import CSS.Pseudo (hover) as X
19-
import CSS.Selector (Path(..), Predicate(..), Refinement(..), Selector(..), child, deep, element, star, with, (##), (**), (|>)) as X
19+
import CSS.Selector (Path(..), Predicate(..), Refinement(..), Selector(..), star, element, (**), (|>), (|+), (##), byId, byClass, pseudo, func, attr, (@=), (^=), ($=), (*=), (~=), (|=)) as X
2020
import CSS.Size (Abs, Angle(..), Deg, Rad, Rel, Size(..), deg, em, ex, nil, pct, pt, px, rad, rem, sym, vh, vmax, vmin, vw) as X
2121
import CSS.String (class IsString, fromString) as X
2222
import CSS.Stylesheet (App(..), CSS, Feature(..), Keyframes(..), MediaQuery(..), MediaType(..), NotOrOnly(..), Rule(..), StyleM(..), fontFace, importUrl, key, keyframes, keyframesFromTo, prefixed, query, rule, runS, select, (?)) as X

src/CSS/Selector.purs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,95 @@ instance isStringSelector :: IsString Selector where
6161
"." -> Selector (Refinement [Class $ drop 1 s]) Star
6262
_ -> Selector (Refinement []) (Elem s)
6363

64+
-- | The star selector applies to all elements.
65+
-- | Maps to `*` in CSS.
6466
star :: Selector
6567
star = Selector (Refinement []) Star
6668

69+
-- | Select elements by name.
6770
element :: String -> Selector
6871
element e = Selector (Refinement []) (Elem e)
6972

73+
-- | The deep selector composer.
74+
-- | Maps to `sel1 sel2` in CSS.
7075
deep :: Selector -> Selector -> Selector
7176
deep a b = Selector (Refinement []) (Deep a b)
7277
infix 6 deep as **
7378

79+
-- | The child selector composer.
80+
-- | Maps to `sel1 > sel2` in CSS.
7481
child :: Selector -> Selector -> Selector
7582
child a b = Selector (Refinement []) (PathChild a b)
7683
infix 6 child as |>
7784

85+
-- | The adjacent selector composer.
86+
-- | Maps to `sel1 + sel2` in CSS.
87+
adjacent :: Selector -> Selector -> Selector
88+
adjacent a b = Selector (Refinement []) (Adjacent a b)
89+
infix 6 child as |+
90+
91+
-- | The filter selector composer, adds a filter to a selector.
92+
-- | Maps to something like `sel#filter`, `sel.filter` or `sel:filter` in CSS,
93+
-- | depending on the filter.
7894
with :: Selector -> Refinement -> Selector
7995
with (Selector (Refinement fs) e) (Refinement ps) = Selector (Refinement (fs <> ps)) e
8096
infix 6 with as ##
97+
98+
-- | Filter elements by id.
99+
byId :: String -> Refinement
100+
byId = Refinement <<< pure <<< Id
101+
102+
-- | Filter elements by class.
103+
byClass :: String -> Refinement
104+
byClass = Refinement <<< pure <<< Class
105+
106+
-- | Filter elements by pseudo selector or pseudo class.
107+
-- | The preferred syntax is to use `:pseudo-selector` or
108+
-- | use one of the predefined ones from `CSS.Pseudo`.
109+
pseudo :: String -> Refinement
110+
pseudo = Refinement <<< pure <<< Pseudo
111+
112+
-- | Filter elements by pseudo selector functions.
113+
-- | The preferred way is to use one of the predefined functions from `CSS.Pseudo`.
114+
func :: String -> Array String -> Refinement
115+
func f = Refinement <<< pure <<< PseudoFunc f
116+
117+
-- | Filter elements based on the presence of a certain attribute.
118+
attr :: String -> Refinement
119+
attr = Refinement <<< pure <<< Attr
120+
121+
-- | Filter elements based on the presence of a
122+
-- | certain attribute with the specified value.
123+
attrVal :: String -> String -> Refinement
124+
attrVal a = Refinement <<< pure <<< AttrVal a
125+
infix 6 with as @=
126+
127+
-- | Filter elements based on the presence of a certain attribute that
128+
-- | begins with the selected value.
129+
attrBegins :: String -> String -> Refinement
130+
attrBegins a = Refinement <<< pure <<< AttrBegins a
131+
infix 6 with as ^=
132+
133+
-- | Filter elements based on the presence of a certain attribute that
134+
-- | ends with the specified value.
135+
attrEnds :: String -> String -> Refinement
136+
attrEnds a = Refinement <<< pure <<< AttrEnds a
137+
infix 6 with as $=
138+
139+
-- | Filter elements based on the presence of a certain attribute that contains
140+
-- | the specified value as a substring.
141+
attrContains :: String -> String -> Refinement
142+
attrContains a = Refinement <<< pure <<< AttrContains a
143+
infix 6 with as *=
144+
145+
-- | Filter elements based on the presence of a certain attribute that
146+
-- | have the specified value contained in a space separated list.
147+
attrSpace :: String -> String -> Refinement
148+
attrSpace a = Refinement <<< pure <<< AttrSpace a
149+
infix 6 with as ~=
150+
151+
-- | Filter elements based on the presence of a certain attribute that
152+
-- | have the specified value contained in a hyphen separated list.
153+
attrHyph :: String -> String -> Refinement
154+
attrHyph a = Refinement <<< pure <<< AttrHyph a
155+
infix 6 with as |=

0 commit comments

Comments
 (0)