Middleware for stripping all html comments from the response content before returning it to the client.
This will also strip inline javascript with htmlcomments put around it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import re
class StripHtmlCommentsMiddleware:
"""
Strips all html comments from response content.
"""
def __init__(self):
self.htmlcomments = re.compile('\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>')
def process_response(self, request, response):
if ("text" in response['Content-Type']):
new_content = self.htmlcomments.sub('', response.content)
response.content = new_content
return response
else:
return response
|
More like this
- Browser-native date input field by kytta 1 month, 1 week ago
- Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
- My firs Snippets by GutemaG 1 month, 2 weeks ago
- FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 3 months ago
Comments
Hi, nice code! Something to note: in case you want some comment to pass through (like the conditionals for IE to load extra css), you can make it adding an extra '-' in the comment tag like this:
Cheers, Pedro
#
Please login first before commenting.