1 from __future__ import absolute_import, division, unicode_literals
6 class Filter(_base.Filter):
7 def __init__(self, source, encoding):
8 _base.Filter.__init__(self, source)
9 self.encoding = encoding
13 meta_found = (self.encoding is None)
16 for token in _base.Filter.__iter__(self):
18 if type == "StartTag":
19 if token["name"].lower() == "head":
22 elif type == "EmptyTag":
23 if token["name"].lower() == "meta":
24 # replace charset with actual encoding
25 has_http_equiv_content_type = False
26 for (namespace, name), value in token["data"].items():
27 if namespace is not None:
29 elif name.lower() == 'charset':
30 token["data"][(namespace, name)] = self.encoding
33 elif name == 'http-equiv' and value.lower() == 'content-type':
34 has_http_equiv_content_type = True
36 if has_http_equiv_content_type and (None, "content") in token["data"]:
37 token["data"][(None, "content")] = 'text/html; charset=%s' % self.encoding
40 elif token["name"].lower() == "head" and not meta_found:
41 # insert meta into empty head
42 yield {"type": "StartTag", "name": "head",
43 "data": token["data"]}
44 yield {"type": "EmptyTag", "name": "meta",
45 "data": {(None, "charset"): self.encoding}}
46 yield {"type": "EndTag", "name": "head"}
50 elif type == "EndTag":
51 if token["name"].lower() == "head" and pending:
52 # insert meta into head (if necessary) and flush pending queue
55 yield {"type": "EmptyTag", "name": "meta",
56 "data": {(None, "charset"): self.encoding}}
62 if state == "in_head":