33 import xml.dom.minidom
34 from .color
import warning
38 while c
and c.nodeType != xml.dom.Node.ELEMENT_NODE:
45 while c
and c.nodeType != xml.dom.Node.ELEMENT_NODE:
51 parent = node.parentNode
54 if not isinstance(by, list):
63 parent.insertBefore(c, node)
66 parent.insertBefore(doc, node)
69 parent.removeChild(node)
74 Helper function to fetch a single attribute value from tag 75 :param tag (xml.dom.Element): DOM element node 76 :param a (str): attribute name 77 :return: attribute value if present, otherwise None 79 if tag.hasAttribute(a):
82 return tag.getAttribute(a)
89 Helper routine for fetching optional tag attributes 90 :param tag (xml.dom.Element): DOM element node 91 :param attrs [str]: list of attributes to fetch 98 Helper routine for fetching required tag attributes 99 :param tag (xml.dom.Element): DOM element node 100 :param attrs [str]: list of attributes to fetch 101 :raise RuntimeError: if required attribute is missing 104 for (res, name)
in zip(result, attrs):
106 raise RuntimeError(
"%s: missing attribute '%s'" % (tag.nodeName, name))
112 Helper routine to fetch required and optional attributes 113 and complain about any additional attributes. 114 :param tag (xml.dom.Element): DOM element node 115 :param required [str]: list of required attributes 116 :param optional [str]: list of optional attributes 120 allowed = required + optional
121 extra = [a
for a
in tag.attributes.keys()
if a
not in allowed
and not a.startswith(
"xmlns:")]
123 warning(
"%s: unknown attribute(s): %s" % (tag.nodeName,
', '.join(extra)))
133 writer.write(indent +
"<" + self.tagName)
135 attrs = self._get_attributes()
136 a_names = list(attrs.keys())
139 for a_name
in a_names:
140 writer.write(
" %s=\"" % a_name)
141 xml.dom.minidom._write_data(writer, attrs[a_name].value)
144 if len(self.childNodes) == 1 \
145 and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
147 self.childNodes[0].writexml(writer,
"",
"",
"")
148 writer.write(
"</%s>%s" % (self.tagName, newl))
150 writer.write(
">%s" % newl)
151 for node
in self.childNodes:
153 if node.nodeType == xml.dom.minidom.Node.TEXT_NODE
and \
154 (
not node.data
or node.data.isspace()):
156 node.writexml(writer, indent + addindent, addindent, newl)
157 writer.write(
"%s</%s>%s" % (indent, self.tagName, newl))
159 writer.write(
"/>%s" % newl)
161 xml.dom.minidom.Element.writexml = fixed_writexml
def fixed_writexml(self, writer, indent="", addindent="", newl="")
def next_sibling_element(node)
def first_child_element(elt)
def reqd_attrs(tag, attrs)
def replace_node(node, by, content_only=False)
def warning(args, kwargs)
def check_attrs(tag, required, optional)
def opt_attrs(tag, attrs)