Login

HTML 5 Firefox 2 Hack

Author:
epicserve
Posted:
November 16, 2009
Language:
Python
Version:
1.1
Tags:
middleware html5
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. Super User Conditional Page Exception Reporting by zbyte64 7 years, 1 month ago
  2. Cookieless Session Middleware by juliocarlos 6 years, 8 months ago
  3. Search DjangoSnippets with Firefox by Natim 5 years, 11 months ago
  4. Closure for FieldListFilter classes with custom sets of ranges by ssokolow 3 years, 2 months ago
  5. XhtmlDegraderMiddleware by dmh 8 years, 1 month ago

Comments

Please login first before commenting.