From 334108015fbde82c7d65194c2ad8bed657712a07 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 00:12:21 -0500 Subject: [PATCH] Abstracting access to inner_html and inner_text to Webrat::XML methods --- lib/webrat/core/field.rb | 6 +++--- lib/webrat/core/label.rb | 2 +- lib/webrat/core/link.rb | 4 ++-- lib/webrat/core/matchers/have_content.rb | 2 +- lib/webrat/core/select_option.rb | 6 +++--- lib/webrat/core/xml.rb | 17 ++++++++++++++++- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 82e02b2..8555b8e 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -159,7 +159,7 @@ module Webrat class ButtonField < Field #:nodoc: def matches_text?(text) - @element.inner_html =~ /#{Regexp.escape(text.to_s)}/i + Webrat::XML.inner_html(@element) =~ /#{Regexp.escape(text.to_s)}/i end def matches_value?(value) @@ -284,7 +284,7 @@ module Webrat protected def default_value - @element.inner_html + Webrat::XML.inner_html(@element) end end @@ -338,7 +338,7 @@ module Webrat selected_options.map do |option| return "" if option.nil? - Webrat::XML.attribute(option, "value") || option.inner_html + Webrat::XML.attribute(option, "value") || Webrat::XML.inner_html(option) end end diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index 7a01521..e2e08c0 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -11,7 +11,7 @@ module Webrat end def text - str = @element.inner_text + str = Webrat::XML.inner_text(@element) str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index e1cdb3a..272f7d9 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -40,11 +40,11 @@ module Webrat end def inner_html - @element.inner_html + Webrat::XML.inner_html(@element) end def text - @element.inner_text + Webrat::XML.inner_text(@element) end protected diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index 8344ed2..2c4f43e 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -8,7 +8,7 @@ module Webrat def matches?(stringlike) @document = Webrat::XML.document(stringlike) - @element = @document.inner_text + @element = Webrat::XML.inner_text(@document) case @content when String diff --git a/lib/webrat/core/select_option.rb b/lib/webrat/core/select_option.rb index f61a162..1a2001f 100644 --- a/lib/webrat/core/select_option.rb +++ b/lib/webrat/core/select_option.rb @@ -8,9 +8,9 @@ module Webrat def matches_text?(text) if text.is_a?(Regexp) - @element.inner_html =~ text + Webrat::XML.inner_html(@element) =~ text else - @element.inner_html == text.to_s + Webrat::XML.inner_html(@element) == text.to_s end end @@ -22,7 +22,7 @@ module Webrat protected def value - Webrat::XML.attribute(@element, "value") || @element.inner_html + Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element) end end diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 1a2d302..8a0de4e 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -6,6 +6,7 @@ module Webrat #:nodoc: Webrat.nokogiri_document(stringlike) else Webrat::XML.hpricot_document(stringlike) + # Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) end end @@ -25,12 +26,26 @@ module Webrat #:nodoc: end end + def self.inner_html(element) + element.inner_html + end + + def self.inner_text(element) + element.inner_text + end + def self.attribute(element, attribute_name) - element[attribute_name] + # case element + # when Nokogiri::XML::Element, Hash + element[attribute_name] + # else + # element.attributes[attribute_name] + # end end def self.xpath_search(element, *searches) searches.flatten.map do |search| + # REXML::XPath.match(element, search) element.xpath(search) end.flatten.compact end