arbre2 2.2.1 → 2.2.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: 66fd31e6deecb33fd6421abc61f4ac53513b0964
4
- data.tar.gz: c2a175e9c2b22df524ba2704c95254b0fae99f71
3
+ metadata.gz: 2a1f9811843c3bcfd91a24ddc44e76515421653d
4
+ data.tar.gz: 86ff8a7501ba4da8f51f1eb0fd025d2f8c8f7789
5
5
  SHA512:
6
- metadata.gz: 8a640b9624f022b05324e720306b1fa6875b2061e686a5f6b37ba50c7745e62a3ab8b6f498ac0c2e85b232c5fcb7bd8a9b7d24ce2bd1a5a8db87bf479057623b
7
- data.tar.gz: cae1590fd832a6287d548c61b6c149539facf9f80fee23d71d6474ba2be93e5ac3ed96dde47e7e87501c3d773b2db4539850021b13f9e4f34a4c74f7eee95316
6
+ metadata.gz: db0e2106409aed13d4486c913d1b5883906ce8185305b67f4c39a0e9536874d1b33edd07a25ffb818e6fd4c0cc045a5d61b70a28599ac6bad1e7f31f9557e6d4
7
+ data.tar.gz: 66e3d422569768883676c183b531c5b22165eb95be4a9baeafef2232cfdec0791d398c7b351c87b016527f199324271f0df5aacde3279c333cadcadc7738c498
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arbre2 (2.2.1)
4
+ arbre2 (2.2.2)
5
5
  activesupport
6
6
 
7
7
  GEM
data/lib/arbre.rb CHANGED
@@ -18,6 +18,7 @@ require 'arbre/child_element_collection'
18
18
 
19
19
  require 'arbre/html/attributes'
20
20
  require 'arbre/html/class_list'
21
+ require 'arbre/html/style_hash'
21
22
  require 'arbre/html/querying'
22
23
  require 'arbre/html/tag'
23
24
  require 'arbre/html/comment'
@@ -20,18 +20,18 @@ module Arbre
20
20
 
21
21
  def [](attribute)
22
22
  if attribute.to_s == 'class'
23
- @attributes['class'] ||= ClassList.new
23
+ classes
24
+ elsif attribute.to_s == 'style'
25
+ style
24
26
  else
25
27
  @attributes[attribute.to_s]
26
28
  end
27
29
  end
28
30
  def []=(attribute, value)
29
31
  if attribute.to_s == 'class'
30
- if value.present?
31
- @attributes['class'] = ClassList.new(value)
32
- else
33
- remove 'class'
34
- end
32
+ self.classes = value
33
+ elsif attribute.to_s == 'style'
34
+ self.style = value
35
35
  elsif value == true
36
36
  @attributes[attribute.to_s] = attribute.to_s
37
37
  elsif value
@@ -41,6 +41,30 @@ module Arbre
41
41
  end
42
42
  end
43
43
 
44
+ def classes
45
+ @attributes['class'] ||= ClassList.new
46
+ end
47
+
48
+ def classes=(value)
49
+ if value.present?
50
+ @attributes['class'] = ClassList.new(value)
51
+ else
52
+ remove 'class'
53
+ end
54
+ end
55
+
56
+ def style
57
+ @attributes['style'] ||= StyleHash.new
58
+ end
59
+
60
+ def style=(value)
61
+ if value.present?
62
+ @attributes['style'] = StyleHash.new(value)
63
+ else
64
+ remove 'style'
65
+ end
66
+ end
67
+
44
68
  def remove(attribute)
45
69
  @attributes.delete attribute.to_s
46
70
  end
@@ -67,6 +91,7 @@ module Arbre
67
91
  def pairs
68
92
  map do |name, value|
69
93
  next if name == 'class' && value.blank?
94
+ next if name == 'style' && value.blank?
70
95
  "#{html_escape(name)}=\"#{html_escape(value)}\""
71
96
  end
72
97
  end
@@ -0,0 +1,55 @@
1
+ module Arbre
2
+ module Html
3
+
4
+ # A style definition for an HTML element.
5
+ class StyleHash < Hash
6
+
7
+ def initialize(value = nil)
8
+ super()
9
+
10
+ case value
11
+ when String
12
+ parse value
13
+ when Hash
14
+ update value
15
+ end
16
+ end
17
+
18
+ def parse(value)
19
+ value.split(';').reject(&:blank?).each do |pair|
20
+ name, value = pair.split(':', 2)
21
+ next unless name && value
22
+ self[name.strip] = value.strip
23
+ end
24
+ end
25
+ private :parse
26
+
27
+ # Alias to the hash itself.
28
+ def style
29
+ self
30
+ end
31
+
32
+ # Make sure to store everything as dasherized values.
33
+ def []=(name, value)
34
+ super name.to_s.underscore.dasherize, value
35
+ end
36
+ def [](name)
37
+ super name.to_s.underscore.dasherize
38
+ end
39
+
40
+ def update(value)
41
+ value.each { |name, value| self[name] = value }
42
+ end
43
+
44
+ def delete(name)
45
+ super name.to_s.underscore.dasherize
46
+ end
47
+
48
+ def to_s
49
+ map{ |n, v| "#{n}: #{v};" }.join(' ')
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
@@ -1,3 +1,4 @@
1
+ require 'active_support/core_ext/array/extract_options'
1
2
  require 'erb'
2
3
 
3
4
  module Arbre
@@ -239,6 +240,13 @@ module Arbre
239
240
  klass.split(' ').all? { |cls| classes.include?(cls) }
240
241
  end
241
242
 
243
+ def style
244
+ self[:style]
245
+ end
246
+ def style=(value)
247
+ self[:style] = value
248
+ end
249
+
242
250
  ######
243
251
  # Rendering
244
252
 
data/lib/arbre/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arbre
2
- VERSION = "2.2.1"
2
+ VERSION = "2.2.2"
3
3
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ include Arbre::Html
3
+
4
+ describe StyleHash do
5
+
6
+ describe "initializer" do
7
+ it "should be able to be initialized without arguments" do
8
+ hash = StyleHash.new
9
+ expect(hash).to be_empty
10
+ end
11
+
12
+ it "should be able to be initialized with a hash" do
13
+ hash = StyleHash.new('one' => 'two')
14
+ expect(hash.to_s).to eql('one: two;')
15
+ end
16
+
17
+ it "should be able to be initialized with a string containing a style definition" do
18
+ hash = StyleHash.new('one: two; three:four;')
19
+ expect(hash.to_s).to eql('one: two; three: four;')
20
+ end
21
+
22
+ it "should convert to dash-case when initialized with a hash" do
23
+ hash = StyleHash.new('styleOne' => 'two')
24
+ expect(hash.to_s).to eql('style-one: two;')
25
+ end
26
+
27
+ end
28
+
29
+ describe '#style' do
30
+ it "should be an alias to itself" do
31
+ hash = StyleHash.new('one:two;')
32
+ expect(hash.style).to be(hash)
33
+ end
34
+ end
35
+
36
+ describe '[]=' do
37
+ it "should convert the used name to dash-case" do
38
+ hash = StyleHash.new
39
+ hash[:style_one] = 'one'
40
+ hash[:style_two] = 'two'
41
+ hash['style-three'] = 'three'
42
+ hash['styleFour'] = 'four'
43
+ expect(hash.to_s).to eql('style-one: one; style-two: two; style-three: three; style-four: four;')
44
+ end
45
+ end
46
+
47
+ describe '[]' do
48
+ it "should convert the used name to dash-case" do
49
+ hash = StyleHash.new('style-one: one; style-two: two; style-three: three; style-four: four;')
50
+ expect(hash[:style_one]).to eql('one')
51
+ expect(hash[:style_two]).to eql('two')
52
+ expect(hash['style-three']).to eql('three')
53
+ expect(hash['styleFour']).to eql('four')
54
+ end
55
+ end
56
+
57
+ end
@@ -10,6 +10,15 @@ describe Arbre do
10
10
  expect(arbre).to be_rendered_as("<span>Hello World</span>")
11
11
  end
12
12
 
13
+ it "should render all attributes properly" do
14
+ arbre do
15
+ div "test", :class => %w(one two), :style => {'one' => 'two'}, 'one' => 'two'
16
+ end
17
+ expect(arbre).to be_rendered_as(<<-HTML)
18
+ <div class="one two" one="two" style="one: two;">test</div>
19
+ HTML
20
+ end
21
+
13
22
  it "should access assigns through instance variables" do
14
23
  assigns[:my_var] = 'Hello World'
15
24
  arbre { span @my_var }
@@ -106,6 +106,32 @@ describe Attributes do
106
106
 
107
107
  end
108
108
 
109
+ describe "style attribute" do
110
+
111
+ it "should return a StyleHash when the class attribute is accessed" do
112
+ expect(attributes[:style]).to be_a(StyleHash)
113
+ expect(attributes[:style]).to be_empty
114
+ expect(attributes['style']).to be_a(StyleHash)
115
+ expect(attributes['style']).to be_empty
116
+ end
117
+
118
+ it "should wrap things into a style when the attribute is set" do
119
+ attributes[:style] = 'one: two;'
120
+ expect(attributes[:style]).to eql(StyleHash.new('one: two;'))
121
+
122
+ attributes[:style] = {'one' => 'two'}
123
+ expect(attributes[:style]).to eql(StyleHash.new('one' => 'two'))
124
+ end
125
+
126
+ it "should not set the attribute if a nil or empty hash is passed" do
127
+ attributes[:style] = nil
128
+ expect(attributes).not_to have_key(:style)
129
+ attributes[:style] = []
130
+ expect(attributes).not_to have_key(:style)
131
+ end
132
+
133
+ end
134
+
109
135
  describe '#remove' do
110
136
 
111
137
  it "should remove the attribute with the given name" do
@@ -76,7 +76,7 @@ describe Tag do
76
76
  end
77
77
  end
78
78
 
79
- describe '.classes' do
79
+ describe '.tag_classes' do
80
80
  it "should add the given classes to the tag" do
81
81
  klass = Class.new(Arbre::Html::Div) { classes 'time-input' }
82
82
  expect(klass.new.tag_classes).to eql(%w[time-input])
@@ -105,33 +105,33 @@ describe Tag do
105
105
  specify { expect(tag.attributes).to be_a(Attributes) }
106
106
 
107
107
  it "should allow setting an attribute through #set_attribute" do
108
- tag.set_attribute :style, 'display: none;'
108
+ tag.set_attribute :autocomplete, 'off'
109
109
  tag.set_attribute 'placeholder', '(test)'
110
- expect(tag.attributes).to eq('style' => 'display: none;', 'placeholder' => '(test)')
110
+ expect(tag.attributes).to eq('autocomplete' => 'off', 'placeholder' => '(test)')
111
111
  end
112
112
 
113
113
  it "should allow getting an attribute through #get_attribute" do
114
- tag.set_attribute :style, 'display: none;'
115
- expect(tag.get_attribute(:style)).to eql('display: none;')
116
- expect(tag.get_attribute('style')).to eql('display: none;')
114
+ tag.set_attribute :autocomplete, 'off'
115
+ expect(tag.get_attribute(:autocomplete)).to eql('off')
116
+ expect(tag.get_attribute('autocomplete')).to eql('off')
117
117
  end
118
118
 
119
119
  it "should allow setting an attribute through an indexer" do
120
- tag[:style] = 'display: none;'
120
+ tag[:autocomplete] = 'off'
121
121
  tag['placeholder'] = '(test)'
122
- expect(tag.attributes).to eq('style' => 'display: none;', 'placeholder' => '(test)')
122
+ expect(tag.attributes).to eq('autocomplete' => 'off', 'placeholder' => '(test)')
123
123
  end
124
124
 
125
125
  it "should allow getting an attribute through an indexer" do
126
- tag[:style] = 'display: none;'
127
- expect(tag[:style]).to eql('display: none;')
128
- expect(tag['style']).to eql('display: none;')
126
+ tag[:autocomplete] = 'off'
127
+ expect(tag[:autocomplete]).to eql('off')
128
+ expect(tag['autocomplete']).to eql('off')
129
129
  end
130
130
 
131
131
  it "should check whether an attribute is set through has_attribute?" do
132
- tag[:style] = 'display: none;'
133
- expect(tag).to have_attribute(:style)
134
- expect(tag).to have_attribute('style')
132
+ tag[:autocomplete] = 'off'
133
+ expect(tag).to have_attribute(:autocomplete)
134
+ expect(tag).to have_attribute('autocomplete')
135
135
  expect(tag).not_to have_attribute(:placeholder)
136
136
  end
137
137
 
@@ -206,7 +206,7 @@ describe Tag do
206
206
  end
207
207
 
208
208
  ######
209
- # ID & classes
209
+ # ID, classes & style
210
210
 
211
211
  describe '#generate_id' do
212
212
  it "should generate an ID for the tag using its object_id" do
@@ -261,6 +261,18 @@ describe Tag do
261
261
  end
262
262
  end
263
263
 
264
+ describe '#style' do
265
+ it "should be the same as the :style attribute" do
266
+ expect(tag.style).to be(tag[:style])
267
+ end
268
+ end
269
+ describe '#style=' do
270
+ it "should set the style attribute" do
271
+ tag.style = 'float: left;'
272
+ expect(tag[:style]).to eql('float' => 'left')
273
+ end
274
+ end
275
+
264
276
  ## Rendering is exemplified in the integration HTML spec.
265
277
 
266
278
  ######
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arbre2
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Bell
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-13 00:00:00.000000000 Z
12
+ date: 2014-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -142,6 +142,7 @@ files:
142
142
  - lib/arbre/html/document.rb
143
143
  - lib/arbre/html/html_tags.rb
144
144
  - lib/arbre/html/querying.rb
145
+ - lib/arbre/html/style_hash.rb
145
146
  - lib/arbre/html/tag.rb
146
147
  - lib/arbre/rails.rb
147
148
  - lib/arbre/rails/layouts.rb
@@ -156,6 +157,7 @@ files:
156
157
  - lib/arbre/rspec/contain_script_matcher.rb
157
158
  - lib/arbre/text_node.rb
158
159
  - lib/arbre/version.rb
160
+ - spec/arbre/html/style_hash_spec.rb
159
161
  - spec/arbre/integration/html_document_spec.rb
160
162
  - spec/arbre/integration/html_spec.rb
161
163
  - spec/arbre/integration/querying_spec.rb
@@ -216,6 +218,7 @@ signing_key:
216
218
  specification_version: 4
217
219
  summary: An Object Oriented DOM Tree in Ruby
218
220
  test_files:
221
+ - spec/arbre/html/style_hash_spec.rb
219
222
  - spec/arbre/integration/html_document_spec.rb
220
223
  - spec/arbre/integration/html_spec.rb
221
224
  - spec/arbre/integration/querying_spec.rb