Added custom middleware to remove spaces between tags and remove html comments. Only if DEBUG = False

This commit is contained in:
Felipe Martín 2013-05-21 13:59:48 +02:00
parent da4c9670dc
commit 2eeab61673
2 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,17 @@
from django.utils.html import strip_spaces_between_tags
from django.conf import settings
import re
class HTMLCleanerMiddleware(object):
def process_response(self, request, response):
if 'text/html' in response['Content-Type']:
content = response.content
if not settings.DEBUG:
# Remove spaces
content = strip_spaces_between_tags(content)
# Remove HTML comments
exp = re.compile('\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>')
content = exp.sub('', content)
response.content = content
return response

View File

@ -101,6 +101,7 @@ MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'herobrine.middleware.HTMLCleanerMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)