xlsxrb 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -339,6 +339,13 @@ module Xlsxrb
339
339
  end
340
340
 
341
341
  # : (untyped value) -> untyped
342
+ # Escapes cell text: ST_Xstring (XmlBuilder.encode_xstring), then XML.
343
+ def xml_escape_text(value)
344
+ str = value.to_s
345
+ str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
346
+ xml_escape(XmlBuilder.encode_xstring(str))
347
+ end
348
+
342
349
  def xml_escape(value)
343
350
  value.to_s
344
351
  .gsub(/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/, "")
@@ -381,9 +388,9 @@ module Xlsxrb
381
388
  rpr << %(<rFont val="#{xml_escape(font[:name])}"/>) if font[:name]
382
389
  rpr << %(<family val="#{font[:family]}"/>) if font[:family]
383
390
  rpr << %(<scheme val="#{font[:scheme]}"/>) if font[:scheme]
384
- "<r><rPr>#{rpr}</rPr><t>#{xml_escape(text)}</t></r>"
391
+ "<r><rPr>#{rpr}</rPr><t>#{xml_escape_text(text)}</t></r>"
385
392
  else
386
- "<r><t>#{xml_escape(text)}</t></r>"
393
+ "<r><t>#{xml_escape_text(text)}</t></r>"
387
394
  end
388
395
  end.join
389
396
  end
@@ -431,7 +438,7 @@ module Xlsxrb
431
438
  "<f#{f_attrs}/>"
432
439
  end
433
440
  parts = %(<c r="#{cell_ref}"#{t_attr}#{s_attr}#{ph_attr}>#{f_tag})
434
- parts << "<v>#{xml_escape(cached_val_str)}</v>" unless value.cached_value.nil?
441
+ parts << "<v>#{xml_escape_text(cached_val_str)}</v>" unless value.cached_value.nil?
435
442
  parts << "</c>"
436
443
  parts
437
444
  when Xlsxrb::Elements::RichText
@@ -462,7 +469,7 @@ module Xlsxrb
462
469
  idx = str_sst[value.to_s]
463
470
  %(<c r="#{cell_ref}" t="s"#{s_attr}#{ph_attr}><v>#{idx}</v></c>)
464
471
  else
465
- %(<c r="#{cell_ref}" t="inlineStr"#{s_attr}#{ph_attr}><is><t>#{xml_escape(value)}</t></is></c>)
472
+ %(<c r="#{cell_ref}" t="inlineStr"#{s_attr}#{ph_attr}><is><t>#{xml_escape_text(value)}</t></is></c>)
466
473
  end
467
474
  end
468
475
  end
@@ -3180,7 +3180,7 @@ module Xlsxrb
3180
3180
  parts << if entry[:type] == :rt
3181
3181
  "<si>#{rich_text_xml(entry[:value])}</si>"
3182
3182
  else
3183
- "<si><t>#{xml_escape(entry[:value])}</t></si>"
3183
+ "<si><t>#{xml_escape_text(entry[:value])}</t></si>"
3184
3184
  end
3185
3185
  end
3186
3186
  parts << "</sst>"
@@ -118,6 +118,24 @@ module Xlsxrb
118
118
  str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
119
119
  end
120
120
 
121
+ XSTRING_ENCODE_RE = /_(?=x\h{4}_)|[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/
122
+ private_constant :XSTRING_ENCODE_RE
123
+
124
+ # Encodes cell text as ISO/IEC 29500-1 ST_Xstring, the inverse of the _xHHHH_ part
125
+ # of unescape: an underscore that starts an _xHHHH_ sequence becomes _x005F_, and a
126
+ # character that XML 1.0 cannot hold, such as U+0001 or U+FFFE, becomes _xHHHH_.
127
+ # Excel and unescape read the original text back. The value must be UTF-8.
128
+ #
129
+ # @param value [String]
130
+ # @return [String]
131
+ #: (String value) -> String
132
+ def self.encode_xstring(value)
133
+ return value unless value.match?(XSTRING_ENCODE_RE)
134
+
135
+ # The underscore is U+005F, so the same format gives _x005F_.
136
+ value.gsub(XSTRING_ENCODE_RE) { |char| format("_x%04X_", char.ord) }
137
+ end
138
+
121
139
  NUM_ENTITY_RE = /&#(?:(\d+)|x([0-9a-fA-F]+));/
122
140
  XSTRING_RE = /_x([0-9A-Fa-f]{4})_/
123
141
  private_constant :NUM_ENTITY_RE, :XSTRING_RE
@@ -11,9 +11,9 @@ module Xlsxrb
11
11
  # puts "#{cell.ref}: #{cell.value}"
12
12
  # end
13
13
  #
14
- # @example Random access or array conversion (cached on-demand)
14
+ # @example Random access or the row's values (cached on-demand)
15
15
  # cell = row[0]
16
- # values = row.to_a
16
+ # values = row.values
17
17
  #
18
18
  # @api public
19
19
  class StreamRow