Add :trace option.

Closes #702.
This commit is contained in:
Norman Clarke 2014-02-01 14:31:17 -08:00
parent 070228c4ba
commit 1c29b49038
4 changed files with 20 additions and 2 deletions

View File

@ -14,6 +14,8 @@
* Make escape_once respect hexadecimal references. (Matt Wildig)
* General performance and memory usage improvements. (Akira Matsuda)
* Don't treat the 'data' attribute specially when merging attribute hashes. (Matt Wildig and Norman Clarke)
* Add a tracing option. When enabled, Haml will output a data-trace attribute on each tag showing the path
to the source Haml file from which it was generated. Thanks [Alex Babkin](https://github.com/ababkin).
## 4.0.5

View File

@ -500,7 +500,9 @@ END
end
def prerender_tag(name, self_close, attributes)
# TODO: consider just passing in the damn options here
if @options[:trace]
attributes.merge!({"data-trace" => @options.filename.split('/views').last + ":" + @node.line.to_s})
end
attributes_string = Compiler.build_attributes(
@options.html?, @options.attr_wrapper, @options.escape_attrs, @options.hyphenate_data_attrs, attributes)
"<#{name}#{attributes_string}#{self_close && @options.xhtml? ? ' /' : ''}>"

View File

@ -23,7 +23,8 @@ module Haml
:ugly => false,
:cdata => false,
:parser_class => ::Haml::Parser,
:compiler_class => ::Haml::Compiler
:compiler_class => ::Haml::Compiler,
:trace => false
}
@valid_formats = [:html4, :html5, :xhtml]
@ -168,6 +169,14 @@ module Haml
# The compiler class to use. Defaults to Haml::Compiler.
attr_accessor :compiler_class
# Enable template tracing. If true, it will add a 'data-trace' attribute to
# each tag generated by Haml. The value of the attribute will be the
# source template name and the line number from which the tag was generated,
# separated by a colon. On Rails applications, the path given will be a
# relative path as from the views directory. On non-Rails applications,
# the path will be the full path.
attr_accessor :trace
def initialize(values = {}, &block)
defaults.each {|k, v| instance_variable_set :"@#{k}", v}
values.each {|k, v| send("#{k}=", v) if defaults.has_key?(k) && !v.nil?}

View File

@ -1986,6 +1986,11 @@ HAML
end
end
def test_tracing
result = render('%p', :trace => true, :filename => 'foo').strip
assert_equal "<p data-trace='foo:1'></p>", result
end
private
def assert_valid_encoding_comment(comment)