@@ -33,7 +33,8 @@ class HTML < Scanner
33
33
)
34
34
35
35
IN_ATTRIBUTE = WordList ::CaseIgnoring . new ( nil ) .
36
- add ( EVENT_ATTRIBUTES , :script )
36
+ add ( EVENT_ATTRIBUTES , :script ) .
37
+ add ( [ 'style' ] , :style )
37
38
38
39
ATTR_NAME = /[\w .:-]+/ # :nodoc:
39
40
TAG_END = /\/ ?>/ # :nodoc:
@@ -75,9 +76,14 @@ def setup
75
76
def scan_java_script encoder , code
76
77
if code && !code . empty?
77
78
@java_script_scanner ||= Scanners ::JavaScript . new '' , :keep_tokens => true
78
- # encoder.begin_group :inline
79
79
@java_script_scanner . tokenize code , :tokens => encoder
80
- # encoder.end_group :inline
80
+ end
81
+ end
82
+
83
+ def scan_css encoder , code , state = [ :initial ]
84
+ if code && !code . empty?
85
+ @css_scanner ||= Scanners ::CSS . new '' , :keep_tokens => true
86
+ @css_scanner . tokenize code , :tokens => encoder , :state => state
81
87
end
82
88
end
83
89
@@ -118,7 +124,7 @@ def scan_tokens encoder, options
118
124
elsif match = scan ( /<\/ [-\w .:]*>?/m )
119
125
in_tag = nil
120
126
encoder . text_token match , :tag
121
- elsif match = scan ( /<(?:(script)|[-\w .:]+)(>)?/m )
127
+ elsif match = scan ( /<(?:(script|style )|[-\w .:]+)(>)?/m )
122
128
encoder . text_token match , :tag
123
129
in_tag = self [ 1 ]
124
130
if self [ 2 ]
@@ -169,17 +175,21 @@ def scan_tokens encoder, options
169
175
encoder . text_token match , :attribute_value
170
176
state = :attribute
171
177
elsif match = scan ( /["']/ )
172
- if in_attribute == :script
173
- encoder . begin_group :inline
174
- encoder . text_token match , :inline_delimiter
178
+ if in_attribute == :script || in_attribute == :style
179
+ encoder . begin_group :string
180
+ encoder . text_token match , :delimiter
175
181
if scan ( /javascript:[ \t ]*/ )
176
182
encoder . text_token matched , :comment
177
183
end
178
184
code = scan_until ( match == '"' ? /(?="|\z )/ : /(?='|\z )/ )
179
- scan_java_script encoder , code
185
+ if in_attribute == :script
186
+ scan_java_script encoder , code
187
+ else
188
+ scan_css encoder , code , [ :block ]
189
+ end
180
190
match = scan ( /["']/ )
181
- encoder . text_token match , :inline_delimiter if match
182
- encoder . end_group :inline
191
+ encoder . text_token match , :delimiter if match
192
+ encoder . end_group :string
183
193
state = :attribute
184
194
in_attribute = nil
185
195
else
@@ -214,19 +224,23 @@ def scan_tokens encoder, options
214
224
215
225
when :in_special_tag
216
226
case in_tag
217
- when 'script'
227
+ when 'script' , 'style'
218
228
encoder . text_token match , :space if match = scan ( /[ \t ]*\n / )
219
229
if scan ( /(\s *<!--)(?:(.*?)(-->)|(.*))/m )
220
230
code = self [ 2 ] || self [ 4 ]
221
231
closing = self [ 3 ]
222
232
encoder . text_token self [ 1 ] , :comment
223
233
else
224
- code = scan_until ( /(?=(?:\n \s *)?<\/ script >)|\z / )
234
+ code = scan_until ( /(?=(?:\n \s *)?<\/ #{ in_tag } >)|\z / )
225
235
closing = false
226
236
end
227
237
unless code . empty?
228
238
encoder . begin_group :inline
229
- scan_java_script encoder , code
239
+ if in_tag == 'script'
240
+ scan_java_script encoder , code
241
+ else
242
+ scan_css encoder , code
243
+ end
230
244
encoder . end_group :inline
231
245
end
232
246
encoder . text_token closing , :comment if closing
0 commit comments