motion-html 1.1.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +26 -11
- data/lib/motion-html.rb +1 -1
- data/lib/project/motion-html.rb +60 -9
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e2faabbfcaa7668e24d6b087636f6f72706e269438dcea8d39147fa3377c6cf5
|
4
|
+
data.tar.gz: 8e21b1c802d230b06d6aa5605cdee6ce76fb4a7734bc73a4edc0cedeb88a38d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e06dda3b1d3997a1c2063a6a90ae01a246df98a853b65d17d3821f9ac91a6143a09bae92da80b0a0547c0998f06bf78cbe4a78d0e03ffc2e3019a9734b25218
|
7
|
+
data.tar.gz: 301cee7bf766da1bd86fb832ea730d5dbc14e1401084a03d647de146169e6e436c16041803686db616f8dcbd96e75cca2a9c91ad55ce41224ffc4560b1adc63b
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# motion-html
|
2
2
|
|
3
3
|
Parse and traverse HTML in your RubyMotion app. It's like Nokogiri for RubyMotion!
|
4
4
|
|
5
|
-
|
5
|
+
`motion-html` uses [HTMLKit](https://github.com/iabudiab/HTMLKit) under the hood.
|
6
6
|
|
7
7
|
Currently, only iOS and OS X are supported.
|
8
8
|
|
@@ -10,7 +10,7 @@ Currently, only iOS and OS X are supported.
|
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
|
-
gem 'motion-html'
|
13
|
+
gem 'motion-html', '~> 2.0'
|
14
14
|
|
15
15
|
And then execute:
|
16
16
|
|
@@ -22,23 +22,38 @@ Initialize a new document:
|
|
22
22
|
```ruby
|
23
23
|
doc = Motion::HTML::Doc.new(html)
|
24
24
|
# or simply:
|
25
|
-
doc =
|
25
|
+
doc = HTML.parse(html)
|
26
26
|
```
|
27
27
|
|
28
|
-
Then query using
|
28
|
+
Then query using CSS3 selectors to find one or an array of elements:
|
29
29
|
```ruby
|
30
|
-
|
30
|
+
array_of_links = doc.all('.photos li a')
|
31
|
+
first_li = doc.first('.photos li')
|
32
|
+
element = doc.find('#one-and-only')
|
31
33
|
```
|
32
34
|
|
33
|
-
|
35
|
+
Each item returned from a query will be an `Element` object. You can then access attributes or text content:
|
34
36
|
```ruby
|
35
|
-
|
36
|
-
|
37
|
-
puts "Link
|
37
|
+
links = doc.all('.photos li a')
|
38
|
+
links.each do |el|
|
39
|
+
puts "Link tag: " + el.tag
|
40
|
+
puts "Link text: " + el.text
|
41
|
+
puts "Link URL: " + el['href']
|
42
|
+
puts "Link attributes: " + el.attributes.inspect
|
38
43
|
end
|
39
44
|
```
|
40
45
|
|
41
|
-
|
46
|
+
With an `Element`, you can traverse to its parent, children, or next sibling:
|
47
|
+
```ruby
|
48
|
+
first_list_item = doc.first('.photos li')
|
49
|
+
unordered_list = first_list_item.parent
|
50
|
+
links = first_list_item.children
|
51
|
+
second_list_item = first_list_item.next_sibling
|
52
|
+
```
|
53
|
+
|
54
|
+
See the [`main_spec.rb`](https://github.com/andrewhavens/motion-html/blob/master/spec/main_spec.rb) file for more examples.
|
55
|
+
|
56
|
+
Feel like something is missing? Submit a GitHub issue if you need anything other features.
|
42
57
|
|
43
58
|
## Contributing
|
44
59
|
|
data/lib/motion-html.rb
CHANGED
data/lib/project/motion-html.rb
CHANGED
@@ -6,19 +6,70 @@ module Motion
|
|
6
6
|
|
7
7
|
class Doc
|
8
8
|
def initialize(html)
|
9
|
-
@doc =
|
9
|
+
@doc = HTMLDocument.documentWithString(html)
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
def all(q)
|
13
|
+
nodes = @doc.querySelectorAll(q)
|
14
|
+
nodes.map {|node| Element.new(node) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def first(q)
|
18
|
+
all(q).first
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :find, :first
|
22
|
+
end
|
23
|
+
|
24
|
+
class Element
|
25
|
+
def initialize(element)
|
26
|
+
@element = element
|
27
|
+
end
|
28
|
+
|
29
|
+
def tag
|
30
|
+
@element.tagName
|
31
|
+
end
|
32
|
+
|
33
|
+
def text
|
34
|
+
@element.textContent
|
35
|
+
end
|
36
|
+
|
37
|
+
def attributes
|
38
|
+
@element.attributes
|
39
|
+
end
|
40
|
+
|
41
|
+
def [](key)
|
42
|
+
attributes[key]
|
43
|
+
end
|
44
|
+
|
45
|
+
def parent
|
46
|
+
el = @element.parentNode
|
47
|
+
Element.new(el) if el.is_a? HTMLElement
|
48
|
+
end
|
49
|
+
|
50
|
+
def children
|
51
|
+
elements = []
|
52
|
+
@element.childNodes.each do |node|
|
53
|
+
elements << Element.new(node) if node.is_a? HTMLElement
|
17
54
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
55
|
+
elements
|
56
|
+
end
|
57
|
+
|
58
|
+
def next_sibling
|
59
|
+
el = @element.nextSibling
|
60
|
+
el = el.nextSibling if el.is_a? HTMLText
|
61
|
+
Element.new(el) if el.is_a? HTMLElement
|
62
|
+
end
|
63
|
+
|
64
|
+
def inspect
|
65
|
+
to_html
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_html
|
69
|
+
@element.outerHTML
|
21
70
|
end
|
22
71
|
end
|
23
72
|
end
|
24
73
|
end
|
74
|
+
|
75
|
+
HTML = Motion::HTML
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: motion-html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Havens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: motion-cocoapods
|
@@ -68,8 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
|
-
|
72
|
-
rubygems_version: 2.5.2
|
71
|
+
rubygems_version: 3.0.6
|
73
72
|
signing_key:
|
74
73
|
specification_version: 4
|
75
74
|
summary: Parse and traverse HTML in your RubyMotion app. It's like Nokogiri for RubyMotion!
|