Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Resolve implied photo relative paths #205

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions mf2py/implied_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,20 @@ def get_photo_child(children):
if not mf2_classes.root(poss_obj.get("class", [])):
return poss_obj

# if element is an img use source if exists
prop_value = get_img_src_alt(el, base_url)
if prop_value is not None:
def resolve_relative_url(prop_value):
if isinstance(prop_value, dict):
prop_value["value"] = try_urljoin(base_url, prop_value["value"])
else:
prop_value = try_urljoin(base_url, prop_value)
return prop_value

# if element is an img use source if exists
if prop_value := get_img_src_alt(el, base_url):
return resolve_relative_url(prop_value)

# if element is an object use data if exists
prop_value = get_attr(el, "data", check_name="object")
if prop_value is not None:
return prop_value
if prop_value := get_attr(el, "data", check_name="object"):
return resolve_relative_url(prop_value)

# find candidate child or grandchild
poss_child = None
Expand All @@ -131,14 +136,12 @@ def get_photo_child(children):
# if a possible child was found parse
if poss_child is not None:
# img get src
prop_value = get_img_src_alt(poss_child, base_url)
if prop_value is not None:
return prop_value
if prop_value := get_img_src_alt(poss_child, base_url):
return resolve_relative_url(prop_value)

# object get data
prop_value = get_attr(poss_child, "data", check_name="object")
if prop_value is not None:
return prop_value
if prop_value := get_attr(poss_child, "data", check_name="object"):
return resolve_relative_url(prop_value)


def url(el, base_url=""):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<base href="http://example.com">
<img class="h-card" alt="Jane Doe" src="jane-img.jpeg">
<object class="h-card" data="jane-object.jpeg">Jane Doe</object>
</html>
11 changes: 11 additions & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ def test_implied_photo():
for i in range(12, 23):
assert "photo" not in result["items"][i]["properties"]

result = parse_fixture("implied_properties/implied_photo_relative_url.html")

assert (
result["items"][0]["properties"]["photo"][0]["value"]
== "http://example.com/jane-img.jpeg"
)
assert (
result["items"][1]["properties"]["photo"][0]
== "http://example.com/jane-object.jpeg"
)


def test_implied_url():
result = parse_fixture("implied_properties/implied_url.html")
Expand Down