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

Ticket #11537: link_tag_using_delete_javascript_and_target_plus_test.diff

File link_tag_using_delete_javascript_and_target_plus_test.diff, 3.1 kB (added by thijslangeveld, 4 months ago)
  • actionpack/test/template/url_helper_test.rb

    old new  
    202202      link_to("Destroy", "http://www.example.com", :method => :delete, :href => '#') 
    203203    ) 
    204204  end 
     205   
     206  def test_link_tag_using_delete_javascript_and_target 
     207    assert_dom_equal( 
     208      "<a href='http://www.thijslangeveld.nl' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.target = this.target;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\" target=\"_top\">Destroy</a>", 
     209      link_to("Destroy", "http://www.thijslangeveld.nl", :method => :delete, :target => '_top') 
     210    ) 
     211  end 
    205212 
    206213  def test_link_tag_using_post_javascript_and_confirm 
    207214    assert_dom_equal( 
  • actionpack/lib/action_view/helpers/url_helper.rb

    old new  
    442442        def convert_options_to_javascript!(html_options, url = '') 
    443443          confirm, popup = html_options.delete("confirm"), html_options.delete("popup") 
    444444 
    445           method, href = html_options.delete("method"), html_options['href'] 
     445          method, href, target = html_options.delete("method"), html_options['href'], html_options['target'] 
    446446 
    447447          html_options["onclick"] = case 
    448448            when popup && method 
     
    454454            when confirm 
    455455              "return #{confirm_javascript_function(confirm)};" 
    456456            when method 
    457               "#{method_javascript_function(method, url, href)}return false;" 
     457              "#{method_javascript_function(method, url, href, target)}return false;" 
    458458            when popup 
    459459              popup_javascript_function(popup) + 'return false;' 
    460460            else 
     
    470470          popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}');" : "window.open(this.href);" 
    471471        end 
    472472 
    473         def method_javascript_function(method, url = '', href = nil
     473        def method_javascript_function(method, url = '', href = nil, target = nil
    474474          action = (href && url.size > 0) ? "'#{url}'" : 'this.href' 
    475475          submit_function = 
    476476            "var f = document.createElement('form'); f.style.display = 'none'; " + 
    477477            "this.parentNode.appendChild(f); f.method = 'POST'; f.action = #{action};" 
    478478 
     479          if target 
     480            submit_function << "f.target = this.target;" 
     481          end 
     482 
    479483          unless method == :post 
    480484            submit_function << "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); " 
    481485            submit_function << "m.setAttribute('name', '_method'); m.setAttribute('value', '#{method}'); f.appendChild(m);"