cocoa-xml 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cocoa-xml.rb CHANGED
@@ -13,7 +13,7 @@ module CocoaXML
13
13
  # object that responds to #read, or #to_str
14
14
  # @return [NSXMLDocument] An NSXMLDocument set to interpret source as HTML
15
15
  def self.HTML(source)
16
- parse source, NSXMLDocumentTidyHTML
16
+ parse source, :html
17
17
  end
18
18
 
19
19
  # Parse an input XML source
@@ -22,7 +22,15 @@ module CocoaXML
22
22
  # object that responds to #read, or #to_str
23
23
  # @return [NSXMLDocument] An NSXMLDocument set to inperpret source as XML
24
24
  def self.XML(source)
25
- parse source, NSXMLDocumentTidyXML
25
+ parse source, :xml
26
+ end
27
+
28
+ def self.test_filter(&block)
29
+ @@test_filter = lambda { |source, type| block.call(source, type) }
30
+ end
31
+
32
+ def self.test_capture(&block)
33
+ @@test_capture = lambda { |document, source, type| block.call(document, source, type) }
26
34
  end
27
35
 
28
36
  private
@@ -36,13 +44,20 @@ module CocoaXML
36
44
  def self.parse(source, type)
37
45
  error = Pointer.new :object
38
46
 
47
+ orig_source, orig_type, source, type = source, type, *@@test_filter.call(source, type) if class_variable_defined?(:@@test_filter)
48
+ type_id = (:xml == type) ? NSXMLDocumentTidyXML : NSXMLDocumentTidyHTML
49
+
39
50
  url = (source.is_a?(NSURL) && source) || NSURL.URLWithString(source.to_str) if source.respond_to?(:to_str)
40
- source = source.read if source.respond_to?(:read)
51
+ unread_source, source = source, source.read if source.respond_to?(:read)
52
+
53
+ doc = unless url.nil?
54
+ ::NSXMLDocument.alloc.initWithContentsOfURL url, options: type_id, error: error
55
+ else
56
+ ::NSXMLDocument.alloc.initWithXMLString source, options: type_id, error: error
57
+ end
58
+
59
+ @@test_capture.call(doc, orig_source || unread_source || source, orig_type || type) if class_variable_defined?(:@@test_capture)
41
60
 
42
- unless url.nil?
43
- ::NSXMLDocument.alloc.initWithContentsOfURL url, options: type, error: error
44
- else
45
- ::NSXMLDocument.alloc.initWithXMLString source, options: type, error: error
46
- end
61
+ doc
47
62
  end
48
63
  end
@@ -1,3 +1,3 @@
1
1
  module CocoaXML
2
- Version = '0.4.4'
2
+ Version = '0.4.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocoa-xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hoffman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-30 00:00:00 -06:00
12
+ date: 2010-02-05 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency