Another fix of URI.parse not preserving empty fragments

This commit is contained in:
Marcel Otto 2017-08-01 01:24:36 +02:00
parent 2f6fa6fe34
commit bb138f7e90
2 changed files with 17 additions and 5 deletions

View file

@ -24,7 +24,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Fixed ### Fixed
- `RDF.uri/1` preserves empty fragments - `RDF.uri/1` and URI parsing of N-Triples and N-Quads decoders preserve empty fragments
- booleans weren't recognized as convertible literals on object positions - booleans weren't recognized as convertible literals on object positions
- N-Triples and N-Quads decoder didn't handle escaping properly - N-Triples and N-Quads decoder didn't handle escaping properly

View file

@ -11,15 +11,27 @@ defmodule RDF.Serialization.ParseHelper do
def to_uri({:iriref, line, value}) do def to_uri({:iriref, line, value}) do
case URI.parse(iri_unescape(value)) do case URI.parse(iri_unescape(value)) do
%URI{scheme: nil} -> {:error, line, "#{value} is not a valid URI"} %URI{scheme: nil} ->
parsed_uri -> {:ok, parsed_uri} {:error, line, "#{value} is not a valid URI"}
parsed_uri ->
if String.ends_with?(value, "#") do
{:ok, %URI{parsed_uri | fragment: ""}}
else
{:ok, parsed_uri}
end
end end
end end
def to_absolute_or_relative_uri({:iriref, line, value}) do def to_absolute_or_relative_uri({:iriref, line, value}) do
case URI.parse(iri_unescape(value)) do case URI.parse(iri_unescape(value)) do
uri = %URI{scheme: scheme} when not is_nil(scheme) -> uri uri = %URI{scheme: scheme} when not is_nil(scheme) ->
_ -> {:relative_uri, value} if String.ends_with?(value, "#") do
%URI{uri | fragment: ""}
else
uri
end
_ ->
{:relative_uri, value}
end end
end end