diff --git a/doc-src/HAML_CHANGELOG.md b/doc-src/HAML_CHANGELOG.md index 9d2e65e1..5be83b14 100644 --- a/doc-src/HAML_CHANGELOG.md +++ b/doc-src/HAML_CHANGELOG.md @@ -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) diff --git a/doc-src/SASS_CHANGELOG.md b/doc-src/SASS_CHANGELOG.md index 2e5d8bbe..a96f0d08 100644 --- a/doc-src/SASS_CHANGELOG.md +++ b/doc-src/SASS_CHANGELOG.md @@ -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 diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb index 1c99e9eb..ce4cad13 100644 --- a/lib/haml/exec.rb +++ b/lib/haml/exec.rb @@ -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 diff --git a/lib/haml/html.rb b/lib/haml/html.rb index e116fc5e..0ed37407 100644 --- a/lib/haml/html.rb +++ b/lib/haml/html.rb @@ -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" diff --git a/lib/sass/css.rb b/lib/sass/css.rb index d1a4996e..8da4f67b 100644 --- a/lib/sass/css.rb +++ b/lib/sass/css.rb @@ -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) diff --git a/test/haml/html2haml_test.rb b/test/haml/html2haml_test.rb index e1cf814b..0b3c4d6f 100644 --- a/test/haml/html2haml_test.rb +++ b/test/haml/html2haml_test.rb @@ -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 = {}) diff --git a/test/sass/css2sass_test.rb b/test/sass/css2sass_test.rb index 3b14b43a..9ab90c58 100644 --- a/test/sass/css2sass_test.rb +++ b/test/sass/css2sass_test.rb @@ -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={})