motion-html 1.1.0 → 2.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a98f7457f2d6df6ef5b6552c790d5c2fb6a64d8b
4
- data.tar.gz: 6afdc1de06f95cb62b1fa0848ba8401ac52b7ea2
2
+ SHA256:
3
+ metadata.gz: e2faabbfcaa7668e24d6b087636f6f72706e269438dcea8d39147fa3377c6cf5
4
+ data.tar.gz: 8e21b1c802d230b06d6aa5605cdee6ce76fb4a7734bc73a4edc0cedeb88a38d2
5
5
  SHA512:
6
- metadata.gz: 4a0f57cf2dd50288d8064b0983b7353f88ed273bed385635e48d28493be3da861a384930933179e611c251974a299beff4c0f2a763abed7a93824399ed0fd20c
7
- data.tar.gz: 79488428c3a146561aa9c1fa15bc6a6951183d20a7170365ea7efaf6c330ddcac755a64403f1ac182cd046029a18f8b6d03eccd83407f423cdf728a47ba7f676
6
+ metadata.gz: 7e06dda3b1d3997a1c2063a6a90ae01a246df98a853b65d17d3821f9ac91a6143a09bae92da80b0a0547c0998f06bf78cbe4a78d0e03ffc2e3019a9734b25218
7
+ data.tar.gz: 301cee7bf766da1bd86fb832ea730d5dbc14e1401084a03d647de146169e6e436c16041803686db616f8dcbd96e75cca2a9c91ad55ce41224ffc4560b1adc63b
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Motion::HTML
1
+ # motion-html
2
2
 
3
3
  Parse and traverse HTML in your RubyMotion app. It's like Nokogiri for RubyMotion!
4
4
 
5
- Motion::HTML uses [IGHTMLQuery](https://github.com/siuying/IGHTMLQuery) under the hood.
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 = Motion::HTML.parse(html)
25
+ doc = HTML.parse(html)
26
26
  ```
27
27
 
28
- Then query using css or xpath selectors. It returns an array of nodes:
28
+ Then query using CSS3 selectors to find one or an array of elements:
29
29
  ```ruby
30
- nodes = doc.query('.photos li a')
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
- Iterate over the results, and use the attributes or content:
35
+ Each item returned from a query will be an `Element` object. You can then access attributes or text content:
34
36
  ```ruby
35
- nodes.each do |node|
36
- puts "Link text: " + node.text
37
- puts "Link URL: " + node['href']
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
- So far, these are the only features that I have needed. Feel free to submit a GitHub issue if you need anything else.
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
 
@@ -10,6 +10,6 @@ lib_dir_path = File.dirname(File.expand_path(__FILE__))
10
10
  Motion::Project::App.setup do |app|
11
11
  app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
12
12
  app.pods do
13
- pod "IGHTMLQuery", "~> 0.9.1"
13
+ pod 'HTMLKit', '~> 3.1'
14
14
  end
15
15
  end
@@ -6,19 +6,70 @@ module Motion
6
6
 
7
7
  class Doc
8
8
  def initialize(html)
9
- @doc = IGHTMLDocument.alloc.initWithHTMLString(html, error: nil)
9
+ @doc = HTMLDocument.documentWithString(html)
10
10
  end
11
11
 
12
- def query(q)
13
- if q =~ %r{^[//|/]}
14
- node_set = @doc.queryWithXPath(q)
15
- else
16
- node_set = @doc.queryWithCSS(q)
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
- nodes = []
19
- node_set.enumerateNodesUsingBlock -> (node, index, stop) { nodes << node }
20
- nodes
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: 1.1.0
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: 2017-03-05 00:00:00.000000000 Z
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
- rubyforge_project:
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!