css-native 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/css-native.rb +8 -0
- data/lib/css-native/rule.rb +49 -66
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae9462bf9fa2a0a6cfa29fa16ae8861b6659b64dbb713ad470c566e54509650d
|
4
|
+
data.tar.gz: 32a3e75aa1627e93343c54ecd0ea278845d9311bfe0c9cf1bc2388dbcd82c8c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b52d3b1168e8efba6901b0debe41460a7baf46d0e0e10c3562fc8d730b491c455d0e6a3c91e0c545c5285b1c53b36e8d7013debe07f072e57a70f8e4a70f2ecb
|
7
|
+
data.tar.gz: 06af858d36aab5b99983c3d13cb2db99b970462714813f3171221f9192300ebae550c4c178df21c14235008ddf6c3ee76a4c910706e43f8ff3df556c45c50141
|
data/lib/css-native.rb
CHANGED
@@ -9,6 +9,10 @@ class CSSNative
|
|
9
9
|
sheet
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.format_element(name)
|
13
|
+
name.to_s
|
14
|
+
end
|
15
|
+
|
12
16
|
def self.format_class(name)
|
13
17
|
".#{name}"
|
14
18
|
end
|
@@ -37,6 +41,10 @@ class CSSNative
|
|
37
41
|
@rules = []
|
38
42
|
end
|
39
43
|
|
44
|
+
def element(name)
|
45
|
+
Rule.new(self).with_element(name)
|
46
|
+
end
|
47
|
+
|
40
48
|
alias_method :klass, :class
|
41
49
|
def class(name = nil)
|
42
50
|
if name.nil?
|
data/lib/css-native/rule.rb
CHANGED
@@ -5,59 +5,42 @@ class CSSNative
|
|
5
5
|
@previous = previous
|
6
6
|
@parent = parent
|
7
7
|
@selector = name.to_s
|
8
|
-
@body = {}
|
9
8
|
@stylesheet = Stylesheet.new(self)
|
10
9
|
end
|
11
10
|
|
12
11
|
# basic selectors
|
12
|
+
def with_element(name, &block)
|
13
|
+
raise GrammarError.new(name) if previous_selector?
|
14
|
+
@previous = :element
|
15
|
+
@selector += CSSNative::format_element(name)
|
16
|
+
chain(&block)
|
17
|
+
end
|
18
|
+
|
13
19
|
def with_class(name, &block)
|
14
|
-
s = CSSNative::format_class(name)
|
15
20
|
@previous = :class
|
16
|
-
@selector +=
|
17
|
-
|
18
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
19
|
-
@parent.rules << to_s
|
20
|
-
else
|
21
|
-
self
|
22
|
-
end
|
21
|
+
@selector += CSSNative::format_class(name)
|
22
|
+
chain(&block)
|
23
23
|
end
|
24
24
|
|
25
25
|
def with_id(name, &block)
|
26
|
-
s = CSSNative::format_id(name)
|
27
26
|
@previous = :id
|
28
|
-
@selector +=
|
29
|
-
|
30
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
31
|
-
@parent.rules << to_s
|
32
|
-
else
|
33
|
-
self
|
34
|
-
end
|
27
|
+
@selector += CSSNative::format_id(name)
|
28
|
+
chain(&block)
|
35
29
|
end
|
36
30
|
|
37
31
|
def with_attribute(name, operation = :none, value = nil, case_sensitive: true, &block)
|
38
|
-
|
39
|
-
@
|
40
|
-
|
41
|
-
if block_given?
|
42
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
43
|
-
@parent.rules << to_s
|
44
|
-
else
|
45
|
-
self
|
46
|
-
end
|
32
|
+
@previous = :attribute
|
33
|
+
@selector += CSSNative::format_attribute(name, operation, value, case_sensitive: case_sensitive)
|
34
|
+
chain(&block)
|
47
35
|
end
|
48
36
|
|
49
37
|
def with(name, *args, type: :element, &block)
|
50
38
|
case type
|
51
39
|
when :element
|
52
|
-
|
53
|
-
|
54
|
-
@previous = (name == :all ? :all : :element)
|
55
|
-
@selector += name
|
56
|
-
if block_given?
|
57
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
58
|
-
@parent.rules << to_s
|
40
|
+
if name == :all
|
41
|
+
all(&block)
|
59
42
|
else
|
60
|
-
|
43
|
+
with_element(name, &block)
|
61
44
|
end
|
62
45
|
when :class
|
63
46
|
with_class(name, &block)
|
@@ -75,12 +58,7 @@ class CSSNative
|
|
75
58
|
raise GrammarError.new("*") if previous_selector?
|
76
59
|
@previous = :all
|
77
60
|
@selector += "*"
|
78
|
-
|
79
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
80
|
-
@parent.rules << to_s
|
81
|
-
else
|
82
|
-
self
|
83
|
-
end
|
61
|
+
chain(&block)
|
84
62
|
end
|
85
63
|
|
86
64
|
# Grouping selectors
|
@@ -107,7 +85,7 @@ class CSSNative
|
|
107
85
|
|
108
86
|
def combinator(c)
|
109
87
|
m = c.to_sym
|
110
|
-
raise GrammarError.new(COMBINATORS[]) if previous_combinator?
|
88
|
+
raise GrammarError.new(COMBINATORS[m]) if previous_combinator?
|
111
89
|
@previous = :combinator
|
112
90
|
@selector += COMBINATORS[m]
|
113
91
|
self
|
@@ -179,19 +157,17 @@ class CSSNative
|
|
179
157
|
pc = name.to_s.gsub("_", "-")
|
180
158
|
m = name.to_s.gsub("-", "_").to_sym
|
181
159
|
raise PseudoClassError.new(method: pc) unless PSEUDO_CLASSES.key?(m)
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
160
|
+
|
161
|
+
args.each? do |arg|
|
162
|
+
unless matches_arg_defs?(PSEUDO_CLASSES[m], arg.to_s)
|
163
|
+
raise PseudoClassError.new(argument: arg, method: pc)
|
164
|
+
end
|
186
165
|
end
|
187
|
-
|
166
|
+
|
167
|
+
@previous = :pseudo_class
|
168
|
+
@selector += ":#{pc}"
|
188
169
|
@selector += "(#{args.join(" ")})" unless args.empty?
|
189
|
-
|
190
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
191
|
-
@parent.rules << to_s
|
192
|
-
else
|
193
|
-
self
|
194
|
-
end
|
170
|
+
chain(&block)
|
195
171
|
end
|
196
172
|
|
197
173
|
# pseudo-elements
|
@@ -219,19 +195,17 @@ class CSSNative
|
|
219
195
|
pe = name.to_s.gsub("_", "-")
|
220
196
|
m = name.to_s.gsub("-", "_").to_sym
|
221
197
|
raise PseudoElementError.new(method: pe) unless PSEUDO_ELEMENTS.key?(m)
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
198
|
+
|
199
|
+
args.each? do |arg|
|
200
|
+
unless matches_arg_defs?(PSEUDO_ELEMENTS[m], arg.to_s)
|
201
|
+
raise PseudoElementError.new(argument: arg, method: pe)
|
202
|
+
end
|
226
203
|
end
|
227
|
-
|
204
|
+
|
205
|
+
@previous = :pseudo_element
|
206
|
+
@selector += "::#{pe}"
|
228
207
|
@selector += "(#{args.join(" ")})" unless args.empty?
|
229
|
-
|
230
|
-
@stylesheet.instance_exec(@stylesheet, &block)
|
231
|
-
@parent.rules << to_s
|
232
|
-
else
|
233
|
-
self
|
234
|
-
end
|
208
|
+
chain(&block)
|
235
209
|
end
|
236
210
|
|
237
211
|
def method_missing(m, *args, &block)
|
@@ -271,13 +245,22 @@ class CSSNative
|
|
271
245
|
previous?(:element, :class, :id, :attribute, :all, :pseudo_class, :pseudo_element)
|
272
246
|
end
|
273
247
|
|
248
|
+
# If a block is given, executes that as a stylesheet. Otherwise, returns self to
|
249
|
+
# facilitate chaining
|
250
|
+
def chain(&block)
|
251
|
+
if block_given?
|
252
|
+
@stylesheet.instance_exec(@stylesheet, &block)
|
253
|
+
@parent.rules << to_s
|
254
|
+
else
|
255
|
+
self
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
274
259
|
def matches_arg_defs?(defs, arg)
|
275
260
|
if defs.nil?
|
276
261
|
arg.nil?
|
277
|
-
elsif defs.empty?
|
278
|
-
true
|
279
262
|
else
|
280
|
-
defs.any? {|d| d === arg}
|
263
|
+
defs.empty? || defs.any? {|d| d === arg}
|
281
264
|
end
|
282
265
|
end
|
283
266
|
end
|