hamlit 1.5.2 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79bcf89bde702278c1bdad3ee833aec2e94eb58a
4
- data.tar.gz: d58885a682edc39e69a4027c4e0834f5d679a9e2
3
+ metadata.gz: 0b37fdbe2260e2bb676c56133ab5c61fb5064900
4
+ data.tar.gz: ae2188f3122c3a02bff15888f0d9891368bd388b
5
5
  SHA512:
6
- metadata.gz: 44fd92f92e8752ed72e56e0d46a5eae2ee69aa12175476f7fa39c8423c86a730522a471b9d9664d2f2ec43cf59108951c284209cae49b24255a134ed9962d7ba
7
- data.tar.gz: 6f20ddab502046ca46ab6fe61d8c04a8655f72ed917e1e51470ed2e26715491269100ba3c3125482a2215800ac919325e2c96c38cc50dba32e7d52e524c4a0f5
6
+ metadata.gz: 5ae27755753ed242c704eac31eb066f702f2383b12851fda96accbcc2a20b3887a0c12f87a73fe45154f152e744f8248137dd8930ab2b78db660347544dea17e
7
+ data.tar.gz: 00af5e2645cf488be39c75708d70e2f14fb23c6f6778b38d74ce82a4d4931e2e010506b53868a260f80ab91a57ec36a36a3a3d28f3439f3b07b6c508d74eae09
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v1.5.3
2
+
3
+ - Support !, !=, !==, &= and ~ as inline operators
4
+
1
5
  ## v1.5.2
2
6
 
3
7
  - Disable html escaping in CSS and JavaScript filter
@@ -48,8 +48,23 @@ module Hamlit
48
48
  count_indent(line) == @current_indent
49
49
  end
50
50
 
51
+ # Validate current line's indentation
52
+ def validate_indentation!(ast)
53
+ width = next_width
54
+ return false if width == @current_indent * 2
55
+
56
+ if width != Hamlit::EOF && (width > @current_indent * 2 || width.odd?)
57
+ ast << [:newline]
58
+ ast << syntax_error(
59
+ "inconsistent indentation: #{2 * @current_indent} spaces used for indentation, "\
60
+ "but the rest of the document was indented using #{width} spaces"
61
+ )
62
+ end
63
+ true
64
+ end
65
+
51
66
  # Validate the template is using consitent indentation, 2 spaces or a tab.
52
- def validate_indentation!(template)
67
+ def validate_indentation_consistency!(template)
53
68
  last_indent = ''
54
69
 
55
70
  indents = template.scan(/^[ \t]+/)
@@ -82,6 +97,10 @@ module Hamlit
82
97
  end
83
98
  lines.join
84
99
  end
100
+
101
+ def has_block?
102
+ next_indent > @current_indent
103
+ end
85
104
  end
86
105
  end
87
106
  end
@@ -20,6 +20,7 @@ module Hamlit
20
20
  end
21
21
 
22
22
  def empty_line?(line)
23
+ line = line.rest if line.respond_to?(:rest)
23
24
  line =~ /\A *\Z/
24
25
  end
25
26
 
data/lib/hamlit/parser.rb CHANGED
@@ -39,7 +39,7 @@ module Hamlit
39
39
 
40
40
  # Reset the parser state.
41
41
  def reset(template)
42
- validate_indentation!(template)
42
+ validate_indentation_consistency!(template)
43
43
  template = replace_hard_tabs(template)
44
44
  template = preprocess_multilines(template)
45
45
 
@@ -52,17 +52,7 @@ module Hamlit
52
52
  def parse_lines
53
53
  ast = []
54
54
  loop do
55
- width = next_width
56
- if width != @current_indent * 2
57
- if width != Hamlit::EOF && (width > @current_indent * 2 || width.odd?)
58
- ast << [:newline]
59
- ast << syntax_error(
60
- "inconsistent indentation: #{2 * @current_indent} spaces used for indentation, "\
61
- "but the rest of the document was indented using #{width} spaces"
62
- )
63
- end
64
- break
65
- end
55
+ break if validate_indentation!(ast)
66
56
 
67
57
  @current_lineno += 1
68
58
  node = parse_line(current_line)
@@ -77,38 +67,50 @@ module Hamlit
77
67
  end
78
68
 
79
69
  # Parse current line and return AST.
80
- def parse_line(line)
70
+ def parse_line(line, inline: false)
81
71
  return [:multi] if empty_line?(line)
82
72
 
83
- scanner = StringScanner.new(line)
73
+ scanner = wrap_scanner(line)
84
74
  scanner.scan(/ +/)
85
- if scanner.scan(/\\/) || scanner.match?(/\#{/)
86
- return parse_text(scanner)
87
- elsif scanner.match?(/&=/)
88
- return parse_script(scanner, force_escape: true)
89
- elsif scanner.match?(/!!!/)
90
- return parse_doctype(scanner)
91
- elsif scanner.scan(/!( |==)/)
92
- return parse_text(scanner, lstrip: true, escape: false)
93
- elsif scanner.match?(/!=/)
94
- return parse_script(scanner, disable_escape: true)
95
- elsif scanner.scan(/==/)
96
- return parse_text(scanner, lstrip: true)
97
- elsif scanner.match?(/[.#](\Z|[^a-zA-Z0-9_-])/)
98
- return parse_text(scanner)
75
+
76
+ unless inline
77
+ ast = parse_outer_line(scanner)
78
+ return ast if ast
99
79
  end
100
80
 
81
+ parse_inner_line(scanner, inline: inline)
82
+ end
83
+
84
+ # Parse a line and return ast if it is acceptable outside an inline tag
85
+ def parse_outer_line(scanner)
86
+ return parse_text(scanner) if scanner.scan(/\\/)
87
+ return parse_text(scanner) if scanner.match?(/\#{/)
88
+ return parse_text(scanner) if scanner.match?(/[.#]($|[^a-zA-Z0-9_-])/)
89
+ return parse_doctype(scanner) if scanner.match?(/!!!/)
90
+
101
91
  case scanner.peek(1)
102
92
  when '%', '.', '#'
103
93
  parse_tag(scanner)
104
- when '=', '~'
105
- parse_script(scanner)
106
- when '-'
107
- parse_silent_script(scanner)
108
94
  when '/'
109
95
  parse_comment(scanner)
110
96
  when ':'
111
97
  parse_filter(scanner)
98
+ end
99
+ end
100
+
101
+ # Parse a line and return ast which is acceptable inside an inline tag
102
+ def parse_inner_line(scanner, inline: false)
103
+ return parse_text(scanner, lstrip: true) if scanner.scan(/==/)
104
+ return parse_text(scanner, lstrip: true, escape: false) if scanner.scan(/!( |==)/)
105
+ return parse_script(scanner, force_escape: true) if scanner.match?(/&=/)
106
+ return parse_script(scanner, disable_escape: true) if scanner.match?(/!=/)
107
+ return parse_text(scanner, lstrip: true, escape: false) if inline && scanner.scan(/!/)
108
+
109
+ case scanner.peek(1)
110
+ when '=', '~'
111
+ parse_script(scanner)
112
+ when '-'
113
+ parse_silent_script(scanner)
112
114
  else
113
115
  parse_text(scanner)
114
116
  end
@@ -124,5 +126,10 @@ module Hamlit
124
126
  def newline_skip_filter?(ast)
125
127
  ast[0..1] == [:haml, :filter] && SKIP_NEWLINE_FILTERS.include?(ast[2])
126
128
  end
129
+
130
+ def wrap_scanner(str)
131
+ return str if str.is_a?(StringScanner)
132
+ StringScanner.new(str)
133
+ end
127
134
  end
128
135
  end
@@ -104,10 +104,6 @@ module Hamlit
104
104
  nil
105
105
  end
106
106
 
107
- def has_block?
108
- next_indent == @current_indent + 1
109
- end
110
-
111
107
  def statement_continuing?
112
108
  same_indent?(next_line) && internal_statement?(next_line, silent_script: true)
113
109
  end
@@ -20,19 +20,12 @@ module Hamlit
20
20
  attrs += parse_tag_id_and_class(scanner)
21
21
  attrs += parse_attributes(scanner)
22
22
 
23
- inner_removal = parse_whitespace_removal(scanner)
24
23
  ast = [:html, :tag, tag, attrs]
24
+ inner_removal = parse_whitespace_removal(scanner)
25
25
 
26
- if scanner.match?(/=/)
27
- ast << parse_script(scanner)
28
- return ast
29
- elsif scanner.scan(/\//)
30
- return ast
31
- elsif scanner.rest.match(/[^ ]/)
32
- ast << parse_text(scanner, lstrip: true)
33
- return ast
34
- elsif next_indent <= @current_indent
35
- return ast << [:multi]
26
+ unless has_block?
27
+ return ast if scanner.scan(/\//)
28
+ return ast << parse_line(scanner, inline: true)
36
29
  end
37
30
 
38
31
  content = [:multi, [:static, "\n"]]
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "1.5.2"
2
+ VERSION = "1.5.3"
3
3
  end
@@ -186,6 +186,68 @@ describe Hamlit::Engine do
186
186
  HTML
187
187
  end
188
188
 
189
+ describe 'inline operator' do
190
+ it 'does not accept backslash operator' do
191
+ assert_render(<<-'HAML', <<-'HTML')
192
+ %span\ foo
193
+ HAML
194
+ <span>\ foo</span>
195
+ HTML
196
+ end
197
+
198
+ it 'accepts != operator' do
199
+ assert_render(<<-'HAML', <<-'HTML')
200
+ %span!= '<nyaa>'
201
+ HAML
202
+ <span><nyaa></span>
203
+ HTML
204
+ end
205
+
206
+ it 'accepts !== operator' do
207
+ assert_render(<<-'HAML', <<-'HTML')
208
+ %span!==#{'<nyaa>'}
209
+ %span!== #{'<nyaa>'}
210
+ !==#{'<nyaa>'}
211
+ !== #{'<nyaa>'}
212
+ HAML
213
+ <span><nyaa></span>
214
+ <span><nyaa></span>
215
+ <nyaa>
216
+ <nyaa>
217
+ HTML
218
+ end
219
+
220
+ it 'accepts &= operator' do
221
+ assert_render(<<-'HAML', <<-'HTML', escape_html: false)
222
+ %span&= '<nyaa>'
223
+ HAML
224
+ <span>&lt;nyaa&gt;</span>
225
+ HTML
226
+ end
227
+
228
+ it 'accepts ! operator' do
229
+ assert_render(<<-'HAML', <<-'HTML')
230
+ %span!#{'<nyaa>'}
231
+ %span! #{'<nyaa>'}
232
+ !#{'<nyaa>'}
233
+ ! #{'<nyaa>'}
234
+ HAML
235
+ <span><nyaa></span>
236
+ <span><nyaa></span>
237
+ !&lt;nyaa&gt;
238
+ <nyaa>
239
+ HTML
240
+ end
241
+
242
+ it 'accepts ~ operator' do
243
+ assert_render(<<-HAML, <<-HTML, escape_html: false)
244
+ %span~ 1
245
+ HAML
246
+ <span>1</span>
247
+ HTML
248
+ end
249
+ end
250
+
189
251
  describe 'whitespace removal' do
190
252
  it 'removes outer whitespace by >' do
191
253
  assert_render(<<-HAML, <<-HTML)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamlit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
@@ -571,4 +571,3 @@ test_files:
571
571
  - spec/rails/vendor/assets/javascripts/.keep
572
572
  - spec/rails/vendor/assets/stylesheets/.keep
573
573
  - spec/spec_helper.rb
574
- has_rdoc: