hexp 0.0.1 → 0.2.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.
Files changed (69) hide show
  1. data/.travis.yml +12 -3
  2. data/Changelog.md +9 -0
  3. data/Gemfile +3 -5
  4. data/Gemfile.devtools +20 -18
  5. data/Gemfile.lock +97 -84
  6. data/Rakefile +16 -0
  7. data/config/flay.yml +2 -2
  8. data/config/flog.yml +1 -1
  9. data/config/reek.yml +42 -18
  10. data/config/rubocop.yml +31 -0
  11. data/config/yardstick.yml +39 -1
  12. data/examples/from_nokogiri.rb +77 -0
  13. data/examples/selector_rewriter_chaining.rb +14 -0
  14. data/examples/todo.rb +138 -0
  15. data/examples/widget.rb +64 -0
  16. data/hexp.gemspec +8 -3
  17. data/lib/hexp.rb +103 -2
  18. data/lib/hexp/builder.rb +256 -0
  19. data/lib/hexp/css_selector.rb +205 -0
  20. data/lib/hexp/css_selector/parser.rb +74 -0
  21. data/lib/hexp/css_selector/sass_parser.rb +22 -0
  22. data/lib/hexp/dom.rb +0 -2
  23. data/lib/hexp/dsl.rb +27 -0
  24. data/lib/hexp/errors.rb +21 -0
  25. data/lib/hexp/h.rb +5 -2
  26. data/lib/hexp/list.rb +67 -9
  27. data/lib/hexp/node.rb +197 -41
  28. data/lib/hexp/node/attributes.rb +176 -0
  29. data/lib/hexp/node/children.rb +44 -0
  30. data/lib/hexp/node/css_selection.rb +73 -0
  31. data/lib/hexp/node/domize.rb +52 -6
  32. data/lib/hexp/node/normalize.rb +19 -9
  33. data/lib/hexp/node/pp.rb +32 -0
  34. data/lib/hexp/node/rewriter.rb +52 -0
  35. data/lib/hexp/node/selector.rb +59 -0
  36. data/lib/hexp/nokogiri/equality.rb +61 -0
  37. data/lib/hexp/nokogiri/reader.rb +27 -0
  38. data/lib/hexp/sass/selector_parser.rb +4 -0
  39. data/lib/hexp/text_node.rb +129 -9
  40. data/lib/hexp/version.rb +1 -1
  41. data/notes +34 -0
  42. data/spec/shared_helper.rb +6 -0
  43. data/spec/spec_helper.rb +2 -6
  44. data/spec/unit/hexp/builder_spec.rb +101 -0
  45. data/spec/unit/hexp/css_selector/attribute_spec.rb +137 -0
  46. data/spec/unit/hexp/css_selector/class_spec.rb +15 -0
  47. data/spec/unit/hexp/css_selector/comma_sequence_spec.rb +20 -0
  48. data/spec/unit/hexp/css_selector/element_spec.rb +11 -0
  49. data/spec/unit/hexp/css_selector/parser_spec.rb +51 -0
  50. data/spec/unit/hexp/css_selector/simple_sequence_spec.rb +48 -0
  51. data/spec/unit/hexp/dsl_spec.rb +55 -0
  52. data/spec/unit/hexp/h_spec.rb +38 -0
  53. data/spec/unit/hexp/list_spec.rb +19 -0
  54. data/spec/unit/hexp/node/attr_spec.rb +55 -0
  55. data/spec/unit/hexp/node/attributes_spec.rb +125 -0
  56. data/spec/unit/hexp/node/children_spec.rb +33 -0
  57. data/spec/unit/hexp/node/class_spec.rb +37 -0
  58. data/spec/unit/hexp/node/css_selection_spec.rb +86 -0
  59. data/spec/unit/hexp/node/normalize_spec.rb +12 -6
  60. data/spec/unit/hexp/node/rewrite_spec.rb +67 -30
  61. data/spec/unit/hexp/node/selector_spec.rb +78 -0
  62. data/spec/unit/hexp/node/text_spec.rb +7 -0
  63. data/spec/unit/hexp/node/to_dom_spec.rb +1 -1
  64. data/spec/unit/hexp/nokogiri/reader_spec.rb +8 -0
  65. data/spec/unit/hexp/parse_spec.rb +23 -0
  66. data/spec/unit/hexp/text_node_spec.rb +25 -0
  67. data/spec/unit/hexp_spec.rb +33 -0
  68. metadata +129 -16
  69. data/lib/hexp/format_error.rb +0 -8
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hexp::Node::Selector do
4
+ subject(:selector) { Hexp::Node::Selector.new(hexp, block) }
5
+ let(:yielded_elements) { [] }
6
+ let(:block) { proc {|el| yielded_elements << el } }
7
+ let(:hexp) { H[:div, [[:span]]] }
8
+
9
+ describe 'as Enumerable' do
10
+ let(:block) { proc {|el| el.tag == :span} }
11
+
12
+ it 'should enumerate elements for which the block returns trueish' do
13
+ expect(selector.to_a).to eq [H[:span]]
14
+ end
15
+ end
16
+
17
+ describe 'rewriting operations' do
18
+ let(:block) { proc {|el| el.tag == :span} }
19
+
20
+ it 'should perform them on elements that match' do
21
+ expect(selector.attr('class', 'matched').to_hexp).to eq(
22
+ H[:div, [[:span, {class: 'matched'}]]]
23
+ )
24
+ end
25
+
26
+ describe 'wrap' do
27
+ let(:hexp) { H[:ul, [[:a, href: 'foo'], [:a, href: 'bar']]] }
28
+ let(:block) { proc {|el| el.tag == :a} }
29
+
30
+ it 'should be able to wrap element' do
31
+ expect(selector.wrap(:li).to_hexp).to eq(
32
+ H[:ul, [[:li, H[:a, href: 'foo']], [:li, H[:a, href: 'bar']]]]
33
+ )
34
+ end
35
+ end
36
+
37
+ describe 'rewrite' do
38
+ let(:hexp) { H[:ul, [[:a, href: 'foo'], [:span]]] }
39
+ let(:block) { proc {|el| el.tag == :a} }
40
+
41
+ it 'should work on matching elements, and skip the rest' do
42
+ expect(selector.rewrite{ H[:br] }.to_hexp).to eq H[:ul, [[:br], [:span]]]
43
+ end
44
+ end
45
+ end
46
+
47
+ context 'with a single element' do
48
+ let(:hexp) { H[:div] }
49
+
50
+ it 'should be lazy' do
51
+ expect(block).to_not receive(:call)
52
+ selector
53
+ end
54
+
55
+ it 'should yield the root element when realized' do
56
+ expect(block).to receive(:call).once.with(H[:div])
57
+ selector.each {}
58
+ end
59
+ end
60
+
61
+ context 'with nested elements' do
62
+ let(:hexp) {
63
+ H[:div, [
64
+ [:span, "hello"],
65
+ [:span, "world"]]]}
66
+
67
+ it 'should traverse the whole tree once, depth-first' do
68
+ selector.each {}
69
+ expect(yielded_elements).to eq [
70
+ Hexp::TextNode.new("hello"),
71
+ H[:span, "hello"],
72
+ Hexp::TextNode.new("world"),
73
+ H[:span, "world"],
74
+ hexp
75
+ ]
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hexp::Node do
4
+ subject { described_class[:p] }
5
+
6
+ its(:text?) { should be_false }
7
+ end
@@ -4,7 +4,7 @@ describe Hexp::Node, 'to_dom' do
4
4
  subject { Hexp::Node[:blink] }
5
5
 
6
6
  it 'should delegate to Domize' do
7
- Hexp::Node::Domize.should_receive(:new).with(subject).and_return( ->{ 'result' } )
7
+ expect(Hexp::Node::Domize).to receive(:new).with(subject, {}).and_return( ->{ 'result' } )
8
8
  subject.to_dom.should == 'result'
9
9
  end
10
10
  end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hexp::Nokogiri::Reader do
4
+ let(:doc) { Nokogiri::HTML::Document.new }
5
+
6
+
7
+
8
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hexp, 'parse' do
4
+ context 'with an empty document' do
5
+ let(:html) { '' }
6
+
7
+ it 'should raise an exception' do
8
+ expect{ Hexp.parse(html) }.to raise_exception Hexp::ParseError
9
+ end
10
+ end
11
+
12
+ it 'should parse a single tag' do
13
+ expect(Hexp.parse('<a>Hello!</a>')).to eq H[:a, 'Hello!']
14
+ end
15
+
16
+ it 'should parse nested tags' do
17
+ expect(Hexp.parse('<a>Ciao <em>Bella</em></a>')).to eq H[:a, ['Ciao ', H[:em, 'Bella']]]
18
+ end
19
+
20
+ it 'should parse attributes' do
21
+ expect(Hexp.parse('<a href="pretty">Ciao Bella</a>')).to eq H[:a, {href: 'pretty'}, 'Ciao Bella']
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hexp::TextNode do
4
+ subject { described_class.new('some string') }
5
+
6
+ describe 'Node triplet' do
7
+ its(:tag) { should be_nil }
8
+ its(:attributes) { should eq({}) }
9
+ its(:attributes) { should be_frozen }
10
+ its(:children) { should eq([]) }
11
+ its(:children) { should be_frozen }
12
+ end
13
+
14
+ describe 'DSL methods' do
15
+ its(:text?) { should be_true }
16
+ its(:rewrite) { should eq(subject) }
17
+ its(:to_hexp) { should eq(subject) }
18
+
19
+ describe 'class?' do
20
+ it 'should always return false' do
21
+ expect(subject.class?('strong')).to be_false
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hexp, 'Array' do
4
+ context 'with an array as input' do
5
+ it 'should return the array' do
6
+ expect(Hexp.Array([:foo])).to eq([:foo])
7
+ end
8
+ end
9
+
10
+ context 'with a single object as an input' do
11
+ it 'should wrap it in an array' do
12
+ expect(Hexp.Array(:foo)).to eq([:foo])
13
+ end
14
+ end
15
+
16
+ context 'with an object that responds to to_ary' do
17
+ let(:array_like) do
18
+ Class.new { def to_ary; [1,2,3] ; end }.new
19
+ end
20
+
21
+ it 'should return the result of to_ary' do
22
+ expect(Hexp.Array(array_like)).to eq([1,2,3])
23
+ end
24
+ end
25
+ end
26
+
27
+ describe Hexp, 'build' do
28
+ it 'should delegate to Hexp::Builder.new' do
29
+ block = proc {}
30
+ expect(Hexp::Builder).to receive(:new).with(:div, class: 'moambe', &block)
31
+ Hexp.build(:div, class: 'moambe', &block)
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hexp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,56 +9,104 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-14 00:00:00.000000000 Z
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: nokogiri
15
+ name: sass
16
+ type: :runtime
16
17
  requirement: !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ~>
20
21
  - !ruby/object:Gem::Version
21
- version: 1.5.9
22
- type: :runtime
22
+ version: '3.2'
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: '3.2'
23
29
  prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ type: :runtime
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.6'
24
39
  version_requirements: !ruby/object:Gem::Requirement
25
40
  none: false
26
41
  requirements:
27
42
  - - ~>
28
43
  - !ruby/object:Gem::Version
29
- version: 1.5.9
44
+ version: '1.6'
45
+ prerelease: false
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: ice_nine
48
+ type: :runtime
32
49
  requirement: !ruby/object:Gem::Requirement
33
50
  none: false
34
51
  requirements:
35
52
  - - ~>
36
53
  - !ruby/object:Gem::Version
37
- version: 0.7.0
38
- type: :runtime
39
- prerelease: false
54
+ version: '0.8'
40
55
  version_requirements: !ruby/object:Gem::Requirement
41
56
  none: false
42
57
  requirements:
43
58
  - - ~>
44
59
  - !ruby/object:Gem::Version
45
- version: 0.7.0
60
+ version: '0.8'
61
+ prerelease: false
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: equalizer
64
+ type: :runtime
48
65
  requirement: !ruby/object:Gem::Requirement
49
66
  none: false
50
67
  requirements:
51
68
  - - ~>
52
69
  - !ruby/object:Gem::Version
53
- version: 0.0.5
54
- type: :runtime
70
+ version: '0.0'
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '0.0'
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ type: :development
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: '10.1'
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ version: '10.1'
55
93
  prerelease: false
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ type: :development
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: '2.14'
56
103
  version_requirements: !ruby/object:Gem::Requirement
57
104
  none: false
58
105
  requirements:
59
106
  - - ~>
60
107
  - !ruby/object:Gem::Version
61
- version: 0.0.5
108
+ version: '2.14'
109
+ prerelease: false
62
110
  description: HTML expressions
63
111
  email:
64
112
  - arne@arnebrasseur.net
@@ -69,6 +117,7 @@ extra_rdoc_files:
69
117
  files:
70
118
  - .gitignore
71
119
  - .travis.yml
120
+ - Changelog.md
72
121
  - Gemfile
73
122
  - Gemfile.devtools
74
123
  - Gemfile.lock
@@ -81,35 +130,75 @@ files:
81
130
  - config/flog.yml
82
131
  - config/mutant.yml
83
132
  - config/reek.yml
133
+ - config/rubocop.yml
84
134
  - config/yardstick.yml
85
135
  - examples/filter.rb
136
+ - examples/from_nokogiri.rb
86
137
  - examples/hexp_rails.rb
138
+ - examples/selector_rewriter_chaining.rb
139
+ - examples/todo.rb
140
+ - examples/widget.rb
87
141
  - hexp.gemspec
88
142
  - lib/hexp.rb
143
+ - lib/hexp/builder.rb
144
+ - lib/hexp/css_selector.rb
145
+ - lib/hexp/css_selector/parser.rb
146
+ - lib/hexp/css_selector/sass_parser.rb
89
147
  - lib/hexp/dom.rb
90
- - lib/hexp/format_error.rb
148
+ - lib/hexp/dsl.rb
149
+ - lib/hexp/errors.rb
91
150
  - lib/hexp/h.rb
92
151
  - lib/hexp/list.rb
93
152
  - lib/hexp/node.rb
153
+ - lib/hexp/node/attributes.rb
154
+ - lib/hexp/node/children.rb
155
+ - lib/hexp/node/css_selection.rb
94
156
  - lib/hexp/node/domize.rb
95
157
  - lib/hexp/node/normalize.rb
96
158
  - lib/hexp/node/pp.rb
159
+ - lib/hexp/node/rewriter.rb
160
+ - lib/hexp/node/selector.rb
97
161
  - lib/hexp/nokogiri/equality.rb
162
+ - lib/hexp/nokogiri/reader.rb
163
+ - lib/hexp/sass/selector_parser.rb
98
164
  - lib/hexp/text_node.rb
99
165
  - lib/hexp/version.rb
166
+ - notes
100
167
  - spec/integration/literal_syntax_spec.rb
101
168
  - spec/shared_helper.rb
102
169
  - spec/spec_helper.rb
170
+ - spec/unit/hexp/builder_spec.rb
171
+ - spec/unit/hexp/css_selector/attribute_spec.rb
172
+ - spec/unit/hexp/css_selector/class_spec.rb
173
+ - spec/unit/hexp/css_selector/comma_sequence_spec.rb
174
+ - spec/unit/hexp/css_selector/element_spec.rb
175
+ - spec/unit/hexp/css_selector/parser_spec.rb
176
+ - spec/unit/hexp/css_selector/simple_sequence_spec.rb
177
+ - spec/unit/hexp/dsl_spec.rb
178
+ - spec/unit/hexp/h_spec.rb
179
+ - spec/unit/hexp/list_spec.rb
180
+ - spec/unit/hexp/node/attr_spec.rb
181
+ - spec/unit/hexp/node/attributes_spec.rb
182
+ - spec/unit/hexp/node/children_spec.rb
183
+ - spec/unit/hexp/node/class_spec.rb
184
+ - spec/unit/hexp/node/css_selection_spec.rb
103
185
  - spec/unit/hexp/node/domize_spec.rb
104
186
  - spec/unit/hexp/node/normalize_spec.rb
105
187
  - spec/unit/hexp/node/pp_spec.rb
106
188
  - spec/unit/hexp/node/rewrite_spec.rb
189
+ - spec/unit/hexp/node/selector_spec.rb
190
+ - spec/unit/hexp/node/text_spec.rb
107
191
  - spec/unit/hexp/node/to_dom_spec.rb
108
192
  - spec/unit/hexp/node/to_hexp_spec.rb
109
193
  - spec/unit/hexp/node/to_html_spec.rb
110
194
  - spec/unit/hexp/nokogiri/equality_spec.rb
195
+ - spec/unit/hexp/nokogiri/reader_spec.rb
196
+ - spec/unit/hexp/parse_spec.rb
197
+ - spec/unit/hexp/text_node_spec.rb
198
+ - spec/unit/hexp_spec.rb
111
199
  homepage: https://github.com/plexus/hexp
112
- licenses: []
200
+ licenses:
201
+ - MIT
113
202
  post_install_message:
114
203
  rdoc_options: []
115
204
  require_paths:
@@ -122,13 +211,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
211
  version: '0'
123
212
  segments:
124
213
  - 0
125
- hash: -3683471669103865183
214
+ hash: -1985616243020717322
126
215
  required_rubygems_version: !ruby/object:Gem::Requirement
127
216
  none: false
128
217
  requirements:
129
218
  - - ! '>='
130
219
  - !ruby/object:Gem::Version
131
220
  version: '0'
221
+ segments:
222
+ - 0
223
+ hash: -1985616243020717322
132
224
  requirements: []
133
225
  rubyforge_project:
134
226
  rubygems_version: 1.8.23
@@ -139,11 +231,32 @@ test_files:
139
231
  - spec/integration/literal_syntax_spec.rb
140
232
  - spec/shared_helper.rb
141
233
  - spec/spec_helper.rb
234
+ - spec/unit/hexp/builder_spec.rb
235
+ - spec/unit/hexp/css_selector/attribute_spec.rb
236
+ - spec/unit/hexp/css_selector/class_spec.rb
237
+ - spec/unit/hexp/css_selector/comma_sequence_spec.rb
238
+ - spec/unit/hexp/css_selector/element_spec.rb
239
+ - spec/unit/hexp/css_selector/parser_spec.rb
240
+ - spec/unit/hexp/css_selector/simple_sequence_spec.rb
241
+ - spec/unit/hexp/dsl_spec.rb
242
+ - spec/unit/hexp/h_spec.rb
243
+ - spec/unit/hexp/list_spec.rb
244
+ - spec/unit/hexp/node/attr_spec.rb
245
+ - spec/unit/hexp/node/attributes_spec.rb
246
+ - spec/unit/hexp/node/children_spec.rb
247
+ - spec/unit/hexp/node/class_spec.rb
248
+ - spec/unit/hexp/node/css_selection_spec.rb
142
249
  - spec/unit/hexp/node/domize_spec.rb
143
250
  - spec/unit/hexp/node/normalize_spec.rb
144
251
  - spec/unit/hexp/node/pp_spec.rb
145
252
  - spec/unit/hexp/node/rewrite_spec.rb
253
+ - spec/unit/hexp/node/selector_spec.rb
254
+ - spec/unit/hexp/node/text_spec.rb
146
255
  - spec/unit/hexp/node/to_dom_spec.rb
147
256
  - spec/unit/hexp/node/to_hexp_spec.rb
148
257
  - spec/unit/hexp/node/to_html_spec.rb
149
258
  - spec/unit/hexp/nokogiri/equality_spec.rb
259
+ - spec/unit/hexp/nokogiri/reader_spec.rb
260
+ - spec/unit/hexp/parse_spec.rb
261
+ - spec/unit/hexp/text_node_spec.rb
262
+ - spec/unit/hexp_spec.rb