Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML Validation #86

Merged
merged 2 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/elixlsx/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Elixlsx.Compiler do

def compinfo_cell_pass_value wci, value do
cond do
is_binary(value) && String.valid?(value)
is_binary(value) && XML.valid?(value)
-> update_in wci.stringdb, &StringDB.register_string(&1, value)
true -> wci
end
Expand Down
10 changes: 8 additions & 2 deletions lib/elixlsx/compiler/string_db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ defmodule Elixlsx.Compiler.StringDB do
def get_id(stringdb, s) do
case Map.fetch(stringdb.strings, s) do
:error ->
raise %ArgumentError{
message: "Invalid key provided for StringDB.get_id: " <> inspect(s)}
if XML.valid?(s) do
raise %ArgumentError{
message: "Invalid key provided for StringDB.get_id: " <> inspect(s)}
else
# if the xml is invalid, then we never wanted it in the stringdb to
# begin with
-1
end
{:ok, id} ->
id
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elixlsx/style/num_fmt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Elixlsx.Style.NumFmt do
props[:yyyymmdd] -> date_yyyy_mm_dd()
props[:datetime] -> date_datetime()
props[:num_format] ->
if String.valid? props[:num_format] do
if XML.valid? props[:num_format] do
%NumFmt{format: props[:num_format]}
else
raise %ArgumentError{
Expand Down
8 changes: 4 additions & 4 deletions lib/elixlsx/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Elixlsx.Util do
@spec encode_col(non_neg_integer) :: String.t
def encode_col(0), do: ""
def encode_col(num) when num <= 26, do: <<num + 64>>

def encode_col(num, suffix \\ "")
def encode_col(num, suffix) when num <= 26, do: <<num + 64>> <> suffix
def encode_col(num, suffix) do
Expand Down Expand Up @@ -47,9 +47,9 @@ defmodule Elixlsx.Util do
def decode_col(""), do: 0
def decode_col(s) when is_binary(s) do
case String.match? s, ~r/^[A-Z]*$/ do
false ->
false ->
raise %ArgumentError{message: "Invalid column string: " <> inspect s}

true ->
# translate list of strings to the base-26 value they represent
Enum.map(String.to_charlist(s), (fn x -> :string.chr(@col_alphabet, x) end)) |>
Expand Down Expand Up @@ -163,7 +163,7 @@ defmodule Elixlsx.Util do
iso_from_datetime(:calendar.now_to_universal_time({div(input, 1000000), rem(input, 1000000), 0}))
# TODO this case should parse the string i guess
# TODO also prominently absent: [char].
String.valid? input ->
XML.valid? input ->
input
true -> raise "Invalid input to iso_timestamp." <> (inspect input)
end
Expand Down
19 changes: 19 additions & 0 deletions lib/elixlsx/xml.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule XML do
@xml_chars_block_1 [9, 10, 13]
@xml_chars_block_2 32..55_295
@xml_chars_block_3 57_344..65_533
@xml_chars_block_4 65_536..1_114_111

# From the xml spec 1.0: https://www.w3.org/TR/REC-xml/#charsets
# Character Range
# any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
# Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
def valid?(<<h::utf8, t::binary>>)
when h in @xml_chars_block_1 or h in @xml_chars_block_2
or h in @xml_chars_block_3 or h in @xml_chars_block_4 do
valid?(t)
end

def valid?(<<>>), do: true
def valid?(_), do: false
end
117 changes: 70 additions & 47 deletions lib/elixlsx/xml_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ defmodule Elixlsx.XMLTemplates do
x when is_number(x) ->
{"n", to_string(x)}
x when is_binary(x) ->
{"s", to_string(StringDB.get_id wci.stringdb, x)}
id = StringDB.get_id(wci.stringdb, x)

if id == -1 do
{:empty, :empty}
else
{"s", to_string(id)}
end
x when is_boolean(x) ->
{"b", if x do "1" else "0" end}
:empty ->
Expand All @@ -178,59 +184,76 @@ defmodule Elixlsx.XMLTemplates do

# TODO i know now about string interpolation, i should probably clean this up. ;)
defp xl_sheet_cols(row, rowidx, wci) do
Enum.zip(row, 1 .. length row) |>
Enum.map(
fn {cell, colidx} ->
{updated_row, _id} =
row
|> List.foldl({"", 1}, fn cell, {acc, colidx} ->
{content, styleID, cellstyle} = split_into_content_style(cell, wci)

if is_nil(content) do
""
{acc, colidx + 1}
else
content = if CellStyle.is_date? cellstyle do
U.to_excel_datetime content
else
content
end
content =
if CellStyle.is_date?(cellstyle) do
U.to_excel_datetime(content)
else
content
end

cv = get_content_type_value(content, wci)
{content_type, content_value, content_opts} =
case cv do
{t, v} -> {t, v, []}
{t, v, opts} -> {t, v, opts}
:error -> raise %ArgumentError{
message: "Invalid column content at " <>
U.to_excel_coords(rowidx, colidx) <> ": "
<> (inspect content)
}
end

case content_type do
:formula ->
value = if not is_nil(content_opts[:value]), do: "<v>#{content_opts[:value]}</v>", else: ""
{content_type, content_value, content_opts} =
case cv do
{t, v} ->
{t, v, []}

{t, v, opts} ->
{t, v, opts}

:error ->
raise %ArgumentError{
message:
"Invalid column content at " <>
U.to_excel_coords(rowidx, colidx) <> ": " <> inspect(content)
}
end

cell_xml =
case content_type do
:formula ->
value =
if not is_nil(content_opts[:value]),
do: "<v>#{content_opts[:value]}</v>",
else: ""

"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
<f>#{content_value}</f>
#{value}
</c>
"""

:empty ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
</c>
"""

type ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}" t="#{type}">
<v>#{content_value}</v>
</c>
"""
end

{acc <> cell_xml, colidx + 1}
end
end)

"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
<f>#{content_value}</f>
#{value}
</c>
"""
:empty ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
</c>
"""
type ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}" t="#{type}">
<v>#{content_value}</v>
</c>
"""
end
end
end) |>
List.foldr("", &<>/2)
updated_row
end

defp xl_merge_cells([]) do
Expand Down
11 changes: 11 additions & 0 deletions test/xml_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule XMLTest do
use ExUnit.Case

test "valid? allows normal string" do
assert XML.valid?("Hello World & Goodbye Cruel World")
end

test "valid? rejects invalid xml characters" do
refute XML.valid?(<<31>>)
end
end