Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow image src without scheme and with external domain #144

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions wagtail_wordpress_import/block_builder_defaults.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.files import File
Expand Down Expand Up @@ -327,8 +328,18 @@ def document_exists(name):

def get_absolute_src(src, domain_prefix=None):
src = src.lstrip("/")
if not src.startswith("http") and domain_prefix:
return domain_prefix + "/" + src
url = urlparse(src)
known_file_extensions = ["jpg", "jpeg", "gif", "png", "webp"]

if not url.scheme and domain_prefix:
first_url_part = url.path.split("/")[0]
if (
"." in first_url_part
and first_url_part.split(".")[1].lower() not in known_file_extensions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: In the following case, the unrecognised local file extension is interpreted as a domain:

>>> get_absolute_src("image.bmp", "https://www.example.com")
'https://image.bmp'

urlparse results have a .hostname attribute that you can use in this situation. You will have to remove the .lstrip("/") line though.

>>> from urllib.parse import urlparse
>>> urlparse("image.bmp").hostname
>>> urlparse("example.com/image.bmp").hostname
>>> urlparse("//example.com/image.bmp").hostname
'example.com'

):
return "{}://{}".format(urlparse(domain_prefix).scheme, url.path)
return "{}/{}".format(domain_prefix, src)

return src


Expand Down
10 changes: 10 additions & 0 deletions wagtail_wordpress_import/test/tests/test_block_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ def test_get_abolute_src_slashes_at_start(self):
"http://www.example.com/folder/fakeimage.jpg",
)

def test_get_abolute_src_slashes_at_start_external_domain(self):
self.assertEqual(
get_absolute_src("//example.com/fakeimage.jpg", "https://www.example.com"),
"https://example.com/fakeimage.jpg",
)
self.assertEqual(
get_absolute_src("//example.com/fakeimage.jpg", "http://www.example.com"),
"http://example.com/fakeimage.jpg",
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a test case for an unrecognised extension on the local domain: get_absolute_src("/fakeimage.badextension", "http://www.example.com")

def test_get_alignment_class_align_left(self):
soup = get_soup(
'<img src="fakeimage.jpg" alt="image alt" class="align-left" />',
Expand Down