2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
require "active_support/core_ext/string/output_safety"
|
2011-02-10 10:45:39 -05:00
|
|
|
|
|
|
|
module ActionView #:nodoc:
|
|
|
|
# = Action View Raw Output Helper
|
|
|
|
module Helpers #:nodoc:
|
|
|
|
module OutputSafetyHelper
|
|
|
|
# This method outputs without escaping a string. Since escaping tags is
|
|
|
|
# now default, this can be used when you don't want Rails to automatically
|
|
|
|
# escape tags. This is not recommended if the data is coming from the user's
|
|
|
|
# input.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
2012-12-01 14:38:07 -05:00
|
|
|
# raw @user.name
|
|
|
|
# # => 'Jimmy <alert>Tables</alert>'
|
2011-02-10 10:45:39 -05:00
|
|
|
def raw(stringish)
|
|
|
|
stringish.to_s.html_safe
|
|
|
|
end
|
|
|
|
|
2014-08-08 17:25:18 -04:00
|
|
|
# This method returns an HTML safe string similar to what <tt>Array#join</tt>
|
2014-06-10 20:33:34 -04:00
|
|
|
# would return. The array is flattened, and all items, including
|
2014-08-08 17:25:18 -04:00
|
|
|
# the supplied separator, are HTML escaped unless they are HTML
|
|
|
|
# safe, and the returned string is marked as HTML safe.
|
2011-02-10 10:45:39 -05:00
|
|
|
#
|
2016-01-20 01:55:06 -05:00
|
|
|
# safe_join([raw("<p>foo</p>"), "<p>bar</p>"], "<br />")
|
2011-02-10 10:45:39 -05:00
|
|
|
# # => "<p>foo</p><br /><p>bar</p>"
|
|
|
|
#
|
2017-01-14 22:06:39 -05:00
|
|
|
# safe_join([raw("<p>foo</p>"), raw("<p>bar</p>")], raw("<br />"))
|
2011-02-10 10:45:39 -05:00
|
|
|
# # => "<p>foo</p><br /><p>bar</p>"
|
|
|
|
#
|
2016-10-28 23:05:58 -04:00
|
|
|
def safe_join(array, sep = $,)
|
2014-06-06 20:43:00 -04:00
|
|
|
sep = ERB::Util.unwrapped_html_escape(sep)
|
2011-02-10 10:45:39 -05:00
|
|
|
|
2014-06-10 20:33:34 -04:00
|
|
|
array.flatten.map! { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe
|
2011-02-10 10:45:39 -05:00
|
|
|
end
|
2016-03-15 15:22:11 -04:00
|
|
|
|
|
|
|
# Converts the array to a comma-separated sentence where the last element is
|
|
|
|
# joined by the connector word. This is the html_safe-aware version of
|
2019-03-08 06:36:16 -05:00
|
|
|
# ActiveSupport's {Array#to_sentence}[https://api.rubyonrails.org/classes/Array.html#method-i-to_sentence].
|
2016-03-15 15:22:11 -04:00
|
|
|
#
|
|
|
|
def to_sentence(array, options = {})
|
|
|
|
options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
|
|
|
|
|
|
|
|
default_connectors = {
|
2016-08-06 13:36:34 -04:00
|
|
|
words_connector: ", ",
|
|
|
|
two_words_connector: " and ",
|
|
|
|
last_word_connector: ", and "
|
2016-03-15 15:22:11 -04:00
|
|
|
}
|
|
|
|
if defined?(I18n)
|
|
|
|
i18n_connectors = I18n.translate(:'support.array', locale: options[:locale], default: {})
|
|
|
|
default_connectors.merge!(i18n_connectors)
|
|
|
|
end
|
|
|
|
options = default_connectors.merge!(options)
|
|
|
|
|
|
|
|
case array.length
|
|
|
|
when 0
|
2016-08-06 12:48:35 -04:00
|
|
|
"".html_safe
|
2016-03-15 15:22:11 -04:00
|
|
|
when 1
|
|
|
|
ERB::Util.html_escape(array[0])
|
|
|
|
when 2
|
|
|
|
safe_join([array[0], array[1]], options[:two_words_connector])
|
|
|
|
else
|
2017-01-15 07:10:05 -05:00
|
|
|
safe_join([safe_join(array[0...-1], options[:words_connector]), options[:last_word_connector], array[-1]], nil)
|
2016-03-15 15:22:11 -04:00
|
|
|
end
|
|
|
|
end
|
2011-02-10 10:45:39 -05:00
|
|
|
end
|
|
|
|
end
|
2012-01-11 17:08:43 -05:00
|
|
|
end
|