module TrkDatatables # rubocop:disable Rails/OutputSafety class RenderHtml @indent = 0 def initialize(search_link, datatable, html_options = {}) @search_link = search_link @datatable = datatable @html_options = html_options self.class.indent = -1 end def result if @search_link.nil? table_tag_client else table_tag_server end end class << self attr_accessor :indent end # https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/output_safety_helper.rb#L33 def safe_join(array, sep = $,) sep = ERB::Util.unwrapped_html_escape(sep) array.map { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe end # rubocop:disable Metrics/AbcSize # # _content_tag :p, 'Hi' # _content_tag :p, class: 'button', 'Hi' # _content_tag :div, do # _content_tag :div, class: 'background' do def _content_tag(tag, options = {}, content = nil) if !options.is_a?(Hash) # content is first argument, do not pass hash as content inline = true content = options options = {} elsif content.present? inline = true end self.class.indent += 1 html = "#{' ' * self.class.indent}<#{tag}".html_safe options.each do |attribute, value| value = value.to_json if value.is_a?(Hash) || value.is_a?(Array) html << " #{attribute}='".html_safe << replace_quote(value) << "'".html_safe end html << if inline '>'.html_safe << content.to_s << "\n".html_safe else ">\n".html_safe << yield << "\n#{' ' * self.class.indent}".html_safe end self.class.indent -= 1 html end # rubocop:enable Metrics/AbcSize def _select_tag(options, search_value) selected = search_value.to_s.split(MULTIPLE_OPTION_SEPARATOR) _content_tag :select, multiple: 'multiple' do safe_join(options.map do |key, value| _content_tag :option, { value: value }.merge(selected.include?(value.to_s) ? { selected: 'selected' } : {}), key end) end end # We need to replace single quote since it is used in option='value' def replace_quote(string) # do not know those two are different # ERB::Util.html_escape string.to_s.gsub 'aaa', 'bbb' # ERB::Util.html_escape string.to_s # since it is safebuffer and html_safe, this will output with single # quotes for example: data-datatable-multiselect='