Project

General

Profile

Patch #40210 » dont-insert-nbsp-to-blank-option-element.patch

Go MAEDA, 2024-02-09 04:35

View differences:

config/initializers/10-patches.rb
34 34

  
35 35
ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
36 36

  
37
# HTML5: <option value=""></option> is invalid, use <option value="">&nbsp;</option> instead
38 37
module ActionView
39 38
  module Helpers
40
    module Tags
41
      SelectRenderer.prepend(Module.new do
42
        def add_options(option_tags, options, value = nil)
43
          if options.delete(:include_blank)
44
            options[:prompt] = '&nbsp;'.html_safe
45
          end
46
          super
47
        end
48
      end)
49
    end
50

  
51 39
    module FormHelper
52 40
      alias :date_field_without_max :date_field
53 41
      def date_field(object_name, method, options = {})
......
56 44
    end
57 45

  
58 46
    module FormTagHelper
59
      alias :select_tag_without_non_empty_blank_option :select_tag
60
      def select_tag(name, option_tags = nil, options = {})
61
        if options.delete(:include_blank)
62
          options[:prompt] = '&nbsp;'.html_safe
63
        end
64
        select_tag_without_non_empty_blank_option(name, option_tags, options)
65
      end
66

  
67 47
      alias :date_field_tag_without_max :date_field_tag
68 48
      def date_field_tag(name, value = nil, options = {})
69 49
        date_field_tag_without_max(name, value, options.reverse_merge(max: '9999-12-31'))
70 50
      end
71 51
    end
72

  
73
    module FormOptionsHelper
74
      alias :options_for_select_without_non_empty_blank_option :options_for_select
75
      def options_for_select(container, selected = nil)
76
        if container.is_a?(Array)
77
          container = container.map {|element| element.presence || ["&nbsp;".html_safe, ""]}
78
        end
79
        options_for_select_without_non_empty_blank_option(container, selected)
80
      end
81
    end
82 52
  end
83 53
end
84 54

  
test/functional/boards_controller_test.rb
177 177
      assert_select 'option[value=""]'
178 178
      assert_select 'option[value="1"]', :text => 'Help'
179 179
    end
180

  
181
    # &nbsp; replaced by nokogiri, not easy to test in DOM assertions
182
    assert_not_include '<option value=""></option>', response.body
183
    assert_include '<option value="">&nbsp;</option>', response.body
184 180
  end
185 181

  
186 182
  def test_new_without_project_boards
(2-2/2)