Module: SublimeDSL::Tools::XML

Defined in:
lib/sublime_dsl/tools/xml.rb

Overview

Tools to process XML.

Constant Summary collapse

FORBIDDEN_CHARS_RE =

Regexp matching forbidden control characters.

/[\x00-\x08\x0b\x0c\x0e-\x1f]/
FORBIDDEN_CHARS_MAP =

Hash { ‘forbidden character’ => ‘abbreviation’ }.

{
  "\x00" => 'NUL',
  "\x01" => 'SOH',
  "\x02" => 'STX',
  "\x03" => 'ETX',
  "\x04" => 'EOT',
  "\x05" => 'ENQ',
  "\x06" => 'ACK',
  "\x07" => 'BEL',
  "\x08" => 'BS',
  # "\x09" => 'HT',
  # "\x0a" => 'LF',
  "\x0b" => 'VT',
  "\x0c" => 'FF',
  # "\x0d" => 'CR',
  "\x0e" => 'SO',
  "\x0f" => 'SI',
  "\x10" => 'DLE',
  "\x11" => 'DC1',
  "\x12" => 'DC2',
  "\x13" => 'DC3',
  "\x14" => 'DC4',
  "\x15" => 'NAK',
  "\x16" => 'SYN',
  "\x17" => 'ETB',
  "\x18" => 'CAN',
  "\x19" => 'EM',
  "\x1a" => 'SUB',
  "\x1b" => 'ESC',
  "\x1c" => 'FS',
  "\x1d" => 'GS',
  "\x1e" => 'RS',
  "\x1f" => 'US'
}

Class Method Summary collapse

Class Method Details

.load(string_or_io) ⇒ Object

Returns a Nokogiri::XML::Document. Raises an exception if the text contains control characters forbidden in an XML document.



53
54
55
56
57
58
59
60
61
# File 'lib/sublime_dsl/tools/xml.rb', line 53

def self.load(string_or_io)
  text = string_or_io.respond_to?(:read) ? string_or_io.read : string_or_io
  if text =~ FORBIDDEN_CHARS_RE
    abbrev = FORBIDDEN_CHARS_MAP[$&]
    raise Error, "illegal control character in XML: #{$&.inspect} (#{abbrev})"
  end

  Nokogiri.XML(text, &:noblanks) # I hate this API
end