Skip to content
Snippets Groups Projects
Commit ca9cf431 authored by Jérémy Bobbio (Lunar)'s avatar Jérémy Bobbio (Lunar)
Browse files

Remove HtmlPrettifyMiddleware (used in development mode)

HtmlPrettifyMiddleware uses the `prettify()` method from BeautifulSoup.
Its documentation says:

> Since it adds whitespace (in the form of newlines), `prettify()` changes
> the meaning of an HTML document and should not be used to reformat one.
> The goal of `prettify()` is to help you visually understand the structure
> of the documents you work with.

Having HtmlPrettifyMiddleware enabled therefore leads to surprising
behavior, like extra spaces surrounding “something” when writing
`<code>something</code>` in a template.

Now that Django templates are prettified using `djlint`, this middleware
only has downsides. Let’s remove it entirely.
parent 697a8a58
No related branches found
No related tags found
No related merge requests found
......@@ -10,9 +10,6 @@ Django development settings for swh-web.
from django.core.cache import cache
from .common import * # noqa
from .common import MIDDLEWARE
MIDDLEWARE += ["swh.web.utils.middlewares.HtmlPrettifyMiddleware"]
AUTH_PASSWORD_VALIDATORS = [] # disable any pwd validation mechanism
......
......@@ -413,6 +413,11 @@ def prettify_html(html: str) -> str:
"""
Prettify an HTML document.
Since it adds whitespace (in the form of newlines), this method changes
the meaning of the HTML document and should not be used for reformatting
purpose. The goal is to help visually understanding the structure of the
document.
Args:
html: Input HTML document
......
......@@ -6,32 +6,9 @@
from htmlmin import minify
from swh.web.utils import prettify_html
from swh.web.utils.exc import handle_view_exception, sentry_capture_exception
class HtmlPrettifyMiddleware(object):
"""
Django middleware for prettifying generated HTML in
development mode.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
if "text/html" in response.get("Content-Type", ""):
if hasattr(response, "content"):
content = response.content
response.content = prettify_html(content)
elif hasattr(response, "streaming_content"):
content = b"".join(response.streaming_content)
response.streaming_content = prettify_html(content)
return response
class HtmlMinifyMiddleware(object):
"""
Django middleware for minifying generated HTML in
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment