Skip to content

Commit

Permalink
Yet another stab at anchor lookup from non-canonical URIs.
Browse files Browse the repository at this point in the history
This fixes looking up anchors in a subresource which changes the base
URI (e.g. the location-independent identifier with base URI change tests
in the official suite).

Refs: f520b40
  • Loading branch information
Julian committed Apr 13, 2023
1 parent 90e977c commit b39ebf9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v0.27.2
-------

* Another fix for looking up anchors from non-canonical URIs, now when they're inside a subresource which has a relative ``$id``.

v0.27.1
-------

Expand Down
19 changes: 11 additions & 8 deletions referencing/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,23 @@ def anchor(self, uri: URI, name: str):
"""
Retrieve a given anchor from a resource which must already be crawled.
"""
resource = self.get(uri)
if resource is None:
canonical_uri = uri
else:
canonical_uri = resource.id() or uri

value = self._anchors.get((canonical_uri, name))
value = self._anchors.get((uri, name))
if value is not None:
return Retrieved(value=value, registry=self)

registry = self.crawl()
value = registry._anchors.get((canonical_uri, name))
value = registry._anchors.get((uri, name))
if value is not None:
return Retrieved(value=value, registry=registry)

resource = self.get(uri)
if resource is not None:
canonical_uri = resource.id()
if canonical_uri is not None:
value = registry._anchors.get((canonical_uri, name))
if value is not None:
return Retrieved(value=value, registry=registry)

if "/" in name:
raise exceptions.InvalidAnchor(
ref=uri,
Expand Down

0 comments on commit b39ebf9

Please sign in to comment.