8000 Merge pull request #145 from rubychan/css-in-html · tricknotes/coderay@f0e6d45 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0e6d45

Browse files
committed
Merge pull request rubychan#145 from rubychan/css-in-html
Scan CSS inside of HTML
2 parents 9651f0b + 6a2f1a2 commit f0e6d45

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

Changes.textile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ h2. Changes in 1.1
1010
* Diff scanner: Highlight inline changes in multi-line changes [#99]
1111
* JavaScript scanner: Highlight multi-line comments in diff correctly
1212
* Ruby scanner: Accept keywords as Ruby 1.9 hash keys [#126]
13+
* HTML scanner displays style tags and attributes now [#145]
1314
* Remove double-click toggle handler from HTML table output
1415
* Fixes to CSS scanner (floats, pseudoclasses)
1516
* Fixed empty tokens and unclosed token groups in HTML, CSS, Diff, Goovy, PHP, Raydebug, Ruby, SQL, and YAML scanners [#144]

lib/coderay/scanners/html.rb

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class HTML < Scanner
3333
)
3434

3535
IN_ATTRIBUTE = WordList::CaseIgnoring.new(nil).
36-
add(EVENT_ATTRIBUTES, :script)
36+
add(EVENT_ATTRIBUTES, :script).
37+
add(['style'], :style)
3738

3839
ATTR_NAME = /[\w.:-]+/ # :nodoc:
3940
TAG_END = /\/?>/ # :nodoc:
@@ -75,9 +76,14 @@ def setup
7576
def scan_java_script encoder, code
7677
if code && !code.empty?
7778
@java_script_scanner ||= Scanners::JavaScript.new '', :keep_tokens => true
78-
# encoder.begin_group :inline
7979
@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
8187
end
8288
end
8389

@@ -118,7 +124,7 @@ def scan_tokens encoder, options
118124
elsif match = scan(/<\/[-\w.:]*>?/m)
119125
in_tag = nil
120126
encoder.text_token match, :tag
121-
elsif match = scan(/<(?:(script)|[-\w.:]+)(>)?/m)
127+
elsif match = scan(/<(?:(script|style)|[-\w.:]+)(>)?/m)
122128
encoder.text_token match, :tag
123129
in_tag = self[1]
124130
if self[2]
@@ -169,17 +175,21 @@ def scan_tokens encoder, options
169175
encoder.text_token match, :attribute_value
170176
state = :attribute
171177
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
175181
if scan(/javascript:[ \t]*/)
176182
encoder.text_token matched, :comment
177183
end
178184
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
180190
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
183193
state = :attribute
184194
in_attribute = nil
185195
else
@@ -214,19 +224,23 @@ def scan_tokens encoder, options
214224

215225
when :in_special_tag
216226
case in_tag
217-
when 'script'
227+
when 'script', 'style'
218228
encoder.text_token match, :space if match = scan(/[ \t]*\n/)
219229
if scan(/(\s*<!--)(?:(.*?)(-->)|(.*))/m)
220230
code = self[2] || self[4]
221231
closing = self[3]
222232
encoder.text_token self[1], :comment
223233
else
224-
code = scan_until(/(?=(?:\n\s*)?<\/script>)|\z/)
234+
code = scan_until(/(?=(?:\n\s*)?<\/#{in_tag}>)|\z/)
225235
closing = false
226236
end
227237
unless code.empty?
228238
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
230244
encoder.end_group :inline
231245
end
232246
encoder.text_token closing, :comment if closing

0 commit comments

Comments
 (0)
0