def serialize_body(cls, packet_type, params): root = microdom.Element(packet_type.encode('utf8'), preserveCase=True) for name, value in params: el = microdom.Element(name.encode('utf8'), preserveCase=True) el.appendChild(microdom.Text(value.encode('utf8'))) root.appendChild(el) data = root.toxml() return data.decode('utf8').encode(cls.ENCODING, 'xmlcharrefreplace')
def test_namespaceInheritance(self): """ Check that unspecified namespace is a thing separate from undefined namespace. This test added after discovering some weirdness in Lore. """ # will only work if childNodes is mutated. not sure why. child = microdom.Element('ol') parent = microdom.Element('div', namespace='http://www.w3.org/1999/xhtml') parent.childNodes = [child] self.assertEqual(parent.toxml(), '<div xmlns="http://www.w3.org/1999/xhtml"><ol></ol></div>')
def test_mutate(self): s = "<foo />" s1 = '<foo a="b"><bar/><foo/></foo>' s2 = '<foo a="b">foo</foo>' d = microdom.parseString(s).documentElement d1 = microdom.parseString(s1).documentElement d2 = microdom.parseString(s2).documentElement d.appendChild(d.cloneNode()) d.setAttribute("a", "b") child = d.childNodes[0] self.assertEqual(child.getAttribute("a"), None) self.assertEqual(child.nodeName, "foo") d.insertBefore(microdom.Element("bar"), child) self.assertEqual(d.childNodes[0].nodeName, "bar") self.assertEqual(d.childNodes[1], child) for n in d.childNodes: self.assertEqual(n.parentNode, d) self.assertTrue(d.isEqualToNode(d1)) d.removeChild(child) self.assertEqual(len(d.childNodes), 1) self.assertEqual(d.childNodes[0].nodeName, "bar") t = microdom.Text("foo") d.replaceChild(t, d.firstChild()) self.assertEqual(d.firstChild(), t) self.assertTrue(d.isEqualToNode(d2))
def fixAPI(document, url): """ Replace API references with links to API documentation. @type document: A DOM Node or Document @param document: The input document which contains all of the content to be presented. @type url: C{str} @param url: A string which will be interpolated with the fully qualified Python name of any API reference encountered in the input document, the result of which will be used as a link to API documentation for that name in the output document. @return: C{None} """ # API references for node in domhelpers.findElementsWithAttribute(document, "class", "API"): fullname = _getAPI(node) node2 = microdom.Element('a', { 'href': url % fullname, 'title': fullname }) node2.childNodes = node.childNodes node.childNodes = [node2] node.removeAttribute('base')
def visitNode_li(self, node): for child in node.childNodes: if getattr(child, 'tagName', None) != 'p': new = microdom.Element('p') new.childNodes = [child] node.replaceChild(new, child) self.visitNodeDefault(node)
def test_isEqualToNode(self): """ L{Element.isEqualToNode} returns C{True} if and only if passed a L{Element} with the same C{nodeName}, C{namespace}, C{childNodes}, and C{attributes}. """ self.assertTrue( microdom.Element("foo", { "a": "b" }, object(), namespace="bar").isEqualToNode( microdom.Element("foo", {"a": "b"}, object(), namespace="bar"))) # Elements with different nodeName values do not compare equal. self.assertFalse( microdom.Element("foo", { "a": "b" }, object(), namespace="bar").isEqualToNode( microdom.Element("bar", {"a": "b"}, object(), namespace="bar"))) # Elements with different namespaces do not compare equal. self.assertFalse( microdom.Element("foo", { "a": "b" }, object(), namespace="bar").isEqualToNode( microdom.Element("foo", {"a": "b"}, object(), namespace="baz"))) # Elements with different childNodes do not compare equal. one = microdom.Element("foo", {"a": "b"}, object(), namespace="bar") two = microdom.Element("foo", {"a": "b"}, object(), namespace="bar") two.appendChild(microdom.Node(object())) self.assertFalse(one.isEqualToNode(two)) # Elements with different attributes do not compare equal. self.assertFalse( microdom.Element("foo", { "a": "b" }, object(), namespace="bar").isEqualToNode( microdom.Element("foo", {"a": "c"}, object(), namespace="bar")))
def test_LMX(self): n = microdom.Element("p") lmx = microdom.lmx(n) lmx.text("foo") b = lmx.b(a="c") b.foo()["z"] = "foo" b.foo() b.add("bar", c="y") s = '<p>foo<b a="c"><foo z="foo"></foo><foo></foo><bar c="y"></bar></b></p>' self.assertEqual(s, n.toxml())
def footnotes(document): """ Find footnotes in the given document, move them to the end of the body, and generate links to them. A footnote is any node with a C{class} attribute set to C{footnote}. Footnote links are generated as superscript. Footnotes are collected in a C{ol} node at the end of the document. @type document: A DOM Node or Document @param document: The input document which contains all of the content to be presented. @return: C{None} """ footnotes = domhelpers.findElementsWithAttribute(document, "class", "footnote") if not footnotes: return footnoteElement = microdom.Element('ol') id = 1 for footnote in footnotes: href = microdom.parseString('<a href="#footnote-%(id)d">' '<super>%(id)d</super></a>' % vars()).documentElement text = ' '.join(domhelpers.getNodeText(footnote).split()) href.setAttribute('title', text) target = microdom.Element('a', attributes={'name': 'footnote-%d' % id}) target.childNodes = [footnote] footnoteContent = microdom.Element('li') footnoteContent.childNodes = [target] footnoteElement.childNodes.append(footnoteContent) footnote.parentNode.replaceChild(href, footnote) id += 1 body = domhelpers.findNodesNamed(document, "body")[0] header = microdom.parseString('<h2>Footnotes</h2>').documentElement body.childNodes.append(header) body.childNodes.append(footnoteElement)
def removeH1(document): """ Replace all C{h1} nodes in the given document with empty C{span} nodes. C{h1} nodes mark up document sections and the output template is given an opportunity to present this information in a different way. @type document: A DOM Node or Document @param document: The input document which contains all of the content to be presented. @return: C{None} """ h1 = domhelpers.findNodesNamed(document, 'h1') empty = microdom.Element('span') for node in h1: node.parentNode.replaceChild(empty, node)
def test_dict(self): """ Returns a dictionary which is hashable. """ n = microdom.Element("p") hash(n)
def testDict(self): n = microdom.Element("p") d = {n: 1} # will fail if Element is unhashable
def textelement(element, tag, text): el = microdom.Element(tag, preserveCase=1) el.appendChild(microdom.Text(text)) element.appendChild(el)