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

Ticket #10422: action_view_hidden_fields_tag_proc_no_inline_styles.diff

File action_view_hidden_fields_tag_proc_no_inline_styles.diff, 11.0 kB (added by toolmantim, 7 months ago)

Patch that removes inline styles and keeps config option

  • actionpack/test/controller/request_forgery_protection_test.rb

    old new  
    7070    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token 
    7171  end 
    7272   
     73  def test_should_render_form_with_token_tag_inside_hidden_fields_tag_proc 
     74    default_proc = ActionView::Base.hidden_fields_tag_proc 
     75    ActionView::Base.hidden_fields_tag_proc = Proc.new {|fields| "<proctag>#{fields}</proctag>"} 
     76    get :index 
     77    assert_select 'form>proctag>input[name=?][value=?]', 'authenticity_token', @token     
     78  ensure 
     79    ActionView::Base.hidden_fields_tag_proc = default_proc 
     80  end 
     81   
    7382  def test_should_render_button_to_with_token_tag 
    7483    get :show_button 
    7584    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token 
    7685  end 
    77  
     86   
    7887  def test_should_allow_get 
    7988    get :index 
    8089    assert_response :success 
  • actionpack/test/template/form_tag_helper_test.rb

    old new  
    3636 
    3737  def test_form_tag_with_method_put 
    3838    actual = form_tag({}, { :method => :put }) 
    39     expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>) 
     39    expected = %(<form action="http://www.example.com" method="post"><div><input type="hidden" name="_method" value="put" /></div>) 
    4040    assert_dom_equal expected, actual 
    4141  end 
    4242   
    4343  def test_form_tag_with_method_delete 
    4444    actual = form_tag({}, { :method => :delete }) 
    45     expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="delete" /></div>) 
     45    expected = %(<form action="http://www.example.com" method="post"><div><input type="hidden" name="_method" value="delete" /></div>) 
    4646    assert_dom_equal expected, actual 
    4747  end 
     48   
     49  def test_form_tag_with_method_put_with_custom_hidden_fields_tag_proc 
     50    default_proc = ActionView::Base.hidden_fields_tag_proc 
     51    ActionView::Base.hidden_fields_tag_proc = Proc.new {|fields| "<proctag>#{fields}</proctag>"} 
     52    actual = form_tag({}, { :method => :put })     
     53    expected = %(<form action="http://www.example.com" method="post"><proctag><input type="hidden" name="_method" value="put" /></proctag>) 
     54    assert_dom_equal expected, actual 
     55  ensure 
     56    ActionView::Base.hidden_fields_tag_proc = default_proc 
     57  end 
    4858 
     59  def test_form_tag_with_method_delete_with_custom_hidden_fields_tag_proc 
     60    default_proc = ActionView::Base.hidden_fields_tag_proc 
     61    ActionView::Base.hidden_fields_tag_proc = Proc.new {|fields| "<proctag>#{fields}</proctag>"} 
     62    actual = form_tag({}, { :method => :delete }) 
     63    expected = %(<form action="http://www.example.com" method="post"><proctag><input type="hidden" name="_method" value="delete" /></proctag>) 
     64    assert_dom_equal expected, actual 
     65  ensure 
     66    ActionView::Base.hidden_fields_tag_proc = default_proc 
     67  end 
     68 
    4969  def test_form_tag_with_block 
    5070    _erbout = '' 
    5171    form_tag("http://example.com") { _erbout.concat "Hello world!" } 
     
    5878    _erbout = '' 
    5979    form_tag("http://example.com", :method => :put) { _erbout.concat "Hello world!" } 
    6080 
    61     expected = %(<form action="http://example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>Hello world!</form>) 
     81    expected = %(<form action="http://example.com" method="post"><div><input type="hidden" name="_method" value="put" /></div>Hello world!</form>) 
    6282    assert_dom_equal expected, _erbout 
    6383  end 
    6484 
  • actionpack/test/template/form_helper_test.rb

    old new  
    331331 
    332332    expected =  
    333333      "<form action='http://www.example.com' id='create-post' method='post'>" + 
    334       "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" + 
     334      "<div><input name='_method' type='hidden' value='put' /></div>" + 
    335335      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    336336      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
    337337      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     
    682682 
    683683    form_for(@post) do |f| end 
    684684 
    685     expected = "<form action=\"/posts/123\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>" 
     685    expected = "<form action=\"/posts/123\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>" 
    686686    assert_equal expected, _erbout 
    687687  end 
    688688 
     
    705705    _erbout = '' 
    706706    form_for([@post, @comment]) {} 
    707707 
    708     expected = %(<form action="#{comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>) 
     708    expected = %(<form action="#{comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div><input name="_method" type="hidden" value="put" /></div></form>) 
    709709    assert_dom_equal expected, _erbout 
    710710  end 
    711711 
     
    724724    _erbout = '' 
    725725    form_for([:admin, @post, @comment]) {} 
    726726   
    727     expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>) 
     727    expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div><input name="_method" type="hidden" value="put" /></div></form>) 
    728728    assert_dom_equal expected, _erbout 
    729729  end 
    730730   
     
    742742 
    743743    form_for(@post, :url => "/super_posts") do |f| end 
    744744 
    745     expected = "<form action=\"/super_posts\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>" 
     745    expected = "<form action=\"/super_posts\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>" 
    746746    assert_equal expected, _erbout 
    747747  end 
    748748 
  • actionpack/test/template/prototype_helper_test.rb

    old new  
    126126  end 
    127127 
    128128  def test_form_remote_tag_with_method 
    129     assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>), 
     129    assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div><input name='_method' type='hidden' value='put' /></div>), 
    130130      form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast  }, :html => { :method => :put }) 
    131131  end 
    132132 
     
    157157    _erbout = '' 
    158158    remote_form_for(@record) {} 
    159159     
    160     expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_path(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 
     160    expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_path(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div><input name='_method' type='hidden' value='put' /></div></form>) 
    161161    assert_dom_equal expected, _erbout 
    162162  end 
    163163 
     
    175175    _erbout = '' 
    176176    remote_form_for([@author, @article]) {} 
    177177     
    178     expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' onsubmit="new Ajax.Request('#{author_article_path(@author, @article)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_article'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>) 
     178    expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' onsubmit="new Ajax.Request('#{author_article_path(@author, @article)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_article'><div><input name='_method' type='hidden' value='put' /></div></form>) 
    179179    assert_dom_equal expected, _erbout 
    180180  end 
    181181 
  • actionpack/lib/action_view/helpers/form_tag_helper.rb

    old new  
    22require 'action_view/helpers/tag_helper' 
    33 
    44module ActionView 
     5  class Base 
     6    # Specify the proc used to surround hidden form fields with a block level element. 
     7    # 
     8    # Defaults to: 
     9    #   Proc.new{|fields| "<div>#{fields}</div>"} 
     10    @@hidden_fields_tag_proc = Proc.new{|fields| "<div>#{fields}</div>"} 
     11    cattr_accessor :hidden_fields_tag_proc 
     12  end 
    513  module Helpers 
    614    # Provides a number of methods for creating form tags that doesn't rely on an ActiveRecord object assigned to the template like 
    715    # FormHelper does. Instead, you provide the names and values manually. 
     
    401409              '' 
    402410            when /^post$/i, "", nil 
    403411              html_options["method"] = "post" 
    404               protect_against_forgery? ? content_tag(:div, token_tag, :style => 'margin:0;padding:0') : '' 
     412              protect_against_forgery? ? Base.hidden_fields_tag_proc.call(token_tag) : '' 
    405413            else 
    406414              html_options["method"] = "post" 
    407               content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag, :style => 'margin:0;padding:0'
     415              Base.hidden_fields_tag_proc.call(tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag
    408416          end 
    409417        end 
    410418