Module: Webrat::XML

Defined in:
lib/webrat/core/xml.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.all_inner_text(element) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/webrat/core/xml.rb', line 48

def self.all_inner_text(element)
  if Webrat.configuration.parse_with_nokogiri?
    element.inner_text
  else
    Hpricot(element.to_s).children.first.inner_text
  end
end

.attribute(element, attribute_name) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/webrat/core/xml.rb', line 76

def self.attribute(element, attribute_name)
  return element[attribute_name] if element.is_a?(Hash)
  
  if Webrat.configuration.parse_with_nokogiri?
    element[attribute_name]
  else
    element.attributes[attribute_name]
  end
end

.css_at(*args) ⇒ Object



90
91
92
# File 'lib/webrat/core/xml.rb', line 90

def self.css_at(*args)
  css_search(*args).first
end

.css_search(element, *searches) ⇒ Object

:nodoc:



104
105
106
# File 'lib/webrat/core/xml.rb', line 104

def self.css_search(element, *searches) #:nodoc:
  xpath_search(element, css_to_xpath(*searches))
end

.css_to_xpath(*selectors) ⇒ Object



108
109
110
111
112
# File 'lib/webrat/core/xml.rb', line 108

def self.css_to_xpath(*selectors)
  selectors.map do |rule|
    Nokogiri::CSS.xpath_for(rule, :prefix => ".//")
  end.flatten.uniq
end

.document(stringlike) ⇒ Object

:nodoc:



8
9
10
11
12
13
14
# File 'lib/webrat/core/xml.rb', line 8

def self.document(stringlike) #:nodoc:
  if Webrat.configuration.parse_with_nokogiri?
    Webrat.nokogiri_document(stringlike)
  else
    Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
  end
end

.html_document(stringlike) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
# File 'lib/webrat/core/xml.rb', line 16

def self.html_document(stringlike) #:nodoc:
  if Webrat.configuration.parse_with_nokogiri?
    Webrat.html_nokogiri_document(stringlike)
  else
    Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
  end
end

.inner_html(element) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/webrat/core/xml.rb', line 40

def self.inner_html(element)
  if Webrat.configuration.parse_with_nokogiri?
    element.inner_html
  else
    element.text
  end
end

.inner_text(element) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/webrat/core/xml.rb', line 56

def self.inner_text(element)
  if Webrat.configuration.parse_with_nokogiri?
    element.inner_text
  else
    if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc)
      element.inner_text
    else
      element.text
    end
  end
end

.to_html(element) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/webrat/core/xml.rb', line 32

def self.to_html(element)
  if Webrat.configuration.parse_with_nokogiri?
    element.to_html
  else
    element.to_s
  end
end

.xml_document(stringlike) ⇒ Object

:nodoc:



24
25
26
27
28
29
30
# File 'lib/webrat/core/xml.rb', line 24

def self.xml_document(stringlike) #:nodoc:
  if Webrat.configuration.parse_with_nokogiri?
    Webrat.xml_nokogiri_document(stringlike)
  else
    Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
  end
end

.xpath_at(*args) ⇒ Object



86
87
88
# File 'lib/webrat/core/xml.rb', line 86

def self.xpath_at(*args)
  xpath_search(*args).first
end

.xpath_search(element, *searches) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/webrat/core/xml.rb', line 94

def self.xpath_search(element, *searches)
  searches.flatten.map do |search|
    if Webrat.configuration.parse_with_nokogiri?
      element.xpath(search)
    else
      REXML::XPath.match(element, search)
    end
  end.flatten.compact
end

.xpath_to(element) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/webrat/core/xml.rb', line 68

def self.xpath_to(element)
  if Webrat.configuration.parse_with_nokogiri?
    element.path
  else
    element.xpath
  end
end