From 7f26415d3c0e4101ce1569a499470e8a32dbfede Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 20 Sep 2005 07:54:55 +0000 Subject: Optimized tag_options to not sort keys, which is no longer necessary when assert_dom_equal and friend is available #1995 [skae]. Added assert_dom_equal and assert_dom_not_equal to compare tags generated by the helpers in an order-indifferent manner #1995 [skae] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2271 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/assertions.rb | 17 ++++++++++++++- actionpack/lib/action_controller/base.rb | 1 + .../vendor/html-scanner/html/node.rb | 24 +++++++++++++++++++++- .../lib/action_view/helpers/asset_tag_helper.rb | 2 +- actionpack/lib/action_view/helpers/tag_helper.rb | 19 ++++++++--------- actionpack/lib/action_view/helpers/url_helper.rb | 17 ++++++++------- 6 files changed, 58 insertions(+), 22 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/assertions.rb b/actionpack/lib/action_controller/assertions.rb index ad70111f3b..1a501bcd74 100644 --- a/actionpack/lib/action_controller/assertions.rb +++ b/actionpack/lib/action_controller/assertions.rb @@ -248,7 +248,22 @@ module Test #:nodoc: tag = find_tag(opts) assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}" end - + + # test 2 html strings to be equivalent, i.e. identical up to reordering of attributes + def assert_dom_equal(expected, actual, message="") + expected_dom = HTML::Document.new(expected).root + actual_dom = HTML::Document.new(actual).root + full_message = build_message(message, " expected to be == to\n.", expected_dom.to_s, actual_dom.to_s) + assert_block(full_message) { expected_dom == actual_dom } + end + + # negated form of +assert_dom_equivalent+ + def assert_dom_not_equal(expected, actual, message="") + expected_dom = HTML::Document.new(expected).root + actual_dom = HTML::Document.new(actual).root + full_message = build_message(message, " expected to be != to\n.", expected_dom.to_s, actual_dom.to_s) + assert_block(full_message) { expected_dom != actual_dom } + end end end end diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 59160c3e81..9291129e3d 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -838,6 +838,7 @@ module ActionController #:nodoc: end def self.action_methods + #puts "action method: #{public_instance_methods.inspect}" @action_methods ||= (public_instance_methods - hidden_actions).inject({}) { |h, k| h[k] = true; h } end diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb index f0f94d19cc..016603a3e7 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb @@ -122,6 +122,18 @@ module HTML #:nodoc: def validate_conditions(conditions) Conditions === conditions ? conditions : Conditions.new(conditions) end + + def ==(node) + return false unless self.class == node.class && children.size == node.children.size + + equivalent = true + + children.size.times do |i| + equivalent &&= children[i] == node.children[i] + end + + equivalent + end class <tag("br") =>
# * tag("input", { "type" => "text"}) => - def tag(name, options = {}, open = false) - "<#{name}#{tag_options(options)}" + (open ? ">" : " />") + def tag(name, options = nil, open = false) + "<#{name}#{tag_options(options.stringify_keys) if options}" + (open ? ">" : " />") end # Examples: # * content_tag("p", "Hello world!") =>

Hello world!

# * content_tag("div", content_tag("p", "Hello world!"), "class" => "strong") => #

Hello world!

- def content_tag(name, content, options = {}) - "<#{name}#{tag_options(options)}>#{content}" + def content_tag(name, content, options = nil) + "<#{name}#{tag_options(options.stringify_keys) if options}>#{content}" end private def tag_options(options) - cleaned_options = options.reject { |key, value| value.nil? } - unless cleaned_options.empty? - " " + cleaned_options.symbolize_keys.map { |key, value| - %(#{key}="#{html_escape(value.to_s)}") - }.sort.join(" ") + if options + options.inject("") do |html_str, (key, value)| + value.nil? ? html_str : html_str << %( #{key}="#{html_escape(value)}") + end end end end end -end \ No newline at end of file +end diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index d154f7c00c..4b7a2270dc 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -39,16 +39,15 @@ module ActionView # link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600'] # link_to "Destroy account", { :action => "destroy" }, :confirm => "Are you sure?", :post => true def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference) - html_options = (html_options || {}).stringify_keys - convert_options_to_javascript!(html_options) - if options.is_a?(String) - content_tag "a", name || options, (html_options || {}).merge("href" => options) + if html_options + html_options = html_options.stringify_keys + convert_options_to_javascript!(html_options) + tag_options = tag_options(html_options) else - content_tag( - "a", name || url_for(options, *parameters_for_method_reference), - (html_options || {}).merge("href" => url_for(options, *parameters_for_method_reference)) - ) + tag_options = nil end + url = options.is_a?(String) ? options : url_for(options, *parameters_for_method_reference) + "#{name||url}" end # Generates a form containing a sole button that submits to the @@ -236,7 +235,7 @@ module ActionView # Returns true if the current page uri is generated by the options passed (in url_for format). def current_page?(options) - url_for(options) == @request.request_uri + url_for(options) == @controller.request.request_uri end private -- cgit v1.2.3