lib/www/mechanize/page.rb in mechanize-0.8.4 vs lib/www/mechanize/page.rb in mechanize-0.8.5
- old
+ new
@@ -31,17 +31,24 @@
@parser = @links = @forms = @meta = @bases = @frames = @iframes = nil
end
def title
- @title ||= if parser && search('//title').inner_text.length > 0
- search('//title').inner_text
+ @title ||= if parser && search('title').inner_text.length > 0
+ search('title').inner_text
end
end
def parser
- @parser ||= body && response ? Mechanize.html_parser.parse(body) : nil
+ return @parser if @parser
+
+ if body && response
+ html_body = body.length > 0 ? body : '<html></html>'
+ @parser = Mechanize.html_parser.parse(html_body)
+ end
+
+ @parser
end
alias :root :parser
# Get the content type
def content_type
@@ -53,11 +60,11 @@
def_delegator :parser, :/, :/
def_delegator :parser, :at, :at
# Find a form matching +criteria+.
# Example:
- # page.form(:action => '/post/login.php') do |f|
+ # page.form_with(:action => '/post/login.php') do |f|
# ...
# end
[:form, :link, :base, :frame, :iframe].each do |type|
eval(<<-eomethod)
def #{type}s_with(criteria)
@@ -78,31 +85,31 @@
eomethod
end
def links
@links ||= WWW::Mechanize::List.new(
- %w{ //a //area }.map do |tag|
+ %w{ a area }.map do |tag|
search(tag).map do |node|
Link.new(node, @mech, self)
end
end.flatten
)
end
def forms
@forms ||= WWW::Mechanize::List.new(
- search('//form').map do |html_form|
+ search('form').map do |html_form|
form = Form.new(html_form, @mech, self)
form.action ||= @uri.to_s
form
end
)
end
def meta
@meta ||= WWW::Mechanize::List.new(
- search('//meta').map do |node|
+ search('meta').map do |node|
next unless node['http-equiv'] && node['content']
(equiv, content) = node['http-equiv'], node['content']
if equiv && equiv.downcase == 'refresh'
if content && content =~ /^\d+\s*;\s*url\s*=\s*'?([^\s']+)/i
node['href'] = $1
@@ -113,22 +120,22 @@
)
end
def bases
@bases ||= WWW::Mechanize::List.new(
- search('//base').map { |node| Base.new(node, @mech, self) }
+ search('base').map { |node| Base.new(node, @mech, self) }
)
end
def frames
@frames ||= WWW::Mechanize::List.new(
- search('//frame').map { |node| Frame.new(node, @mech, self) }
+ search('frame').map { |node| Frame.new(node, @mech, self) }
)
end
def iframes
@iframes ||= WWW::Mechanize::List.new(
- search('//iframe').map { |node| Frame.new(node, @mech, self) }
+ search('iframe').map { |node| Frame.new(node, @mech, self) }
)
end
end
end
end