instance method xml_name_escape

Ruby on Rails 6.1.7.10

Since v5.2.8.1 Private

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

xml_name_escape(name)

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

Parameters

name req
Source
# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 133
    def xml_name_escape(name)
      name = name.to_s
      return "" if name.blank?

      starting_char = name[0].gsub(TAG_NAME_START_REGEXP, TAG_NAME_REPLACEMENT_CHAR)

      return starting_char if name.size == 1

      following_chars = name[1..-1].gsub(TAG_NAME_FOLLOWING_REGEXP, TAG_NAME_REPLACEMENT_CHAR)

      starting_char + following_chars
    end

Defined in activesupport/lib/active_support/core_ext/string/output_safety.rb line 133 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ERB::Util

Type at least 2 characters to search.

↑↓ navigate · open · esc close