Module: Xlsxtream::XML

Defined in:
lib/xlsxtream/xml.rb

Constant Summary collapse

XML_ESCAPES =
{
  '&' => '&',
  '"' => '"',
  '<' => '&lt;',
  '>' => '&gt;',
}.freeze
HEX_ESCAPE_REGEXP =

Escape first underscore of ST_Xstring sequences in input strings to appear as plaintext in Excel

/_(x[0-9A-Fa-f]{4}_)/.freeze
XML_ESCAPE_UNDERSCORE =
'_x005f_\1'.freeze
XML_DECLARATION =
%'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n'.freeze
WS_AROUND_TAGS =
/(?<=>)\s+|\s+(?=<)/.freeze
UNSAFE_ATTR_CHARS =
/[&"<>]/.freeze
UNSAFE_VALUE_CHARS =
/[&<>]/.freeze
INVALID_XML10_CHARS =

www.w3.org/TR/REC-xml/#NT-Char: Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/.freeze
ESCAPE_CHAR =

ST_Xstring escaping

lambda { |c| '_x%04X_'.freeze % c.ord }.freeze

Class Method Summary collapse

Class Method Details

.escape_attr(string) ⇒ Object



39
40
41
# File 'lib/xlsxtream/xml.rb', line 39

def escape_attr(string)
  string.gsub(UNSAFE_ATTR_CHARS, XML_ESCAPES)
end

.escape_value(string) ⇒ Object



43
44
45
46
47
48
# File 'lib/xlsxtream/xml.rb', line 43

def escape_value(string)
  string
    .gsub(UNSAFE_VALUE_CHARS, XML_ESCAPES)
    .gsub(HEX_ESCAPE_REGEXP, XML_ESCAPE_UNDERSCORE)
    .gsub(INVALID_XML10_CHARS, &ESCAPE_CHAR)
end

.headerObject



31
32
33
# File 'lib/xlsxtream/xml.rb', line 31

def header
  XML_DECLARATION
end

.strip(xml) ⇒ Object



35
36
37
# File 'lib/xlsxtream/xml.rb', line 35

def strip(xml)
  xml.gsub(WS_AROUND_TAGS, ''.freeze)
end