Login

HTML 5 Firefox 2 Hack

Author:
epicserve
Posted:
November 16, 2009
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

This is a hack to get HTML 5 to work for Firefox 2. It Sends the xhtml content-type to all Gecko based browsers where version is less than 1.9, or "rv:1.9pre" or "rv:1.9a" or "rv:1.9bx" where x is less than 5. I created this hack based on the information I found on this site, http://html5doctor.com/how-to-get-html5-working-in-ie-and-firefox-2/.

Just put this code in one of your middleware files (i.e. mysite/middleware.py) and then add it in your MIDDLEWARE_CLASSES in your settings.py.

Example:

MIDDLEWARE_CLASSES = (
    ...
    'mysite.middleware.HTML5Firefox2Hack',
    ...
)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class HTML5Firefox2Hack(object):
	"""This is a hack to get HTML 5 to work for Firefox 2. It Sends the xhtml
	content-type to all Gecko based browsers where version is less than 1.9,
	or "rv:1.9pre" or "rv:1.9a" or "rv:1.9bx" where x is less than 5. I
	created this hack based on the information I found on this site,
	http://html5doctor.com/how-to-get-html5-working-in-ie-and-firefox-2/."""
	
	def process_response(self, request, response):
		
		status_code = getattr(response, "status_code", None)
		meta        = getattr(request, "META", None)
		user_agent  = meta.get('HTTP_USER_AGENT', '')
		
		
		import re
		m = re.search('rv:1\.(([1-8]|9pre|9a|9b[0-4])\.[0-9.]+).*Gecko', user_agent)
		if m:
			
			import htmlentitydefs
			
			response['Content-Type']='application/xhtml+xml;  charset=utf-8'
			for i in htmlentitydefs.name2codepoint.items():
				response.content = response.content.replace('&%s;' % i[0], '&#%s;' % i[1])
		
		return response

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 4 weeks ago

Comments

Please login first before commenting.