Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8794: link_to_with_blank_options_uses_first_arg_for_url_4.diff

File link_to_with_blank_options_uses_first_arg_for_url_4.diff, 2.8 kB (added by gbuesing, 2 years ago)

html_escapes link text when first argument is not a String

  • test/template/url_helper_test.rb

    old new  
    1414        url 
    1515      end 
    1616    end 
     17     
     18    def polymorphic_path(options) 
     19      @controller.url 
     20    end 
     21     
    1722    @controller = @controller.new 
    1823    @controller.url = "http://www.example.com" 
    1924  end 
     
    98103  def test_link_with_nil_html_options 
    99104    assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", {:action => 'myaction'}, nil) 
    100105  end 
     106   
     107  def test_link_tag_first_arg_used_for_url_when_options_blank 
     108    @workshop, @session = Workshop.new('foo workshop', true), Session.new('foo session', true) 
     109    assert_dom_equal "<a href=\"http://www.example.com\">foo session</a>", link_to(@session) 
     110    assert_dom_equal "<a href=\"http://www.example.com\">foo session</a>", link_to([@workshop, @session]) 
     111    assert_dom_equal "<a href=\"http://www.example.com/foo\">http://www.example.com/foo</a>", link_to("http://www.example.com/foo") 
     112  end 
     113   
     114  def test_link_tag_first_arg_used_for_url_when_options_blank_escapes_html 
     115     @session = Session.new('<a href="http://www.foo.com">foo</a> session', true) 
     116     assert_dom_equal "<a href=\"http://www.example.com\">&lt;a href=&quot;http://www.foo.com&quot;&gt;foo&lt;/a&gt; session</a>", link_to(@session) 
     117  end 
    101118 
    102119  def test_link_tag_with_custom_onclick 
    103120    assert_dom_equal "<a href=\"http://www.example.com\" onclick=\"alert('yay!')\">Hello</a>", link_to("Hello", "http://www.example.com", :onclick => "alert('yay!')") 
  • lib/action_view/helpers/url_helper.rb

    old new  
    133133      #        var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');  
    134134      #        m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a> 
    135135      def link_to(name, options = {}, html_options = nil) 
     136        options = name if options.blank? 
    136137        url = options.is_a?(String) ? options : self.url_for(options) 
     138        name ||= url 
     139        name = name.last if name.is_a?(Array) 
     140        name = html_escape(name.to_s) unless name.is_a?(String) 
    137141 
    138142        if html_options 
    139143          html_options = html_options.stringify_keys 
     
    145149        end 
    146150         
    147151        href_attr = "href=\"#{url}\"" unless href 
    148         "<a #{href_attr}#{tag_options}>#{name || url}</a>" 
     152        "<a #{href_attr}#{tag_options}>#{name}</a>" 
    149153      end 
    150154 
    151155      # Generates a form containing a single button that submits to the URL created