示例#1
0
 def test_serializeComment(self):
     """
     Test that comments are correctly flattened and escaped.
     """
     return gatherResults([
         self.assertFlattensTo(Comment('foo bar'), '<!--foo bar-->'),
         self.assertFlattensTo(Comment('foo -- bar'), '<!--foo - - bar-->'),
     ])
示例#2
0
 def test_commentRepr(self):
     """
     L{Comment.__repr__} returns a value which makes it easy to see what's in
     the comment.
     """
     self.assertEquals(repr(Comment(u"hello there")),
                       "Comment(u'hello there')")
示例#3
0
 def test_serializeUnicode(self):
     """
     Test that unicode is encoded correctly in the appropriate places, and
     raises an error when it occurs in inappropriate place.
     """
     snowman = "\N{SNOWMAN}"
     self.assertFlattensImmediately(snowman, b"\xe2\x98\x83")
     self.assertFlattensImmediately(tags.p(snowman), b"<p>\xe2\x98\x83</p>")
     self.assertFlattensImmediately(Comment(snowman), b"<!--\xe2\x98\x83-->")
     self.assertFlattensImmediately(CDATA(snowman), b"<![CDATA[\xe2\x98\x83]]>")
     self.assertFlatteningRaises(Tag(snowman), UnicodeEncodeError)
     self.assertFlatteningRaises(
         Tag("p", attributes={snowman: ""}), UnicodeEncodeError
     )
示例#4
0
 def test_serializeUnicode(self):
     """
     Test that unicode is encoded correctly in the appropriate places, and
     raises an error when it occurs in inappropriate place.
     """
     snowman = u'\N{SNOWMAN}'
     return gatherResults([
         self.assertFlattensTo(snowman, '\xe2\x98\x83'),
         self.assertFlattensTo(tags.p(snowman), '<p>\xe2\x98\x83</p>'),
         self.assertFlattensTo(Comment(snowman), '<!--\xe2\x98\x83-->'),
         self.assertFlattensTo(CDATA(snowman), '<![CDATA[\xe2\x98\x83]]>'),
         self.assertFlatteningRaises(Tag(snowman), UnicodeEncodeError),
         self.assertFlatteningRaises(Tag('p', attributes={snowman: ''}),
                                     UnicodeEncodeError),
     ])
示例#5
0
    def test_commentEscaping(self):
        """
        The data in a L{Comment} is escaped and mangled in the flattened output
        so that the result is a legal SGML and XML comment.

        SGML comment syntax is complicated and hard to use. This rule is more
        restrictive, and more compatible:

        Comments start with <!-- and end with --> and never contain -- or >.

        Also by XML syntax, a comment may not end with '-'.

        @see: U{http://www.w3.org/TR/REC-xml/#sec-comments}
        """
        def verifyComment(c):
            self.assertTrue(
                c.startswith(b"<!--"),
                "{!r} does not start with the comment prefix".format(c),
            )
            self.assertTrue(
                c.endswith(b"-->"),
                "{!r} does not end with the comment suffix".format(c),
            )
            # If it is shorter than 7, then the prefix and suffix overlap
            # illegally.
            self.assertTrue(
                len(c) >= 7,
                "{!r} is too short to be a legal comment".format(c))
            content = c[4:-3]
            self.assertNotIn(b"--", content)
            self.assertNotIn(b">", content)
            if content:
                self.assertNotEqual(content[-1], b"-")

        results = []
        for c in [
                "",
                "foo---bar",
                "foo---bar-",
                "foo>bar",
                "foo-->bar",
                "----------------",
        ]:
            d = flattenString(None, Comment(c))
            d.addCallback(verifyComment)
            results.append(d)
        return gatherResults(results)
示例#6
0
 def test_serializeComment(self):
     """
     Test that comments are correctly flattened and escaped.
     """
     return (self.assertFlattensTo(Comment("foo bar"), b"<!--foo bar-->"),)
示例#7
0
 def test_serializeComment(self) -> None:
     """
     Test that comments are correctly flattened and escaped.
     """
     self.assertFlattensImmediately(Comment("foo bar"), b"<!--foo bar-->")