1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Add encoding error checks for html2haml and css2sass as well.

Closes gh-39
This commit is contained in:
Nathan Weizenbaum 2009-09-23 15:54:46 -07:00
parent 2c060d7f74
commit 8e28b1749e
7 changed files with 58 additions and 7 deletions

View file

@ -19,8 +19,8 @@ that causes attributes to use a double-quote mark rather than single-quote.
### Ruby 1.9 Support
Haml now produces more descriptive errors when given a template
with invalid byte sequences for that template's encoding,
Haml and `html2haml` now produce more descriptive errors
when given a template with invalid byte sequences for that template's encoding,
including the line number and the offending character.
## [2.2.4](http://github.com/nex3/haml/commit/2.2.4)

View file

@ -18,8 +18,8 @@ as well as a backtrace of Sass imports.
### Ruby 1.9 Support
Sass now produces more descriptive errors when given a template
with invalid byte sequences for that template's encoding,
Sass and `css2sass` now produce more descriptive errors
when given a template with invalid byte sequences for that template's encoding,
including the line number and the offending character.
### `css2sass` Error Handling

View file

@ -413,6 +413,9 @@ END
@module_opts[:rhtml] &&= @options[:no_rhtml] != false
output.write(::Haml::HTML.new(input, @module_opts).render)
rescue ::Haml::Error => e
raise "#{e.is_a?(::Haml::SyntaxError) ? "Syntax error" : "Error"} on line " +
"#{get_line e}: #{e.message}"
end
end

View file

@ -80,6 +80,8 @@ module Haml
template = template.read
end
Haml::Util.check_encoding(template) {|msg, line| raise Haml::Error.new(msg, line)}
if @options[:rhtml]
match_to_html(template, /<%=(.*?)-?%>/m, 'loud')
match_to_html(template, /<%-?(.*?)-?%>/m, 'silent')
@ -128,9 +130,7 @@ module Haml
# @see Haml::HTML::Node#to_haml
def to_haml(tabs, options)
attrs = public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0]
if attrs == nil
raise Exception.new("Invalid doctype")
end
raise Haml::SyntaxError.new("Invalid doctype") if attrs == nil
type, version, strictness = attrs.map { |a| a.downcase }
if type == "html"

View file

@ -82,6 +82,10 @@ module Sass
# @return [String] The resulting Sass code
# @raise [Sass::SyntaxError] if there's an error parsing the CSS template
def render
Haml::Util.check_encoding(@template.string) do |msg, line|
raise Sass::SyntaxError.new(msg, :line => line)
end
build_tree.to_sass(0, @options).strip + "\n"
rescue Sass::SyntaxError => err
err.modify_backtrace(:filename => @options[:filename] || '(css)', :line => @line)

View file

@ -90,6 +90,28 @@ HAML
HTML
end
# Encodings
unless Haml::Util.ruby1_8?
def test_encoding_error
render("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
assert(false, "Expected exception")
rescue Haml::Error => e
assert_equal(3, e.line)
assert_equal('Invalid UTF-8 character "\xFE"', e.message)
end
def test_ascii_incompatible_encoding_error
template = "foo\nbar\nb_z".encode("utf-16le")
template[9] = "\xFE".force_encoding("utf-16le")
render(template)
assert(false, "Expected exception")
rescue Haml::Error => e
assert_equal(3, e.line)
assert_equal('Invalid UTF-16LE character "\xFE"', e.message)
end
end
protected
def render(text, options = {})

View file

@ -264,6 +264,28 @@ CSS
assert_equal('Invalid CSS after "foo ": expected "{", was "}aaaaaaaaaaaaaa..."', err.message)
end
# Encodings
unless Haml::Util.ruby1_8?
def test_encoding_error
css2sass("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
assert(false, "Expected exception")
rescue Sass::SyntaxError => e
assert_equal(3, e.sass_line)
assert_equal('Invalid UTF-8 character "\xFE"', e.message)
end
def test_ascii_incompatible_encoding_error
template = "foo\nbar\nb_z".encode("utf-16le")
template[9] = "\xFE".force_encoding("utf-16le")
css2sass(template)
assert(false, "Expected exception")
rescue Sass::SyntaxError => e
assert_equal(3, e.sass_line)
assert_equal('Invalid UTF-16LE character "\xFE"', e.message)
end
end
private
def css2sass(string, opts={})