Module: Lich::Common::XmlEntities

Defined in:
documented/common/xml_entities.rb

Overview

Decodes the five standard XML entities. The SAX parsers run Ox with convert_special: false (so Ox never turns a numeric entity into UTF-8 that would then be mis-encoded), which means Ox leaves the standard entities literal -- this restores them. Shared by Lich::Common::XMLParser and Lich::DragonRealms::DRParser.

Class Method Summary collapse

Class Method Details

.decode(str) ⇒ Object

& is decoded last so an already-encoded entity such as > round-trips to the literal > rather than being double-decoded to >.



15
16
17
18
19
# File 'documented/common/xml_entities.rb', line 15

def self.decode(str)
  return str unless str.include?('&')

  str.gsub('&lt;', '<').gsub('&gt;', '>').gsub('&quot;', '"').gsub('&apos;', "'").gsub('&amp;', '&')
end

.encode(str) ⇒ Object

Encodes the three markup-significant characters so a raw value can be safely embedded in an XML-like payload sent to a frontend. & is encoded first so the ampersands introduced by encoding < and > are not themselves double-encoded.



25
26
27
# File 'documented/common/xml_entities.rb', line 25

def self.encode(str)
  str.to_s.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
end