Skip to content

Commit

Permalink
Apply comment and method name suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lavham committed Apr 5, 2023
1 parent 355bd93 commit c51490f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/xlsxtream/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ def escape_attr(string)
string.gsub(UNSAFE_ATTR_CHARS, XML_ESCAPES)
end

# Add underscore to strings that merely look like hex values, preventing manipulation into invalid UTF8
# Ensure that we escape the first underscore character in plaintext strings that match the format for Excel escape sequences.
# This ensures that these strings are displayed as plaintext and not incorrectly parsed as escape sequences by Excel
# Per Microsoft Open Specifications for Excel:
# Underscore (0x005f): This character shall be escaped only when used to escape the first underscore character in the format _xHHHH_.
def encode_underscores_using_x005f(string)
def escape_strings_that_match_excel_escape_sequence(string)
string.gsub(HEX_ESCAPE_REGEXP) do |match|
match.sub("_", XML_ESCAPE_UNDERSCORE)
end
end

def escape_value(string)
excel_safe_string = encode_underscores_using_x005f(string)
excel_safe_string = escape_strings_that_match_excel_escape_sequence(string)
excel_safe_string.gsub(UNSAFE_VALUE_CHARS, XML_ESCAPES).gsub(INVALID_XML10_CHARS, &ESCAPE_CHAR)
end

Expand Down

0 comments on commit c51490f

Please sign in to comment.