From bb138f7e909e212d8cab4fd7d92585b9d2dfb916 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Tue, 1 Aug 2017 01:24:36 +0200 Subject: [PATCH] Another fix of URI.parse not preserving empty fragments --- CHANGELOG.md | 2 +- lib/rdf/serialization/parse_helper.ex | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5541a7..09f2c00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and ### 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 - N-Triples and N-Quads decoder didn't handle escaping properly diff --git a/lib/rdf/serialization/parse_helper.ex b/lib/rdf/serialization/parse_helper.ex index 574628a..22db1e5 100644 --- a/lib/rdf/serialization/parse_helper.ex +++ b/lib/rdf/serialization/parse_helper.ex @@ -11,15 +11,27 @@ defmodule RDF.Serialization.ParseHelper do def to_uri({:iriref, line, value}) do case URI.parse(iri_unescape(value)) do - %URI{scheme: nil} -> {:error, line, "#{value} is not a valid URI"} - parsed_uri -> {:ok, parsed_uri} + %URI{scheme: nil} -> + {: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 def to_absolute_or_relative_uri({:iriref, line, value}) do case URI.parse(iri_unescape(value)) do - uri = %URI{scheme: scheme} when not is_nil(scheme) -> uri - _ -> {:relative_uri, value} + uri = %URI{scheme: scheme} when not is_nil(scheme) -> + if String.ends_with?(value, "#") do + %URI{uri | fragment: ""} + else + uri + end + _ -> + {:relative_uri, value} end end