Skip to content

Commit

Permalink
Fix looking up anchors from non-canonical URIs.
Browse files Browse the repository at this point in the history
E.g. if we have http://example.com, a retrieval URI,
we need to be able to lookup anchors within it even if the
resource has some internal ID.
  • Loading branch information
Julian committed Apr 3, 2023
1 parent c9de6c2 commit f520b40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

v0.27.0
-------

* Support looking up anchors from non-canonical URIs.
In other words, if you add a resource at the URI ``http://example.com``, then looking up the anchor ``http://example.com#foo`` now works even if the resource has some internal ``$id`` saying its canonical URI is ``http://somethingelse.example.com``.

v0.26.4
-------

Expand Down
12 changes: 9 additions & 3 deletions referencing/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,20 @@ def remove(self, uri: URI):

def anchor(self, uri: URI, name: str):
"""
Retrieve the given anchor, which must already have been found.
Retrieve a given anchor from a resource which must already be crawled.
"""
value = self._anchors.get((uri, name))
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))
if value is not None:
return Retrieved(value=value, registry=self)

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

0 comments on commit f520b40

Please sign in to comment.