haml 4.0.7 → 5.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +36 -0
  3. data/.gitignore +19 -0
  4. data/.gitmodules +3 -0
  5. data/.yardopts +2 -3
  6. data/CHANGELOG.md +146 -4
  7. data/FAQ.md +4 -14
  8. data/Gemfile +16 -0
  9. data/MIT-LICENSE +2 -2
  10. data/README.md +90 -47
  11. data/REFERENCE.md +160 -74
  12. data/Rakefile +44 -63
  13. data/TODO +24 -0
  14. data/benchmark.rb +70 -0
  15. data/haml.gemspec +45 -0
  16. data/lib/haml/.gitattributes +1 -0
  17. data/lib/haml/attribute_builder.rb +219 -0
  18. data/lib/haml/attribute_compiler.rb +237 -0
  19. data/lib/haml/attribute_parser.rb +150 -0
  20. data/lib/haml/buffer.rb +12 -175
  21. data/lib/haml/compiler.rb +110 -320
  22. data/lib/haml/engine.rb +34 -41
  23. data/lib/haml/error.rb +28 -24
  24. data/lib/haml/escapable.rb +77 -0
  25. data/lib/haml/exec.rb +38 -20
  26. data/lib/haml/filters.rb +22 -27
  27. data/lib/haml/generator.rb +42 -0
  28. data/lib/haml/helpers/action_view_extensions.rb +4 -2
  29. data/lib/haml/helpers/action_view_mods.rb +45 -60
  30. data/lib/haml/helpers/action_view_xss_mods.rb +2 -0
  31. data/lib/haml/helpers/safe_erubi_template.rb +20 -0
  32. data/lib/haml/helpers/safe_erubis_template.rb +5 -1
  33. data/lib/haml/helpers/xss_mods.rb +23 -13
  34. data/lib/haml/helpers.rb +134 -89
  35. data/lib/haml/options.rb +63 -69
  36. data/lib/haml/parser.rb +319 -227
  37. data/lib/haml/plugin.rb +54 -0
  38. data/lib/haml/railtie.rb +43 -12
  39. data/lib/haml/sass_rails_filter.rb +18 -4
  40. data/lib/haml/template/options.rb +13 -2
  41. data/lib/haml/template.rb +13 -6
  42. data/lib/haml/temple_engine.rb +124 -0
  43. data/lib/haml/temple_line_counter.rb +30 -0
  44. data/lib/haml/util.rb +83 -202
  45. data/lib/haml/version.rb +3 -1
  46. data/lib/haml.rb +2 -0
  47. data/yard/default/.gitignore +1 -0
  48. data/yard/default/fulldoc/html/css/common.sass +15 -0
  49. data/yard/default/layout/html/footer.erb +12 -0
  50. metadata +73 -115
  51. data/lib/haml/template/plugin.rb +0 -41
  52. data/test/engine_test.rb +0 -2013
  53. data/test/erb/_av_partial_1.erb +0 -12
  54. data/test/erb/_av_partial_2.erb +0 -8
  55. data/test/erb/action_view.erb +0 -62
  56. data/test/erb/standard.erb +0 -55
  57. data/test/filters_test.rb +0 -254
  58. data/test/gemfiles/Gemfile.rails-3.0.x +0 -5
  59. data/test/gemfiles/Gemfile.rails-3.1.x +0 -6
  60. data/test/gemfiles/Gemfile.rails-3.2.x +0 -5
  61. data/test/gemfiles/Gemfile.rails-4.0.x +0 -5
  62. data/test/haml-spec/LICENSE +0 -14
  63. data/test/haml-spec/README.md +0 -106
  64. data/test/haml-spec/lua_haml_spec.lua +0 -38
  65. data/test/haml-spec/perl_haml_test.pl +0 -81
  66. data/test/haml-spec/ruby_haml_test.rb +0 -23
  67. data/test/haml-spec/tests.json +0 -660
  68. data/test/helper_test.rb +0 -583
  69. data/test/markaby/standard.mab +0 -52
  70. data/test/mocks/article.rb +0 -6
  71. data/test/parser_test.rb +0 -105
  72. data/test/results/content_for_layout.xhtml +0 -12
  73. data/test/results/eval_suppressed.xhtml +0 -9
  74. data/test/results/helpers.xhtml +0 -70
  75. data/test/results/helpful.xhtml +0 -10
  76. data/test/results/just_stuff.xhtml +0 -70
  77. data/test/results/list.xhtml +0 -12
  78. data/test/results/nuke_inner_whitespace.xhtml +0 -40
  79. data/test/results/nuke_outer_whitespace.xhtml +0 -148
  80. data/test/results/original_engine.xhtml +0 -20
  81. data/test/results/partial_layout.xhtml +0 -5
  82. data/test/results/partial_layout_erb.xhtml +0 -5
  83. data/test/results/partials.xhtml +0 -21
  84. data/test/results/render_layout.xhtml +0 -3
  85. data/test/results/silent_script.xhtml +0 -74
  86. data/test/results/standard.xhtml +0 -162
  87. data/test/results/tag_parsing.xhtml +0 -23
  88. data/test/results/very_basic.xhtml +0 -5
  89. data/test/results/whitespace_handling.xhtml +0 -90
  90. data/test/template_test.rb +0 -354
  91. data/test/templates/_av_partial_1.haml +0 -9
  92. data/test/templates/_av_partial_1_ugly.haml +0 -9
  93. data/test/templates/_av_partial_2.haml +0 -5
  94. data/test/templates/_av_partial_2_ugly.haml +0 -5
  95. data/test/templates/_layout.erb +0 -3
  96. data/test/templates/_layout_for_partial.haml +0 -3
  97. data/test/templates/_partial.haml +0 -8
  98. data/test/templates/_text_area.haml +0 -3
  99. data/test/templates/_text_area_helper.html.haml +0 -4
  100. data/test/templates/action_view.haml +0 -47
  101. data/test/templates/action_view_ugly.haml +0 -47
  102. data/test/templates/breakage.haml +0 -8
  103. data/test/templates/content_for_layout.haml +0 -8
  104. data/test/templates/eval_suppressed.haml +0 -11
  105. data/test/templates/helpers.haml +0 -55
  106. data/test/templates/helpful.haml +0 -11
  107. data/test/templates/just_stuff.haml +0 -85
  108. data/test/templates/list.haml +0 -12
  109. data/test/templates/nuke_inner_whitespace.haml +0 -32
  110. data/test/templates/nuke_outer_whitespace.haml +0 -144
  111. data/test/templates/original_engine.haml +0 -17
  112. data/test/templates/partial_layout.haml +0 -3
  113. data/test/templates/partial_layout_erb.erb +0 -4
  114. data/test/templates/partialize.haml +0 -1
  115. data/test/templates/partials.haml +0 -12
  116. data/test/templates/render_layout.haml +0 -2
  117. data/test/templates/silent_script.haml +0 -45
  118. data/test/templates/standard.haml +0 -43
  119. data/test/templates/standard_ugly.haml +0 -43
  120. data/test/templates/tag_parsing.haml +0 -21
  121. data/test/templates/very_basic.haml +0 -4
  122. data/test/templates/whitespace_handling.haml +0 -87
  123. data/test/test_helper.rb +0 -81
  124. data/test/util_test.rb +0 -63
data/lib/haml/options.rb CHANGED
@@ -1,53 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Haml
2
4
  # This class encapsulates all of the configuration options that Haml
3
5
  # understands. Please see the {file:REFERENCE.md#options Haml Reference} to
4
6
  # learn how to set the options.
5
7
  class Options
6
-
7
- @defaults = {
8
- :attr_wrapper => "'",
9
- :autoclose => %w(area base basefont br col command embed frame
10
- hr img input isindex keygen link menuitem meta
11
- param source track wbr),
12
- :encoding => "UTF-8",
13
- :escape_attrs => true,
14
- :escape_html => false,
15
- :filename => '(haml)',
16
- :format => :html5,
17
- :hyphenate_data_attrs => true,
18
- :line => 1,
19
- :mime_type => 'text/html',
20
- :preserve => %w(textarea pre code),
21
- :remove_whitespace => false,
22
- :suppress_eval => false,
23
- :ugly => false,
24
- :cdata => false,
25
- :parser_class => ::Haml::Parser,
26
- :compiler_class => ::Haml::Compiler
27
- }
28
-
29
8
  @valid_formats = [:html4, :html5, :xhtml]
9
+ @buffer_option_keys = [:autoclose, :preserve, :attr_wrapper, :format,
10
+ :encoding, :escape_html, :escape_filter_interpolations, :escape_attrs, :hyphenate_data_attrs, :cdata]
11
+
12
+ class << self
13
+ # The default option values.
14
+ # @return Hash
15
+ def defaults
16
+ @defaults ||= Haml::TempleEngine.options.to_hash.merge(encoding: 'UTF-8')
17
+ end
30
18
 
31
- @buffer_option_keys = [:autoclose, :preserve, :attr_wrapper, :ugly, :format,
32
- :encoding, :escape_html, :escape_attrs, :hyphenate_data_attrs, :cdata]
19
+ # An array of valid values for the `:format` option.
20
+ # @return Array
21
+ attr_reader :valid_formats
33
22
 
34
- # The default option values.
35
- # @return Hash
36
- def self.defaults
37
- @defaults
38
- end
23
+ # An array of keys that will be used to provide a hash of options to
24
+ # {Haml::Buffer}.
25
+ # @return Hash
26
+ attr_reader :buffer_option_keys
39
27
 
40
- # An array of valid values for the `:format` option.
41
- # @return Array
42
- def self.valid_formats
43
- @valid_formats
44
- end
28
+ # Returns a subset of defaults: those that {Haml::Buffer} cares about.
29
+ # @return [{Symbol => Object}] The options hash
30
+ def buffer_defaults
31
+ @buffer_defaults ||= buffer_option_keys.inject({}) do |hash, key|
32
+ hash.merge(key => defaults[key])
33
+ end
34
+ end
45
35
 
46
- # An array of keys that will be used to provide a hash of options to
47
- # {Haml::Buffer}.
48
- # @return Hash
49
- def self.buffer_option_keys
50
- @buffer_option_keys
36
+ def wrap(options)
37
+ if options.is_a?(Options)
38
+ options
39
+ else
40
+ Options.new(options)
41
+ end
42
+ end
51
43
  end
52
44
 
53
45
  # The character that should wrap element attributes. This defaults to `'`
@@ -63,7 +55,6 @@ module Haml
63
55
  attr_accessor :autoclose
64
56
 
65
57
  # The encoding to use for the HTML output.
66
- # Only available on Ruby 1.9 or higher.
67
58
  # This can be a string or an `Encoding` Object. Note that Haml **does not**
68
59
  # automatically re-encode Ruby values; any strings coming from outside the
69
60
  # application should be converted before being passed into the Haml
@@ -91,6 +82,13 @@ module Haml
91
82
  # Defaults to false.
92
83
  attr_accessor :escape_html
93
84
 
85
+ # Sets whether or not to escape HTML-sensitive characters in interpolated strings.
86
+ # See also {file:REFERENCE.md#escaping_html Escaping HTML} and
87
+ # {file:REFERENCE.md#unescaping_html Unescaping HTML}.
88
+ #
89
+ # Defaults to the current value of `escape_html`.
90
+ attr_accessor :escape_filter_interpolations
91
+
94
92
  # The name of the Haml file being parsed.
95
93
  # This is only used as information when exceptions are raised. This is
96
94
  # automatically assigned when working through ActionView, so it's really
@@ -137,7 +135,7 @@ module Haml
137
135
  # formatting errors.
138
136
  #
139
137
  # Defaults to `false`.
140
- attr_reader :remove_whitespace
138
+ attr_accessor :remove_whitespace
141
139
 
142
140
  # Whether or not attribute hashes and Ruby scripts designated by `=` or `~`
143
141
  # should be evaluated. If this is `true`, said scripts are rendered as empty
@@ -146,13 +144,6 @@ module Haml
146
144
  # Defaults to `false`.
147
145
  attr_accessor :suppress_eval
148
146
 
149
- # If set to `true`, Haml makes no attempt to properly indent or format the
150
- # HTML output. This significantly improves rendering performance but makes
151
- # viewing the source unpleasant.
152
- #
153
- # Defaults to `true` in Rails production mode, and `false` everywhere else.
154
- attr_accessor :ugly
155
-
156
147
  # Whether to include CDATA sections around javascript and css blocks when
157
148
  # using the `:javascript` or `:css` filters.
158
149
  #
@@ -169,9 +160,20 @@ module Haml
169
160
  # The compiler class to use. Defaults to Haml::Compiler.
170
161
  attr_accessor :compiler_class
171
162
 
172
- def initialize(values = {}, &block)
163
+ # Enable template tracing. If true, it will add a 'data-trace' attribute to
164
+ # each tag generated by Haml. The value of the attribute will be the
165
+ # source template name and the line number from which the tag was generated,
166
+ # separated by a colon. On Rails applications, the path given will be a
167
+ # relative path as from the views directory. On non-Rails applications,
168
+ # the path will be the full path.
169
+ attr_accessor :trace
170
+
171
+ # Key is filter name in String and value is Class to use. Defaults to {}.
172
+ attr_accessor :filters
173
+
174
+ def initialize(values = {})
173
175
  defaults.each {|k, v| instance_variable_set :"@#{k}", v}
174
- values.reject {|k, v| !defaults.has_key?(k) || v.nil?}.each {|k, v| send("#{k}=", v)}
176
+ values.each {|k, v| send("#{k}=", v) if defaults.has_key?(k) && !v.nil?}
175
177
  yield if block_given?
176
178
  end
177
179
 
@@ -188,8 +190,7 @@ module Haml
188
190
  send "#{key}=", value
189
191
  end
190
192
 
191
- [:escape_attrs, :hyphenate_data_attrs, :remove_whitespace, :suppress_eval,
192
- :ugly].each do |method|
193
+ [:escape_attrs, :hyphenate_data_attrs, :remove_whitespace, :suppress_eval].each do |method|
193
194
  class_eval(<<-END)
194
195
  def #{method}?
195
196
  !! @#{method}
@@ -240,22 +241,13 @@ module Haml
240
241
  xhtml? || @cdata
241
242
  end
242
243
 
243
- def remove_whitespace=(value)
244
- @ugly = true if value
245
- @remove_whitespace = value
244
+ def encoding=(value)
245
+ return unless value
246
+ @encoding = value.is_a?(Encoding) ? value.name : value.to_s
247
+ @encoding = "UTF-8" if @encoding.upcase == "US-ASCII"
246
248
  end
247
249
 
248
- if RUBY_VERSION < "1.9"
249
- attr_writer :encoding
250
- else
251
- def encoding=(value)
252
- return unless value
253
- @encoding = value.is_a?(Encoding) ? value.name : value.to_s
254
- @encoding = "UTF-8" if @encoding.upcase == "US-ASCII"
255
- end
256
- end
257
-
258
- # Returns a subset of options: those that {Haml::Buffer} cares about.
250
+ # Returns a non-default subset of options: those that {Haml::Buffer} cares about.
259
251
  # All of the values here are such that when `#inspect` is called on the hash,
260
252
  # it can be `Kernel#eval`ed to get the same result back.
261
253
  #
@@ -264,7 +256,10 @@ module Haml
264
256
  # @return [{Symbol => Object}] The options hash
265
257
  def for_buffer
266
258
  self.class.buffer_option_keys.inject({}) do |hash, key|
267
- hash[key] = send(key)
259
+ value = public_send(key)
260
+ if self.class.buffer_defaults[key] != value
261
+ hash[key] = value
262
+ end
268
263
  hash
269
264
  end
270
265
  end
@@ -274,6 +269,5 @@ module Haml
274
269
  def defaults
275
270
  self.class.defaults
276
271
  end
277
-
278
272
  end
279
273
  end