forma 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,74 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module Forma::Html
3
-
4
- class Element
5
-
6
- # Text for this element.
7
- attr_accessor :text
8
-
9
- # Safe text for this element.
10
- attr_accessor :safe
11
-
12
- def initialize(tag, h = {})
13
- self.tag = tag
14
- h = h.symbolize_keys
15
- self.text = h[:text]
16
- self.safe = h[:safe]
17
- @attributes = Attributes.new(h[:attrs] || h[:attributes])
18
- end
19
-
20
- def tag
21
- @tag
22
- end
23
-
24
- def tag=(t)
25
- raise "element's tag should be defined" if t.blank?
26
- @tag = t.to_s.downcase
27
- end
28
-
29
- def attributes
30
- @attributes
31
- end
32
-
33
- alias_method :attrs, :attributes
34
-
35
- def html
36
- generate_html
37
- end
38
-
39
- private
40
-
41
- def has_inner_content?
42
- not self.text.blank? or not self.safe.blank?
43
- end
44
-
45
- def generate_html
46
- h = ''
47
- if has_inner_content?
48
- h << '<' << generate_tag_and_attributes << '>'
49
- h << generate_inner_html
50
- h << '</' << self.tag << '>'
51
- else
52
- h << '<' << generate_tag_and_attributes << '/>'
53
- end
54
- h
55
- end
56
-
57
- def generate_tag_and_attributes
58
- h = ''
59
- attrs = @attributes.html
60
- h << self.tag
61
- h << ' ' << attrs unless attrs.blank?
62
- h
63
- end
64
-
65
- def generate_inner_html
66
- h = ''
67
- h << ERB::Util.html_escape(self.text) unless self.text.blank?
68
- h << self.safe unless self.safe.blank?
69
- h
70
- end
71
-
72
- end
73
-
74
- end
@@ -1,54 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
- include Forma::Html
4
-
5
- def test_attributes_emptiness(attrs, empty = true)
6
- if empty
7
- specify { attrs.klass.should be_empty }
8
- specify { attrs.style.should be_empty }
9
- specify { attrs.should be_empty }
10
- specify { attrs.html.should be_nil }
11
- else
12
- specify { attrs.should_not be_empty }
13
- specify { attrs.html.should_not be_nil }
14
- end
15
- end
16
-
17
- describe 'Empty attributes' do
18
- test_attributes_emptiness(Attributes.new)
19
- test_attributes_emptiness(Attributes.new(class: []))
20
- test_attributes_emptiness(Attributes.new(style: {}))
21
- test_attributes_emptiness(Attributes.new(class: [], style: {}))
22
- test_attributes_emptiness(Attributes.new(id: '1'), false)
23
- test_attributes_emptiness(Attributes.new(class: 'myclass'), false)
24
- test_attributes_emptiness(Attributes.new(style: {'font-size' => '12px'}), false)
25
- end
26
-
27
- describe 'Attributes creation' do
28
- before(:all) do
29
- @attrs = Attributes.new(id: 'id-1', class: 'myclass', 'data-method' => 'delete', style: { 'font-size' => '10px', 'color' => 'red' })
30
- @attrs.add_class('important')
31
- @attrs.add_style('font-weigh', 'bold')
32
- end
33
- specify { @attrs.klass.should == [ 'myclass', 'important' ] }
34
- specify { @attrs.style['font-size'].should == '10px' }
35
- specify { @attrs.style['color'].should == 'red' }
36
- specify { @attrs.style['font-weigh'].should == 'bold' }
37
- end
38
-
39
- describe 'Html generation' do
40
- before(:all) do
41
- @attr1 = Attributes.new(class: 'myclass')
42
- @attr2 = Attributes.new(class: ['class1', 'class2'])
43
- @attr3 = Attributes.new(style: {'font-size' => '14px', 'color' => 'red'})
44
- @attr4 = Attributes.new(class: ['class1', 'class2'], style: {'font-size' => '14px', 'color' => 'red'})
45
- @attr5 = Attributes.new(id: 'id1', class: ['class1', 'class2'], style: {'font-size' => '14px', 'color' => 'red'})
46
- @attr6 = Attributes.new(id: 'id1', class: ['class1', 'class2'], style: {'font-size' => '14px', 'color' => 'red'}, 'data-method' => 'delete')
47
- end
48
- specify { @attr1.html.should == 'class="myclass"' }
49
- specify { @attr2.html.should == 'class="class1 class2"' }
50
- specify { @attr3.html.should == 'style="font-size:14px;color:red"' }
51
- specify { @attr4.html.should == 'class="class1 class2" style="font-size:14px;color:red"' }
52
- specify { @attr5.html.should == 'class="class1 class2" style="font-size:14px;color:red" id="id1"' }
53
- specify { @attr6.html.should == 'class="class1 class2" style="font-size:14px;color:red" id="id1" data-method="delete"' }
54
- end
@@ -1,21 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'spec_helper'
3
- include Forma::Html
4
-
5
- describe 'Element creation' do
6
- compare_object(Element.new('div'), { tag: 'div', text: nil, safe: nil, html: '<div/>' })
7
- compare_object(Element.new('SPAN', text: 'some text'), { tag: 'span', text: 'some text', safe: nil, html: '<span>some text</span>' } )
8
- compare_object(Element.new('div', text: '<a>b</a>'), { html: '<div>&lt;a&gt;b&lt;/a&gt;</div>' })
9
- compare_object(Element.new('div', safe: '<a>b</a>'), { html: '<div><a>b</a></div>' })
10
- end
11
-
12
- describe 'Element html generation' do
13
- before(:all) do
14
- @el1 = Element.new('div')
15
- @el2 = Element.new('div', text: 'text')
16
- @el3 = Element.new('div', text: 'text', attrs: { class: 'class1' })
17
- end
18
- specify { @el1.html.should == '<div/>' }
19
- specify { @el2.html.should == '<div>text</div>' }
20
- specify { @el3.html.should == '<div class="class1">text</div>' }
21
- end