(self)
| 802 | self.assertSoupEquals(old_text, new_text) |
| 803 | |
| 804 | def testRewrittenMetaTag(self): |
| 805 | no_shift_jis_html = '''<html><head>\n<meta http-equiv="Content-language" content="ja" /></head><body><pre>\n\x82\xb1\x82\xea\x82\xcdShift-JIS\x82\xc5\x83R\x81[\x83f\x83B\x83\x93\x83O\x82\xb3\x82\xea\x82\xbd\x93\xfa\x96{\x8c\xea\x82\xcc\x83t\x83@\x83C\x83\x8b\x82\xc5\x82\xb7\x81B\n</pre></body></html>''' |
| 806 | soup = BeautifulSoup(no_shift_jis_html) |
| 807 | |
| 808 | # Beautiful Soup used to try to rewrite the meta tag even if the |
| 809 | # meta tag got filtered out by the strainer. This test makes |
| 810 | # sure that doesn't happen. |
| 811 | strainer = SoupStrainer('pre') |
| 812 | soup = BeautifulSoup(no_shift_jis_html, parseOnlyThese=strainer) |
| 813 | self.assertEquals(soup.contents[0].name, 'pre') |
| 814 | |
| 815 | meta_tag = ('<meta content="text/html; charset=x-sjis" ' |
| 816 | 'http-equiv="Content-type" />') |
| 817 | shift_jis_html = ( |
| 818 | '<html><head>\n%s\n' |
| 819 | '<meta http-equiv="Content-language" content="ja" />' |
| 820 | '</head><body><pre>\n' |
| 821 | '\x82\xb1\x82\xea\x82\xcdShift-JIS\x82\xc5\x83R\x81[\x83f' |
| 822 | '\x83B\x83\x93\x83O\x82\xb3\x82\xea\x82\xbd\x93\xfa\x96{\x8c' |
| 823 | '\xea\x82\xcc\x83t\x83@\x83C\x83\x8b\x82\xc5\x82\xb7\x81B\n' |
| 824 | '</pre></body></html>') % meta_tag |
| 825 | soup = BeautifulSoup(shift_jis_html) |
| 826 | if soup.originalEncoding != "shift-jis": |
| 827 | raise Exception("Test failed when parsing shift-jis document " |
| 828 | "with meta tag '%s'." |
| 829 | "If you're running Python >=2.4, or you have " |
| 830 | "cjkcodecs installed, this is a real problem. " |
| 831 | "Otherwise, ignore it." % meta_tag) |
| 832 | self.assertEquals(soup.originalEncoding, "shift-jis") |
| 833 | |
| 834 | content_type_tag = soup.meta['content'] |
| 835 | self.assertEquals(content_type_tag[content_type_tag.find('charset='):], |
| 836 | 'charset=%SOUP-ENCODING%') |
| 837 | content_type = str(soup.meta) |
| 838 | index = content_type.find('charset=') |
| 839 | self.assertEqual(content_type[index:index+len('charset=utf8')+1], |
| 840 | 'charset=utf-8') |
| 841 | content_type = soup.meta.__str__('shift-jis') |
| 842 | index = content_type.find('charset=') |
| 843 | self.assertEqual(content_type[index:index+len('charset=shift-jis')], |
| 844 | 'charset=shift-jis') |
| 845 | |
| 846 | self.assertEquals(str(soup), ( |
| 847 | '<html><head>\n' |
| 848 | '<meta content="text/html; charset=utf-8" ' |
| 849 | 'http-equiv="Content-type" />\n' |
| 850 | '<meta http-equiv="Content-language" content="ja" />' |
| 851 | '</head><body><pre>\n' |
| 852 | '\xe3\x81\x93\xe3\x82\x8c\xe3\x81\xafShift-JIS\xe3\x81\xa7\xe3' |
| 853 | '\x82\xb3\xe3\x83\xbc\xe3\x83\x87\xe3\x82\xa3\xe3\x83\xb3\xe3' |
| 854 | '\x82\xb0\xe3\x81\x95\xe3\x82\x8c\xe3\x81\x9f\xe6\x97\xa5\xe6' |
| 855 | '\x9c\xac\xe8\xaa\x9e\xe3\x81\xae\xe3\x83\x95\xe3\x82\xa1\xe3' |
| 856 | '\x82\xa4\xe3\x83\xab\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\n' |
| 857 | '</pre></body></html>')) |
| 858 | self.assertEquals(soup.renderContents("shift-jis"), |
| 859 | shift_jis_html.replace('x-sjis', 'shift-jis')) |
| 860 | |
| 861 | isolatin ="""<html><meta http-equiv="Content-type" content="text/html; charset=ISO-Latin-1" />Sacr\xe9 bleu!</html>""" |
nothing calls this directly
no test coverage detected