From 9930d00f7a691a8d718d0608199bbf3828192958 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Dec 2020 21:50:53 +0100 Subject: [PATCH] Test markdown image lazy loading extensions --- articles/markdown.py | 6 ++++-- articles/tests/conftest.py | 8 +++++++- articles/tests/test_html_views.py | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/articles/markdown.py b/articles/markdown.py index ced57d6..acefada 100644 --- a/articles/markdown.py +++ b/articles/markdown.py @@ -11,14 +11,16 @@ from markdown.inlinepatterns import ( class LazyImageInlineProcessor(ImageInlineProcessor): def handleMatch(self, m, data): el, match_start, index = super().handleMatch(m, data) - el.set("loading", "lazy") + if el is not None: + el.set("loading", "lazy") return el, match_start, index class LazyImageReferenceInlineProcessor(ImageReferenceInlineProcessor): def makeTag(self, href, title, text): el = super().makeTag(href, title, text) - el.set("loading", "lazy") + if el is not None: + el.set("loading", "lazy") return el diff --git a/articles/tests/conftest.py b/articles/tests/conftest.py index e95b008..9d9c2a4 100644 --- a/articles/tests/conftest.py +++ b/articles/tests/conftest.py @@ -19,7 +19,13 @@ def published_article(author: User) -> Article: author=author, published_at=timezone.now(), slug="some-article-slug", - content="## some article markdown\n\n[an article link](https://article.com)", + content=( + "## some article markdown\n\n" + "[an article link](https://article.com)\n" + "![an image](https://article.com)\n" + "![a referenced image][1]\n\n" + "[1]: https://example.com/image.png" + ), ) diff --git a/articles/tests/test_html_views.py b/articles/tests/test_html_views.py index 91feeb7..957c10f 100644 --- a/articles/tests/test_html_views.py +++ b/articles/tests/test_html_views.py @@ -117,3 +117,11 @@ def test_logged_in_user_doesnt_have_plausible(client: Client, author: User, sett res = client.get(reverse("articles-list")) content = res.content.decode("utf-8") assert "https://plausible.augendre.info/js/plausible.js" not in content + + +@pytest.mark.django_db +def test_image_is_lazy(client: Client, published_article: Article): + res = client.get(reverse("article-detail", kwargs={"slug": published_article.slug})) + assert res.status_code == 200 + content = res.content.decode("utf-8") + assert content.count('loading="lazy"') == 2