Class: RubyXL::Text

Inherits:
OOXMLObject show all
Defined in:
lib/rubyXL/objects/text.rb

Overview

Constant Summary collapse

INVALID_XML10_CHARS =

http://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}])/
ESCAPED_UNICODE =
/_x([0-9A-F]{4})_/

Instance Attribute Summary

Attributes included from OOXMLObjectInstanceMethods

#local_namespaces

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OOXMLObjectInstanceMethods

#==, included, #index_in_collection, #init_child_nodes, #initialize, #write_xml

Class Method Details

.escape_bad_xml_chars(str) ⇒ Object



31
32
33
34
# File 'lib/rubyXL/objects/text.rb', line 31

def self.escape_bad_xml_chars(str)
  return str unless str.is_a?(String)
  str.gsub(INVALID_XML10_CHARS) { |bad_char| format('_x%04X_', bad_char.ord) }
end

Instance Method Details

#before_write_xml ⇒ Object



21
22
23
24
25
# File 'lib/rubyXL/objects/text.rb', line 21

def before_write_xml
  preserve_whitespace
  self.value = RubyXL::Text::escape_bad_xml_chars(value)
  true
end

#to_s ⇒ Object



27
28
29
# File 'lib/rubyXL/objects/text.rb', line 27

def to_s
  value.to_s.gsub(ESCAPED_UNICODE) { Regexp.last_match(1).hex.chr(::Encoding::UTF_8) }
end