xlsxrb 0.1.18 → 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.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +200 -0
  3. data/README.md +35 -12
  4. data/lib/ruby_lsp/xlsxrb/completion_listener.rb +410 -80
  5. data/lib/xlsxrb/chart_builder.rb +251 -386
  6. data/lib/xlsxrb/dsl_helpers.rb +169 -68
  7. data/lib/xlsxrb/elements/cell.rb +37 -157
  8. data/lib/xlsxrb/elements/column.rb +11 -19
  9. data/lib/xlsxrb/elements/coordinate_access.rb +15 -86
  10. data/lib/xlsxrb/elements/image.rb +5 -72
  11. data/lib/xlsxrb/elements/metadata.rb +463 -817
  12. data/lib/xlsxrb/elements/row.rb +27 -145
  13. data/lib/xlsxrb/elements/styles.rb +30 -88
  14. data/lib/xlsxrb/elements/types.rb +6 -99
  15. data/lib/xlsxrb/elements/workbook.rb +19 -126
  16. data/lib/xlsxrb/elements/worksheet.rb +45 -316
  17. data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -1
  18. data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -7
  19. data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +32 -17
  20. data/lib/xlsxrb/ooxml/reader.rb +49 -51
  21. data/lib/xlsxrb/ooxml/styles_parser.rb +26 -2
  22. data/lib/xlsxrb/ooxml/utils.rb +15 -2
  23. data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
  24. data/lib/xlsxrb/ooxml/workbook_writer.rb +184 -94
  25. data/lib/xlsxrb/ooxml/worksheet_parser.rb +61 -29
  26. data/lib/xlsxrb/ooxml/worksheet_writer.rb +353 -261
  27. data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +28 -11
  28. data/lib/xlsxrb/ooxml/writer/features_xml.rb +62 -16
  29. data/lib/xlsxrb/ooxml/writer/styles_xml.rb +1 -1
  30. data/lib/xlsxrb/ooxml/writer.rb +106 -42
  31. data/lib/xlsxrb/ooxml/xml_builder.rb +18 -0
  32. data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
  33. data/lib/xlsxrb/ooxml/zip_reader.rb +65 -15
  34. data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
  35. data/lib/xlsxrb/stream_row.rb +14 -118
  36. data/lib/xlsxrb/stream_sheet.rb +14 -261
  37. data/lib/xlsxrb/stream_writer.rb +807 -1537
  38. data/lib/xlsxrb/style_builder.rb +715 -593
  39. data/lib/xlsxrb/version.rb +1 -1
  40. data/lib/xlsxrb/workbook_builder.rb +403 -286
  41. data/lib/xlsxrb/worksheet_builder.rb +1297 -1458
  42. data/lib/xlsxrb.rb +281 -1079
  43. data/sig/generated/xlsxrb/chart_builder.rbs +202 -205
  44. data/sig/generated/xlsxrb/dsl_helpers.rbs +92 -16
  45. data/sig/generated/xlsxrb/elements/cell.rbs +12 -76
  46. data/sig/generated/xlsxrb/elements/column.rbs +0 -7
  47. data/sig/generated/xlsxrb/elements/coordinate_access.rbs +5 -41
  48. data/sig/generated/xlsxrb/elements/image.rbs +3 -29
  49. data/sig/generated/xlsxrb/elements/metadata.rbs +138 -151
  50. data/sig/generated/xlsxrb/elements/row.rbs +10 -53
  51. data/sig/generated/xlsxrb/elements/styles.rbs +1 -39
  52. data/sig/generated/xlsxrb/elements/workbook.rbs +14 -78
  53. data/sig/generated/xlsxrb/elements/worksheet.rbs +5 -121
  54. data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +0 -4
  55. data/sig/generated/xlsxrb/ooxml/reader.rbs +0 -3
  56. data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
  57. data/sig/generated/xlsxrb/ooxml/utils.rbs +13 -3
  58. data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +17 -0
  59. data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
  60. data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +58 -3
  61. data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +10 -0
  62. data/sig/generated/xlsxrb/ooxml/writer.rbs +20 -14
  63. data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +12 -0
  64. data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
  65. data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +22 -7
  66. data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
  67. data/sig/generated/xlsxrb/stream_row.rbs +9 -44
  68. data/sig/generated/xlsxrb/stream_sheet.rbs +1 -158
  69. data/sig/generated/xlsxrb/stream_writer.rbs +491 -974
  70. data/sig/generated/xlsxrb/style_builder.rbs +464 -239
  71. data/sig/generated/xlsxrb/workbook_builder.rbs +124 -139
  72. data/sig/generated/xlsxrb/worksheet_builder.rbs +922 -686
  73. data/sig/generated/xlsxrb.rbs +56 -188
  74. data/sig/rexml.rbs +8 -0
  75. metadata +3 -5
  76. data/lib/xlsxrb/utils.rb +0 -251
  77. data/sig/generated/xlsxrb/utils.rbs +0 -200
@@ -26,21 +26,64 @@ module Xlsxrb
26
26
 
27
27
  INTEGER_STRINGS = (0..65_535).map { |n| -n.to_s }.freeze
28
28
 
29
- attr_accessor :escape_formulas
29
+ # Matches a value that claims to be a Time through is_a? without being one, such as
30
+ # ActiveSupport::TimeWithZone, which `when Time` does not match. The writers try it
31
+ # after their Time and Date branches, so other values never reach it. Such a value
32
+ # is written as the Time that to_time gives.
33
+ # @api private
34
+ module TimeLike
35
+ #: (untyped value) -> bool
36
+ def self.===(value) = value.is_a?(Time)
37
+
38
+ # A Time with the value's year, month, day, hour, minute, second, and UTC offset,
39
+ # which keeps its wall clock.
40
+ #: (untyped value) -> Time
41
+ def self.to_time(value)
42
+ seconds = value.sec + (value.respond_to?(:subsec) ? value.subsec : 0)
43
+ Time.new(value.year, value.month, value.day, value.hour, value.min, seconds, value.utc_offset)
44
+ end
45
+ end
30
46
 
31
- def initialize(io, date1904: false, escape_formulas: false)
47
+ def initialize(io, date1904: false)
32
48
  @io = io
33
49
  @date1904 = date1904 ? true : false
34
- @escape_formulas = escape_formulas ? true : false
35
50
  @builder = XmlBuilder.new(@io)
36
51
  @row_buffer = String.new(capacity: 65_536)
52
+ @style_attrs = {} # style id => '" s="<id>' fragment, built once per id
37
53
  @started = false
38
54
  @finished = false
55
+ @column_style_ids = nil # column index => resolved style id, see column_style_ids
56
+ @column_style_ids_map = nil
57
+ @time_like_style_resolver = nil
58
+ @strict_excel_mode = false
59
+ end
60
+
61
+ # Makes the row writers reject text longer than a cell holds (32,767 characters),
62
+ # as the strict Excel mode of the caller asks.
63
+ # @api private
64
+ #: (bool strict) -> void
65
+ def strict_excel_mode=(strict)
66
+ @strict_excel_mode = strict ? true : false
39
67
  end
40
68
 
69
+ # Sets what gives the style id for a Time-like value in a styled cell, so that a
70
+ # style without a number format can be given its date-time variant.
71
+ # @api private
72
+ #: (untyped resolver) -> void
73
+ attr_writer :time_like_style_resolver
74
+
41
75
  def set_column_style(col_index, style_id)
42
76
  @column_styles ||= {}
43
77
  @column_styles[col_index] = style_id
78
+ @column_style_ids = nil
79
+ end
80
+
81
+ # Drops the resolved column style ids, for a caller that changed the style id a
82
+ # name in the style map stands for.
83
+ # @api private
84
+ #: () -> void
85
+ def forget_column_style_ids
86
+ @column_style_ids = nil
44
87
  end
45
88
 
46
89
  # Write the worksheet header. Call once before writing rows.
@@ -111,6 +154,8 @@ module Xlsxrb
111
154
  buf << ' outlineLevel="' << attrs[:outline_level].to_s << '"' if attrs[:outline_level]
112
155
  buf << ">"
113
156
 
157
+ row_style = resolve_style_id(attrs[:style_index], nil)
158
+ style_attrs = @style_attrs
114
159
  cells.each do |cell|
115
160
  if cell.is_a?(Elements::Cell)
116
161
  value = cell.value
@@ -139,37 +184,45 @@ module Xlsxrb
139
184
  end
140
185
 
141
186
  cell_style_id = resolve_style_id(cell_style_id, nil)
142
- col_style = @column_styles && col_idx ? @column_styles[col_idx] : nil
143
- col_style = resolve_style_id(col_style, nil)
144
- row_style = resolve_style_id(attrs[:style_index], nil)
145
- style_id = cell_style_id.nil? ? (row_style || col_style) : cell_style_id
187
+ style_id = cell_style_id.nil? ? (row_style || (@column_styles && col_idx && resolve_style_id(@column_styles[col_idx], nil))) : cell_style_id
146
188
  style_id = nil unless style_id.is_a?(Integer)
147
189
 
148
190
  raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" if value.is_a?(Float) && !value.finite?
149
191
 
150
- # Fast path for common unstyled cells
151
- if !formula && !style_id && !cell_type_val
192
+ # Fast path for common cells, styled or not
193
+ if !formula && !cell_type_val
194
+ # '" s="<id>' to append after the cell reference, or "" for an unstyled cell
195
+ s_attr = style_id ? (style_attrs[style_id] ||= -%(" s="#{style_id})) : ""
152
196
  case value
153
197
  when Integer, Float
154
- buf << '<c r="' << col_ref << '"><v>' << value.to_s << "</v></c>"
198
+ buf << '<c r="' << col_ref << s_attr << '"><v>' << value.to_s << "</v></c>"
155
199
  next
156
200
  when String
157
201
  if sst_index && (idx = sst_index[value])
158
- buf << '<c r="' << col_ref << '" t="s"><v>' << idx.to_s << "</v></c>"
202
+ buf << '<c r="' << col_ref << s_attr << '" t="s"><v>' << idx.to_s << "</v></c>"
159
203
  next
160
204
  end
161
205
  when true
162
- buf << '<c r="' << col_ref << '" t="b"><v>1</v></c>'
206
+ buf << '<c r="' << col_ref << s_attr << '" t="b"><v>1</v></c>'
163
207
  next
164
208
  when false
165
- buf << '<c r="' << col_ref << '" t="b"><v>0</v></c>'
209
+ buf << '<c r="' << col_ref << s_attr << '" t="b"><v>0</v></c>'
166
210
  next
167
211
  when nil
168
- buf << '<c r="' << col_ref << '"/>'
212
+ # An empty cell keeps only its own style, not the row or column style.
213
+ if cell_style_id.is_a?(Integer)
214
+ buf << '<c r="' << col_ref << '" s="' << cell_style_id.to_s << '"/>'
215
+ else
216
+ buf << '<c r="' << col_ref << '"/>'
217
+ end
218
+ next
219
+ when DateTime
220
+ serial = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
221
+ buf << '<c r="' << col_ref << s_attr << '"><v>' << serial.to_s << "</v></c>"
169
222
  next
170
223
  when Date
171
224
  serial = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)
172
- buf << '<c r="' << col_ref << '"><v>' << serial.to_s << "</v></c>"
225
+ buf << '<c r="' << col_ref << s_attr << '"><v>' << serial.to_s << "</v></c>"
173
226
  next
174
227
  end
175
228
  end
@@ -192,7 +245,7 @@ module Xlsxrb
192
245
  if formula
193
246
  if formula.is_a?(Xlsxrb::Elements::Formula)
194
247
  formula_expr = formula.expression
195
- formula_ca = formula.calculate_always
248
+ formula_ca = formula.calculate_always?
196
249
  formula_type = formula.type
197
250
  formula_ref = formula.ref
198
251
  formula_si = formula.shared_index
@@ -254,10 +307,12 @@ module Xlsxrb
254
307
  when false
255
308
  xml_val = "0"
256
309
  type = "b"
310
+ when Time, DateTime
311
+ xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
257
312
  when Date
258
313
  xml_val = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)
259
- when Time
260
- xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
314
+ when TimeLike # after Date, so Date values skip TimeLike.===
315
+ xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)
261
316
  when BigDecimal
262
317
  xml_val = value.to_s("F")
263
318
  when Xlsxrb::Elements::CellError
@@ -274,7 +329,7 @@ module Xlsxrb
274
329
  if xml_val.is_a?(Xlsxrb::Elements::RichText)
275
330
  write_inline_rich_text(buf, xml_val)
276
331
  else
277
- buf << "><is><t>" << escape_xml(xml_val.to_s) << "</t></is></c>"
332
+ buf << "><is><t>" << escape_text(xml_val.to_s) << "</t></is></c>"
278
333
  end
279
334
  elsif formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?)
280
335
  f_attrs = +""
@@ -300,7 +355,7 @@ module Xlsxrb
300
355
  else
301
356
  v_str = xml_val.to_s
302
357
  v_str = v_str.dup.force_encoding(Encoding::UTF_8) if v_str.encoding == Encoding::BINARY
303
- v_str = escape_xml(v_str) if type == "str" || xml_val.is_a?(String)
358
+ v_str = escape_text(v_str) if type == "str" || xml_val.is_a?(String)
304
359
  buf << "<v>" << v_str << "</v></c>"
305
360
  end
306
361
  else
@@ -347,7 +402,7 @@ module Xlsxrb
347
402
 
348
403
  buf << '<c r="' << col_ref << row_num_str << '"><v>' << value.to_s << "</v></c>"
349
404
  when String
350
- raise ArgumentError, "Cell text length #{value.length} exceeds Excel limit of 32,767 characters" if @strict_excel_mode && value.length > 32_767
405
+ DslHelpers.check_cell_text_length!(value) if @strict_excel_mode && value.bytesize > DslHelpers::MAX_CELL_TEXT_LENGTH
351
406
 
352
407
  if sst_index
353
408
  idx = (sst_index[value] ||= begin
@@ -357,12 +412,19 @@ module Xlsxrb
357
412
  idx_str = idx < 65_536 ? INTEGER_STRINGS[idx] : idx.to_s
358
413
  buf << '<c r="' << col_ref << row_num_str << '" t="s"><v>' << idx_str << "</v></c>"
359
414
  else
360
- buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' << escape_xml(value) << "</t></is></c>"
415
+ buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' << escape_text(value) << "</t></is></c>"
361
416
  end
362
417
  when true
363
418
  buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>1</v></c>'
364
419
  when false
365
420
  buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>0</v></c>'
421
+ when Time, DateTime
422
+ time_style_id = style_map ? style_map["__xlsxrb_time"] : nil
423
+ buf << if time_style_id
424
+ "<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
425
+ else
426
+ "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
427
+ end
366
428
  when Date
367
429
  date_style_id = style_map ? style_map["__xlsxrb_date"] : nil
368
430
  buf << if date_style_id
@@ -370,12 +432,12 @@ module Xlsxrb
370
432
  else
371
433
  "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>"
372
434
  end
373
- when Time
435
+ when TimeLike # after Date, so Date values skip TimeLike.===
374
436
  time_style_id = style_map ? style_map["__xlsxrb_time"] : nil
375
437
  buf << if time_style_id
376
- "<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
438
+ "<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>"
377
439
  else
378
- "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
440
+ "<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>"
379
441
  end
380
442
  when Xlsxrb::Elements::Formula
381
443
  raise ArgumentError, "Non-finite Float #{value.cached_value} is not supported in XLSX" if value.cached_value.is_a?(Float) && !value.cached_value.finite?
@@ -394,7 +456,7 @@ module Xlsxrb
394
456
  f_attrs << %( ref="#{value.ref}") if value.ref
395
457
  end
396
458
  end
397
- f_attrs << ' ca="1"' if value.calculate_always
459
+ f_attrs << ' ca="1"' if value.calculate_always?
398
460
  c_type = ""
399
461
  case value.cached_value
400
462
  when true
@@ -481,7 +543,7 @@ module Xlsxrb
481
543
  buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"'
482
544
  write_inline_rich_text(buf, value)
483
545
  else
484
- buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"inlineStr\"><is><t>#{escape_xml(value.to_s)}</t></is></c>"
546
+ buf << "<c r=\"#{col_ref}#{row_num_str}\" t=\"inlineStr\"><is><t>#{escape_text(value.to_s)}</t></is></c>"
485
547
  end
486
548
  end
487
549
  end
@@ -520,215 +582,7 @@ module Xlsxrb
520
582
  max_len = [max_len, styles_len].max
521
583
  end
522
584
 
523
- col_index = 0
524
- while col_index < max_len
525
- value = col_index < values.length ? values[col_index] : nil
526
- cell_style_id = single_style_id
527
- if is_styles_collection
528
- style_name = if styles.is_a?(Array)
529
- col_index < styles.length ? styles[col_index] : nil
530
- else
531
- styles[col_index]
532
- end
533
- cell_style_id = resolve_style_id(style_name, style_map)
534
- end
535
-
536
- if cell_style_id.nil?
537
- col_style = @column_styles ? @column_styles[col_index] : nil
538
- col_style_id = resolve_style_id(col_style, style_map)
539
- style_id = row_style_id || col_style_id
540
- else
541
- style_id = cell_style_id
542
- end
543
-
544
- col_ref = COLUMN_LETTERS[col_index] || column_letter(col_index)
545
-
546
- if value.nil?
547
- buf << '<c r="' << col_ref << row_num_str << '" s="' << cell_style_id.to_s << '"/>' if cell_style_id
548
- col_index += 1
549
- next
550
- end
551
-
552
- raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" if value.is_a?(Float) && !value.finite?
553
-
554
- # Fast path: unstyled numbers, booleans, and simple strings (majority of cells)
555
- if style_id.nil? && !value.is_a?(Xlsxrb::Elements::Formula) && !value.is_a?(Hash)
556
- case value
557
- when Integer
558
- val_str = INTEGER_STRINGS[value] || value.to_s
559
- buf << '<c r="' << col_ref << row_num_str << '"><v>' << val_str << "</v></c>"
560
- col_index += 1
561
- next
562
- when Float
563
- buf << '<c r="' << col_ref << row_num_str << '"><v>' << value.to_s << "</v></c>"
564
- col_index += 1
565
- next
566
- when String
567
- if sst_index
568
- idx = sst_index[value]
569
- unless idx
570
- sst << value
571
- idx = sst.size - 1
572
- sst_index[value] = idx
573
- end
574
- idx_str = INTEGER_STRINGS[idx] || idx.to_s
575
- buf << '<c r="' << col_ref << row_num_str << '" t="s"><v>' << idx_str << "</v></c>"
576
- else
577
- buf << '<c r="' << col_ref << row_num_str << '" t="inlineStr"><is><t>' << escape_xml(value) << "</t></is></c>"
578
- end
579
- col_index += 1
580
- next
581
- when true
582
- buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>1</v></c>'
583
- col_index += 1
584
- next
585
- when false
586
- buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>0</v></c>'
587
- col_index += 1
588
- next
589
- when Date
590
- buf << '<c r="' << col_ref << row_num_str << '"><v>' << Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904).to_s << "</v></c>"
591
- col_index += 1
592
- next
593
- when Time
594
- buf << '<c r="' << col_ref << row_num_str << '"><v>' << Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904).to_s << "</v></c>"
595
- col_index += 1
596
- next
597
- end
598
- end
599
-
600
- # General path: styled, formula, rich text, or complex cell
601
- formula_expr = nil
602
- formula_ca = false
603
- formula_type = nil
604
- formula_ref = nil
605
- formula_si = nil
606
- xml_val = value
607
- type = nil
608
-
609
- case value
610
- when Xlsxrb::Elements::Formula
611
- formula_expr = value.expression
612
- formula_ca = value.calculate_always
613
- formula_type = value.type
614
- formula_ref = value.ref
615
- formula_si = value.shared_index
616
- xml_val = value.cached_value
617
- case value.cached_value
618
- when String
619
- type = "str"
620
- when true
621
- type = "b"
622
- xml_val = "1"
623
- when false
624
- type = "b"
625
- xml_val = "0"
626
- when Xlsxrb::Elements::CellError
627
- type = "e"
628
- xml_val = value.cached_value.code
629
- end
630
- when Hash
631
- if value.key?(:formula)
632
- formula_expr = value[:formula] || value[:expression]
633
- formula_ca = value[:calculate_always] || false
634
- formula_type = value[:type]
635
- formula_ref = value[:ref]
636
- formula_si = value[:si] || value[:shared_index]
637
- xml_val = if value.key?(:value)
638
- value[:value]
639
- elsif value.key?(:cached_value)
640
- value[:cached_value]
641
- end
642
- case xml_val
643
- when String
644
- type = "str"
645
- when true
646
- type = "b"
647
- xml_val = "1"
648
- when false
649
- type = "b"
650
- xml_val = "0"
651
- when Xlsxrb::Elements::CellError
652
- type = "e"
653
- xml_val = xml_val.code
654
- end
655
- end
656
- when String, Xlsxrb::Elements::RichText
657
- if sst_index
658
- idx = sst_index[value]
659
- unless idx
660
- sst << value
661
- idx = sst.size - 1
662
- sst_index[value] = idx
663
- end
664
- xml_val = idx
665
- type = "s"
666
- else
667
- xml_val = value
668
- type = "inlineStr"
669
- end
670
- when true
671
- xml_val = "1"
672
- type = "b"
673
- when false
674
- xml_val = "0"
675
- type = "b"
676
- when Date
677
- xml_val = Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)
678
- when Time
679
- xml_val = Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)
680
- when BigDecimal
681
- xml_val = value.to_s("F")
682
- when Xlsxrb::Elements::CellError
683
- xml_val = value.code
684
- type = "e"
685
- end
686
-
687
- raise ArgumentError, "Non-finite Float #{xml_val} is not supported in XLSX" if xml_val.is_a?(Float) && !xml_val.finite?
688
-
689
- formula_expr = formula_expr[1..] if formula_expr&.start_with?("=")
690
-
691
- buf << '<c r="' << col_ref << row_num_str << '"'
692
- buf << ' s="' << style_id.to_s << '"' if style_id
693
- buf << ' t="' << type << '"' if type
694
- if formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?)
695
- f_attrs = +""
696
- if formula_type
697
- case formula_type.to_sym
698
- when :array
699
- f_attrs << ' t="array"'
700
- f_attrs << %( ref="#{formula_ref}") if formula_ref
701
- when :shared
702
- f_attrs << ' t="shared"'
703
- f_attrs << %( si="#{formula_si}") if formula_si
704
- f_attrs << %( ref="#{formula_ref}") if formula_ref
705
- end
706
- end
707
- f_attrs << ' ca="1"' if formula_ca
708
- if formula_expr && !formula_expr.empty?
709
- buf << "><f#{f_attrs}>" << escape_xml(formula_expr) << "</f>"
710
- else
711
- buf << "><f#{f_attrs}/>"
712
- end
713
- if xml_val.nil?
714
- buf << "<v>0</v></c>"
715
- else
716
- v_str = xml_val.to_s
717
- v_str = escape_xml(v_str) if type == "str" || xml_val.is_a?(String)
718
- buf << "<v>" << v_str << "</v></c>"
719
- end
720
- elsif type == "inlineStr"
721
- if xml_val.is_a?(Xlsxrb::Elements::RichText)
722
- write_inline_rich_text(buf, xml_val)
723
- else
724
- buf << "><is><t>" << escape_xml(xml_val.to_s) << "</t></is></c>"
725
- end
726
- else
727
- buf << "><v>" << xml_val.to_s << "</v></c>"
728
- end
729
-
730
- col_index += 1
731
- end
585
+ write_styled_cells(buf, values, max_len, row_num_str, styles, is_styles_collection, single_style_id, row_style_id, style_map, sst, sst_index)
732
586
 
733
587
  buf << "</row>"
734
588
  return unless buf.bytesize >= 32_768
@@ -794,8 +648,218 @@ module Xlsxrb
794
648
 
795
649
  private
796
650
 
651
+ # Writes the cells of a row that has cell styles, row attributes, or column styles.
652
+ # It is not inlined in write_row_values because YJIT ran the same loop there about
653
+ # 1.6 times slower, after the row attribute handling.
654
+ def write_styled_cells(buf, values, max_len, row_num_str, styles, is_styles_collection, single_style_id, row_style_id, style_map, sst, sst_index)
655
+ style_attrs = @style_attrs
656
+ column_ids = @column_styles && column_style_ids(style_map)
657
+ col_index = 0
658
+ while col_index < max_len
659
+ # Indexing past the end of values or styles (Array or Hash) yields nil.
660
+ value = values[col_index]
661
+ cell_style_id = is_styles_collection ? resolve_style_id(styles[col_index], style_map) : single_style_id
662
+ style_id = cell_style_id || row_style_id || (column_ids && column_ids[col_index])
663
+ col_ref = COLUMN_LETTERS[col_index] || column_letter(col_index)
664
+ col_index += 1
665
+
666
+ if value.nil?
667
+ buf << '<c r="' << col_ref << row_num_str << '" s="' << cell_style_id.to_s << '"/>' if cell_style_id
668
+ next
669
+ end
670
+
671
+ # '" s="<id>' to append after the cell reference, or "" for an unstyled cell
672
+ s_attr = style_id ? (style_attrs[style_id] ||= -%(" s="#{style_id})) : ""
673
+ case value
674
+ when Integer
675
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << (value >= 0 && value < 65_536 ? INTEGER_STRINGS[value] : value.to_s) << "</v></c>"
676
+ when Float
677
+ raise ArgumentError, "Non-finite Float #{value} is not supported in XLSX" unless value.finite?
678
+
679
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << value.to_s << "</v></c>"
680
+ when String
681
+ DslHelpers.check_cell_text_length!(value) if @strict_excel_mode && value.bytesize > DslHelpers::MAX_CELL_TEXT_LENGTH
682
+
683
+ if sst_index
684
+ idx = sst_index[value]
685
+ unless idx
686
+ sst << value
687
+ idx = sst.size - 1
688
+ sst_index[value] = idx
689
+ end
690
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="s"><v>' << (INTEGER_STRINGS[idx] || idx.to_s) << "</v></c>"
691
+ else
692
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="inlineStr"><is><t>' << escape_text(value) << "</t></is></c>"
693
+ end
694
+ when true
695
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="b"><v>1</v></c>'
696
+ when false
697
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="b"><v>0</v></c>'
698
+ when Time, DateTime
699
+ if style_id.nil? && style_map && (time_style_id = style_map["__xlsxrb_time"])
700
+ s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id})
701
+ end
702
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904).to_s << "</v></c>"
703
+ when Date
704
+ if style_id.nil? && style_map && (date_style_id = style_map["__xlsxrb_date"])
705
+ s_attr = style_attrs[date_style_id] ||= -%(" s="#{date_style_id})
706
+ end
707
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904).to_s << "</v></c>"
708
+ when TimeLike
709
+ if style_id.nil?
710
+ time_style_id = style_map && style_map["__xlsxrb_time"]
711
+ elsif @time_like_style_resolver
712
+ time_style_id = @time_like_style_resolver.call(style_id)
713
+ end
714
+ s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id}) if time_style_id
715
+ buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904).to_s << "</v></c>"
716
+ else
717
+ write_complex_cell(buf, col_ref, row_num_str, value, style_id, sst, sst_index)
718
+ end
719
+ end
720
+ end
721
+
722
+ # The style id of each column style, indexed by column, resolved once instead of
723
+ # for every cell. It is resolved again when the style map is another Hash, or after
724
+ # set_column_style or forget_column_style_ids.
725
+ #: (Hash[untyped, Integer]? style_map) -> Array[Integer?]
726
+ def column_style_ids(style_map)
727
+ ids = @column_style_ids
728
+ return ids if ids && @column_style_ids_map.equal?(style_map)
729
+
730
+ ids = []
731
+ @column_styles&.each { |col_index, style| ids[col_index] = resolve_style_id(style, style_map) }
732
+ @column_style_ids_map = style_map
733
+ @column_style_ids = ids
734
+ end
735
+
736
+ # Writes a cell for write_styled_cells whose value is a formula, rich text, BigDecimal,
737
+ # cell error, or another value that write_styled_cells does not write itself.
738
+ def write_complex_cell(buf, col_ref, row_num_str, value, style_id, sst, sst_index)
739
+ formula_expr = nil
740
+ formula_ca = false
741
+ formula_type = nil
742
+ formula_ref = nil
743
+ formula_si = nil
744
+ xml_val = value
745
+ type = nil
746
+
747
+ case value
748
+ when Xlsxrb::Elements::Formula
749
+ formula_expr = value.expression
750
+ formula_ca = value.calculate_always?
751
+ formula_type = value.type
752
+ formula_ref = value.ref
753
+ formula_si = value.shared_index
754
+ xml_val = value.cached_value
755
+ case value.cached_value
756
+ when String
757
+ type = "str"
758
+ when true
759
+ type = "b"
760
+ xml_val = "1"
761
+ when false
762
+ type = "b"
763
+ xml_val = "0"
764
+ when Xlsxrb::Elements::CellError
765
+ type = "e"
766
+ xml_val = value.cached_value.code
767
+ end
768
+ when Hash
769
+ if value.key?(:formula)
770
+ formula_expr = value[:formula] || value[:expression]
771
+ formula_ca = value[:calculate_always] || false
772
+ formula_type = value[:type]
773
+ formula_ref = value[:ref]
774
+ formula_si = value[:si] || value[:shared_index]
775
+ xml_val = if value.key?(:value)
776
+ value[:value]
777
+ elsif value.key?(:cached_value)
778
+ value[:cached_value]
779
+ end
780
+ case xml_val
781
+ when String
782
+ type = "str"
783
+ when true
784
+ type = "b"
785
+ xml_val = "1"
786
+ when false
787
+ type = "b"
788
+ xml_val = "0"
789
+ when Xlsxrb::Elements::CellError
790
+ type = "e"
791
+ xml_val = xml_val.code
792
+ end
793
+ end
794
+ when Xlsxrb::Elements::RichText
795
+ if sst_index
796
+ idx = sst_index[value]
797
+ unless idx
798
+ sst << value
799
+ idx = sst.size - 1
800
+ sst_index[value] = idx
801
+ end
802
+ xml_val = idx
803
+ type = "s"
804
+ else
805
+ xml_val = value
806
+ type = "inlineStr"
807
+ end
808
+ when BigDecimal
809
+ xml_val = value.to_s("F")
810
+ when Xlsxrb::Elements::CellError
811
+ xml_val = value.code
812
+ type = "e"
813
+ end
814
+
815
+ raise ArgumentError, "Non-finite Float #{xml_val} is not supported in XLSX" if xml_val.is_a?(Float) && !xml_val.finite?
816
+
817
+ formula_expr = formula_expr[1..] if formula_expr&.start_with?("=")
818
+
819
+ buf << '<c r="' << col_ref << row_num_str << '"'
820
+ buf << ' s="' << style_id.to_s << '"' if style_id
821
+ buf << ' t="' << type << '"' if type
822
+ if formula_expr || (formula_type && formula_type.to_sym == :shared && !formula_si.nil?)
823
+ f_attrs = +""
824
+ if formula_type
825
+ case formula_type.to_sym
826
+ when :array
827
+ f_attrs << ' t="array"'
828
+ f_attrs << %( ref="#{formula_ref}") if formula_ref
829
+ when :shared
830
+ f_attrs << ' t="shared"'
831
+ f_attrs << %( si="#{formula_si}") if formula_si
832
+ f_attrs << %( ref="#{formula_ref}") if formula_ref
833
+ end
834
+ end
835
+ f_attrs << ' ca="1"' if formula_ca
836
+ if formula_expr && !formula_expr.empty?
837
+ buf << "><f#{f_attrs}>" << escape_xml(formula_expr) << "</f>"
838
+ else
839
+ buf << "><f#{f_attrs}/>"
840
+ end
841
+ if xml_val.nil?
842
+ buf << "<v>0</v></c>"
843
+ else
844
+ v_str = xml_val.to_s
845
+ v_str = escape_text(v_str) if type == "str" || xml_val.is_a?(String)
846
+ buf << "<v>" << v_str << "</v></c>"
847
+ end
848
+ elsif type == "inlineStr"
849
+ if xml_val.is_a?(Xlsxrb::Elements::RichText)
850
+ write_inline_rich_text(buf, xml_val)
851
+ else
852
+ buf << "><is><t>" << escape_text(xml_val.to_s) << "</t></is></c>"
853
+ end
854
+ else
855
+ buf << "><v>" << xml_val.to_s << "</v></c>"
856
+ end
857
+ end
858
+
797
859
  def resolve_style_id(style, style_map)
798
860
  return nil if style.nil?
861
+ # Without names to look up, an index is the style id.
862
+ return style if style_map.nil? && style.is_a?(Integer)
799
863
 
800
864
  if style_map
801
865
  mapped = style_map[style] || style_map[style.to_s]
@@ -819,7 +883,7 @@ module Xlsxrb
819
883
  apply_styles = props.fetch(:apply_styles, outline_props[:apply_styles])
820
884
  summary_below = props.fetch(:summary_below, props.fetch(:outline_below, outline_props[:summary_below]))
821
885
  summary_right = props.fetch(:summary_right, props.fetch(:outline_right, outline_props[:summary_right]))
822
- show_outline_symbols = props.fetch(:show_outline_symbols, props.fetch(:show_symbols, outline_props[:show_outline_symbols]))
886
+ show_outline_symbols = props.fetch(:show_outline_symbols, outline_props[:show_outline_symbols])
823
887
  has_outline = !apply_styles.nil? || !summary_below.nil? || !summary_right.nil? || !show_outline_symbols.nil?
824
888
 
825
889
  has_children = tc_attrs || !props[:fit_to_page].nil? || has_outline
@@ -836,8 +900,11 @@ module Xlsxrb
836
900
  @builder.empty_tag("outlinePr", outline_attrs) unless outline_attrs.empty?
837
901
  end
838
902
 
839
- unless props[:fit_to_page].nil?
840
- @builder.empty_tag("pageSetUpPr", { fitToPage: props[:fit_to_page] ? "1" : "0" })
903
+ psp = props[:page_setup_properties] || props[:page_setup_pr]
904
+ fit_to_page = props[:fit_to_page]
905
+ fit_to_page = psp[:fit_to_page] if fit_to_page.nil? && psp.is_a?(Hash)
906
+ unless fit_to_page.nil?
907
+ @builder.empty_tag("pageSetUpPr", { fitToPage: fit_to_page ? "1" : "0" })
841
908
  end
842
909
 
843
910
  @builder.close_tag("sheetPr")
@@ -857,7 +924,7 @@ module Xlsxrb
857
924
  attrs[:tint] = tc[:tint].to_s if tc[:tint]
858
925
  attrs.empty? ? nil : attrs
859
926
  elsif tc.is_a?(String) || tc.is_a?(Symbol)
860
- { rgb: tc.to_s.delete_prefix("#") }
927
+ { rgb: tab_color_rgb(tc) }
861
928
  elsif props[:tab_color_theme]
862
929
  attrs = { theme: props[:tab_color_theme].to_s }
863
930
  attrs[:tint] = props[:tab_color_tint].to_s if props[:tab_color_tint]
@@ -869,6 +936,18 @@ module Xlsxrb
869
936
  end
870
937
  end
871
938
 
939
+ # Writes a hex tab color as given (without "#") and resolves a color name such as :red to
940
+ # aRGB hex, as for cell styles.
941
+ def tab_color_rgb(color)
942
+ hex = color.to_s.delete_prefix("#")
943
+ return hex if hex.match?(/\A(\h{6}|\h{8})\z/)
944
+
945
+ rgb = StyleBuilder.resolve_color(color.is_a?(Symbol) ? color : color.to_s.to_sym).to_s
946
+ raise ArgumentError, "unknown tab color: #{color.inspect}" unless rgb.match?(/\A\h{8}\z/)
947
+
948
+ rgb
949
+ end
950
+
872
951
  def write_sheet_views(freeze_pane: nil, split_pane: nil, selection: nil, sheet_view: nil)
873
952
  @builder.open_tag("sheetViews")
874
953
  sv_attrs = { workbookViewId: "0" }
@@ -895,16 +974,18 @@ module Xlsxrb
895
974
  @builder.open_tag("sheetView", sv_attrs)
896
975
  if freeze_pane
897
976
  pane_attrs = {}
898
- pane_attrs[:xSplit] = freeze_pane[:col].to_s if freeze_pane[:col]&.positive?
899
- pane_attrs[:ySplit] = freeze_pane[:row].to_s if freeze_pane[:row]&.positive?
900
- top_left_col = column_letter(freeze_pane[:col] || 0)
901
- top_left_row = (freeze_pane[:row] || 0) + 1
977
+ col_val = freeze_pane[:column] || freeze_pane[:col]
978
+ row_val = freeze_pane[:row]
979
+ pane_attrs[:xSplit] = col_val.to_s if col_val&.positive?
980
+ pane_attrs[:ySplit] = row_val.to_s if row_val&.positive?
981
+ top_left_col = column_letter(col_val || 0)
982
+ top_left_row = (row_val || 0) + 1
902
983
  pane_attrs[:topLeftCell] = freeze_pane[:top_left_cell] || "#{top_left_col}#{top_left_row}"
903
984
  pane_attrs[:state] = "frozen"
904
985
  # Determine active pane
905
- pane_attrs[:activePane] = freeze_pane[:active_pane] || if (freeze_pane[:col] || 0).positive? && (freeze_pane[:row] || 0).positive?
986
+ pane_attrs[:activePane] = freeze_pane[:active_pane] || if (col_val || 0).positive? && (row_val || 0).positive?
906
987
  "bottomRight"
907
- elsif (freeze_pane[:col] || 0).positive?
988
+ elsif (col_val || 0).positive?
908
989
  "topRight"
909
990
  else
910
991
  "bottomLeft"
@@ -1359,6 +1440,8 @@ module Xlsxrb
1359
1440
  attrs[:pageOrder] = opts[:page_order].to_s if opts[:page_order]
1360
1441
  attrs[:blackAndWhite] = "1" if opts[:black_and_white]
1361
1442
  attrs[:draft] = "1" if opts[:draft]
1443
+ attrs[:horizontalDpi] = opts[:horizontal_dpi].to_s if opts[:horizontal_dpi].to_i.positive?
1444
+ attrs[:verticalDpi] = opts[:vertical_dpi].to_s if opts[:vertical_dpi].to_i.positive?
1362
1445
  @builder.empty_tag("pageSetup", attrs) unless attrs.empty?
1363
1446
  end
1364
1447
 
@@ -1474,7 +1557,8 @@ module Xlsxrb
1474
1557
  group = group.to_h if group.is_a?(Elements::MetadataRecord)
1475
1558
  sg_attrs = {}
1476
1559
  sg_attrs[:type] = group[:type] if group[:type]
1477
- sg_attrs[:displayEmptyCellsAs] = group[:display_empty] if group[:display_empty]
1560
+ disp_empty = group[:display_empty_cells_as] || group[:display_empty]
1561
+ sg_attrs[:displayEmptyCellsAs] = disp_empty if disp_empty
1478
1562
  sg_attrs[:markers] = "1" if group[:markers]
1479
1563
  sg_attrs[:high] = "1" if group[:high]
1480
1564
  sg_attrs[:low] = "1" if group[:low]
@@ -1504,10 +1588,10 @@ module Xlsxrb
1504
1588
  sp = sp.to_h if sp.is_a?(Elements::MetadataRecord)
1505
1589
  @builder.open_tag("x14:sparkline")
1506
1590
  @builder.open_tag("xm:f")
1507
- @builder.text(sp[:data_ref] || sp[:formula] || sp[:f])
1591
+ @builder.text(sp[:formula] || sp[:data_ref] || sp[:f])
1508
1592
  @builder.close_tag("xm:f")
1509
1593
  @builder.open_tag("xm:sqref")
1510
- @builder.text(sp[:location_ref] || sp[:sqref] || sp[:location])
1594
+ @builder.text(sp[:sqref] || sp[:location_ref] || sp[:location])
1511
1595
  @builder.close_tag("xm:sqref")
1512
1596
  @builder.close_tag("x14:sparkline")
1513
1597
  end
@@ -1535,10 +1619,7 @@ module Xlsxrb
1535
1619
  # --- Columns ---
1536
1620
 
1537
1621
  def write_columns(columns)
1538
- default_width = if @sheet_format
1539
- @sheet_format[:default_column_width] || @sheet_format[:default_col_width] ||
1540
- @sheet_format["default_column_width"] || @sheet_format["default_col_width"]
1541
- end
1622
+ default_width = @sheet_format.to_h[:default_column_width]
1542
1623
  @builder.open_tag("cols")
1543
1624
  columns.each do |col|
1544
1625
  if col.is_a?(Elements::Column)
@@ -1546,11 +1627,11 @@ module Xlsxrb
1546
1627
  max = col.unmapped_data[:max] || (col.index + 1)
1547
1628
  style_id = col.style_index
1548
1629
  width = col.width || (style_id ? default_width : nil)
1549
- hidden = col.hidden
1550
- custom_width = col.custom_width || !width.nil?
1630
+ hidden = col.hidden?
1631
+ custom_width = col.custom_width? || !width.nil?
1551
1632
  outline_level = col.outline_level
1552
- collapsed = col.unmapped_data[:collapsed]
1553
- best_fit = (col.respond_to?(:best_fit) && col.best_fit) || col.unmapped_data[:best_fit]
1633
+ collapsed = col.collapsed? || col.unmapped_data[:collapsed]
1634
+ best_fit = col.best_fit? || col.unmapped_data[:best_fit]
1554
1635
  else
1555
1636
  min = col[:min] || ((col[:index] || 0) + 1)
1556
1637
  max = col[:max] || ((col[:index] || 0) + 1)
@@ -1619,7 +1700,7 @@ module Xlsxrb
1619
1700
  f_attrs[:t] = formula.type.to_s if formula.type
1620
1701
  f_attrs[:ref] = formula.ref if formula.ref
1621
1702
  f_attrs[:si] = formula.shared_index.to_s if formula.shared_index
1622
- f_attrs[:ca] = "1" if formula.calculate_always
1703
+ f_attrs[:ca] = "1" if formula.calculate_always?
1623
1704
  else
1624
1705
  formula_text = formula_text[1..] if formula_text.is_a?(String) && formula_text.start_with?("=")
1625
1706
  f_type = cell[:formula_type] || cell[:type]
@@ -1673,8 +1754,8 @@ module Xlsxrb
1673
1754
  def write_inline_rich_text(buf, rich_text)
1674
1755
  buf << "><is>"
1675
1756
  rich_text.runs.each do |run|
1676
- font = run.is_a?(Hash) ? run[:font] : run.font
1677
- run_text = run.is_a?(Hash) ? run[:text] : run.text
1757
+ font = run.respond_to?(:font) ? run.font : run[:font]
1758
+ text = run.respond_to?(:text) ? run.text : run[:text]
1678
1759
  if font && !font.empty?
1679
1760
  buf << "<r><rPr>"
1680
1761
  buf << "<b/>" if font[:bold]
@@ -1732,7 +1813,7 @@ module Xlsxrb
1732
1813
  else
1733
1814
  buf << "<r><t>"
1734
1815
  end
1735
- buf << escape_xml(run_text.to_s) << "</t></r>"
1816
+ buf << escape_text(text.to_s) << "</t></r>"
1736
1817
  end
1737
1818
  buf << "</is></c>"
1738
1819
  end
@@ -1748,6 +1829,17 @@ module Xlsxrb
1748
1829
  str = str.gsub(INVALID_XML_CHARS_RE, "") if str.match?(INVALID_XML_CHARS_RE)
1749
1830
  str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
1750
1831
  end
1832
+
1833
+ # Escapes cell text: encodes it as ST_Xstring (XmlBuilder.encode_xstring), so that
1834
+ # text that looks like _xHHHH_ and characters XML 1.0 cannot hold are read back as
1835
+ # written, then escapes the XML special characters.
1836
+ def escape_text(value)
1837
+ str = value.to_s
1838
+ str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
1839
+ # Most text has neither; the checks cost less than encode_xstring's own.
1840
+ str = XmlBuilder.encode_xstring(str) if str.include?("_x") || str.match?(INVALID_XML_CHARS_RE)
1841
+ str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
1842
+ end
1751
1843
  end
1752
1844
  end
1753
1845
  end