From 7b3b8a6ad71290d3e84f4ba10329f2855c5c3522 Mon Sep 17 00:00:00 2001 From: Jurriaan Pruis Date: Wed, 12 Mar 2014 22:04:50 +0100 Subject: Fix autowidth in combination with sanitizing By only sanitizing just before serialisation. --- lib/axlsx/workbook/worksheet/cell.rb | 15 ++++++++++++--- lib/axlsx/workbook/worksheet/cell_serializer.rb | 6 +++--- lib/axlsx/workbook/worksheet/rich_text_run.rb | 5 +++-- test/workbook/worksheet/tc_worksheet.rb | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 46391229..cd9e1829 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -363,6 +363,17 @@ module Axlsx string_width(value, font_size) end end + + # Returns the sanatized value + # TODO find a better way to do this as it accounts for 30% of + # processing time in benchmarking... + def clean_value + if type == :string && !Axlsx::trust_input + Axlsx::sanitize(::CGI.escapeHTML(@value.to_s)) + else + @value.to_s + end + end private @@ -452,9 +463,7 @@ module Axlsx #consumer is responsible for ensuring the iso_8601 format when specifying this type v else - # TODO find a better way to do this as it accounts for 30% of - # processing time in benchmarking... - Axlsx::trust_input ? v.to_s : Axlsx::sanitize(::CGI.escapeHTML(v.to_s)) + v.to_s end end diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 73b7c110..9a9f9465 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -28,7 +28,7 @@ module Axlsx elsif cell.contains_rich_text? cell.value.to_xml_string(str) else - str << ('' << cell.value.to_s << '') + str << ('' << cell.clean_value << '') end str end @@ -86,7 +86,7 @@ module Axlsx # @param [String] str The string the serialized content will be appended to. # @return [String] def formula_serialization(cell, str='') - str << ('t="str">' << cell.value.to_s.sub('=', '') << '') + str << ('t="str">' << cell.clean_value.to_s.sub('=', '') << '') str << ('' << cell.formula_value.to_s << '') unless cell.formula_value.nil? end @@ -95,7 +95,7 @@ module Axlsx # @param [String] str The string the serialized content will be appended to. # @return [String] def array_formula_serialization(cell, str='') - str << ('t="str">' << '' << cell.value.to_s.sub('{=', '').sub(/}$/, '') << '') + str << ('t="str">' << '' << cell.clean_value.to_s.sub('{=', '').sub(/}$/, '') << '') str << ('' << cell.formula_value.to_s << '') unless cell.formula_value.nil? end diff --git a/lib/axlsx/workbook/worksheet/rich_text_run.rb b/lib/axlsx/workbook/worksheet/rich_text_run.rb index e91514ed..e56dc3ae 100644 --- a/lib/axlsx/workbook/worksheet/rich_text_run.rb +++ b/lib/axlsx/workbook/worksheet/rich_text_run.rb @@ -16,7 +16,7 @@ module Axlsx end def value=(value) - @value = Axlsx::trust_input ? value.to_s : ::CGI.escapeHTML(Axlsx::sanitize(value.to_s)) + @value = value end attr_accessor :cell @@ -208,7 +208,8 @@ module Axlsx str << ('<' << key.to_s << ' val="' << xml_value(data[key]) << '"/>') end end - str << ('' << @value.to_s << '') + clean_value = Axlsx::trust_input ? @value.to_s : ::CGI.escapeHTML(Axlsx::sanitize(@value.to_s)) + str << ('' << clean_value << '') end private diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index b399b306..574522a2 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -402,7 +402,7 @@ class TestWorksheet < Test::Unit::TestCase Axlsx::trust_input = false nasties = "\v\u2028\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u001f" @ws.add_row [nasties] - assert_equal(nil, @ws.rows.last.cells.last.value.index("\v")) + assert_equal(0, @ws.rows.last.cells.last.value.index("\v")) assert_equal(nil, @ws.to_xml_string.index("\v")) Axlsx::trust_input = old end -- cgit v1.2.3