xlsxrb 0.2.1 → 0.2.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -0
- data/README.md +24 -2
- data/lib/xlsxrb/dsl_helpers.rb +16 -0
- data/lib/xlsxrb/elements/styles.rb +15 -6
- data/lib/xlsxrb/number_formatter.rb +26 -8
- data/lib/xlsxrb/ooxml/cfb.rb +11 -3
- data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
- data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
- data/lib/xlsxrb/ooxml/crypto.rb +3 -0
- data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
- data/lib/xlsxrb/ooxml/workbook_writer.rb +30 -19
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +43 -38
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +99 -99
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
- data/lib/xlsxrb/ooxml/writer.rb +166 -159
- data/lib/xlsxrb/ooxml/xml_builder.rb +17 -7
- data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
- data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
- data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
- data/lib/xlsxrb/stream_sheet.rb +26 -11
- data/lib/xlsxrb/stream_writer.rb +9 -6
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/worksheet_builder.rb +6 -5
- data/lib/xlsxrb.rb +68 -21
- data/sig/generated/xlsxrb/dsl_helpers.rbs +9 -0
- data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
- data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +8 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +7 -2
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
- data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
- data/sig/generated/xlsxrb/stream_writer.rbs +6 -2
- data/sig/generated/xlsxrb/worksheet_builder.rbs +3 -1
- data/sig/generated/xlsxrb.rbs +38 -8
- metadata +9 -3
|
@@ -218,19 +218,16 @@ module Xlsxrb
|
|
|
218
218
|
else
|
|
219
219
|
(bg_img.respond_to?(:data) ? bg_img.data : bg_img)
|
|
220
220
|
end
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
else
|
|
230
|
-
"png"
|
|
231
|
-
end
|
|
232
|
-
end
|
|
221
|
+
# The data is the image itself and is never read as a path (ADR 0012).
|
|
222
|
+
bg_ext = if bg_img.is_a?(Hash)
|
|
223
|
+
(bg_img[:ext] || (bg_img[:filename] && File.extname(bg_img[:filename]).delete_prefix(".")) || "png").to_s.downcase
|
|
224
|
+
elsif bg_img.respond_to?(:filename)
|
|
225
|
+
File.extname(bg_img.filename).delete_prefix(".").downcase
|
|
226
|
+
else
|
|
227
|
+
"png"
|
|
228
|
+
end
|
|
233
229
|
if bg_data && !bg_data.empty?
|
|
230
|
+
bg_ext = media_extension(bg_ext)
|
|
234
231
|
p_rid_num = sheet_rels.size + 1
|
|
235
232
|
picture_rid = "rId#{p_rid_num}"
|
|
236
233
|
sheet_rels << { id: picture_rid, type: "#{DOC_REL}/image", target: "../media/sheet#{idx + 1}_bg.#{bg_ext}" }
|
|
@@ -293,7 +290,7 @@ module Xlsxrb
|
|
|
293
290
|
sheet_images.each do |img|
|
|
294
291
|
# One media file per image: a sheet can hold several.
|
|
295
292
|
media_idx += 1
|
|
296
|
-
media_name = "image#{media_idx}.#{img[:ext]
|
|
293
|
+
media_name = "image#{media_idx}.#{media_extension(img[:ext])}"
|
|
297
294
|
zip.add_binary_entry("xl/media/#{media_name}", img[:file_data])
|
|
298
295
|
drawing_rels_data << { type: :image, target: "../media/#{media_name}" }
|
|
299
296
|
drawing_parts << { kind: :pic, img: img, rid_index: drawing_rels_data.size }
|
|
@@ -1250,6 +1247,20 @@ module Xlsxrb
|
|
|
1250
1247
|
ESCAPE_MAP = { "&" => "&", "<" => "<", ">" => ">", '"' => """, "'" => "'" }.freeze
|
|
1251
1248
|
private_constant :INVALID_XML_CHARS_RE, :ESCAPE_RE, :ESCAPE_MAP
|
|
1252
1249
|
|
|
1250
|
+
MEDIA_EXTENSION_RE = /\A[A-Za-z0-9]{1,10}\z/
|
|
1251
|
+
private_constant :MEDIA_EXTENSION_RE
|
|
1252
|
+
|
|
1253
|
+
# Returns the extension for a media part name. It becomes part of a ZIP entry name, a
|
|
1254
|
+
# relationship target, and a content type, so anything but letters and digits is
|
|
1255
|
+
# rejected.
|
|
1256
|
+
#: (untyped ext) -> String
|
|
1257
|
+
def media_extension(ext)
|
|
1258
|
+
str = (ext || "png").to_s
|
|
1259
|
+
raise ArgumentError, "Invalid image extension: #{str.inspect}. Use letters and digits only, such as \"png\"." unless str.match?(MEDIA_EXTENSION_RE)
|
|
1260
|
+
|
|
1261
|
+
str
|
|
1262
|
+
end
|
|
1263
|
+
|
|
1253
1264
|
def escape_xml(value)
|
|
1254
1265
|
str = value.to_s
|
|
1255
1266
|
str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
|
|
@@ -1293,7 +1304,7 @@ module Xlsxrb
|
|
|
1293
1304
|
buf << "<u/>"
|
|
1294
1305
|
else
|
|
1295
1306
|
mapped_u = StyleBuilder::UNDERLINE_MAP[font[:underline].to_sym] || font[:underline]
|
|
1296
|
-
buf << '<u val="' << mapped_u
|
|
1307
|
+
buf << '<u val="' << escape_xml(mapped_u) << '"/>'
|
|
1297
1308
|
end
|
|
1298
1309
|
end
|
|
1299
1310
|
va_val = font[:vert_align] || font[:vertical_align]
|
|
@@ -1301,18 +1312,18 @@ module Xlsxrb
|
|
|
1301
1312
|
va_val ||= "subscript" if font[:subscript]
|
|
1302
1313
|
if va_val
|
|
1303
1314
|
mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val.to_s
|
|
1304
|
-
buf << '<vertAlign val="' << mapped_va << '"/>'
|
|
1315
|
+
buf << '<vertAlign val="' << escape_xml(mapped_va) << '"/>'
|
|
1305
1316
|
end
|
|
1306
1317
|
font_sz = font[:sz] || font[:size]
|
|
1307
|
-
buf << '<sz val="' << font_sz
|
|
1318
|
+
buf << '<sz val="' << escape_xml(font_sz) << '"/>' if font_sz
|
|
1308
1319
|
color_attrs = color_tag_attrs(font[:color] || font)
|
|
1309
1320
|
if color_attrs
|
|
1310
|
-
attrs_str = color_attrs.map { |k, v| %(#{k}="#{v}") }.join(" ")
|
|
1321
|
+
attrs_str = color_attrs.map { |k, v| %(#{k}="#{escape_xml(v)}") }.join(" ")
|
|
1311
1322
|
buf << "<color #{attrs_str}/>"
|
|
1312
1323
|
end
|
|
1313
1324
|
buf << '<rFont val="' << escape_xml(font[:name]) << '"/>' if font[:name]
|
|
1314
|
-
buf << '<family val="' << font[:family]
|
|
1315
|
-
buf << '<scheme val="' << font[:scheme]
|
|
1325
|
+
buf << '<family val="' << escape_xml(font[:family]) << '"/>' if font[:family]
|
|
1326
|
+
buf << '<scheme val="' << escape_xml(font[:scheme]) << '"/>' if font[:scheme]
|
|
1316
1327
|
buf << "</rPr><t>"
|
|
1317
1328
|
else
|
|
1318
1329
|
buf << "<r><t>"
|
|
@@ -145,6 +145,13 @@ module Xlsxrb
|
|
|
145
145
|
hl_tag_end = xml.index(">", hl_start + 1)
|
|
146
146
|
break unless hl_tag_end
|
|
147
147
|
|
|
148
|
+
# "hyperlink" in text or a comment rather than in a tag: the tag found before it has
|
|
149
|
+
# already ended. Continue after the word, so that the position always advances.
|
|
150
|
+
if hl_tag_end < hl_start_term
|
|
151
|
+
pos = hl_start_term + 9
|
|
152
|
+
next
|
|
153
|
+
end
|
|
154
|
+
|
|
148
155
|
hl_tag = xml.byteslice(hl_start, hl_tag_end - hl_start)
|
|
149
156
|
ref = tag_attr(hl_tag, ' ref="')
|
|
150
157
|
rid = tag_attr(hl_tag, ' r:id="') || tag_attr(hl_tag, ' id="') || hl_tag[/(?:[a-zA-Z0-9_]+:)?id="([^"]+)"/, 1]
|
|
@@ -547,7 +554,7 @@ module Xlsxrb
|
|
|
547
554
|
def self.resolve_cell_value(type, raw_value, is_xml, shared_strings)
|
|
548
555
|
case type
|
|
549
556
|
when "s"
|
|
550
|
-
raw_value ? (shared_strings
|
|
557
|
+
raw_value ? shared_string_at(shared_strings, raw_value) : nil
|
|
551
558
|
when "b"
|
|
552
559
|
if raw_value
|
|
553
560
|
v = strip_ws(raw_value)
|
|
@@ -568,7 +575,7 @@ module Xlsxrb
|
|
|
568
575
|
text = strip_ws(raw_value)
|
|
569
576
|
return nil if text.empty?
|
|
570
577
|
|
|
571
|
-
if text.match?(STRICT_NUMERIC_RE)
|
|
578
|
+
if text.bytesize <= MAX_NUMERIC_TEXT_BYTES && text.match?(STRICT_NUMERIC_RE)
|
|
572
579
|
Integer(text, 10, exception: false) ||
|
|
573
580
|
Float(text, exception: false) ||
|
|
574
581
|
normalize_text(raw_value)
|
|
@@ -594,10 +601,30 @@ module Xlsxrb
|
|
|
594
601
|
text = strip_ws(raw)
|
|
595
602
|
return nil if text.empty?
|
|
596
603
|
|
|
597
|
-
raise(ParseError, "invalid numeric cell value: #{text.inspect}") unless text.match?(STRICT_NUMERIC_RE)
|
|
604
|
+
raise(ParseError, "invalid numeric cell value: #{text[0, 40].inspect}") unless text.bytesize <= MAX_NUMERIC_TEXT_BYTES && text.match?(STRICT_NUMERIC_RE)
|
|
598
605
|
|
|
599
606
|
Integer(text, 10, exception: false) || Float(text, exception: false) ||
|
|
600
|
-
raise(ParseError, "invalid numeric cell value: #{text.inspect}")
|
|
607
|
+
raise(ParseError, "invalid numeric cell value: #{text[0, 40].inspect}")
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# Excel's last row. A larger row number is not a worksheet row, and an unbounded one
|
|
611
|
+
# would be built digit by digit into an arbitrarily large Integer.
|
|
612
|
+
MAX_ROW_NUMBER = 1_048_576
|
|
613
|
+
|
|
614
|
+
# Longer text is not read as a number. Excel writes numbers in at most 25 characters;
|
|
615
|
+
# converting millions of digits to an Integer takes time that grows faster than their
|
|
616
|
+
# number, and matching them takes memory in proportion.
|
|
617
|
+
MAX_NUMERIC_TEXT_BYTES = 400
|
|
618
|
+
private_constant :MAX_NUMERIC_TEXT_BYTES
|
|
619
|
+
|
|
620
|
+
# Looks up a shared string by the index in a cell's <v>. An index that is negative or
|
|
621
|
+
# too long to be one reads as an empty string, as an index past the end does.
|
|
622
|
+
#: (Array[untyped] shared_strings, String raw_index) -> untyped
|
|
623
|
+
def self.shared_string_at(shared_strings, raw_index)
|
|
624
|
+
return "" if raw_index.bytesize > 10
|
|
625
|
+
|
|
626
|
+
index = raw_index.to_i
|
|
627
|
+
index.negative? ? "" : (shared_strings[index] || "")
|
|
601
628
|
end
|
|
602
629
|
|
|
603
630
|
# SECURITY NOTE: Decodes only standard predefined XML entities in a single pass.
|
|
@@ -710,6 +737,8 @@ module Xlsxrb
|
|
|
710
737
|
break unless cb.between?(48, 57)
|
|
711
738
|
|
|
712
739
|
parsed_r = (parsed_r * 10) + (cb - 48)
|
|
740
|
+
raise ParseError, "row number exceeds #{MAX_ROW_NUMBER}" if parsed_r > MAX_ROW_NUMBER
|
|
741
|
+
|
|
713
742
|
ri += 1
|
|
714
743
|
end
|
|
715
744
|
row_index = parsed_r - 1
|
|
@@ -782,6 +811,10 @@ module Xlsxrb
|
|
|
782
811
|
|
|
783
812
|
private_class_method :fast_scan_rows_direct
|
|
784
813
|
|
|
814
|
+
# Bytes of one row, or of markup between rows, held while streaming a worksheet.
|
|
815
|
+
MAX_STREAM_BUFFER_BYTES = 50 * 1024 * 1024
|
|
816
|
+
private_constant :MAX_STREAM_BUFFER_BYTES
|
|
817
|
+
|
|
785
818
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
786
819
|
def self.scan_rows_stream(source, shared_strings, _part_name, styles = nil, date1904 = false, pad_empty_cells = false, &block)
|
|
787
820
|
# rubocop:enable Style/OptionalBooleanParameter
|
|
@@ -795,6 +828,11 @@ module Xlsxrb
|
|
|
795
828
|
row_end_len = 6
|
|
796
829
|
sd_end_tag = "</sheetData>"
|
|
797
830
|
last_row_index = -1
|
|
831
|
+
# SECURITY NOTE: The buffer is searched again each time a chunk is added. These hold
|
|
832
|
+
# how far the previous searches got without a match, so that a row or a stretch of
|
|
833
|
+
# other markup spanning many chunks is searched once, not once per chunk.
|
|
834
|
+
sd_end_scan_from = 0
|
|
835
|
+
row_end_scan_from = 0
|
|
798
836
|
|
|
799
837
|
push_chunk = lambda do |chunk|
|
|
800
838
|
return if in_sheet_data == :closed
|
|
@@ -837,7 +875,8 @@ module Xlsxrb
|
|
|
837
875
|
|
|
838
876
|
pos = 0
|
|
839
877
|
last_consumed_pos = 0
|
|
840
|
-
sd_end_pos = buffer.index(sd_end_tag)
|
|
878
|
+
sd_end_pos = buffer.index(sd_end_tag, sd_end_scan_from)
|
|
879
|
+
sd_end_scan_from = sd_end_pos || [buffer.bytesize - sd_end_tag.bytesize, 0].max
|
|
841
880
|
|
|
842
881
|
loop do
|
|
843
882
|
row_start = buffer.index(row_start_pattern, pos)
|
|
@@ -848,14 +887,24 @@ module Xlsxrb
|
|
|
848
887
|
return
|
|
849
888
|
end
|
|
850
889
|
|
|
851
|
-
|
|
890
|
+
if row_start.nil?
|
|
891
|
+
# No row starts here: keep only the end, which may hold the start of a tag.
|
|
892
|
+
keep_from = buffer.bytesize - [row_start_len, sd_end_tag.bytesize].max
|
|
893
|
+
last_consumed_pos = keep_from if keep_from > last_consumed_pos
|
|
894
|
+
break
|
|
895
|
+
end
|
|
852
896
|
|
|
853
897
|
next_pos = row_start + row_start_len
|
|
854
|
-
|
|
898
|
+
if next_pos >= buffer.bytesize
|
|
899
|
+
last_consumed_pos = row_start if row_start > last_consumed_pos
|
|
900
|
+
break
|
|
901
|
+
end
|
|
855
902
|
|
|
856
903
|
nb = buffer.getbyte(next_pos)
|
|
857
904
|
unless TAG_DELIM_BYTES.include?(nb)
|
|
905
|
+
# Another element whose name starts with "row": nothing before it is needed.
|
|
858
906
|
pos = next_pos
|
|
907
|
+
last_consumed_pos = pos
|
|
859
908
|
next
|
|
860
909
|
end
|
|
861
910
|
|
|
@@ -865,10 +914,14 @@ module Xlsxrb
|
|
|
865
914
|
is_self_closing = buffer.getbyte(tag_end - 1) == 47
|
|
866
915
|
|
|
867
916
|
unless is_self_closing
|
|
868
|
-
|
|
917
|
+
row_end_from = tag_end + 1
|
|
918
|
+
row_end_from = row_end_scan_from if row_end_scan_from > row_end_from
|
|
919
|
+
row_end_scan_from = 0
|
|
920
|
+
row_end = buffer.index(row_end_tag, row_end_from)
|
|
869
921
|
if row_end.nil?
|
|
870
|
-
|
|
871
|
-
|
|
922
|
+
# The row continues in the next chunk. It will then start where the consumed
|
|
923
|
+
# part of the buffer ends, so the position is kept relative to that.
|
|
924
|
+
row_end_scan_from = [buffer.bytesize - row_end_len - last_consumed_pos, 0].max
|
|
872
925
|
break
|
|
873
926
|
end
|
|
874
927
|
end
|
|
@@ -886,6 +939,8 @@ module Xlsxrb
|
|
|
886
939
|
break unless cb.between?(48, 57)
|
|
887
940
|
|
|
888
941
|
parsed_r = (parsed_r * 10) + (cb - 48)
|
|
942
|
+
raise ParseError, "row number exceeds #{MAX_ROW_NUMBER}" if parsed_r > MAX_ROW_NUMBER
|
|
943
|
+
|
|
889
944
|
ri += 1
|
|
890
945
|
end
|
|
891
946
|
row_index = parsed_r - 1
|
|
@@ -955,7 +1010,11 @@ module Xlsxrb
|
|
|
955
1010
|
last_consumed_pos = pos
|
|
956
1011
|
end
|
|
957
1012
|
|
|
958
|
-
|
|
1013
|
+
if last_consumed_pos.positive?
|
|
1014
|
+
buffer.slice!(0...last_consumed_pos)
|
|
1015
|
+
sd_end_scan_from = [sd_end_scan_from - last_consumed_pos, 0].max
|
|
1016
|
+
end
|
|
1017
|
+
raise ParseError, "malformed worksheet XML: a row or tag exceeds #{MAX_STREAM_BUFFER_BYTES} bytes" if buffer.bytesize > MAX_STREAM_BUFFER_BYTES
|
|
959
1018
|
end
|
|
960
1019
|
|
|
961
1020
|
catch(:done) do
|
|
@@ -1013,6 +1072,48 @@ module Xlsxrb
|
|
|
1013
1072
|
ATTR_T = /\bt="([a-zA-Z]+)"/
|
|
1014
1073
|
ATTR_S = /\bs="(\d+)"/
|
|
1015
1074
|
|
|
1075
|
+
CELL_CLOSE_RE = %r{</(?:[a-zA-Z0-9_]+:)?c>}
|
|
1076
|
+
UNCLOSED_CELL_RE = %r{<(?:[a-zA-Z0-9_]+:)?c(?:\s[^>]*)?(?<!/)>}
|
|
1077
|
+
INLINE_STRING_OPEN_RE = /<(?:[a-zA-Z0-9_]+:)?is[\s>]/
|
|
1078
|
+
INLINE_STRING_CLOSE_RE = %r{</(?:[a-zA-Z0-9_]+:)?is>}
|
|
1079
|
+
private_constant :CELL_CLOSE_RE, :UNCLOSED_CELL_RE, :INLINE_STRING_OPEN_RE, :INLINE_STRING_CLOSE_RE
|
|
1080
|
+
|
|
1081
|
+
# Raises ParseError for a row whose last <c> or <is> start tag has no end tag.
|
|
1082
|
+
#
|
|
1083
|
+
# SECURITY NOTE: CELL_FAST_RE looks ahead for </c> and </is>. For a start tag with no end
|
|
1084
|
+
# tag after it, that search runs to the end of the row and fails, once per such tag, so
|
|
1085
|
+
# a row of 40,000 unclosed <c> tags (a 3 KB file) took minutes. Every search made here
|
|
1086
|
+
# is a single pass over the row.
|
|
1087
|
+
#: (String chunk) -> void
|
|
1088
|
+
def self.verify_cells_closed!(chunk)
|
|
1089
|
+
inline = chunk.include?("<is") || chunk.include?(":is")
|
|
1090
|
+
return if !inline && chunk.end_with?("</c>")
|
|
1091
|
+
|
|
1092
|
+
last_close = chunk.byterindex(CELL_CLOSE_RE)
|
|
1093
|
+
tail_from = last_close ? last_close + 2 : 0
|
|
1094
|
+
raise ParseError, "malformed worksheet XML: <c> element is not closed" if chunk.byteindex(UNCLOSED_CELL_RE, tail_from)
|
|
1095
|
+
return unless inline
|
|
1096
|
+
|
|
1097
|
+
last_close = chunk.byterindex(INLINE_STRING_CLOSE_RE)
|
|
1098
|
+
tail_from = last_close ? last_close + 2 : 0
|
|
1099
|
+
raise ParseError, "malformed worksheet XML: <is> element is not closed" if chunk.byteindex(INLINE_STRING_OPEN_RE, tail_from)
|
|
1100
|
+
end
|
|
1101
|
+
|
|
1102
|
+
private_class_method :verify_cells_closed!
|
|
1103
|
+
|
|
1104
|
+
# Returns the bytes of a row with every sequence that is not UTF-8 replaced by U+FFFD,
|
|
1105
|
+
# so that the text, formulas, and attributes cut from it are valid strings. A row that
|
|
1106
|
+
# is valid, as nearly all are, is returned as is.
|
|
1107
|
+
#: (String chunk) -> String
|
|
1108
|
+
def self.scrub_row(chunk)
|
|
1109
|
+
encoding = chunk.encoding
|
|
1110
|
+
chunk.force_encoding(Encoding::UTF_8)
|
|
1111
|
+
chunk = chunk.scrub unless chunk.valid_encoding?
|
|
1112
|
+
chunk.force_encoding(encoding)
|
|
1113
|
+
end
|
|
1114
|
+
|
|
1115
|
+
private_class_method :scrub_row
|
|
1116
|
+
|
|
1016
1117
|
def self.fast_parse_cells_direct(xml, from, to, shared_strings, row_source, prefix = "")
|
|
1017
1118
|
cells = []
|
|
1018
1119
|
fast_scan_cells_direct(xml, from, to, shared_strings, row_source, prefix) { |c| cells << c }
|
|
@@ -1022,7 +1123,8 @@ module Xlsxrb
|
|
|
1022
1123
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
1023
1124
|
def self.fast_scan_cells_direct(xml, from, to, shared_strings, row_source, _prefix = "", styles = nil, date1904 = false, &)
|
|
1024
1125
|
# rubocop:enable Style/OptionalBooleanParameter
|
|
1025
|
-
chunk = xml.byteslice(from, to - from)
|
|
1126
|
+
chunk = scrub_row(xml.byteslice(from, to - from))
|
|
1127
|
+
verify_cells_closed!(chunk)
|
|
1026
1128
|
row_idx = row_source.is_a?(Hash) ? row_source[:row] : row_source
|
|
1027
1129
|
col_idx = 0
|
|
1028
1130
|
matched_any = false
|
|
@@ -1118,6 +1220,8 @@ module Xlsxrb
|
|
|
1118
1220
|
chunk = xml.byteslice(from, to - from)
|
|
1119
1221
|
return [] if chunk.nil? || chunk.empty?
|
|
1120
1222
|
|
|
1223
|
+
chunk = scrub_row(chunk)
|
|
1224
|
+
verify_cells_closed!(chunk)
|
|
1121
1225
|
col_idx = 0
|
|
1122
1226
|
matched_any = false
|
|
1123
1227
|
row_values = []
|
|
@@ -145,13 +145,13 @@ module Xlsxrb
|
|
|
145
145
|
buf << '<row r="' << row_num_str << '"'
|
|
146
146
|
buf << ' s="' << attrs[:style_index].to_s << '" customFormat="1"' if attrs[:style_index].is_a?(Integer)
|
|
147
147
|
if attrs[:height]
|
|
148
|
-
buf << ' ht="' << attrs[:height]
|
|
148
|
+
buf << ' ht="' << escape_xml(attrs[:height]) << '" customHeight="1"'
|
|
149
149
|
elsif attrs[:custom_height]
|
|
150
150
|
buf << ' customHeight="1"'
|
|
151
151
|
end
|
|
152
152
|
buf << ' hidden="1"' if attrs[:hidden]
|
|
153
153
|
buf << ' collapsed="1"' if attrs[:collapsed]
|
|
154
|
-
buf << ' outlineLevel="' << attrs[:outline_level]
|
|
154
|
+
buf << ' outlineLevel="' << escape_xml(attrs[:outline_level]) << '"' if attrs[:outline_level]
|
|
155
155
|
buf << ">"
|
|
156
156
|
|
|
157
157
|
row_style = resolve_style_id(attrs[:style_index], nil)
|
|
@@ -169,10 +169,14 @@ module Xlsxrb
|
|
|
169
169
|
value = cell[:value]
|
|
170
170
|
cell_style_id = cell[:style_index]
|
|
171
171
|
col_idx = cell[:column_index]
|
|
172
|
-
col_ref = cell[:ref]
|
|
172
|
+
col_ref = if cell[:ref]
|
|
173
|
+
escape_xml(cell[:ref])
|
|
174
|
+
elsif col_idx
|
|
175
|
+
"#{column_letter(col_idx)}#{row_num_str}"
|
|
176
|
+
end
|
|
173
177
|
formula = cell[:formula]
|
|
174
178
|
formula_ca = cell[:formula_ca]
|
|
175
|
-
cell_type_val = cell[:type]
|
|
179
|
+
cell_type_val = cell[:type] && escape_xml(cell[:type])
|
|
176
180
|
else
|
|
177
181
|
value = cell
|
|
178
182
|
cell_style_id = nil
|
|
@@ -337,11 +341,11 @@ module Xlsxrb
|
|
|
337
341
|
case formula_type.to_sym
|
|
338
342
|
when :array
|
|
339
343
|
f_attrs << ' t="array"'
|
|
340
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
344
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
341
345
|
when :shared
|
|
342
346
|
f_attrs << ' t="shared"'
|
|
343
|
-
f_attrs << %( si="#{formula_si}") if formula_si
|
|
344
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
347
|
+
f_attrs << %( si="#{escape_xml(formula_si)}") if formula_si
|
|
348
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
345
349
|
end
|
|
346
350
|
end
|
|
347
351
|
f_attrs << ' ca="1"' if formula_ca
|
|
@@ -420,21 +424,21 @@ module Xlsxrb
|
|
|
420
424
|
buf << '<c r="' << col_ref << row_num_str << '" t="b"><v>0</v></c>'
|
|
421
425
|
when Time, DateTime
|
|
422
426
|
time_style_id = style_map ? style_map["__xlsxrb_time"] : nil
|
|
423
|
-
buf << if time_style_id
|
|
427
|
+
buf << if time_style_id.is_a?(Integer)
|
|
424
428
|
"<c r=\"#{col_ref}#{row_num_str}\" s=\"#{time_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
|
|
425
429
|
else
|
|
426
430
|
"<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(value, date1904: @date1904)}</v></c>"
|
|
427
431
|
end
|
|
428
432
|
when Date
|
|
429
433
|
date_style_id = style_map ? style_map["__xlsxrb_date"] : nil
|
|
430
|
-
buf << if date_style_id
|
|
434
|
+
buf << if date_style_id.is_a?(Integer)
|
|
431
435
|
"<c r=\"#{col_ref}#{row_num_str}\" s=\"#{date_style_id}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>"
|
|
432
436
|
else
|
|
433
437
|
"<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904)}</v></c>"
|
|
434
438
|
end
|
|
435
439
|
when TimeLike # after Date, so Date values skip TimeLike.===
|
|
436
440
|
time_style_id = style_map ? style_map["__xlsxrb_time"] : nil
|
|
437
|
-
buf << if time_style_id
|
|
441
|
+
buf << if time_style_id.is_a?(Integer)
|
|
438
442
|
"<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>"
|
|
439
443
|
else
|
|
440
444
|
"<c r=\"#{col_ref}#{row_num_str}\"><v>#{Xlsxrb::Ooxml::Utils.datetime_to_serial(TimeLike.to_time(value), date1904: @date1904)}</v></c>"
|
|
@@ -449,11 +453,11 @@ module Xlsxrb
|
|
|
449
453
|
case value.type.to_sym
|
|
450
454
|
when :array
|
|
451
455
|
f_attrs << ' t="array"'
|
|
452
|
-
f_attrs << %( ref="#{value.ref}") if value.ref
|
|
456
|
+
f_attrs << %( ref="#{escape_xml(value.ref)}") if value.ref
|
|
453
457
|
when :shared
|
|
454
458
|
f_attrs << ' t="shared"'
|
|
455
|
-
f_attrs << %( si="#{value.shared_index}") if value.shared_index
|
|
456
|
-
f_attrs << %( ref="#{value.ref}") if value.ref
|
|
459
|
+
f_attrs << %( si="#{escape_xml(value.shared_index)}") if value.shared_index
|
|
460
|
+
f_attrs << %( ref="#{escape_xml(value.ref)}") if value.ref
|
|
457
461
|
end
|
|
458
462
|
end
|
|
459
463
|
f_attrs << ' ca="1"' if value.calculate_always?
|
|
@@ -489,12 +493,12 @@ module Xlsxrb
|
|
|
489
493
|
case f_type.to_sym
|
|
490
494
|
when :array
|
|
491
495
|
f_attrs << ' t="array"'
|
|
492
|
-
f_attrs << %( ref="#{value[:ref]}") if value[:ref]
|
|
496
|
+
f_attrs << %( ref="#{escape_xml(value[:ref])}") if value[:ref]
|
|
493
497
|
when :shared
|
|
494
498
|
f_attrs << ' t="shared"'
|
|
495
499
|
si = value[:si] || value[:shared_index]
|
|
496
|
-
f_attrs << %( si="#{si}") if si
|
|
497
|
-
f_attrs << %( ref="#{value[:ref]}") if value[:ref]
|
|
500
|
+
f_attrs << %( si="#{escape_xml(si)}") if si
|
|
501
|
+
f_attrs << %( ref="#{escape_xml(value[:ref])}") if value[:ref]
|
|
498
502
|
end
|
|
499
503
|
end
|
|
500
504
|
f_attrs << ' ca="1"' if value[:calculate_always]
|
|
@@ -566,13 +570,13 @@ module Xlsxrb
|
|
|
566
570
|
buf << ' s="' << row_style_id.to_s << '" customFormat="1"' if row_style_id.is_a?(Integer)
|
|
567
571
|
if attrs
|
|
568
572
|
if attrs[:height]
|
|
569
|
-
buf << ' ht="' << attrs[:height]
|
|
573
|
+
buf << ' ht="' << escape_xml(attrs[:height]) << '" customHeight="1"'
|
|
570
574
|
elsif attrs[:custom_height]
|
|
571
575
|
buf << ' customHeight="1"'
|
|
572
576
|
end
|
|
573
577
|
buf << ' hidden="1"' if attrs[:hidden]
|
|
574
578
|
buf << ' collapsed="1"' if attrs[:collapsed]
|
|
575
|
-
buf << ' outlineLevel="' << attrs[:outline_level]
|
|
579
|
+
buf << ' outlineLevel="' << escape_xml(attrs[:outline_level]) << '"' if attrs[:outline_level]
|
|
576
580
|
end
|
|
577
581
|
buf << ">"
|
|
578
582
|
|
|
@@ -696,12 +700,12 @@ module Xlsxrb
|
|
|
696
700
|
when false
|
|
697
701
|
buf << '<c r="' << col_ref << row_num_str << s_attr << '" t="b"><v>0</v></c>'
|
|
698
702
|
when Time, DateTime
|
|
699
|
-
if style_id.nil? && style_map && (time_style_id = style_map["__xlsxrb_time"])
|
|
703
|
+
if style_id.nil? && style_map && (time_style_id = style_map["__xlsxrb_time"]).is_a?(Integer)
|
|
700
704
|
s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id})
|
|
701
705
|
end
|
|
702
706
|
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
707
|
when Date
|
|
704
|
-
if style_id.nil? && style_map && (date_style_id = style_map["__xlsxrb_date"])
|
|
708
|
+
if style_id.nil? && style_map && (date_style_id = style_map["__xlsxrb_date"]).is_a?(Integer)
|
|
705
709
|
s_attr = style_attrs[date_style_id] ||= -%(" s="#{date_style_id})
|
|
706
710
|
end
|
|
707
711
|
buf << '<c r="' << col_ref << row_num_str << s_attr << '"><v>' << Xlsxrb::Ooxml::Utils.date_to_serial(value, date1904: @date1904).to_s << "</v></c>"
|
|
@@ -711,7 +715,7 @@ module Xlsxrb
|
|
|
711
715
|
elsif @time_like_style_resolver
|
|
712
716
|
time_style_id = @time_like_style_resolver.call(style_id)
|
|
713
717
|
end
|
|
714
|
-
s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id}) if time_style_id
|
|
718
|
+
s_attr = style_attrs[time_style_id] ||= -%(" s="#{time_style_id}) if time_style_id.is_a?(Integer)
|
|
715
719
|
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
720
|
else
|
|
717
721
|
write_complex_cell(buf, col_ref, row_num_str, value, style_id, sst, sst_index)
|
|
@@ -825,11 +829,11 @@ module Xlsxrb
|
|
|
825
829
|
case formula_type.to_sym
|
|
826
830
|
when :array
|
|
827
831
|
f_attrs << ' t="array"'
|
|
828
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
832
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
829
833
|
when :shared
|
|
830
834
|
f_attrs << ' t="shared"'
|
|
831
|
-
f_attrs << %( si="#{formula_si}") if formula_si
|
|
832
|
-
f_attrs << %( ref="#{formula_ref}") if formula_ref
|
|
835
|
+
f_attrs << %( si="#{escape_xml(formula_si)}") if formula_si
|
|
836
|
+
f_attrs << %( ref="#{escape_xml(formula_ref)}") if formula_ref
|
|
833
837
|
end
|
|
834
838
|
end
|
|
835
839
|
f_attrs << ' ca="1"' if formula_ca
|
|
@@ -863,7 +867,8 @@ module Xlsxrb
|
|
|
863
867
|
|
|
864
868
|
if style_map
|
|
865
869
|
mapped = style_map[style] || style_map[style.to_s]
|
|
866
|
-
|
|
870
|
+
# A style id is written into an attribute as it is, so only an Integer is one.
|
|
871
|
+
return mapped if mapped.is_a?(Integer)
|
|
867
872
|
end
|
|
868
873
|
|
|
869
874
|
return 0 if [:default, :none, "default", "none"].include?(style)
|
|
@@ -1767,7 +1772,7 @@ module Xlsxrb
|
|
|
1767
1772
|
buf << "<u/>"
|
|
1768
1773
|
else
|
|
1769
1774
|
mapped_u = StyleBuilder::UNDERLINE_MAP[u_val.to_sym] || u_val
|
|
1770
|
-
buf << '<u val="' << mapped_u
|
|
1775
|
+
buf << '<u val="' << escape_xml(mapped_u) << '"/>'
|
|
1771
1776
|
end
|
|
1772
1777
|
end
|
|
1773
1778
|
va_val = font[:vert_align] || font[:vertical_align]
|
|
@@ -1775,40 +1780,40 @@ module Xlsxrb
|
|
|
1775
1780
|
va_val ||= "subscript" if font[:subscript]
|
|
1776
1781
|
if va_val
|
|
1777
1782
|
mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val
|
|
1778
|
-
buf << '<vertAlign val="' << mapped_va
|
|
1783
|
+
buf << '<vertAlign val="' << escape_xml(mapped_va) << '"/>'
|
|
1779
1784
|
end
|
|
1780
1785
|
font_sz = font[:sz] || font[:size]
|
|
1781
|
-
buf << '<sz val="' << font_sz
|
|
1786
|
+
buf << '<sz val="' << escape_xml(font_sz) << '"/>' if font_sz
|
|
1782
1787
|
if font[:color]
|
|
1783
1788
|
if font[:color].is_a?(Hash)
|
|
1784
1789
|
c_hash = font[:color]
|
|
1785
1790
|
if c_hash[:rgb]
|
|
1786
1791
|
norm = StyleBuilder.normalize_color(c_hash[:rgb], alpha: true) || c_hash[:rgb].to_s
|
|
1787
|
-
buf << '<color rgb="' << norm << '"/>'
|
|
1792
|
+
buf << '<color rgb="' << escape_xml(norm) << '"/>'
|
|
1788
1793
|
elsif c_hash[:theme]
|
|
1789
|
-
tint_attr = c_hash[:tint] ? %( tint="#{c_hash[:tint]}") : ""
|
|
1790
|
-
buf << '<color theme="' << c_hash[:theme]
|
|
1794
|
+
tint_attr = c_hash[:tint] ? %( tint="#{escape_xml(c_hash[:tint])}") : ""
|
|
1795
|
+
buf << '<color theme="' << escape_xml(c_hash[:theme]) << '"' << tint_attr << "/>"
|
|
1791
1796
|
elsif c_hash[:indexed]
|
|
1792
|
-
buf << '<color indexed="' << c_hash[:indexed]
|
|
1797
|
+
buf << '<color indexed="' << escape_xml(c_hash[:indexed]) << '"/>'
|
|
1793
1798
|
elsif c_hash[:auto]
|
|
1794
1799
|
buf << '<color auto="1"/>'
|
|
1795
1800
|
end
|
|
1796
1801
|
else
|
|
1797
1802
|
raw_color = font[:color]
|
|
1798
1803
|
norm_color = StyleBuilder.normalize_color(raw_color, alpha: true) || raw_color.to_s
|
|
1799
|
-
buf << '<color rgb="' << norm_color << '"/>'
|
|
1804
|
+
buf << '<color rgb="' << escape_xml(norm_color) << '"/>'
|
|
1800
1805
|
end
|
|
1801
1806
|
elsif font[:theme]
|
|
1802
|
-
tint_attr = font[:tint] ? %( tint="#{font[:tint]}") : ""
|
|
1803
|
-
buf << '<color theme="' << font[:theme]
|
|
1807
|
+
tint_attr = font[:tint] ? %( tint="#{escape_xml(font[:tint])}") : ""
|
|
1808
|
+
buf << '<color theme="' << escape_xml(font[:theme]) << '"' << tint_attr << "/>"
|
|
1804
1809
|
elsif font[:indexed]
|
|
1805
|
-
buf << '<color indexed="' << font[:indexed]
|
|
1810
|
+
buf << '<color indexed="' << escape_xml(font[:indexed]) << '"/>'
|
|
1806
1811
|
elsif font[:auto]
|
|
1807
1812
|
buf << '<color auto="1"/>'
|
|
1808
1813
|
end
|
|
1809
1814
|
buf << '<rFont val="' << escape_xml(font[:name]) << '"/>' if font[:name]
|
|
1810
|
-
buf << '<family val="' << font[:family]
|
|
1811
|
-
buf << '<scheme val="' << font[:scheme]
|
|
1815
|
+
buf << '<family val="' << escape_xml(font[:family]) << '"/>' if font[:family]
|
|
1816
|
+
buf << '<scheme val="' << escape_xml(font[:scheme]) << '"/>' if font[:scheme]
|
|
1812
1817
|
buf << "</rPr><t>"
|
|
1813
1818
|
else
|
|
1814
1819
|
buf << "<r><t>"
|