haml 4.0.6 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +72 -0
  5. data/.yardopts +2 -3
  6. data/CHANGELOG.md +138 -4
  7. data/FAQ.md +4 -14
  8. data/Gemfile +16 -0
  9. data/MIT-LICENSE +2 -2
  10. data/README.md +79 -42
  11. data/REFERENCE.md +142 -67
  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.rb +2 -0
  17. data/lib/haml/.gitattributes +1 -0
  18. data/lib/haml/attribute_builder.rb +164 -0
  19. data/lib/haml/attribute_compiler.rb +235 -0
  20. data/lib/haml/attribute_parser.rb +150 -0
  21. data/lib/haml/buffer.rb +29 -136
  22. data/lib/haml/compiler.rb +110 -320
  23. data/lib/haml/engine.rb +34 -41
  24. data/lib/haml/error.rb +28 -24
  25. data/lib/haml/escapable.rb +77 -0
  26. data/lib/haml/exec.rb +38 -20
  27. data/lib/haml/filters.rb +22 -27
  28. data/lib/haml/generator.rb +42 -0
  29. data/lib/haml/helpers.rb +134 -89
  30. data/lib/haml/helpers/action_view_extensions.rb +4 -2
  31. data/lib/haml/helpers/action_view_mods.rb +45 -60
  32. data/lib/haml/helpers/action_view_xss_mods.rb +2 -0
  33. data/lib/haml/helpers/safe_erubi_template.rb +20 -0
  34. data/lib/haml/helpers/safe_erubis_template.rb +5 -1
  35. data/lib/haml/helpers/xss_mods.rb +23 -13
  36. data/lib/haml/options.rb +63 -69
  37. data/lib/haml/parser.rb +292 -228
  38. data/lib/haml/plugin.rb +37 -0
  39. data/lib/haml/railtie.rb +38 -12
  40. data/lib/haml/sass_rails_filter.rb +18 -4
  41. data/lib/haml/template.rb +13 -6
  42. data/lib/haml/template/options.rb +13 -2
  43. data/lib/haml/temple_engine.rb +123 -0
  44. data/lib/haml/temple_line_counter.rb +30 -0
  45. data/lib/haml/util.rb +83 -202
  46. data/lib/haml/version.rb +3 -1
  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 -108
  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/helper_test.rb +0 -583
  63. data/test/markaby/standard.mab +0 -52
  64. data/test/mocks/article.rb +0 -6
  65. data/test/parser_test.rb +0 -105
  66. data/test/results/content_for_layout.xhtml +0 -12
  67. data/test/results/eval_suppressed.xhtml +0 -9
  68. data/test/results/helpers.xhtml +0 -70
  69. data/test/results/helpful.xhtml +0 -10
  70. data/test/results/just_stuff.xhtml +0 -70
  71. data/test/results/list.xhtml +0 -12
  72. data/test/results/nuke_inner_whitespace.xhtml +0 -40
  73. data/test/results/nuke_outer_whitespace.xhtml +0 -148
  74. data/test/results/original_engine.xhtml +0 -20
  75. data/test/results/partial_layout.xhtml +0 -5
  76. data/test/results/partial_layout_erb.xhtml +0 -5
  77. data/test/results/partials.xhtml +0 -21
  78. data/test/results/render_layout.xhtml +0 -3
  79. data/test/results/silent_script.xhtml +0 -74
  80. data/test/results/standard.xhtml +0 -162
  81. data/test/results/tag_parsing.xhtml +0 -23
  82. data/test/results/very_basic.xhtml +0 -5
  83. data/test/results/whitespace_handling.xhtml +0 -90
  84. data/test/template_test.rb +0 -354
  85. data/test/templates/_av_partial_1.haml +0 -9
  86. data/test/templates/_av_partial_1_ugly.haml +0 -9
  87. data/test/templates/_av_partial_2.haml +0 -5
  88. data/test/templates/_av_partial_2_ugly.haml +0 -5
  89. data/test/templates/_layout.erb +0 -3
  90. data/test/templates/_layout_for_partial.haml +0 -3
  91. data/test/templates/_partial.haml +0 -8
  92. data/test/templates/_text_area.haml +0 -3
  93. data/test/templates/_text_area_helper.html.haml +0 -4
  94. data/test/templates/action_view.haml +0 -47
  95. data/test/templates/action_view_ugly.haml +0 -47
  96. data/test/templates/breakage.haml +0 -8
  97. data/test/templates/content_for_layout.haml +0 -8
  98. data/test/templates/eval_suppressed.haml +0 -11
  99. data/test/templates/helpers.haml +0 -55
  100. data/test/templates/helpful.haml +0 -11
  101. data/test/templates/just_stuff.haml +0 -85
  102. data/test/templates/list.haml +0 -12
  103. data/test/templates/nuke_inner_whitespace.haml +0 -32
  104. data/test/templates/nuke_outer_whitespace.haml +0 -144
  105. data/test/templates/original_engine.haml +0 -17
  106. data/test/templates/partial_layout.haml +0 -3
  107. data/test/templates/partial_layout_erb.erb +0 -4
  108. data/test/templates/partialize.haml +0 -1
  109. data/test/templates/partials.haml +0 -12
  110. data/test/templates/render_layout.haml +0 -2
  111. data/test/templates/silent_script.haml +0 -45
  112. data/test/templates/standard.haml +0 -43
  113. data/test/templates/standard_ugly.haml +0 -43
  114. data/test/templates/tag_parsing.haml +0 -21
  115. data/test/templates/very_basic.haml +0 -4
  116. data/test/templates/whitespace_handling.haml +0 -87
  117. data/test/test_helper.rb +0 -81
  118. data/test/util_test.rb +0 -63
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActionView
2
4
  module Helpers
3
5
  module CaptureHelper
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ module Haml
6
+ class ErubiTemplateHandler < ActionView::Template::Handlers::ERB::Erubi
7
+
8
+ def initialize(*args, &blk)
9
+ @newline_pending = 0
10
+ super
11
+ end
12
+ end
13
+
14
+ class SafeErubiTemplate < Tilt::ErubiTemplate
15
+ def prepare
16
+ @options.merge! engine_class: Haml::ErubiTemplateHandler
17
+ super
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
1
5
  module Haml
2
6
 
3
7
  class ErubisTemplateHandler < ActionView::Template::Handlers::Erubis
@@ -26,4 +30,4 @@ module Haml
26
30
  [super, '@output_buffer.to_s'].join("\n")
27
31
  end
28
32
  end
29
- end
33
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Haml
2
4
  module Helpers
3
5
  # This module overrides Haml helpers to work properly
@@ -6,12 +8,15 @@ module Haml
6
8
  # to work with Rails' XSS protection methods.
7
9
  module XssMods
8
10
  def self.included(base)
9
- %w[html_escape find_and_preserve preserve list_of surround
10
- precede succeed capture_haml haml_concat haml_indent
11
- haml_tag escape_once].each do |name|
11
+ %w[find_and_preserve preserve list_of surround
12
+ precede succeed capture_haml haml_concat haml_internal_concat haml_indent].each do |name|
12
13
  base.send(:alias_method, "#{name}_without_haml_xss", name)
13
14
  base.send(:alias_method, name, "#{name}_with_haml_xss")
14
15
  end
16
+ # Those two always have _without_haml_xss
17
+ %w[html_escape escape_once].each do |name|
18
+ base.send(:alias_method, name, "#{name}_with_haml_xss")
19
+ end
15
20
  end
16
21
 
17
22
  # Don't escape text that's already safe,
@@ -61,24 +66,29 @@ module Haml
61
66
  Haml::Util.html_safe(capture_haml_without_haml_xss(*args, &block))
62
67
  end
63
68
 
64
- # Input is escaped
69
+ # Input will be escaped unless this is in a `with_raw_haml_concat`
70
+ # block. See #Haml::Helpers::ActionViewExtensions#with_raw_haml_concat.
65
71
  def haml_concat_with_haml_xss(text = "")
66
- raw = instance_variable_defined?('@_haml_concat_raw') ? @_haml_concat_raw : false
67
- haml_concat_without_haml_xss(raw ? text : haml_xss_html_escape(text))
72
+ raw = instance_variable_defined?(:@_haml_concat_raw) ? @_haml_concat_raw : false
73
+ if raw
74
+ haml_internal_concat_raw text
75
+ else
76
+ haml_internal_concat text
77
+ end
78
+ ErrorReturn.new("haml_concat")
68
79
  end
69
80
 
81
+ # Input is escaped
82
+ def haml_internal_concat_with_haml_xss(text="", newline=true, indent=true)
83
+ haml_internal_concat_without_haml_xss(haml_xss_html_escape(text), newline, indent)
84
+ end
85
+ private :haml_internal_concat_with_haml_xss
86
+
70
87
  # Output is always HTML safe
71
88
  def haml_indent_with_haml_xss
72
89
  Haml::Util.html_safe(haml_indent_without_haml_xss)
73
90
  end
74
91
 
75
- # Input is escaped, haml_concat'ed output is always HTML safe
76
- def haml_tag_with_haml_xss(name, *rest, &block)
77
- name = haml_xss_html_escape(name.to_s)
78
- rest.unshift(haml_xss_html_escape(rest.shift.to_s)) unless [Symbol, Hash, NilClass].any? {|t| rest.first.is_a? t}
79
- with_raw_haml_concat {haml_tag_without_haml_xss(name, *rest, &block)}
80
- end
81
-
82
92
  # Output is always HTML safe
83
93
  def escape_once_with_haml_xss(*args)
84
94
  Haml::Util.html_safe(escape_once_without_haml_xss(*args))
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'strscan'
2
4
 
3
5
  module Haml
@@ -59,7 +61,7 @@ module Haml
59
61
  SILENT_SCRIPT,
60
62
  ESCAPE,
61
63
  FILTER
62
- ]
64
+ ].freeze
63
65
 
64
66
  # The value of the character that designates that a line is part
65
67
  # of a multiline string.
@@ -71,26 +73,25 @@ module Haml
71
73
  # foo.each do | bar |
72
74
  # = bar
73
75
  #
74
- BLOCK_WITH_SPACES = /do[\s]*\|[\s]*[^\|]*[\s]+\|\z/
76
+ BLOCK_WITH_SPACES = /do\s*\|\s*[^\|]*\s+\|\z/
75
77
 
76
- MID_BLOCK_KEYWORDS = %w[else elsif rescue ensure end when]
77
- START_BLOCK_KEYWORDS = %w[if begin case unless]
78
+ MID_BLOCK_KEYWORDS = %w[else elsif rescue ensure end when].freeze
79
+ START_BLOCK_KEYWORDS = %w[if begin case unless].freeze
78
80
  # Try to parse assignments to block starters as best as possible
79
81
  START_BLOCK_KEYWORD_REGEX = /(?:\w+(?:,\s*\w+)*\s*=\s*)?(#{START_BLOCK_KEYWORDS.join('|')})/
80
82
  BLOCK_KEYWORD_REGEX = /^-?\s*(?:(#{MID_BLOCK_KEYWORDS.join('|')})|#{START_BLOCK_KEYWORD_REGEX.source})\b/
81
83
 
82
84
  # The Regex that matches a Doctype command.
83
- DOCTYPE_REGEX = /(\d(?:\.\d)?)?[\s]*([a-z]*)\s*([^ ]+)?/i
85
+ DOCTYPE_REGEX = /(\d(?:\.\d)?)?\s*([a-z]*)\s*([^ ]+)?/i
84
86
 
85
87
  # The Regex that matches a literal string or symbol value
86
88
  LITERAL_VALUE_REGEX = /:(\w*)|(["'])((?!\\|\#\{|\#@|\#\$|\2).|\\.)*\2/
87
89
 
88
- def initialize(template, options)
89
- # :eod is a special end-of-document marker
90
- @template = (template.rstrip).split(/\r\n|\r|\n/) + [:eod, :eod]
91
- @options = options
92
- @flat = false
93
- @index = 0
90
+ ID_KEY = 'id'.freeze
91
+ CLASS_KEY = 'class'.freeze
92
+
93
+ def initialize(options)
94
+ @options = Options.wrap(options)
94
95
  # Record the indent levels of "if" statements to validate the subsequent
95
96
  # elsif and else statements are indented at the appropriate level.
96
97
  @script_level_stack = []
@@ -98,15 +99,27 @@ module Haml
98
99
  @template_tabs = 0
99
100
  end
100
101
 
101
- def parse
102
+ def call(template)
103
+ match = template.rstrip.scan(/(([ \t]+)?(.*?))(?:\Z|\r\n|\r|\n)/m)
104
+ # discard the last match which is always blank
105
+ match.pop
106
+ @template = match.each_with_index.map do |(full, whitespace, text), index|
107
+ Line.new(whitespace, text.rstrip, full, index, self, false)
108
+ end
109
+ # Append special end-of-document marker
110
+ @template << Line.new(nil, '-#', '-#', @template.size, self, true)
111
+
102
112
  @root = @parent = ParseNode.new(:root)
103
- @haml_comment = false
113
+ @flat = false
114
+ @filter_buffer = nil
104
115
  @indentation = nil
105
116
  @line = next_line
106
117
 
107
118
  raise SyntaxError.new(Error.message(:indenting_at_start), @line.index) if @line.tabs != 0
108
119
 
109
- while next_line
120
+ loop do
121
+ next_line
122
+
110
123
  process_indent(@line) unless @line.text.empty?
111
124
 
112
125
  if flat?
@@ -118,75 +131,103 @@ module Haml
118
131
  end
119
132
 
120
133
  @tab_up = nil
121
- process_line(@line.text, @line.index) unless @line.text.empty? || @haml_comment
122
- if @parent.type != :haml_comment && (block_opened? || @tab_up)
134
+ process_line(@line) unless @line.text.empty?
135
+ if block_opened? || @tab_up
123
136
  @template_tabs += 1
124
137
  @parent = @parent.children.last
125
138
  end
126
139
 
127
- if !@haml_comment && !flat? && @next_line.tabs - @line.tabs > 1
140
+ if !flat? && @next_line.tabs - @line.tabs > 1
128
141
  raise SyntaxError.new(Error.message(:deeper_indenting, @next_line.tabs - @line.tabs), @next_line.index)
129
142
  end
130
143
 
131
144
  @line = @next_line
132
145
  end
133
-
134
146
  # Close all the open tags
135
147
  close until @parent.type == :root
136
148
  @root
137
149
  rescue Haml::Error => e
138
- e.backtrace.unshift "#{@options[:filename]}:#{(e.line ? e.line + 1 : @index) + @options[:line] - 1}"
150
+ e.backtrace.unshift "#{@options.filename}:#{(e.line ? e.line + 1 : @line.index + 1) + @options.line - 1}"
139
151
  raise
140
152
  end
141
153
 
154
+ def compute_tabs(line)
155
+ return 0 if line.text.empty? || !line.whitespace
156
+
157
+ if @indentation.nil?
158
+ @indentation = line.whitespace
159
+
160
+ if @indentation.include?(?\s) && @indentation.include?(?\t)
161
+ raise SyntaxError.new(Error.message(:cant_use_tabs_and_spaces), line.index)
162
+ end
163
+
164
+ @flat_spaces = @indentation * (@template_tabs+1) if flat?
165
+ return 1
166
+ end
167
+
168
+ tabs = line.whitespace.length / @indentation.length
169
+ return tabs if line.whitespace == @indentation * tabs
170
+ return @template_tabs + 1 if flat? && line.whitespace =~ /^#{@flat_spaces}/
171
+
172
+ message = Error.message(:inconsistent_indentation,
173
+ human_indentation(line.whitespace),
174
+ human_indentation(@indentation)
175
+ )
176
+ raise SyntaxError.new(message, line.index)
177
+ end
142
178
 
143
179
  private
144
180
 
145
181
  # @private
146
- class Line < Struct.new(:text, :unstripped, :full, :index, :compiler, :eod)
182
+ Line = Struct.new(:whitespace, :text, :full, :index, :parser, :eod) do
147
183
  alias_method :eod?, :eod
148
184
 
149
185
  # @private
150
186
  def tabs
151
- line = self
152
- @tabs ||= compiler.instance_eval do
153
- break 0 if line.text.empty? || !(whitespace = line.full[/^\s+/])
154
-
155
- if @indentation.nil?
156
- @indentation = whitespace
157
-
158
- if @indentation.include?(?\s) && @indentation.include?(?\t)
159
- raise SyntaxError.new(Error.message(:cant_use_tabs_and_spaces), line.index)
160
- end
161
-
162
- @flat_spaces = @indentation * (@template_tabs+1) if flat?
163
- break 1
164
- end
165
-
166
- tabs = whitespace.length / @indentation.length
167
- break tabs if whitespace == @indentation * tabs
168
- break @template_tabs + 1 if flat? && whitespace =~ /^#{@flat_spaces}/
187
+ @tabs ||= parser.compute_tabs(self)
188
+ end
169
189
 
170
- message = Error.message(:inconsistent_indentation,
171
- Haml::Util.human_indentation(whitespace),
172
- Haml::Util.human_indentation(@indentation)
173
- )
174
- raise SyntaxError.new(message, line.index)
175
- end
190
+ def strip!(from)
191
+ self.text = text[from..-1]
192
+ self.text.lstrip!
193
+ self
176
194
  end
177
195
  end
178
196
 
179
197
  # @private
180
- class ParseNode < Struct.new(:type, :line, :value, :parent, :children)
198
+ ParseNode = Struct.new(:type, :line, :value, :parent, :children) do
181
199
  def initialize(*args)
182
200
  super
183
201
  self.children ||= []
184
202
  end
185
203
 
186
204
  def inspect
187
- text = "(#{type} #{value.inspect}"
188
- children.each {|c| text << "\n" << c.inspect.gsub(/^/, " ")}
189
- text + ")"
205
+ %Q[(#{type} #{value.inspect}#{children.each_with_object(''.dup) {|c, s| s << "\n#{c.inspect.gsub!(/^/, ' ')}"}})].dup
206
+ end
207
+ end
208
+
209
+ # @param [String] new - Hash literal including dynamic values.
210
+ # @param [String] old - Hash literal including dynamic values or Ruby literal of multiple Hashes which MUST be interpreted as method's last arguments.
211
+ DynamicAttributes = Struct.new(:new, :old) do
212
+ undef :old=
213
+ def old=(value)
214
+ unless value =~ /\A{.*}\z/m
215
+ raise ArgumentError.new('Old attributes must start with "{" and end with "}"')
216
+ end
217
+ self[:old] = value
218
+ end
219
+
220
+ # This will be a literal for Haml::Buffer#attributes's last argument, `attributes_hashes`.
221
+ def to_literal
222
+ [new, stripped_old].compact.join(', ')
223
+ end
224
+
225
+ private
226
+
227
+ # For `%foo{ { foo: 1 }, bar: 2 }`, :old is "{ { foo: 1 }, bar: 2 }" and this method returns " { foo: 1 }, bar: 2 " for last argument.
228
+ def stripped_old
229
+ return nil if old.nil?
230
+ old.sub!(/\A{/, '').sub!(/}\z/m, '')
190
231
  end
191
232
  end
192
233
 
@@ -195,98 +236,104 @@ module Haml
195
236
  return unless line.tabs <= @template_tabs && @template_tabs > 0
196
237
 
197
238
  to_close = @template_tabs - line.tabs
198
- to_close.times {|i| close unless to_close - 1 - i == 0 && mid_block_keyword?(line.text)}
239
+ to_close.times {|i| close unless to_close - 1 - i == 0 && continuation_script?(line.text)}
240
+ end
241
+
242
+ def continuation_script?(text)
243
+ text[0] == SILENT_SCRIPT && mid_block_keyword?(text)
244
+ end
245
+
246
+ def mid_block_keyword?(text)
247
+ MID_BLOCK_KEYWORDS.include?(block_keyword(text))
199
248
  end
200
249
 
201
250
  # Processes a single line of Haml.
202
251
  #
203
252
  # This method doesn't return anything; it simply processes the line and
204
253
  # adds the appropriate code to `@precompiled`.
205
- def process_line(text, index)
206
- @index = index + 1
207
-
208
- case text[0]
209
- when DIV_CLASS; push div(text)
254
+ def process_line(line)
255
+ case line.text[0]
256
+ when DIV_CLASS; push div(line)
210
257
  when DIV_ID
211
- return push plain(text) if text[1] == ?{
212
- push div(text)
213
- when ELEMENT; push tag(text)
214
- when COMMENT; push comment(text[1..-1].strip)
258
+ return push plain(line) if %w[{ @ $].include?(line.text[1])
259
+ push div(line)
260
+ when ELEMENT; push tag(line)
261
+ when COMMENT; push comment(line.text[1..-1].lstrip)
215
262
  when SANITIZE
216
- return push plain(text[3..-1].strip, :escape_html) if text[1..2] == "=="
217
- return push script(text[2..-1].strip, :escape_html) if text[1] == SCRIPT
218
- return push flat_script(text[2..-1].strip, :escape_html) if text[1] == FLAT_SCRIPT
219
- return push plain(text[1..-1].strip, :escape_html) if text[1] == ?\s
220
- push plain(text)
263
+ return push plain(line.strip!(3), :escape_html) if line.text[1, 2] == '=='
264
+ return push script(line.strip!(2), :escape_html) if line.text[1] == SCRIPT
265
+ return push flat_script(line.strip!(2), :escape_html) if line.text[1] == FLAT_SCRIPT
266
+ return push plain(line.strip!(1), :escape_html) if line.text[1] == ?\s || line.text[1..2] == '#{'
267
+ push plain(line)
221
268
  when SCRIPT
222
- return push plain(text[2..-1].strip) if text[1] == SCRIPT
223
- push script(text[1..-1])
224
- when FLAT_SCRIPT; push flat_script(text[1..-1])
225
- when SILENT_SCRIPT; push silent_script(text)
226
- when FILTER; push filter(text[1..-1].downcase)
269
+ return push plain(line.strip!(2)) if line.text[1] == SCRIPT
270
+ line.text = line.text[1..-1]
271
+ push script(line)
272
+ when FLAT_SCRIPT; push flat_script(line.strip!(1))
273
+ when SILENT_SCRIPT
274
+ return push haml_comment(line.text[2..-1]) if line.text[1] == SILENT_COMMENT
275
+ push silent_script(line)
276
+ when FILTER; push filter(line.text[1..-1].downcase)
227
277
  when DOCTYPE
228
- return push doctype(text) if text[0...3] == '!!!'
229
- return push plain(text[3..-1].strip, false) if text[1..2] == "=="
230
- return push script(text[2..-1].strip, false) if text[1] == SCRIPT
231
- return push flat_script(text[2..-1].strip, false) if text[1] == FLAT_SCRIPT
232
- return push plain(text[1..-1].strip, false) if text[1] == ?\s
233
- push plain(text)
234
- when ESCAPE; push plain(text[1..-1])
235
- else; push plain(text)
278
+ return push doctype(line.text) if line.text[0, 3] == '!!!'
279
+ return push plain(line.strip!(3), false) if line.text[1, 2] == '=='
280
+ return push script(line.strip!(2), false) if line.text[1] == SCRIPT
281
+ return push flat_script(line.strip!(2), false) if line.text[1] == FLAT_SCRIPT
282
+ return push plain(line.strip!(1), false) if line.text[1] == ?\s || line.text[1..2] == '#{'
283
+ push plain(line)
284
+ when ESCAPE
285
+ line.text = line.text[1..-1]
286
+ push plain(line)
287
+ else; push plain(line)
236
288
  end
237
289
  end
238
290
 
239
291
  def block_keyword(text)
240
- return unless keyword = text.scan(BLOCK_KEYWORD_REGEX)[0]
292
+ return unless (keyword = text.scan(BLOCK_KEYWORD_REGEX)[0])
241
293
  keyword[0] || keyword[1]
242
294
  end
243
295
 
244
- def mid_block_keyword?(text)
245
- MID_BLOCK_KEYWORDS.include?(block_keyword(text))
246
- end
247
-
248
296
  def push(node)
249
297
  @parent.children << node
250
298
  node.parent = @parent
251
299
  end
252
300
 
253
- def plain(text, escape_html = nil)
301
+ def plain(line, escape_html = nil)
254
302
  if block_opened?
255
303
  raise SyntaxError.new(Error.message(:illegal_nesting_plain), @next_line.index)
256
304
  end
257
305
 
258
- unless contains_interpolation?(text)
259
- return ParseNode.new(:plain, @index, :text => text)
306
+ unless contains_interpolation?(line.text)
307
+ return ParseNode.new(:plain, line.index + 1, :text => line.text)
260
308
  end
261
309
 
262
- escape_html = @options[:escape_html] if escape_html.nil?
263
- script(unescape_interpolation(text, escape_html), false)
310
+ escape_html = @options.escape_html && @options.mime_type != 'text/plain' if escape_html.nil?
311
+ line.text = unescape_interpolation(line.text, escape_html)
312
+ script(line, false)
264
313
  end
265
314
 
266
- def script(text, escape_html = nil, preserve = false)
267
- raise SyntaxError.new(Error.message(:no_ruby_code, '=')) if text.empty?
268
- text = handle_ruby_multiline(text)
269
- escape_html = @options[:escape_html] if escape_html.nil?
315
+ def script(line, escape_html = nil, preserve = false)
316
+ raise SyntaxError.new(Error.message(:no_ruby_code, '=')) if line.text.empty?
317
+ line = handle_ruby_multiline(line)
318
+ escape_html = @options.escape_html if escape_html.nil?
270
319
 
271
- keyword = block_keyword(text)
320
+ keyword = block_keyword(line.text)
272
321
  check_push_script_stack(keyword)
273
322
 
274
- ParseNode.new(:script, @index, :text => text, :escape_html => escape_html,
323
+ ParseNode.new(:script, line.index + 1, :text => line.text, :escape_html => escape_html,
275
324
  :preserve => preserve, :keyword => keyword)
276
325
  end
277
326
 
278
- def flat_script(text, escape_html = nil)
279
- raise SyntaxError.new(Error.message(:no_ruby_code, '~')) if text.empty?
280
- script(text, escape_html, :preserve)
327
+ def flat_script(line, escape_html = nil)
328
+ raise SyntaxError.new(Error.message(:no_ruby_code, '~')) if line.text.empty?
329
+ script(line, escape_html, :preserve)
281
330
  end
282
331
 
283
- def silent_script(text)
284
- return haml_comment(text[2..-1]) if text[1] == SILENT_COMMENT
285
-
286
- raise SyntaxError.new(Error.message(:no_end), @index - 1) if text[1..-1].strip == "end"
332
+ def silent_script(line)
333
+ raise SyntaxError.new(Error.message(:no_end), line.index) if line.text[1..-1].strip == 'end'
287
334
 
288
- text = handle_ruby_multiline(text)
289
- keyword = block_keyword(text)
335
+ line = handle_ruby_multiline(line)
336
+ keyword = block_keyword(line.text)
290
337
 
291
338
  check_push_script_stack(keyword)
292
339
 
@@ -308,8 +355,8 @@ module Haml
308
355
  end
309
356
  end
310
357
 
311
- ParseNode.new(:silent_script, @index,
312
- :text => text[1..-1], :keyword => keyword)
358
+ ParseNode.new(:silent_script, @line.index + 1,
359
+ :text => line.text[1..-1], :keyword => keyword)
313
360
  end
314
361
 
315
362
  def check_push_script_stack(keyword)
@@ -323,18 +370,25 @@ module Haml
323
370
  end
324
371
 
325
372
  def haml_comment(text)
326
- @haml_comment = block_opened?
327
- ParseNode.new(:haml_comment, @index, :text => text)
373
+ if filter_opened?
374
+ @flat = true
375
+ @filter_buffer = String.new
376
+ @filter_buffer << "#{text}\n" unless text.empty?
377
+ text = @filter_buffer
378
+ # If we don't know the indentation by now, it'll be set in Line#tabs
379
+ @flat_spaces = @indentation * (@template_tabs+1) if @indentation
380
+ end
381
+
382
+ ParseNode.new(:haml_comment, @line.index + 1, :text => text)
328
383
  end
329
384
 
330
385
  def tag(line)
331
386
  tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
332
- nuke_inner_whitespace, action, value, last_line = parse_tag(line)
387
+ nuke_inner_whitespace, action, value, last_line = parse_tag(line.text)
333
388
 
334
- preserve_tag = @options[:preserve].include?(tag_name)
389
+ preserve_tag = @options.preserve.include?(tag_name)
335
390
  nuke_inner_whitespace ||= preserve_tag
336
- preserve_tag = false if @options[:ugly]
337
- escape_html = (action == '&' || (action != '!' && @options[:escape_html]))
391
+ escape_html = (action == '&' || (action != '!' && @options.escape_html))
338
392
 
339
393
  case action
340
394
  when '/'; self_closing = true
@@ -369,22 +423,20 @@ module Haml
369
423
  end
370
424
 
371
425
  attributes = Parser.parse_class_and_id(attributes)
372
- attributes_list = []
426
+ dynamic_attributes = DynamicAttributes.new
373
427
 
374
428
  if attributes_hashes[:new]
375
429
  static_attributes, attributes_hash = attributes_hashes[:new]
376
- Buffer.merge_attrs(attributes, static_attributes) if static_attributes
377
- attributes_list << attributes_hash
430
+ AttributeBuilder.merge_attributes!(attributes, static_attributes) if static_attributes
431
+ dynamic_attributes.new = attributes_hash
378
432
  end
379
433
 
380
434
  if attributes_hashes[:old]
381
435
  static_attributes = parse_static_hash(attributes_hashes[:old])
382
- Buffer.merge_attrs(attributes, static_attributes) if static_attributes
383
- attributes_list << attributes_hashes[:old] unless static_attributes || @options[:suppress_eval]
436
+ AttributeBuilder.merge_attributes!(attributes, static_attributes) if static_attributes
437
+ dynamic_attributes.old = attributes_hashes[:old] unless static_attributes || @options.suppress_eval
384
438
  end
385
439
 
386
- attributes_list.compact!
387
-
388
440
  raise SyntaxError.new(Error.message(:illegal_nesting_self_closing), @next_line.index) if block_opened? && self_closing
389
441
  raise SyntaxError.new(Error.message(:no_ruby_code, action), last_line - 1) if parse && value.empty?
390
442
  raise SyntaxError.new(Error.message(:self_closing_content), last_line - 1) if self_closing && !value.empty?
@@ -393,56 +445,70 @@ module Haml
393
445
  raise SyntaxError.new(Error.message(:illegal_nesting_line, tag_name), @next_line.index)
394
446
  end
395
447
 
396
- self_closing ||= !!(!block_opened? && value.empty? && @options[:autoclose].any? {|t| t === tag_name})
448
+ self_closing ||= !!(!block_opened? && value.empty? && @options.autoclose.any? {|t| t === tag_name})
397
449
  value = nil if value.empty? && (block_opened? || self_closing)
398
- value = handle_ruby_multiline(value) if parse
450
+ line.text = value
451
+ line = handle_ruby_multiline(line) if parse
399
452
 
400
- ParseNode.new(:tag, @index, :name => tag_name, :attributes => attributes,
401
- :attributes_hashes => attributes_list, :self_closing => self_closing,
453
+ ParseNode.new(:tag, line.index + 1, :name => tag_name, :attributes => attributes,
454
+ :dynamic_attributes => dynamic_attributes, :self_closing => self_closing,
402
455
  :nuke_inner_whitespace => nuke_inner_whitespace,
403
456
  :nuke_outer_whitespace => nuke_outer_whitespace, :object_ref => object_ref,
404
457
  :escape_html => escape_html, :preserve_tag => preserve_tag,
405
- :preserve_script => preserve_script, :parse => parse, :value => value)
458
+ :preserve_script => preserve_script, :parse => parse, :value => line.text)
406
459
  end
407
460
 
408
461
  # Renders a line that creates an XHTML tag and has an implicit div because of
409
462
  # `.` or `#`.
410
463
  def div(line)
411
- tag('%div' + line)
464
+ line.text = "%div#{line.text}"
465
+ tag(line)
412
466
  end
413
467
 
414
468
  # Renders an XHTML comment.
415
- def comment(line)
416
- conditional, line = balance(line, ?[, ?]) if line[0] == ?[
417
- line.strip!
418
- conditional << ">" if conditional
469
+ def comment(text)
470
+ if text[0..1] == '!['
471
+ revealed = true
472
+ text = text[1..-1]
473
+ else
474
+ revealed = false
475
+ end
419
476
 
420
- if block_opened? && !line.empty?
477
+ conditional, text = balance(text, ?[, ?]) if text[0] == ?[
478
+ text.strip!
479
+
480
+ if contains_interpolation?(text)
481
+ parse = true
482
+ text = unescape_interpolation(text)
483
+ else
484
+ parse = false
485
+ end
486
+
487
+ if block_opened? && !text.empty?
421
488
  raise SyntaxError.new(Haml::Error.message(:illegal_nesting_content), @next_line.index)
422
489
  end
423
490
 
424
- ParseNode.new(:comment, @index, :conditional => conditional, :text => line)
491
+ ParseNode.new(:comment, @line.index + 1, :conditional => conditional, :text => text, :revealed => revealed, :parse => parse)
425
492
  end
426
493
 
427
494
  # Renders an XHTML doctype or XML shebang.
428
- def doctype(line)
495
+ def doctype(text)
429
496
  raise SyntaxError.new(Error.message(:illegal_nesting_header), @next_line.index) if block_opened?
430
- version, type, encoding = line[3..-1].strip.downcase.scan(DOCTYPE_REGEX)[0]
431
- ParseNode.new(:doctype, @index, :version => version, :type => type, :encoding => encoding)
497
+ version, type, encoding = text[3..-1].strip.downcase.scan(DOCTYPE_REGEX)[0]
498
+ ParseNode.new(:doctype, @line.index + 1, :version => version, :type => type, :encoding => encoding)
432
499
  end
433
500
 
434
501
  def filter(name)
435
502
  raise Error.new(Error.message(:invalid_filter_name, name)) unless name =~ /^\w+$/
436
503
 
437
- @filter_buffer = String.new
438
-
439
504
  if filter_opened?
440
505
  @flat = true
506
+ @filter_buffer = String.new
441
507
  # If we don't know the indentation by now, it'll be set in Line#tabs
442
508
  @flat_spaces = @indentation * (@template_tabs+1) if @indentation
443
509
  end
444
510
 
445
- ParseNode.new(:filter, @index, :name => name, :text => @filter_buffer)
511
+ ParseNode.new(:filter, @line.index + 1, :name => name, :text => @filter_buffer)
446
512
  end
447
513
 
448
514
  def close
@@ -452,13 +518,17 @@ module Haml
452
518
  end
453
519
 
454
520
  def close_filter(_)
455
- @flat = false
456
- @flat_spaces = nil
457
- @filter_buffer = nil
521
+ close_flat_section
458
522
  end
459
523
 
460
524
  def close_haml_comment(_)
461
- @haml_comment = false
525
+ close_flat_section
526
+ end
527
+
528
+ def close_flat_section
529
+ @flat = false
530
+ @flat_spaces = nil
531
+ @filter_buffer = nil
462
532
  end
463
533
 
464
534
  def close_silent_script(node)
@@ -466,7 +536,7 @@ module Haml
466
536
 
467
537
  # Post-process case statements to normalize the nesting of "when" clauses
468
538
  return unless node.value[:keyword] == "case"
469
- return unless first = node.children.first
539
+ return unless (first = node.children.first)
470
540
  return unless first.type == :silent_script && first.value[:keyword] == "when"
471
541
  return if first.children.empty?
472
542
  # If the case node has a "when" child with children, it's the
@@ -485,29 +555,39 @@ module Haml
485
555
  # that can then be merged with another attributes hash.
486
556
  def self.parse_class_and_id(list)
487
557
  attributes = {}
488
- list.scan(/([#.])([-:_a-zA-Z0-9]+)/) do |type, property|
558
+ return attributes if list.empty?
559
+
560
+ list.scan(/([#.])([-:_a-zA-Z0-9\@]+)/) do |type, property|
489
561
  case type
490
562
  when '.'
491
- if attributes['class']
492
- attributes['class'] += " "
563
+ if attributes[CLASS_KEY]
564
+ attributes[CLASS_KEY] += " "
493
565
  else
494
- attributes['class'] = ""
566
+ attributes[CLASS_KEY] = ""
495
567
  end
496
- attributes['class'] += property
497
- when '#'; attributes['id'] = property
568
+ attributes[CLASS_KEY] += property
569
+ when '#'; attributes[ID_KEY] = property
498
570
  end
499
571
  end
500
572
  attributes
501
573
  end
502
574
 
575
+ # This method doesn't use Haml::AttributeParser because currently it depends on Ripper and Rubinius doesn't provide it.
576
+ # Ideally this logic should be placed in Haml::AttributeParser instead of here and this method should use it.
577
+ #
578
+ # @param [String] text - Hash literal or text inside old attributes
579
+ # @return [Hash,nil] - Return nil if text is not static Hash literal
503
580
  def parse_static_hash(text)
504
581
  attributes = {}
582
+ return attributes if text.empty?
583
+
584
+ text = text[1...-1] # strip brackets
505
585
  scanner = StringScanner.new(text)
506
586
  scanner.scan(/\s+/)
507
587
  until scanner.eos?
508
- return unless key = scanner.scan(LITERAL_VALUE_REGEX)
588
+ return unless (key = scanner.scan(LITERAL_VALUE_REGEX))
509
589
  return unless scanner.scan(/\s*=>\s*/)
510
- return unless value = scanner.scan(LITERAL_VALUE_REGEX)
590
+ return unless (value = scanner.scan(LITERAL_VALUE_REGEX))
511
591
  return unless scanner.scan(/\s*(?:,|$)\s*/)
512
592
  attributes[eval(key).to_s] = eval(value).to_s
513
593
  end
@@ -515,20 +595,20 @@ module Haml
515
595
  end
516
596
 
517
597
  # Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value
518
- def parse_tag(line)
519
- match = line.scan(/%([-:\w]+)([-:\w\.\#]*)(.*)/)[0]
520
- raise SyntaxError.new(Error.message(:invalid_tag, line)) unless match
598
+ def parse_tag(text)
599
+ match = text.scan(/%([-:\w]+)([-:\w.#\@]*)(.+)?/)[0]
600
+ raise SyntaxError.new(Error.message(:invalid_tag, text)) unless match
521
601
 
522
602
  tag_name, attributes, rest = match
523
603
 
524
- if attributes =~ /[\.#](\.|#|\z)/
604
+ if !attributes.empty? && (attributes =~ /[.#](\.|#|\z)/)
525
605
  raise SyntaxError.new(Error.message(:illegal_element))
526
606
  end
527
607
 
528
608
  new_attributes_hash = old_attributes_hash = last_line = nil
529
- object_ref = "nil"
609
+ object_ref = :nil
530
610
  attributes_hashes = {}
531
- while rest
611
+ while rest && !rest.empty?
532
612
  case rest[0]
533
613
  when ?{
534
614
  break if old_attributes_hash
@@ -539,38 +619,46 @@ module Haml
539
619
  new_attributes_hash, rest, last_line = parse_new_attributes(rest)
540
620
  attributes_hashes[:new] = new_attributes_hash
541
621
  when ?[
542
- break unless object_ref == "nil"
622
+ break unless object_ref == :nil
543
623
  object_ref, rest = balance(rest, ?[, ?])
544
624
  else; break
545
625
  end
546
626
  end
547
627
 
548
- if rest
628
+ if rest && !rest.empty?
549
629
  nuke_whitespace, action, value = rest.scan(/(<>|><|[><])?([=\/\~&!])?(.*)?/)[0]
550
- nuke_whitespace ||= ''
551
- nuke_outer_whitespace = nuke_whitespace.include? '>'
552
- nuke_inner_whitespace = nuke_whitespace.include? '<'
630
+ if nuke_whitespace
631
+ nuke_outer_whitespace = nuke_whitespace.include? '>'
632
+ nuke_inner_whitespace = nuke_whitespace.include? '<'
633
+ end
553
634
  end
554
635
 
555
- if @options[:remove_whitespace]
636
+ if @options.remove_whitespace
556
637
  nuke_outer_whitespace = true
557
638
  nuke_inner_whitespace = true
558
639
  end
559
640
 
560
- value = value.to_s.strip
641
+ if value.nil?
642
+ value = ''
643
+ else
644
+ value.strip!
645
+ end
561
646
  [tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
562
- nuke_inner_whitespace, action, value, last_line || @index]
647
+ nuke_inner_whitespace, action, value, last_line || @line.index + 1]
563
648
  end
564
649
 
565
- def parse_old_attributes(line)
566
- line = line.dup
567
- last_line = @index
650
+ # @return [String] attributes_hash - Hash literal starting with `{` and ending with `}`
651
+ # @return [String] rest
652
+ # @return [Integer] last_line
653
+ def parse_old_attributes(text)
654
+ text = text.dup
655
+ last_line = @line.index + 1
568
656
 
569
657
  begin
570
- attributes_hash, rest = balance(line, ?{, ?})
658
+ attributes_hash, rest = balance(text, ?{, ?})
571
659
  rescue SyntaxError => e
572
- if line.strip[-1] == ?, && e.message == Error.message(:unbalanced_brackets)
573
- line << "\n" << @next_line.text
660
+ if text.strip[-1] == ?, && e.message == Error.message(:unbalanced_brackets)
661
+ text << "\n#{@next_line.text}"
574
662
  last_line += 1
575
663
  next_line
576
664
  retry
@@ -579,14 +667,15 @@ module Haml
579
667
  raise e
580
668
  end
581
669
 
582
- attributes_hash = attributes_hash[1...-1] if attributes_hash
583
670
  return attributes_hash, rest, last_line
584
671
  end
585
672
 
586
- def parse_new_attributes(line)
587
- line = line.dup
588
- scanner = StringScanner.new(line)
589
- last_line = @index
673
+ # @return [Array<Hash,String,nil>] - [static_attributes (Hash), dynamic_attributes (nil or String starting with `{` and ending with `}`)]
674
+ # @return [String] rest
675
+ # @return [Integer] last_line
676
+ def parse_new_attributes(text)
677
+ scanner = StringScanner.new(text)
678
+ last_line = @line.index + 1
590
679
  attributes = {}
591
680
 
592
681
  scanner.scan(/\(\s*/)
@@ -595,14 +684,15 @@ module Haml
595
684
  break if name.nil?
596
685
 
597
686
  if name == false
598
- text = (Haml::Util.balance(line, ?(, ?)) || [line]).first
687
+ scanned = Haml::Util.balance(text, ?(, ?))
688
+ text = scanned ? scanned.first : text
599
689
  raise Haml::SyntaxError.new(Error.message(:invalid_attribute_list, text.inspect), last_line - 1)
600
690
  end
601
691
  attributes[name] = value
602
692
  scanner.scan(/\s*/)
603
693
 
604
694
  if scanner.eos?
605
- line << " " << @next_line.text
695
+ text << " #{@next_line.text}"
606
696
  last_line += 1
607
697
  next_line
608
698
  scanner.scan(/\s*/)
@@ -610,12 +700,12 @@ module Haml
610
700
  end
611
701
 
612
702
  static_attributes = {}
613
- dynamic_attributes = "{"
703
+ dynamic_attributes = "{".dup
614
704
  attributes.each do |name, (type, val)|
615
705
  if type == :static
616
706
  static_attributes[name] = val
617
707
  else
618
- dynamic_attributes << inspect_obj(name) << " => " << val << ","
708
+ dynamic_attributes << "#{inspect_obj(name)} => #{val},"
619
709
  end
620
710
  end
621
711
  dynamic_attributes << "}"
@@ -625,7 +715,7 @@ module Haml
625
715
  end
626
716
 
627
717
  def parse_new_attribute(scanner)
628
- unless name = scanner.scan(/[-:\w]+/)
718
+ unless (name = scanner.scan(/[-:\w]+/))
629
719
  return if scanner.scan(/\)/)
630
720
  return false
631
721
  end
@@ -634,8 +724,8 @@ module Haml
634
724
  return name, [:static, true] unless scanner.scan(/=/) #/end
635
725
 
636
726
  scanner.scan(/\s*/)
637
- unless quote = scanner.scan(/["']/)
638
- return false unless var = scanner.scan(/(@@?|\$)?\w+/)
727
+ unless (quote = scanner.scan(/["']/))
728
+ return false unless (var = scanner.scan(/(@@?|\$)?\w+/))
639
729
  return name, [:dynamic, var]
640
730
  end
641
731
 
@@ -650,35 +740,16 @@ module Haml
650
740
 
651
741
  return name, [:static, content.first[1]] if content.size == 1
652
742
  return name, [:dynamic,
653
- '"' + content.map {|(t, v)| t == :str ? inspect_obj(v)[1...-1] : "\#{#{v}}"}.join + '"']
654
- end
655
-
656
- def raw_next_line
657
- text = @template.shift
658
- return unless text
659
-
660
- index = @template_index
661
- @template_index += 1
662
-
663
- return text, index
743
+ %!"#{content.each_with_object(''.dup) {|(t, v), s| s << (t == :str ? inspect_obj(v)[1...-1] : "\#{#{v}}")}}"!]
664
744
  end
665
745
 
666
746
  def next_line
667
- text, index = raw_next_line
668
- return unless text
669
-
670
- # :eod is a special end-of-document marker
671
- line =
672
- if text == :eod
673
- Line.new '-#', '-#', '-#', index, self, true
674
- else
675
- Line.new text.strip, text.lstrip.chomp, text, index, self, false
676
- end
747
+ line = @template.shift || raise(StopIteration)
677
748
 
678
749
  # `flat?' here is a little outdated,
679
750
  # so we have to manually check if either the previous or current line
680
751
  # closes the flat block, as well as whether a new block is opened.
681
- line_defined = instance_variable_defined?('@line')
752
+ line_defined = instance_variable_defined?(:@line)
682
753
  @line.tabs if line_defined
683
754
  unless (flat? && !closes_flat?(line) && !closes_flat?(@line)) ||
684
755
  (line_defined && @line.text[0] == ?: && line.full =~ %r[^#{@line.full[/^\s+/]}\s])
@@ -694,21 +765,17 @@ module Haml
694
765
  line && !line.text.empty? && line.full !~ /^#{@flat_spaces}/
695
766
  end
696
767
 
697
- def un_next_line(line)
698
- @template.unshift line
699
- @template_index -= 1
700
- end
701
-
702
768
  def handle_multiline(line)
703
769
  return unless is_multiline?(line.text)
704
770
  line.text.slice!(-1)
705
- while new_line = raw_next_line.first
706
- break if new_line == :eod
707
- next if new_line.strip.empty?
708
- break unless is_multiline?(new_line.strip)
709
- line.text << new_line.strip[0...-1]
771
+ loop do
772
+ new_line = @template.first
773
+ break if new_line.eod?
774
+ next @template.shift if new_line.text.strip.empty?
775
+ break unless is_multiline?(new_line.text.strip)
776
+ line.text << new_line.text.strip[0...-1]
777
+ @template.shift
710
778
  end
711
- un_next_line new_line
712
779
  end
713
780
 
714
781
  # Checks whether or not `line` is in a multiline sequence.
@@ -716,18 +783,18 @@ module Haml
716
783
  text && text.length > 1 && text[-1] == MULTILINE_CHAR_VALUE && text[-2] == ?\s && text !~ BLOCK_WITH_SPACES
717
784
  end
718
785
 
719
- def handle_ruby_multiline(text)
720
- text = text.rstrip
721
- return text unless is_ruby_multiline?(text)
722
- un_next_line @next_line.full
786
+ def handle_ruby_multiline(line)
787
+ line.text.rstrip!
788
+ return line unless is_ruby_multiline?(line.text)
723
789
  begin
724
- new_line = raw_next_line.first
725
- break if new_line == :eod
726
- next if new_line.strip.empty?
727
- text << " " << new_line.strip
728
- end while is_ruby_multiline?(new_line.strip)
790
+ # Use already fetched @next_line in the first loop. Otherwise, fetch next
791
+ new_line = new_line.nil? ? @next_line : @template.shift
792
+ break if new_line.eod?
793
+ next if new_line.text.empty?
794
+ line.text << " #{new_line.text.rstrip}"
795
+ end while is_ruby_multiline?(new_line.text)
729
796
  next_line
730
- text
797
+ line
731
798
  end
732
799
 
733
800
  # `text' is a Ruby multiline block if it:
@@ -735,16 +802,13 @@ module Haml
735
802
  # - but not "?," which is a character literal
736
803
  # (however, "x?," is a method call and not a literal)
737
804
  # - and not "?\," which is a character literal
738
- #
739
805
  def is_ruby_multiline?(text)
740
806
  text && text.length > 1 && text[-1] == ?, &&
741
- !((text[-3..-2] =~ /\W\?/) || text[-3..-2] == "?\\")
807
+ !((text[-3, 2] =~ /\W\?/) || text[-3, 2] == "?\\")
742
808
  end
743
809
 
744
810
  def balance(*args)
745
- res = Haml::Util.balance(*args)
746
- return res if res
747
- raise SyntaxError.new(Error.message(:unbalanced_brackets))
811
+ Haml::Util.balance(*args) or raise(SyntaxError.new(Error.message(:unbalanced_brackets)))
748
812
  end
749
813
 
750
814
  def block_opened?
@@ -754,7 +818,7 @@ module Haml
754
818
  # Same semantics as block_opened?, except that block_opened? uses Line#tabs,
755
819
  # which doesn't interact well with filter lines
756
820
  def filter_opened?
757
- @next_line.full =~ (@indentation ? /^#{@indentation * @template_tabs}/ : /^\s/)
821
+ @next_line.full =~ (@indentation ? /^#{@indentation * (@template_tabs + 1)}/ : /^\s/)
758
822
  end
759
823
 
760
824
  def flat?