Skip to content

Commit df464f4

Browse files
author
Steve Canny
committed
oxml: add unit tests for OxmlElement
1 parent 97f27d0 commit df464f4

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

docx/oxml/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,23 @@ def register_element_cls(tag, cls):
4040
namespace[tagroot] = cls
4141

4242

43-
def OxmlElement(nsptag_str, attrs=None, nsmap=None):
43+
def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
4444
"""
4545
Return a 'loose' lxml element having the tag specified by *nsptag_str*.
4646
*nsptag_str* must contain the standard namespace prefix, e.g. 'a:tbl'.
4747
The resulting element is an instance of the custom element class for this
4848
tag name if one is defined. A dictionary of attribute values may be
49-
provided as *attrs*; they are set if present.
49+
provided as *attrs*; they are set if present. All namespaces defined in
50+
the dict *nsdecls* are declared in the element using the key as the
51+
prefix and the value as the namespace name. If *nsdecls* is not provided,
52+
a single namespace declaration is added based on the prefix on
53+
*nsptag_str*.
5054
"""
5155
nsptag = NamespacePrefixedTag(nsptag_str)
52-
_nsmap = nsptag.nsmap if nsmap is None else nsmap
56+
if nsdecls is None:
57+
nsdecls = nsptag.nsmap
5358
return oxml_parser.makeelement(
54-
nsptag.clark_name, attrib=attrs, nsmap=_nsmap
59+
nsptag.clark_name, attrib=attrs, nsmap=nsdecls
5560
)
5661

5762

docx/oxml/shape.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def new(cls, cx, cy, shape_id, pic):
102102
name = 'Picture %d' % shape_id
103103
uri = nsmap['pic']
104104

105-
inline = OxmlElement('wp:inline', nsmap=nspfxmap('wp', 'r'))
105+
inline = OxmlElement('wp:inline', nsdecls=nspfxmap('wp', 'r'))
106106
inline.append(CT_PositiveSize2D.new('wp:extent', cx, cy))
107107
inline.append(CT_NonVisualDrawingProps.new(
108108
'wp:docPr', shape_id, name
@@ -149,7 +149,7 @@ def new(cls, pic_id, filename, rId, cx, cy):
149149
contents required to define a viable picture element, based on the
150150
values passed as parameters.
151151
"""
152-
pic = OxmlElement('pic:pic', nsmap=nspfxmap('pic', 'r'))
152+
pic = OxmlElement('pic:pic', nsdecls=nspfxmap('pic', 'r'))
153153
pic.append(CT_PictureNonVisual.new(pic_id, filename))
154154
pic.append(CT_BlipFillProperties.new(rId))
155155
pic.append(CT_ShapeProperties.new(cx, cy))

tests/oxml/test__init__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,42 @@
1010

1111
from lxml import etree
1212

13-
from docx.oxml import oxml_parser, parse_xml, register_element_cls
13+
from docx.oxml import (
14+
OxmlElement, oxml_parser, parse_xml, register_element_cls
15+
)
1416
from docx.oxml.ns import qn
1517
from docx.oxml.shared import OxmlBaseElement
1618

1719

20+
class DescribeOxmlElement(object):
21+
22+
def it_returns_an_lxml_element_with_matching_tag_name(self):
23+
element = OxmlElement('a:foo')
24+
assert isinstance(element, etree._Element)
25+
assert element.tag == (
26+
'{http://schemas.openxmlformats.org/drawingml/2006/main}foo'
27+
)
28+
29+
def it_adds_supplied_attributes(self):
30+
element = OxmlElement('a:foo', {'a': 'b', 'c': 'd'})
31+
assert etree.tostring(element) == (
32+
'<a:foo xmlns:a="http://schemas.openxmlformats.org/drawingml/200'
33+
'6/main" a="b" c="d"/>'
34+
)
35+
36+
def it_adds_additional_namespace_declarations_when_supplied(self):
37+
element = OxmlElement(
38+
'a:foo', nsdecls={
39+
'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
40+
'x': 'other'
41+
}
42+
)
43+
assert etree.tostring(element) == (
44+
'<a:foo xmlns:a="http://schemas.openxmlformats.org/drawingml/200'
45+
'6/main" xmlns:x="other"/>'
46+
)
47+
48+
1849
class DescribeOxmlParser(object):
1950

2051
def it_strips_whitespace_between_elements(self, whitespace_fixture):

0 commit comments

Comments
 (0)