Exemple #1
0
def test_strip():
    xml = """<html>
        <head><title>The Title</title></head>
        <body>
        <h1>Header 1</h1>
        <div class="1">
        <h2>Header 1.1</h2>
        <h2>Header 1.2</h2>
        <div class="2">
        <h3>Header  1.2.1</h3>
        <h3>Header  1.2.2</h3>
        <div class="2">
        <p>Hello, World!</p>
        </div>
        <h3>Header  1.2.3</h3>
        </div>
        <h2>Header 1.3</h2>
        </div>
        </body>
        </html>"""
    xml_stream = XML(xml, fragment=False)
    assert type(xml_stream) is ElementStream
    xml_stream_stripped = xml_stream.strip(levels=6)
    assert type(xml_stream_stripped) is ElementStream
    from kid import XMLSerializer
    serializer = XMLSerializer(decl=False)
    xml_stripped = serializer.serialize(xml_stream_stripped, fragment=True)
    assert xml_stripped == 'Hello, World!'
Exemple #2
0
def test_strip():
    xml = """<html>
        <head><title>The Title</title></head>
        <body>
        <h1>Header 1</h1>
        <div class="1">
        <h2>Header 1.1</h2>
        <h2>Header 1.2</h2>
        <div class="2">
        <h3>Header  1.2.1</h3>
        <h3>Header  1.2.2</h3>
        <div class="2">
        <p>Hello, World!</p>
        </div>
        <h3>Header  1.2.3</h3>
        </div>
        <h2>Header 1.3</h2>
        </div>
        </body>
        </html>"""
    xml_stream = XML(xml, fragment=False)
    assert type(xml_stream) is ElementStream
    xml_stream_stripped = xml_stream.strip(levels=6)
    assert type(xml_stream_stripped) is ElementStream
    from kid import XMLSerializer
    serializer = XMLSerializer(decl=False)
    xml_stripped = serializer.serialize(xml_stream_stripped, fragment=True)
    assert xml_stripped == 'Hello, World!'
Exemple #3
0
def test_expand():
    doc = XML("<doc><hello>world</hello></doc>", fragment=False)
    assert type(doc) is ElementStream
    doc = doc.expand()
    assert type(doc) == type(Element('doc'))
    assert doc.tag == 'doc'
    assert doc[0].tag == 'hello'
    assert doc[0].text == 'world'
Exemple #4
0
def test_xml_with_entity_map():
    xml = '&nbsp;'
    assert list(XML(xml))[0][1] == u'\xa0'
    xml = '&codswallop;'
    try:
        e = list(XML(xml))
    except Exception, e:
        e = str(e)
Exemple #5
0
def test_expand():
    doc = XML("<doc><hello>world</hello></doc>", fragment=False)
    assert type(doc) is ElementStream
    doc = doc.expand()
    assert type(doc) == type(Element('doc'))
    assert doc.tag == 'doc'
    assert doc[0].tag == 'hello'
    assert doc[0].text == 'world'
    def transform(self, stream=None, filters=[]):
        """
        Execute the template and apply any match transformations.

        If stream is specified, it must be one of the following:

        Element
          A kid.Element.
        ElementStream
          An `pull.ElementStream` instance or other iterator that yields
          stream events.
        string
          A file or URL unless the string starts with
          '<' in which case it is considered an XML document
          and processed as if it had been an Element.

        By default, the `pull` method is called to obtain the stream.
        """
        if stream is None:
            stream = self.pull()
        elif isinstance(stream, basestring):
            if xml_sniff(stream):
                stream = XML(stream, fragment=False)
            else:
                stream = document(stream)
        elif hasattr(stream, 'tag'):
            stream = ElementStream(stream)
        else:
            stream = ElementStream.ensure(stream)
        for f in filters + self._filters:
            stream = f(stream, self)
        return stream
Exemple #7
0
def test_xml_type():
    doc = XML("<doc>hello world</doc>", fragment=False)
    assert type(doc) is ElementStream
    doc = XML("hello world", fragment=True)
    assert type(doc) is ElementStream
Exemple #8
0
    xml_stripped = serializer.serialize(xml_stream_stripped, fragment=True)
    assert xml_stripped == 'Hello, World!'


def test_xml_with_entity_map():
    xml = '&nbsp;'
    assert list(XML(xml))[0][1] == u'\xa0'
    xml = '&codswallop;'
    try:
        e = list(XML(xml))
    except Exception, e:
        e = str(e)
    assert 'undefined entity &codswallop;' in e
    xml = '&nbsp;, &codswallop;!'
    entity_map = {'nbsp': u'Hello', 'codswallop': u'World'}
    assert list(XML(xml, entity_map=entity_map))[0][1] == u'Hello, World!'


def test_load_template_with_entity_map():
    xml = '<html>&nbsp;</html>'
    s = load_template(xml).serialize(encoding='ascii')
    assert s.endswith('<html>&#160;</html>')
    xml = '<html>&codswallop;</html>'
    try:
        e = load_template(xml).serialize(encoding='ascii')
    except Exception, e:
        e = str(e)
    assert 'undefined entity &codswallop;' in e
    xml = '<html>&nbsp;, &codswallop;!</html>'
    entity_map = {'nbsp': u'Hello', 'codswallop': u'World'}
    s = load_template(xml, entity_map=entity_map).serialize(encoding='ascii')