Module: Philiprehberger::XmlBuilder::Escaper

Defined in:
lib/philiprehberger/xml_builder/escaper.rb

Overview

XML entity escaping for text content and attribute values.

Constant Summary collapse

ENTITIES =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  '"' => '&quot;',
  "'" => '&apos;'
}.freeze
ENTITY_PATTERN =
Regexp.union(ENTITIES.keys).freeze

Class Method Summary collapse

Class Method Details

.escape(text) ⇒ String

Escape special XML characters in a string.

Parameters:

  • text (String)

    the text to escape

Returns:

  • (String)

    the escaped text



21
22
23
# File 'lib/philiprehberger/xml_builder/escaper.rb', line 21

def self.escape(text)
  text.to_s.gsub(ENTITY_PATTERN, ENTITIES)
end