|
1 | 1 | require 'set'
|
| 2 | +require 'escape_utils' |
2 | 3 |
|
3 | 4 | module CodeRay
|
4 | 5 | module Encoders
|
@@ -126,22 +127,6 @@ class HTML < Encoder
|
126 | 127 |
|
127 | 128 | protected
|
128 | 129 |
|
129 |
| - def self.make_html_escape_hash |
130 |
| - { |
131 |
| - '&' => '&', |
132 |
| - '"' => '"', |
133 |
| - '>' => '>', |
134 |
| - '<' => '<', |
135 |
| - # "\t" => will be set to ' ' * options[:tab_width] during setup |
136 |
| - }.tap do |hash| |
137 |
| - # Escape ASCII control codes except \x9 == \t and \xA == \n. |
138 |
| - (Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' } |
139 |
| - end |
140 |
| - end |
141 |
| - |
142 |
| - HTML_ESCAPE = make_html_escape_hash |
143 |
| - HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1F]/ |
144 |
| - |
145 | 130 | TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
|
146 | 131 | h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
|
147 | 132 | end
|
@@ -180,7 +165,7 @@ def setup options
|
180 | 165 |
|
181 | 166 | @break_lines = (options[:break_lines] == true)
|
182 | 167 |
|
183 |
| - @HTML_ESCAPE = HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width]) |
| 168 | + @expand_tab = ' ' * options[:tab_width] |
184 | 169 |
|
185 | 170 | @opened = []
|
186 | 171 | @last_opened = nil
|
@@ -218,7 +203,13 @@ def finish options
|
218 | 203 | def text_token text, kind
|
219 | 204 | style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
|
220 | 205 |
|
221 |
| - text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } if text =~ /#{HTML_ESCAPE_PATTERN}/o |
| 206 | + text = EscapeUtils.escape_html(text) |
| 207 | + if text.index(/[\0-\t\xB-\x1F]/) |
| 208 | + # Escape ASCII control codes except \x9 == \t and \xA == \n. |
| 209 | + text.tr!("\0-\x8\xB-\x1F", ' ') if text.index(/[\0-\x8\xB-\x1F]/) |
| 210 | + text.gsub!("\t", @expand_tab) if text.index("\t") |
| 211 | + end |
| 212 | + |
222 | 213 | text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
|
223 | 214 |
|
224 | 215 | if style
|
|
0 commit comments