hamlit 0.6.1 → 0.6.2

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: 9c08c9aec751d231ce8bfa1c8a2f47ee8f7d3be0
4
- data.tar.gz: 479dc3dac50ba716b8a77a78b52e334a8d670b9e
3
+ metadata.gz: f16b0dcc3dc1e76c8ba6218f808a519780a39870
4
+ data.tar.gz: 27bf912e63d3d08194ccac8b55a677cbd718dbda
5
5
  SHA512:
6
- metadata.gz: 068959a68a071911879281fbe37e5d7b8c5feca7b8b7e4529396dcc71f99d2e127b3d4f600dc5b5edcd0ed244033976e20216f8dea58fdbe45a63a2404e6ac1a
7
- data.tar.gz: a9680ba6c4321d62f48edaf7deb83e7c90e1d2ca53e0de234440abd85c5dd0ccc3011f31d4e636d651c4accf02b01eb30a99034640a2599789bc3a16a294100c
6
+ metadata.gz: a5321dc3369b2336e7850a32b5ea1d60ca9780d7df3a97dd95f640201e3112bdf51a810191f5bf7e1e6bc7b91648c24602e5d5887d0c2805768d6c672d8a9738
7
+ data.tar.gz: 4f5488a790b6ef92ac7f499ffabbb014b1b80dcc521dc2faca2ce1ac1afa20aab0abfc15cacbc8ad028405882aaf4475e7c767055a57818c450a7048f6467fb2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.6.2
2
+
3
+ - Don't render falsy attributes
4
+ - https://github.com/k0kubun/hamlit/issues/2
5
+ - Thanks to @eagletmt
6
+
1
7
  ## v0.6.1
2
8
 
3
9
  - Bugfix of line numbers for better error backtrace
data/README.md CHANGED
@@ -29,9 +29,6 @@ Hamlit's rendering is **7.24x times faster** than original haml.
29
29
  is the same as [slim-template/slim](https://github.com/slim-template/slim)'s one for fairness.
30
30
  ([The result on travis CI](https://travis-ci.org/k0kubun/hamlit/jobs/57333515))
31
31
 
32
- Note that there are [some incompatibilities](https://github.com/k0kubun/hamlit/issues) related to performance.
33
- You may want [faml](https://github.com/eagletmt/faml) for a better compatibility.
34
-
35
32
  ### Better parser
36
33
 
37
34
  Haml's attribute parser is not so good. For example, raises syntax error for `%a{ b: '}' }`.
@@ -71,12 +68,6 @@ code by Hamlit is very fast.
71
68
  Not only relying on temple optimizers, but also Hamlit's compiler cares about many cases
72
69
  to optimize performance such as string interpolation.
73
70
 
74
- ## TODO
75
-
76
- Currently there are some important incompatibilities that should be fixed.
77
-
78
- - Remove falsy attributes [#2](https://github.com/k0kubun/hamlit/issues/2)
79
-
80
71
  ## License
81
72
 
82
73
  MIT License
@@ -15,14 +15,27 @@ module Hamlit
15
15
  include Concerns::Balanceable
16
16
  include Concerns::Ripperable
17
17
 
18
- # For performance, only data can be nested.
18
+ # Only data can be nested for performance.
19
19
  NESTABLE_ATTRIBUTES = %w[data].freeze
20
20
  IGNORED_EXPRESSIONS = %w[false nil].freeze
21
21
 
22
+ # Only boolean attributes can be deleted for performance.
23
+ BOOLEAN_ATTRIBUTES = %w[
24
+ autofocus
25
+ checked
26
+ data
27
+ disabled
28
+ formnovalidate
29
+ multiple
30
+ readonly
31
+ ].freeze
32
+
22
33
  def compile_old_attribute(str)
23
34
  raise RuntimeBuild unless Ripper.sexp(str)
24
35
 
25
36
  attrs = parse_old_attributes(str)
37
+ assert_no_boolean_attributes!(attrs)
38
+
26
39
  format_attributes(attrs).map do |key, value|
27
40
  next true_attribute(key) if value == 'true'
28
41
  assert_static_value!(value) if NESTABLE_ATTRIBUTES.include?(key)
@@ -56,6 +69,14 @@ module Hamlit
56
69
  end
57
70
  end
58
71
 
72
+ # Give up static compilation when attributes have deletable
73
+ # attributes, such as 'checked'.
74
+ def assert_no_boolean_attributes!(attrs)
75
+ if BOOLEAN_ATTRIBUTES.any? { |key| attrs.keys.include?(key) }
76
+ raise RuntimeBuild
77
+ end
78
+ end
79
+
59
80
  # Parse brace-balanced string and return the result as hash
60
81
  def parse_old_attributes(str)
61
82
  attributes = {}
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -40,19 +40,6 @@ describe Hamlit::Engine do
40
40
  HTML
41
41
  end
42
42
 
43
- it 'renders false or nil attributes' do
44
- assert_render(<<-'HAML', <<-HTML)
45
- - hash = { checked: false }
46
- %input{ hash }
47
- %input{ checked: false }
48
- %input{ checked: nil }
49
- HAML
50
- <input>
51
- <input>
52
- <input>
53
- HTML
54
- end
55
-
56
43
  it 'accepts even illegal input for haml' do
57
44
  assert_render(<<-'HAML', <<-HTML)
58
45
  %span{ class: '}}}', id: '{}}' } }{
@@ -86,6 +73,64 @@ describe Hamlit::Engine do
86
73
  HTML
87
74
  end
88
75
 
76
+ describe 'deletable attributes' do
77
+ it 'deletes attributes whose value is nil or false' do
78
+ assert_render(<<-'HAML', <<-HTML)
79
+ - hash = { checked: false }
80
+ %input{ hash }
81
+ %input{ checked: false }
82
+ %input{ checked: nil }
83
+ - checked = nil
84
+ %input{ checked: checked }
85
+ - checked = false
86
+ %input{ checked: checked }
87
+ HAML
88
+ <input>
89
+ <input>
90
+ <input>
91
+ <input>
92
+ <input>
93
+ HTML
94
+ end
95
+
96
+ it 'deletes some limited attributes with dynamic value' do
97
+ assert_render(<<-'HAML', <<-HTML)
98
+ - val = false
99
+ #foo.bar{ autofocus: val }
100
+ #foo.bar{ checked: val }
101
+ #foo.bar{ data: { disabled: val } }
102
+ #foo.bar{ disabled: val }
103
+ #foo.bar{ formnovalidate: val }
104
+ #foo.bar{ multiple: val }
105
+ #foo.bar{ readonly: val }
106
+ HAML
107
+ <div class='bar' id='foo'></div>
108
+ <div class='bar' id='foo'></div>
109
+ <div class='bar' id='foo'></div>
110
+ <div class='bar' id='foo'></div>
111
+ <div class='bar' id='foo'></div>
112
+ <div class='bar' id='foo'></div>
113
+ <div class='bar' id='foo'></div>
114
+ HTML
115
+ end
116
+
117
+ it 'does not delete some attributes, for optimization' do
118
+ assert_render(<<-'HAML', <<-HTML)
119
+ - val = false
120
+ %a{ href: val }
121
+ %a{ class: val }
122
+ - val = nil
123
+ %a{ href: val }
124
+ %a{ class: val }
125
+ HAML
126
+ <a href='false'></a>
127
+ <a class='false'></a>
128
+ <a href=''></a>
129
+ <a class=''></a>
130
+ HTML
131
+ end
132
+ end
133
+
89
134
  describe 'html escape' do
90
135
  it 'escapes attribute values on static attributes' do
91
136
  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: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun