Edgewall Software

Ticket #253: genshi-default-namespaces.patch

File genshi-default-namespaces.patch, 3.6 KB (added by llasram@…, 15 years ago)
  • genshi/output.py

     
    569569    def __call__(self, stream):
    570570        prefixes = dict([(v, [k]) for k, v in self.prefixes.items()])
    571571        namespaces = {XML_NAMESPACE.uri: ['xml']}
     572        default = ['']
    572573        def _push_ns(prefix, uri):
    573574            namespaces.setdefault(uri, []).append(prefix)
    574575            prefixes.setdefault(prefix, []).append(uri)
     
    592593
    593594                tagname = tag.localname
    594595                tagns = tag.namespace
    595                 if tagns:
     596                if tagns and tagns != default[-1]:
    596597                    if tagns in namespaces:
    597598                        prefix = namespaces[tagns][-1]
    598599                        if prefix:
    599600                            tagname = u'%s:%s' % (prefix, tagname)
    600601                    else:
    601602                        _push_ns_attr((u'xmlns', tagns))
    602                         _push_ns('', tagns)
     603                        default.append(tagns)
    603604
    604605                new_attrs = []
    605606                for attr, value in attrs:
     
    622623            elif kind is END:
    623624                tagname = data.localname
    624625                tagns = data.namespace
    625                 if tagns:
     626                if tagns and tagns != default[-1]:
    626627                    prefix = namespaces[tagns][-1]
    627628                    if prefix:
    628629                        tagname = u'%s:%s' % (prefix, tagname)
     
    630631
    631632            elif kind is START_NS:
    632633                prefix, uri = data
    633                 if uri not in namespaces:
     634                if prefix is '':
     635                    if uri != default[-1]:
     636                        _push_ns_attr(_make_ns_attr(prefix, uri))
     637                    default.append(uri)
     638                elif uri not in namespaces:
    634639                    prefix = prefixes.get(uri, [prefix])[-1]
    635640                    _push_ns_attr(_make_ns_attr(prefix, uri))
    636                 _push_ns(prefix, uri)
     641                if prefix is not '':
     642                    _push_ns(prefix, uri)
    637643
    638644            elif kind is END_NS:
    639                 if data in prefixes:
     645                uri = None
     646                if data is '':
     647                    uri = default.pop()
     648                elif data in prefixes:
    640649                    uris = prefixes.get(data)
    641650                    uri = uris.pop()
    642651                    if not uris:
     
    646655                        uri_prefixes.pop()
    647656                        if not uri_prefixes:
    648657                            del namespaces[uri]
    649                     if ns_attrs:
    650                         attr = _make_ns_attr(data, uri)
    651                         if attr in ns_attrs:
    652                             ns_attrs.remove(attr)
     658                if uri and ns_attrs:
     659                    attr = _make_ns_attr(data, uri)
     660                    if attr in ns_attrs:
     661                        ns_attrs.remove(attr)
    653662
    654663            else:
    655664                yield kind, data, pos
  • genshi/template/tests/directives.py

     
    847847        # FIXME: there should be a way to strip out unwanted/unused namespaces,
    848848        #        such as the "x" in this example
    849849        self.assertEqual("""<html xmlns:x="http://www.example.org/">
    850           <div>Foo</div>
     850          <div xmlns="http://www.example.org/">Foo</div>
    851851        </html>""", str(tmpl.generate()))
    852852
    853853    def test_match_with_position_predicate(self):