Skip to content

Commit e4008f2

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

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

lib/xlsx_reader/parsers/relationships_parser.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ 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, Map.update!(state, key, fn rels -> Map.put_new(rels, id, sanitize_target(target)) end)}
4646
else
4747
_ ->
4848
{:ok, state}
@@ -75,4 +75,8 @@ defmodule XlsxReader.Parsers.RelationshipsParser do
7575
defp extract_relationship_attributes(attributes) do
7676
Utils.map_attributes(attributes, @relationship_attributes_mapping)
7777
end
78+
79+
# Remove leading "/" to deal with file containing absolute paths
80+
defp sanitize_target("/" <> target), do: target
81+
defp sanitize_target(target), do: target
7882
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)