Skip to content

Commit 3d84138

Browse files
committed
Handle relationships with targets given as absolute path
1 parent 7b8c2f6 commit 3d84138

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/xlsx_reader/parsers/relationships_parser.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ defmodule XlsxReader.Parsers.RelationshipsParser do
4242
def handle_event(:start_element, {"Relationship", attributes}, state) do
4343
with %{id: id, target: target, type: type} <- extract_relationship_attributes(attributes),
4444
{:ok, key} <- Map.fetch(@types, type) do
45-
{:ok, Map.update!(state, key, fn rels -> Map.put_new(rels, id, target) end)}
45+
{:ok,
46+
Map.update!(state, key, fn rels -> Map.put_new(rels, id, sanitize_target(target)) end)}
4647
else
4748
_ ->
4849
{:ok, state}
@@ -75,4 +76,8 @@ defmodule XlsxReader.Parsers.RelationshipsParser do
7576
defp extract_relationship_attributes(attributes) do
7677
Utils.map_attributes(attributes, @relationship_attributes_mapping)
7778
end
79+
80+
# Remove leading "/" to deal with file containing absolute paths
81+
defp sanitize_target("/" <> target), do: target
82+
defp sanitize_target(target), do: target
7883
end

test/fixtures/package/xl/_rels/workbook.xml.rels

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<Relationship Id="rId3" Target="theme/theme1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"/>
66
<Relationship Id="rId4" Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>
77
<Relationship Id="rId5" Target="worksheets/sheet2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>
8-
<Relationship Id="rId6" Target="worksheets/sheet3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>
8+
<!-- Leading / added to test sanitization of absolute path -->
9+
<Relationship Id="rId6" Target="/worksheets/sheet3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>
910
</Relationships>

0 commit comments

Comments
 (0)