コード例 #1
0
 def test_wbr_serialize(self):
     chunks = ChunkList(Chunk(u'今日は'), Chunk(u'ご飯を'), Chunk(u'食べます。'))
     result = chunks.wbr_serialize()
     expected = ('<span style="word-break: keep-all;">'
                 u'今日は<wbr></wbr>ご飯を<wbr></wbr>食べます。'
                 '</span>')
     self.assertEqual(result, expected,
                      'Chunks should be separated by WBR tags.')
コード例 #2
0
    def test_span_serialize(self):
        chunks = ChunkList(Chunk('Hello'), Chunk.space(), Chunk(u'今天'),
                           Chunk(u'天气'), Chunk(u'很好'))
        attributes = {'class': 'foo'}
        expected = ('<span>'
                    'Hello '
                    u'<span class="foo">今天</span>'
                    u'<span class="foo">天气</span>'
                    u'<span class="foo">很好</span>'
                    '</span>')
        result = chunks.span_serialize(attributes, None)
        self.assertEqual(result, expected,
                         'The chunks should be compiled to a HTML code.')

        chunks = ChunkList(Chunk('Hey<'), Chunk('<script>alert(1)</script>'),
                           Chunk('>guys'))
        attributes = {'class': 'foo'}
        expected = ('<span>'
                    'Hey&lt;&lt;script&gt;alert(1)&lt;/script&gt;&gt;guys'
                    '</span>')
        result = chunks.span_serialize(attributes, None)
        self.assertEqual(result, expected,
                         'HTML tags included in a chunk should be encoded.')

        chunks = ChunkList(Chunk(u'去年'), Chunk(u'インフルエンザに'), Chunk(u'かかった。'))
        attributes = {'class': 'foo'}
        expected = ('<span>'
                    u'<span class="foo">去年</span>'
                    u'インフルエンザに'
                    u'<span class="foo">かかった。</span>'
                    '</span>')
        result = chunks.span_serialize(attributes, 6)
        self.assertEqual(
            result, expected,
            'Chunks that exceed the max length should not be enclosed by a span.'
        )
コード例 #3
0
 def setUp(self):
     self.chunks = ChunkList(Chunk('ab', dependency=None),
                             Chunk('cde', dependency=True),
                             Chunk('fgh', dependency=False))
コード例 #4
0
 def test_insert_breaklines(self):
     chunks = ChunkList(Chunk(u'これが '), Chunk('Android'))
     chunks._insert_breaklines()
     self.assertEqual(
         [u'これが', '\n', 'Android'], [chunk.word for chunk in chunks],
         'Trailing spaces in CJK chunk should be converted to breaklines.')