Skip to content

Commit 14cb350

Browse files
author
Steve Canny
committed
bld: update cxml parser to latest
A feature added to the cxml parser in python-pptx automatically generates namespace prefixes for attributes. Update the cxml Element object to use this latest code. Also, don't generate a namespace declration for the xml: namespace prefix.
1 parent f2bdc00 commit 14cb350

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

tests/unitutil/cxml.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def nsdecls(*nspfxs):
5050
"""
5151
nsdecls = ''
5252
for nspfx in nspfxs:
53+
if nspfx == 'xml':
54+
continue
5355
nsdecls += ' xmlns:%s="%s"' % (nspfx, nsmap[nspfx])
5456
return nsdecls
5557

@@ -105,16 +107,25 @@ def is_root(self, value):
105107
self._is_root = bool(value)
106108

107109
@property
108-
def nspfx(self):
110+
def local_nspfxs(self):
109111
"""
110-
The namespace prefix of this element, the empty string (``''``) if
111-
the tag is in the default namespace.
112+
The namespace prefixes local to this element, both on the tagname and
113+
all of its attributes. An empty string (``''``) is used to represent
114+
the default namespace for an element tag having no prefix.
112115
"""
113-
tagname = self._tagname
114-
idx = tagname.find(':')
115-
if idx == -1:
116-
return ''
117-
return tagname[:idx]
116+
def nspfx(name, is_element=False):
117+
idx = name.find(':')
118+
if idx == -1:
119+
return '' if is_element else None
120+
return name[:idx]
121+
122+
nspfxs = [nspfx(self._tagname, True)]
123+
for name, val in self._attrs:
124+
pfx = nspfx(name)
125+
if pfx is None or pfx in nspfxs:
126+
continue
127+
nspfxs.append(pfx)
128+
return nspfxs
118129

119130
@property
120131
def nspfxs(self):
@@ -129,7 +140,7 @@ def merge(seq, seq_2):
129140
continue
130141
seq.append(item)
131142

132-
nspfxs = [self.nspfx]
143+
nspfxs = self.local_nspfxs
133144
for child in self._children:
134145
merge(nspfxs, child.nspfxs)
135146
return nspfxs

0 commit comments

Comments
 (0)