html2bbcode 1.0.0

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.
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Bbcode::ClosingTags do
4
+ let(:bbcode_element) { double 'bbcode element'}
5
+ subject{ described_class.new bbcode_element }
6
+
7
+ describe '#tags' do
8
+ context 'the names contain the * (li html) tag' do
9
+ before { subject.stub names: %w(*) }
10
+
11
+ its(:tags) { should eq '' }
12
+ end
13
+
14
+ context 'the names contain any other tag' do
15
+ before { subject.stub names: %w(b) }
16
+
17
+ its(:tags) { should eq '[/b]' }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Bbcode::Element do
4
+ let(:html_element) { double 'html element' }
5
+ subject { described_class.new html_element }
6
+
7
+ describe '#opening_tags' do
8
+ let(:opening_tags) { double 'opening tags', tags: %w(b) }
9
+ before { subject.stub get_opening_tags: opening_tags }
10
+
11
+ it 'delegates method to the opening tags object' do
12
+ subject.opening_tags
13
+ expect(opening_tags).to have_received :tags
14
+ end
15
+ end
16
+
17
+ describe '#closing_tags' do
18
+ let(:closing_tags) { double 'closing tags', tags: %w(b) }
19
+ before { subject.stub get_closing_tags: closing_tags }
20
+
21
+ it 'delegates method to the closing tags object' do
22
+ subject.closing_tags
23
+ expect(closing_tags).to have_received :tags
24
+ end
25
+ end
26
+
27
+ describe '#names' do
28
+ before { subject.stub name: 'a' }
29
+
30
+ its(:names) { should match_array ['a'] }
31
+ end
32
+
33
+ describe '#name' do
34
+ let(:rules) { double 'rules', bbcode_name: 'b' }
35
+ before { subject.stub rules: rules }
36
+
37
+ it 'delegates method to the rules object' do
38
+ subject.name
39
+ expect(rules).to have_received :bbcode_name
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Bbcode::OpeningTags do
4
+ let(:bbcode_element) { double 'bbcode element'}
5
+ subject{ described_class.new bbcode_element }
6
+
7
+ describe '#tags' do
8
+ before { subject.stub names: %w(b u) }
9
+
10
+ its(:tags) { should eq '[b][u]' }
11
+
12
+ context 'the names contain the url tag' do
13
+ let(:href) { 'www.example.com' }
14
+ before { subject.stub names: %w(url), href: href, text: text }
15
+
16
+ context 'the text is the same like the href attribute' do
17
+ let(:text) { href }
18
+
19
+ its(:tags) { should eq '[url]' }
20
+ end
21
+
22
+ context 'the text is different than the href attribute' do
23
+ let(:text) { 'A title for the link' }
24
+
25
+ its(:tags) { should eq "[url=#{href}]" }
26
+ end
27
+ end
28
+
29
+ context 'the names contain the size tag' do
30
+ before { subject.stub names: %w(size), font_size: 15 }
31
+
32
+ its(:tags) { should eq "[size=15]" }
33
+ end
34
+
35
+ context 'the names contain the color tag' do
36
+ before { subject.stub names: %w(color), font_color: 'blue' }
37
+
38
+ its(:tags) { should eq "[color=blue]" }
39
+ end
40
+
41
+ context 'the names contain the * (li html) tag' do
42
+ before { subject.stub names: %w(*) }
43
+
44
+ its(:tags) { should eq '[*]' }
45
+ end
46
+
47
+ context 'the names contain the list tag' do
48
+ before { subject.stub names: %w(list) }
49
+
50
+ context 'the html tag is ul' do
51
+ before { subject.stub html_name: 'ul' }
52
+
53
+ its(:tags) { should eq '[list]'}
54
+ end
55
+
56
+ context 'the html tag is ol' do
57
+ before { subject.stub html_name: 'ol' }
58
+
59
+ its(:tags) { should eq '[list=1]'}
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,167 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Bbcode::Rules do
4
+ let(:bbcode_element) { double 'bbcode elemenet' }
5
+ subject { described_class.new bbcode_element }
6
+ before { subject.stub html_name: html_name }
7
+
8
+ %w(b i u s img table tr td).each do |tag_name|
9
+ context "html element name is: #{tag_name}" do
10
+ let(:html_name) { tag_name }
11
+
12
+ its(:bbcode_name) { should eq tag_name }
13
+ end
14
+ end
15
+
16
+ context 'html element name is: a' do
17
+ let(:html_name) { 'a' }
18
+ before { subject.stub html_href: href }
19
+
20
+ context 'html element contains a href attribute' do
21
+ let(:href) { 'www.example.com' }
22
+
23
+ its(:bbcode_name) { should eq 'url' }
24
+ end
25
+
26
+ context 'html element does not contain a href attribute' do
27
+ let(:href) { nil }
28
+
29
+ its(:bbcode_name) { should be_nil }
30
+ end
31
+ end
32
+
33
+ context 'html element name is: blockquote' do
34
+ let(:html_name) { 'blockquote' }
35
+
36
+ its(:bbcode_name) { should eq 'quote' }
37
+ end
38
+
39
+ context 'html element name is: pre' do
40
+ let(:html_name) { 'pre' }
41
+
42
+ its(:bbcode_name) { should eq 'code' }
43
+ end
44
+
45
+ context 'html element name is: strong' do
46
+ let(:html_name) { 'strong' }
47
+
48
+ its(:bbcode_name) { should eq 'b' }
49
+ end
50
+
51
+ context 'html element name is: em' do
52
+ let(:html_name) { 'em' }
53
+
54
+ its(:bbcode_name) { should eq 'i' }
55
+ end
56
+
57
+ context 'html element name is: ins' do
58
+ let(:html_name) { 'ins' }
59
+
60
+ its(:bbcode_name) { should eq 'u' }
61
+ end
62
+
63
+ context 'html element name is: del' do
64
+ let(:html_name) { 'del' }
65
+
66
+ its(:bbcode_name) { should eq 's' }
67
+ end
68
+
69
+ context 'html element name is: font' do
70
+ let(:html_name) { 'font' }
71
+
72
+ before { subject.stub html_font_size: nil, html_color: nil }
73
+
74
+ its(:bbcode_name) { should be_nil }
75
+
76
+ context 'attributes contain the size' do
77
+ before { subject.stub html_font_size: '15' }
78
+
79
+ its(:bbcode_name) { should eq 'size' }
80
+ end
81
+
82
+ context 'attributes contain the color' do
83
+ before { subject.stub html_color: 'red' }
84
+
85
+ its(:bbcode_name) { should eq 'color' }
86
+ end
87
+ end
88
+
89
+ context 'html element name is ul' do
90
+ let(:html_name) { 'ul' }
91
+
92
+ its(:bbcode_name) { should eq 'list' }
93
+ end
94
+
95
+ context 'html element name is ol' do
96
+ let(:html_name) { 'ol' }
97
+
98
+ its(:bbcode_name) { should eq 'list' }
99
+ end
100
+
101
+ context 'html element name is li' do
102
+ let(:html_name) { 'li' }
103
+
104
+ its(:bbcode_name) { should eq '*' }
105
+ end
106
+
107
+ context 'html element name is: span' do
108
+ let(:html_name) { 'span' }
109
+ before { subject.stub(html_styles: style) }
110
+
111
+ context 'html element does not contain a style' do
112
+ let(:style) { {} }
113
+
114
+ its(:bbcode_name) { should be_empty }
115
+ end
116
+
117
+ context 'html element contains a style' do
118
+ context 'the style is font-weight: bold' do
119
+ let(:style) { {'font-weight' => 'bold'} }
120
+
121
+ its(:bbcode_name) { should match_array %w(b) }
122
+ end
123
+
124
+ context 'the style is text-decoration: underline' do
125
+ let(:style) { {'text-decoration' => 'underline'} }
126
+
127
+ its(:bbcode_name) { should match_array %w(u) }
128
+ end
129
+
130
+ context 'the style is text-decoration: line-through' do
131
+ let(:style) { {'text-decoration' => 'line-through'} }
132
+
133
+ its(:bbcode_name) { should match_array %w(s) }
134
+ end
135
+
136
+ context 'the style is text-decoration: line-through' do
137
+ let(:style) { {'font-style' => 'italic'} }
138
+
139
+ its(:bbcode_name) { should match_array %w(i) }
140
+ end
141
+
142
+ context 'the style is font-size: 15px' do
143
+ let(:style) { {'font-size' => '15px'} }
144
+
145
+ its(:bbcode_name) { should match_array %w(size) }
146
+ end
147
+
148
+ context 'the style is color: red' do
149
+ let(:style) { {'color' => 'red'} }
150
+
151
+ its(:bbcode_name) { should match_array %w(color) }
152
+ end
153
+
154
+ context 'the style are font-style: italic and font-weight: bold' do
155
+ let(:style) { {'font-style' => 'italic', 'font-weight' => 'bold'} }
156
+
157
+ its(:bbcode_name) { should match_array %w(b i) }
158
+ end
159
+
160
+ context 'the style are font-weight: bold and margin-left: 20px' do
161
+ let(:style) { {'font-weight' => 'bold', 'margin-left' => '20px'} }
162
+
163
+ its(:bbcode_name) { should match_array %w(b) }
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Html::Attributes do
4
+ describe '#has_html_attribtues' do
5
+ class Element
6
+ extend Html2Bbcode::Html::Attributes
7
+ has_html_attributes :style
8
+ end
9
+
10
+ let(:html_element) { double 'html element' }
11
+
12
+ subject { Element.new }
13
+
14
+ before { subject.stub element: html_element }
15
+
16
+ context 'attributes does not contain given attribute' do
17
+ before { html_element.stub :[] }
18
+
19
+ its(:style) { should be_nil }
20
+ end
21
+
22
+ context 'attributes contains given attribute' do
23
+ before { html_element.stub(:[]).with('style').and_return 'color: red;' }
24
+
25
+ its(:style) { should eq 'color: red;' }
26
+ end
27
+
28
+ context 'given attribute is not defined in the html attributes' do
29
+ it 'raises an error about missing method' do
30
+ expect { subject.src }.to raise_error
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Html::Color do
4
+ let(:color) { nil }
5
+ subject { described_class.new color }
6
+
7
+ its(:get) { should be_nil }
8
+
9
+ context 'an element has a color attribute' do
10
+ context 'the color is a name of the color' do
11
+ let(:color) { 'blue' }
12
+
13
+ its(:get) { should eq color }
14
+ end
15
+
16
+ context 'the color is a hex value' do
17
+ let(:color) { '#003322' }
18
+
19
+ its(:get) { should eq color }
20
+ end
21
+
22
+ context 'the color is a rgb value' do
23
+ let(:color) { 'rgb(0, 64, 255)' }
24
+
25
+ its(:get) { should eq '#0040ff' }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,167 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode::Html::Element do
4
+ let(:nokogiri_html_element) { Nokogiri::HTML::Document.parse input_html }
5
+ let(:input_html) { 'Sample text' }
6
+
7
+ subject { described_class.new nokogiri_html_element }
8
+
9
+ it 'has the html attributes interface' do
10
+ expect(described_class).to respond_to :has_html_attributes
11
+ end
12
+
13
+ it 'has html attributes' do
14
+ expect(subject).to respond_to :href, :src, :size, :styles, :color
15
+ end
16
+
17
+ describe '#process' do
18
+ its(:process) { should eq input_html }
19
+
20
+ context 'contains nested html tags' do
21
+ let(:input_html) { 'normal <b> bolded words <i> italic and bolded </i> bolded </b> normal' }
22
+
23
+ its(:process) { should eq 'normal [b] bolded words [i] italic and bolded [/i] bolded [/b] normal'}
24
+ end
25
+
26
+ context 'contains the img tag' do
27
+ let(:input_html) { 'normal <img src="secret.png"></img>' }
28
+
29
+ its(:process) { should eq 'normal [img]secret.png[/img]'}
30
+ end
31
+
32
+ context 'contains the span tag with the font size & text decoration styles' do
33
+ let(:input_html) { '<span style="font-size: 15cm; text-decoration: underline;">text 15 cm and underline</span>' }
34
+
35
+ context 'the size unit is not set' do
36
+ subject { described_class.new nokogiri_html_element }
37
+
38
+ its(:process) { should eq '[u][size=15]text 15 cm and underline[/size][/u]'}
39
+ end
40
+
41
+ context 'the size unit is set' do
42
+ subject { described_class.new nokogiri_html_element, size_unit: 'mm' }
43
+
44
+ its(:process) { should eq '[u][size=150]text 15 cm and underline[/size][/u]'}
45
+ end
46
+ end
47
+
48
+ context 'contains the font tag with the color attribute & span tag with font-color style' do
49
+ let(:input_html) { '<font color="blue">blue text</font><span style="color: red;">red text</span>' }
50
+
51
+ its(:process) { should eq '[color=blue]blue text[/color][color=red]red text[/color]'}
52
+ end
53
+
54
+ context 'contains the ordered list' do
55
+ let(:input_html) { '<ol><li>first</li> <li>second</li></ol>'}
56
+
57
+ its(:process) { should eq '[list=1][*]first [*]second[/list]'}
58
+ end
59
+
60
+ context 'contains the unordered list' do
61
+ let(:input_html) { '<ul><li>first</li> <li>second</li></ul>'}
62
+
63
+ its(:process) { should eq '[list][*]first [*]second[/list]'}
64
+ end
65
+
66
+ context 'contains the table' do
67
+ let(:input_html) { '<table><tr><td>date</td></tr><tr><td>second date</td></tr></table>' }
68
+
69
+ its(:process) { should eq '[table][tr][td]date[/td][/tr][tr][td]second date[/td][/tr][/table]' }
70
+ end
71
+
72
+ context 'contains the pre tag with a content' do
73
+ let(:input_html) { '<pre><b>bolded</b></pre>' }
74
+
75
+ its(:process) { should eq '[code]<b>bolded</b>[/code]' }
76
+ end
77
+
78
+ context 'contains the br tag' do
79
+ let(:input_html) { '<br><br/>' }
80
+
81
+ its(:process) { should eq "\n\n" }
82
+ end
83
+ end
84
+
85
+ describe '#build_bbcode_element' do
86
+ before { subject.stub bbcode_opening_tags: '[a]', process: 'sample text', bbcode_closing_tags: '[/a]' }
87
+
88
+ its(:build_bbcode_element) { should eq '[a]sample text[/a]' }
89
+ end
90
+
91
+ describe '#to_bbcode_element' do
92
+ before { subject.stub(element?: false, text?: false) }
93
+
94
+ its(:to_bbcode_element) { should be_nil }
95
+
96
+ context 'the element name is br' do
97
+ before { subject.stub(name: 'br') }
98
+
99
+ its(:to_bbcode_element) { should eq "\n" }
100
+ end
101
+
102
+ context 'the element is an element in the nokogiri structure' do
103
+ let(:bbcode_element) { '[b]bolded[/b]' }
104
+ before { subject.stub(element?: true, build_bbcode_element: bbcode_element) }
105
+
106
+ its(:to_bbcode_element) { should eq bbcode_element }
107
+ end
108
+
109
+ context 'the element is a text in the nokogiri structure' do
110
+ let(:text) { 'Sample text' }
111
+ before { subject.stub(text?: true, text: text) }
112
+
113
+ its(:to_bbcode_element) { should eq text }
114
+ end
115
+ end
116
+
117
+ describe '#font_color' do
118
+ let(:element_color) { double 'element color', get: 'red' }
119
+ before { subject.stub element_color: element_color }
120
+
121
+ it 'delegates the method to the color object' do
122
+ subject.font_color
123
+ expect(element_color).to have_received :get
124
+ end
125
+ end
126
+
127
+ describe '#font_size' do
128
+ before do
129
+ nokogiri_html_element.stub(:[]).with('style').and_return nil
130
+ nokogiri_html_element.stub(:[]).with('size').and_return nil
131
+ end
132
+
133
+ its(:font_size) { should be_nil }
134
+
135
+ shared_examples_for 'a size unit converter' do
136
+ context 'size unit is the same like the unit of the size attribute' do
137
+ subject { described_class.new nokogiri_html_element, size_unit: 'cm' }
138
+
139
+ its(:font_size) { should eq 15 }
140
+ end
141
+
142
+ context 'size unit is different than the unit of the size attribute' do
143
+ subject { described_class.new nokogiri_html_element, size_unit: 'mm' }
144
+
145
+ its(:font_size) { should eq 150 }
146
+ end
147
+
148
+ context 'size unit is not set' do
149
+ subject { described_class.new nokogiri_html_element }
150
+
151
+ its(:font_size) { should eq 15 }
152
+ end
153
+ end
154
+
155
+ context 'element has a size attribute' do
156
+ before { nokogiri_html_element.stub(:[]).with('size').and_return '15cm' }
157
+
158
+ it_behaves_like 'a size unit converter'
159
+ end
160
+
161
+ context 'element has a style about the font size' do
162
+ before { nokogiri_html_element.stub(:[]).with('style').and_return 'font-size: 15cm;' }
163
+
164
+ it_behaves_like 'a size unit converter'
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2Bbcode do
4
+ let(:input_html) { 'Sample text' }
5
+
6
+ subject { Html2Bbcode.new input_html }
7
+
8
+ describe '#convert' do
9
+ let(:html_element) { double 'html element', process: true }
10
+
11
+ before { subject.stub html_element: html_element }
12
+
13
+ it 'calls process on the html element' do
14
+ subject.convert
15
+ expect(html_element).to have_received :process
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'custom unit definitions' do
4
+ describe 'pixels' do
5
+ subject { Unit.new('1 px') }
6
+
7
+ it 'has the unit' do
8
+ expect { subject }.to_not raise_error
9
+ end
10
+
11
+ it 'converts properly to the other unit' do
12
+ expect(subject.to('in')).to eq(Unit.new('1/72 in'))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'html2bbcode'
3
+ require 'pry'
4
+
5
+ RSpec.configure do |config|
6
+ config.order = 'random'
7
+ end