opal-haml 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e33b7eaf522a6cecf841934bcef00144f3cd5594
4
- data.tar.gz: acc6c1d3be312df2dd0224685ac09d9084762017
3
+ metadata.gz: 9a51df8f11890733b25009b477cd1858d435d23c
4
+ data.tar.gz: 9d1269a086e15e682d2ea661ffe5e780066ba77b
5
5
  SHA512:
6
- metadata.gz: de66f0e58bdddfadc3423c8259abc289a08630e8084a5af14c27d38e27a9b7b9060e34fe15888e52bc03ab2b9a10545eb996c3394fd72f083866a411906ae941
7
- data.tar.gz: 91d0745a2c2202cc4b113bf9059fc70bdf355b551463b272fb5efe0ef5d13b6e98ebef205f04b02e7a1c83c1828f8bd4476a2392c467ba9c74a37368fe652aaf
6
+ metadata.gz: 82c5a14a140dc07be2be3afbd300e339b67fa69ec293fc3c92178bad911b981c0e7421450a5423eeb32ba2d0b13bf1edf89216c94ad11873a1aa90fdff01ecfc
7
+ data.tar.gz: 794c500356aeb83d467ac1eff35aa79127f0996929ebe292443e72f1c99030e9663c4404a6908822984a463adf0cdedc50293e0e6e37e25f28654d5f563b907c
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- ## 0.3.0 2015-08-07
1
+ ## 0.4.1 2015-11-02
2
+
3
+ * Add support for nested attribute hashes: `%div{ data: { foo: 123 } }` now becomes `<div data-foo="123"></div>`.
4
+
5
+ * Attribute values are now properly escaped.
6
+
7
+ ## 0.4.0 2015-08-07
2
8
 
3
9
  * Add support for Opal 0.8.
4
10
 
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module Haml
3
- VERSION = '0.4.0'
3
+ VERSION = '0.4.1'
4
4
  end
5
5
  end
data/opal/opal-haml.rb CHANGED
@@ -9,20 +9,36 @@ class Template
9
9
  attributes = class_id
10
10
 
11
11
  attributes_hashes.each do |hash|
12
- attributes.update hash
12
+ attributes.update hash do |_, oldval, newval|
13
+ Array(oldval) + Array(newval)
14
+ end
13
15
  end
14
16
 
15
- result = attributes.collect do |attr, value|
16
- if value == true
17
- next " #{attr}"
18
- elsif value == false
19
- next
17
+ result = []
18
+ _render_attributes attributes, result, ''
19
+
20
+ result.join
21
+ end
22
+
23
+ private
24
+
25
+ def _render_attributes(attributes, out, prefix)
26
+ attributes.each do |attr, value|
27
+ attr_name = prefix + attr
28
+
29
+ case value
30
+ when true then out << " #{attr_name}"
31
+ when false, nil then next
32
+ when Hash then _render_attributes(value, out, attr_name + '-')
20
33
  else
21
- " #{attr}='#{value}'"
34
+ value = value.compact.join ' ' if value.is_a? Array
35
+ out << " #{attr_name}='#{_attribute_escape value}'"
22
36
  end
23
37
  end
38
+ end
24
39
 
25
- result.join
40
+ def _attribute_escape(value)
41
+ value.to_s.gsub /['"&<>]/, '"' => '&quot;', "'" => '&apos;', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;'
26
42
  end
27
43
  end
28
44
  end
@@ -12,11 +12,38 @@ describe Template::OutputBuffer do
12
12
  expect(result).to include("name='foo'", "height='100'")
13
13
  end
14
14
 
15
+ it "can compile nested attributes" do
16
+ result = attributes(data: { foo: { bar: 'baz' }, another: 'one' })
17
+ expect(result).to include("data-foo-bar='baz'")
18
+ expect(result).to include("data-another='one'")
19
+ end
20
+
21
+ it "combines input hashes" do
22
+ result = subject.attributes({ 'class' => 'foo' }, nil, { class: 'bar', data: 'baz' })
23
+ expect(result).to include("class='foo bar'")
24
+ expect(result).to include("data='baz'")
25
+ end
26
+
27
+ it "removes nil from arrays" do
28
+ result = attributes(foo: [ 'bar', nil, 'baz' ])
29
+ expect(result).to include("foo='bar baz'")
30
+ end
31
+
32
+ it "escapes attribute values" do
33
+ result = attributes(foo: 'a<>&"\'b')
34
+ expect(result).to include("foo='a&lt;&gt;&amp;&quot;&apos;b'")
35
+ end
36
+
15
37
  it "skips the attribute for false values" do
16
38
  result = attributes(name: false)
17
39
  expect(result).to_not include('name')
18
40
  end
19
41
 
42
+ it "skips the attribute for nil values" do
43
+ result = attributes(name: nil)
44
+ expect(result).to_not include('name')
45
+ end
46
+
20
47
  it "generates a simple named attribute for true values" do
21
48
  result = attributes(closable: true)
22
49
  expect(result).to include('closable')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-haml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Beynon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-27 00:00:00.000000000 Z
11
+ date: 2015-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.4.6
121
+ rubygems_version: 2.4.8
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: Run Haml templates client-side with Opal