@@ -2,17 +2,17 @@ module CodeRay
2
2
module Scanners
3
3
4
4
class CSS < Scanner
5
-
5
+
6
6
register_for :css
7
-
7
+
8
8
KINDS_NOT_LOC = [
9
9
:comment ,
10
10
:class , :pseudo_class , :type ,
11
11
:constant , :directive ,
12
12
:key , :value , :operator , :color , :float , :string ,
13
13
:error , :important ,
14
14
] # :nodoc:
15
-
15
+
16
16
module RE # :nodoc:
17
17
Hex = /[0-9a-fA-F]/
18
18
Unicode = /\\ #{ Hex } {1,6}(?:\r \n |\s )?/ # differs from standard because it allows uppercase hex too
@@ -23,47 +23,47 @@ module RE # :nodoc:
23
23
String1 = /"(?:[^\n \r \f \\ "]|\\ #{ NL } |#{ Escape } )*"?/ # TODO: buggy regexp
24
24
String2 = /'(?:[^\n \r \f \\ ']|\\ #{ NL } |#{ Escape } )*'?/ # TODO: buggy regexp
25
25
String = /#{ String1 } |#{ String2 } /
26
-
26
+
27
27
HexColor = /#(?:#{ Hex } {6}|#{ Hex } {3})/
28
28
Color = /#{ HexColor } /
29
-
29
+
30
30
Num = /-?(?:[0-9]+|[0-9]*\. [0-9]+)/
31
31
Name = /#{ NMChar } +/
32
32
Ident = /-?#{ NMStart } #{ NMChar } */
33
33
AtKeyword = /@#{ Ident } /
34
34
Percentage = /#{ Num } %/
35
-
35
+
36
36
reldimensions = %w[ em ex px ]
37
37
absdimensions = %w[ in cm mm pt pc ]
38
38
Unit = Regexp . union ( *( reldimensions + absdimensions + %w[ s ] ) )
39
-
39
+
40
40
Dimension = /#{ Num } #{ Unit } /
41
-
41
+
42
42
Comment = %r! /\* (?: .*? \* / | .* ) !mx
43
43
Function = /(?:url|alpha|attr|counters?)\( (?:[^)\n \r \f ]|\\ \) )*\) ?/
44
-
44
+
45
45
Id = /##{ Name } /
46
46
Class = /\. #{ Name } /
47
47
PseudoClass = /:#{ Name } /
48
48
AttributeSelector = /\[ [^\] ]*\] ?/
49
49
end
50
-
50
+
51
51
protected
52
-
52
+
53
53
def setup
54
54
@state = :initial
55
55
@value_expected = nil
56
56
end
57
-
57
+
58
58
def scan_tokens encoder , options
59
59
states = Array ( options [ :state ] || @state )
60
60
value_expected = @value_expected
61
-
61
+
62
62
until eos?
63
-
63
+
64
64
if match = scan ( /\s +/ )
65
65
encoder . text_token match , :space
66
-
66
+
67
67
elsif case states . last
68
68
when :initial , :media
69
69
if match = scan ( /(?>#{ RE ::Ident } )(?!\( )|\* /ox )
@@ -89,7 +89,7 @@ def scan_tokens encoder, options
89
89
states . push :media_before_name
90
90
next
91
91
end
92
-
92
+
93
93
when :block
94
94
if match = scan ( /(?>#{ RE ::Ident } )(?!\( )/ox )
95
95
if value_expected
@@ -99,50 +99,50 @@ def scan_tokens encoder, options
99
99
end
100
100
next
101
101
end
102
-
102
+
103
103
when :media_before_name
104
104
if match = scan ( RE ::Ident )
105
105
encoder . text_token match , :type
106
106
states [ -1 ] = :media_after_name
107
107
next
108
108
end
109
-
109
+
110
110
when :media_after_name
111
111
if match = scan ( /\{ / )
112
112
encoder . text_token match , :operator
113
113
states [ -1 ] = :media
114
114
next
115
115
end
116
-
116
+
117
117
else
118
118
#:nocov:
119
119
raise_inspect 'Unknown state' , encoder
120
120
#:nocov:
121
-
121
+
122
122
end
123
-
123
+
124
124
elsif match = scan ( /\/ \* (?:.*?\* \/ |\z )/m )
125
125
encoder . text_token match , :comment
126
-
126
+
127
127
elsif match = scan ( /\{ / )
128
128
value_expected = false
129
129
encoder . text_token match , :operator
130
130
states . push :block
131
-
131
+
132
132
elsif match = scan ( /\} / )
133
133
value_expected = false
134
134
encoder . text_token match , :operator
135
135
if states . last == :block || states . last == :media
136
136
states . pop
137
137
end
138
-
138
+
139
139
elsif match = scan ( /#{ RE ::String } /o )
140
140
encoder . begin_group :string
141
141
encoder . text_token match [ 0 , 1 ] , :delimiter
142
142
encoder . text_token match [ 1 ..-2 ] , :content if match . size > 2
143
143
encoder . text_token match [ -1 , 1 ] , :delimiter if match . size >= 2
144
144
encoder . end_group :string
145
-
145
+
146
146
elsif match = scan ( /#{ RE ::Function } /o )
147
147
encoder . begin_group :function
148
148
start = match [ /^\w +\( / ]
@@ -154,46 +154,46 @@ def scan_tokens encoder, options
154
154
encoder . text_token match [ start . size ..-1 ] , :content
155
155
end
156
156
encoder . end_group :function
157
-
157
+
158
158
elsif match = scan ( /(?: #{ RE ::Dimension } | #{ RE ::Percentage } | #{ RE ::Num } )/ox )
159
159
encoder . text_token match , :float
160
-
160
+
161
161
elsif match = scan ( /#{ RE ::Color } /o )
162
162
encoder . text_token match , :color
163
-
163
+
164
164
elsif match = scan ( /! *important/ )
165
165
encoder . text_token match , :important
166
-
166
+
167
167
elsif match = scan ( /(?:rgb|hsl)a?\( [^()\n ]*\) ?/ )
168
168
encoder . text_token match , :color
169
-
169
+
170
170
elsif match = scan ( RE ::AtKeyword )
171
171
encoder . text_token match , :directive
172
-
172
+
173
173
elsif match = scan ( / [+>:;,.=()\/ ] /x )
174
174
if match == ':'
175
175
value_expected = true
176
176
elsif match == ';'
177
177
value_expected = false
178
178
end
179
179
encoder . text_token match , :operator
180
-
180
+
181
181
else
182
182
encoder . text_token getch , :error
183
-
183
+
184
184
end
185
-
185
+
186
186
end
187
-
187
+
188
188
if options [ :keep_state ]
189
189
@state = states
190
190
@value_expected = value_expected
191
191
end
192
-
192
+
193
193
encoder
194
194
end
195
-
195
+
196
196
end
197
-
197
+
198
198
end
199
199
end
0 commit comments