8000 Scan CSS inside of HTML by korny · Pull Request #145 · rubychan/coderay · GitHub
[go: up one dir, main page]

Skip to content

Scan CSS inside of HTML #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ h2. Changes in 1.1
* Diff scanner: Highlight inline changes in multi-line changes [#99]
* JavaScript scanner: Highlight multi-line comments in diff correctly
* Ruby scanner: Accept keywords as Ruby 1.9 hash keys [#126]
* HTML scanner displays style tags and attributes now [#145]
* Remove double-click toggle handler from HTML table output
* Fixes to CSS scanner (floats, pseudoclasses)
* Fixed empty tokens and unclosed token groups in HTML, CSS, Diff, Goovy, PHP, Raydebug, Ruby, SQL, and YAML scanners [#144]
Expand Down
40 changes: 27 additions & 13 deletions lib/coderay/scanners/html.rb
Original file li 10000 ne number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class HTML < Scanner
)

IN_ATTRIBUTE = WordList::CaseIgnoring.new(nil).
add(EVENT_ATTRIBUTES, :script)
add(EVENT_ATTRIBUTES, :script).
add(['style'], :style)

ATTR_NAME = /[\w.:-]+/ # :nodoc:
TAG_END = /\/?>/ # :nodoc:
Expand Down Expand Up @@ -75,9 +76,14 @@ def setup
def scan_java_script encoder, code
if code && !code.empty?
@java_script_scanner ||= Scanners::JavaScript.new '', :keep_tokens => true
# encoder.begin_group :inline
@java_script_scanner.tokenize code, :tokens => encoder
# encoder.end_group :inline
end
end

def scan_css encoder, code, state = [:initial]
if code && !code.empty?
@css_scanner ||= Scanners::CSS.new '', :keep_tokens => true
@css_scanner.tokenize code, :tokens => encoder, :state => state
end
end

Expand Down Expand Up @@ -118,7 +124,7 @@ def scan_tokens encoder, options
elsif match = scan(/<\/[-\w.:]*>?/m)
in_tag = nil
encoder.text_token match, :tag
elsif match = scan(/<(?:(script)|[-\w.:]+)(>)?/m)
elsif match = scan(/<(?:(script|style)|[-\w.:]+)(>)?/m)
encoder.text_token match, :tag
in_tag = self[1]
if self[2]
Expand Down Expand Up @@ -169,17 +175,21 @@ def scan_tokens encoder, options
encoder.text_token match, :attribute_value
state = :attribute
elsif match = scan(/["']/)
if in_attribute == :script
encoder.begin_group :inline
encoder.text_token match, :inline_delimiter
if in_attribute == :script || in_attribute == :style
encoder.begin_group :string
encoder.text_token match, :delimiter
if scan(/javascript:[ \t]*/)
encoder.text_token matched, :comment
end
code = scan_until(match == '"' ? /(?="|\z)/ : /(?='|\z)/)
scan_java_script encoder, code
if in_attribute == :script
scan_java_script encoder, code
else
scan_css encoder, code, [:block]
end
match = scan(/["']/)
encoder.text_token match, :inline_delimiter if match
encoder.end_group :inline
encoder.text_token match, :delimiter if match
encoder.end_group :string
state = :attribute
in_attribute = nil
else
Expand Down Expand Up @@ -214,19 +224,23 @@ def scan_tokens encoder, options

when :in_special_tag
case in_tag
when 'script'
when 'script', 'style'
encoder.text_token match, :space if match = scan(/[ \t]*\n/)
if scan(/(\s*<!--)(?:(.*?)(-->)|(.*))/m)
code = self[2] || self[4]
closing = self[3]
encoder.text_token self[1], :comment
else
code = scan_until(/(?=(?:\n\s*)?<\/script>)|\z/)
code = scan_until(/(?=(?:\n\s*)?<\/#{in_tag}>)|\z/)
closing = false
end
unless code.empty?
encoder.begin_group :inline
scan_java_script encoder, code
if in_tag == 'script'
scan_java_script encoder, code
else
scan_css encoder, code
end
encoder.end_group :inline
end
encoder.text_token closing, :comment if closing
Expand Down
0