Skip to content

Commit 00eeb88

Browse files
committed
use EscapeUtils to escape HTML
1 parent 7493dcb commit 00eeb88

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

coderay.gemspec

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Gem::Specification.new do |s|
2929
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
3030
s.require_paths = ['lib']
3131

32+
s.add_dependency "escape_utils", "~> 0.3"
33+
3234
s.rubyforge_project = s.name
3335
s.rdoc_options = '-SNw2', "-m#{readme_file}", '-t CodeRay Documentation'
3436
s.extra_rdoc_files = readme_file

lib/coderay/encoders/html.rb

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'set'
2+
require 'escape_utils'
23

34
module CodeRay
45
module Encoders
@@ -126,22 +127,6 @@ class HTML < Encoder
126127

127128
protected
128129

129-
def self.make_html_escape_hash
130-
{
131-
'&' => '&amp;',
132-
'"' => '&quot;',
133-
'>' => '&gt;',
134-
'<' => '&lt;',
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-
145130
TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
146131
h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
147132
end
@@ -180,7 +165,7 @@ def setup options
180165

181166
@break_lines = (options[:break_lines] == true)
182167

183-
@HTML_ESCAPE = HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width])
168+
@expand_tab = ' ' * options[:tab_width]
184169

185170
@opened = []
186171
@last_opened = nil
@@ -218,7 +203,13 @@ def finish options
218203
def text_token text, kind
219204
style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
220205

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+
222213
text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
223214

224215
if style

0 commit comments

Comments
 (0)