hamlit 1.0.0 → 1.1.0

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: b50a51777e07c1765575e17d91f655ce47a11be6
4
- data.tar.gz: e22d32256fbd305a75a48bfe6801468da1252f43
3
+ metadata.gz: d5a1e096423ee86064c348eb368f24123485c237
4
+ data.tar.gz: 4eea016823ef282a698e59cf75af15a98aad9d19
5
5
  SHA512:
6
- metadata.gz: 3d0436c3c221d564f568ba11ccae65dc1876c048067614a1102612a58028d752d5a69518fc303b10bd5626f6155549ed1b49fde0aa692695f83cfa1bf9f0c701
7
- data.tar.gz: 2438e651b2b48e2d7158162004f4de35f9a084363331f1684434d5238905ed1a422bfe172720e8919f93c8cd59c94b0c2ceed64460f93c7550eb293c120d5c8c
6
+ metadata.gz: 70ca8fad5f406015583d08a0e80e2d878e879ebaf15aa3d6139e8bb91d8871dce7897d23eb9d6bb066854cab88140c0bd6782b176dcf7753e361bd21d0a80153
7
+ data.tar.gz: 1e78de32103cd32ec2a277ed95eefc1e1cacbd70431db6b8c812982d488d0fbe292e288bf834ab8569a7dba26e02b17575d79404c2f27233e2bc13c07fca1bcb
@@ -1,3 +1,9 @@
1
+ ## v1.1.0
2
+
3
+ - Join id and class attributes
4
+ - https://github.com/k0kubun/hamlit/issues/23
5
+ - Thanks to @felixbuenemann
6
+
1
7
  ## v1.0.0
2
8
 
3
9
  - Use escape\_utils gem for faster escape\_html
data/README.md CHANGED
@@ -15,19 +15,21 @@ or just replace `gem "haml"` with `gem "hamlit"`.
15
15
  ## Features
16
16
  ### Fast rendering
17
17
 
18
- Hamlit's rendering is **7.24x times faster** than original haml.
18
+ ![](http://i.gyazo.com/4fe00ff2ac2fa959dfcf86a5e27dc914.png)
19
+
20
+ Hamlit's rendering is **7.16x times faster** than original haml.
19
21
 
20
22
  ```
21
- erubis: 114417.8 i/s
22
- hamlit: 107367.5 i/s - 1.07x slower
23
- slim: 104728.0 i/s - 1.09x slower
24
- faml: 87624.2 i/s - 1.31x slower
25
- haml: 15796.0 i/s - 7.24x slower
23
+ erubis: 114501.6 i/s
24
+ hamlit: 112888.1 i/s - 1.01x slower
25
+ slim: 103298.5 i/s - 1.11x slower
26
+ faml: 88675.4 i/s - 1.29x slower
27
+ haml: 15750.6 i/s - 7.27x slower
26
28
  ```
27
29
 
28
- [This benchmark](https://github.com/k0kubun/hamlit/blob/b6f112aa1f51816ab9a3a81bd7810ed9cffd26aa/benchmarks/benchmark.rb)
30
+ [This benchmark](https://github.com/k0kubun/hamlit/blob/4e5655c4ba1d51c85b4551c3b22baa6d7780d208/benchmarks/benchmark.rb)
29
31
  is the same as [slim-template/slim](https://github.com/slim-template/slim)'s one for fairness.
30
- ([The result on travis CI](https://travis-ci.org/k0kubun/hamlit/jobs/57333515))
32
+ ([The result on travis CI](https://travis-ci.org/k0kubun/hamlit/jobs/58162910))
31
33
 
32
34
  ### Better parser
33
35
 
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = ">= 2.0.0"
20
21
 
21
22
  spec.add_dependency "escape_utils"
22
23
  spec.add_dependency "temple", "~> 0.7.3"
@@ -27,6 +27,7 @@ module Hamlit
27
27
  next
28
28
  end
29
29
 
30
+ value = refine_joinable_value(key, value) if value.is_a?(Array)
30
31
  escaped = Temple::Utils.escape_html(value)
31
32
  result += " #{key}=#{@quote}#{escaped}#{@quote}"
32
33
  end
@@ -35,6 +36,15 @@ module Hamlit
35
36
 
36
37
  private
37
38
 
39
+ def refine_joinable_value(key, value)
40
+ case key
41
+ when :id
42
+ value = value.join('_')
43
+ when :class
44
+ value = value.join(' ')
45
+ end
46
+ end
47
+
38
48
  def merge_attributes(base, target)
39
49
  result = {}
40
50
  base = flatten_attributes(base)
@@ -46,7 +56,7 @@ module Hamlit
46
56
  when :id
47
57
  result[key] = [base[key], target[key]].compact.join('_')
48
58
  else
49
- result[key] = [base[key], target[key]].compact.join(' ')
59
+ result[key] = [base[key], target[key]].compact.map(&:to_s).sort.join(' ')
50
60
  end
51
61
  else
52
62
  result[key] = base[key].nil? ? target[key] : base[key]
@@ -19,6 +19,10 @@ module Hamlit
19
19
  NESTABLE_ATTRIBUTES = %w[data].freeze
20
20
  IGNORED_EXPRESSIONS = %w[false nil].freeze
21
21
 
22
+ # Class and id are joined if the value is an array.
23
+ JOIN_ATTRIBUTES = %w[class id].freeze
24
+ JOINABLE_TOKENS = %i[on_ident on_qwords_beg on_lbracket].freeze
25
+
22
26
  # Only boolean attributes can be deleted for performance.
23
27
  BOOLEAN_ATTRIBUTES = %w[
24
28
  autofocus
@@ -38,7 +42,8 @@ module Hamlit
38
42
 
39
43
  format_attributes(attrs).map do |key, value|
40
44
  next true_attribute(key) if value == 'true'
41
- assert_static_value!(value) if NESTABLE_ATTRIBUTES.include?(key)
45
+ assert_static_value!(value) if NESTABLE_ATTRIBUTES.include?(key)
46
+ detect_joinable_value!(value) if JOIN_ATTRIBUTES.include?(key)
42
47
 
43
48
  [:html, :attr, key, [:dynamic, value]]
44
49
  end
@@ -69,6 +74,14 @@ module Hamlit
69
74
  end
70
75
  end
71
76
 
77
+ # Give up static compilation when the value is a variable or an array.
78
+ def detect_joinable_value!(value)
79
+ tokens = Ripper.lex(value)
80
+ tokens.each do |(row, col), type, str|
81
+ raise RuntimeBuild if JOINABLE_TOKENS.include?(type)
82
+ end
83
+ end
84
+
72
85
  # Give up static compilation when attributes have deletable
73
86
  # attributes, such as 'checked'.
74
87
  def assert_no_boolean_attributes!(attrs)
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -73,6 +73,34 @@ describe Hamlit::Engine do
73
73
  HTML
74
74
  end
75
75
 
76
+ describe 'joinable attributes' do
77
+ it 'joins class with a space' do
78
+ assert_render(<<-'HAML', <<-HTML)
79
+ - val = ['a', 'b', 'c']
80
+ %p{ class: val }
81
+ %p{ class: %w[a b c] }
82
+ %p{ class: ['a', 'b', 'c'] }
83
+ HAML
84
+ <p class='a b c'></p>
85
+ <p class='a b c'></p>
86
+ <p class='a b c'></p>
87
+ HTML
88
+ end
89
+
90
+ it 'joins id with an underscore' do
91
+ assert_render(<<-'HAML', <<-HTML)
92
+ - val = ['a', 'b', 'c']
93
+ %p{ id: val }
94
+ %p{ id: %w[a b c] }
95
+ %p{ id: ['a', 'b', 'c'] }
96
+ HAML
97
+ <p id='a_b_c'></p>
98
+ <p id='a_b_c'></p>
99
+ <p id='a_b_c'></p>
100
+ HTML
101
+ end
102
+ end
103
+
76
104
  describe 'deletable attributes' do
77
105
  it 'deletes attributes whose value is nil or false' do
78
106
  assert_render(<<-'HAML', <<-HTML)
@@ -118,15 +146,11 @@ describe Hamlit::Engine do
118
146
  assert_render(<<-'HAML', <<-HTML)
119
147
  - val = false
120
148
  %a{ href: val }
121
- %a{ class: val }
122
149
  - val = nil
123
150
  %a{ href: val }
124
- %a{ class: val }
125
151
  HAML
126
152
  <a href='false'></a>
127
- <a class='false'></a>
128
153
  <a href=''></a>
129
- <a class=''></a>
130
154
  HTML
131
155
  end
132
156
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamlit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-12 00:00:00.000000000 Z
11
+ date: 2015-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -468,7 +468,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
468
468
  requirements:
469
469
  - - ">="
470
470
  - !ruby/object:Gem::Version
471
- version: '0'
471
+ version: 2.0.0
472
472
  required_rubygems_version: !ruby/object:Gem::Requirement
473
473
  requirements:
474
474
  - - ">="
@@ -568,4 +568,3 @@ test_files:
568
568
  - spec/rails/vendor/assets/javascripts/.keep
569
569
  - spec/rails/vendor/assets/stylesheets/.keep
570
570
  - spec/spec_helper.rb
571
- has_rdoc: