Method: ERB::Util.xml_name_escape
- Defined in:
- activesupport/lib/active_support/core_ext/erb/util.rb
.xml_name_escape(name) ⇒ Object
A utility method for escaping XML names of tags and names of attributes.
xml_name_escape('1 < 2 & 3')
# => "1___2___3"
It follows the requirements of the specification: www.w3.org/TR/REC-xml/#NT-Name
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'activesupport/lib/active_support/core_ext/erb/util.rb', line 142 def xml_name_escape(name) name = name.to_s return "" if name.blank? return name if name.match?(SAFE_XML_TAG_NAME_REGEXP) starting_char = name[0] starting_char.gsub!(INVALID_TAG_NAME_START_REGEXP, TAG_NAME_REPLACEMENT_CHAR) return starting_char if name.size == 1 following_chars = name[1..-1] following_chars.gsub!(INVALID_TAG_NAME_FOLLOWING_REGEXP, TAG_NAME_REPLACEMENT_CHAR) starting_char << following_chars end |