diff options
| author | Stefan Behnel <stefan_ml@behnel.de> | 2019-04-05 16:49:01 +0200 |
|---|---|---|
| committer | Stefan Behnel <stefan_ml@behnel.de> | 2019-04-05 16:49:01 +0200 |
| commit | 90cd354b9049beaed710a42fc7bddaf9448abe0a (patch) | |
| tree | e91171ac5343f491f392114e75916607ba89b14b /src | |
| parent | 9bfd19e3e1e5169352e8c72a01edc93615e050ab (diff) | |
| download | python-lxml-90cd354b9049beaed710a42fc7bddaf9448abe0a.tar.gz | |
Refactor duplicate code.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lxml/apihelpers.pxi | 18 | ||||
| -rw-r--r-- | src/lxml/etree.pyx | 14 | ||||
| -rw-r--r-- | src/lxml/readonlytree.pxi | 27 |
3 files changed, 28 insertions, 31 deletions
diff --git a/src/lxml/apihelpers.pxi b/src/lxml/apihelpers.pxi index 5bdfbe9c..b61f1238 100644 --- a/src/lxml/apihelpers.pxi +++ b/src/lxml/apihelpers.pxi @@ -236,6 +236,24 @@ cdef int _setNodeNamespaces(xmlNode* c_node, _Document doc, return 0 +cdef dict _build_nsmap(xmlNode* c_node): + """ + Namespace prefix->URI mapping known in the context of this Element. + This includes all namespace declarations of the parents. + """ + cdef xmlNs* c_ns + nsmap = {} + while c_node is not NULL and c_node.type == tree.XML_ELEMENT_NODE: + c_ns = c_node.nsDef + while c_ns is not NULL: + prefix = funicodeOrNone(c_ns.prefix) + if prefix not in nsmap: + nsmap[prefix] = funicodeOrNone(c_ns.href) + c_ns = c_ns.next + c_node = c_node.parent + return nsmap + + cdef _iter_nsmap(nsmap): """ Create a reproducibly ordered iterable from an nsmap mapping. diff --git a/src/lxml/etree.pyx b/src/lxml/etree.pyx index dfd6bba3..fe6ae883 100644 --- a/src/lxml/etree.pyx +++ b/src/lxml/etree.pyx @@ -1078,20 +1078,8 @@ cdef public class _Element [ type LxmlElementType, object LxmlElement ]: Note that changing the returned dict has no effect on the Element. """ - cdef xmlNode* c_node - cdef xmlNs* c_ns _assertValidNode(self) - nsmap = {} - c_node = self._c_node - while c_node is not NULL and c_node.type == tree.XML_ELEMENT_NODE: - c_ns = c_node.nsDef - while c_ns is not NULL: - prefix = funicodeOrNone(c_ns.prefix) - if prefix not in nsmap: - nsmap[prefix] = funicodeOrNone(c_ns.href) - c_ns = c_ns.next - c_node = c_node.parent - return nsmap + return _build_nsmap(self._c_node) # not in ElementTree, read-only property base: diff --git a/src/lxml/readonlytree.pxi b/src/lxml/readonlytree.pxi index becdb58d..cc25f98e 100644 --- a/src/lxml/readonlytree.pxi +++ b/src/lxml/readonlytree.pxi @@ -290,25 +290,16 @@ cdef class _ReadOnlyElementProxy(_ReadOnlyProxy): return funicode(self._c_node.ns.prefix) return None - property nsmap: - u"""Namespace prefix->URI mapping known in the context of this - Element. + @property + def nsmap(self): + """Namespace prefix->URI mapping known in the context of this + Element. This includes all namespace declarations of the + parents. + + Note that changing the returned dict has no effect on the Element. """ - def __get__(self): - self._assertNode() - cdef xmlNode* c_node - cdef xmlNs* c_ns - nsmap = {} - c_node = self._c_node - while c_node is not NULL and c_node.type == tree.XML_ELEMENT_NODE: - c_ns = c_node.nsDef - while c_ns is not NULL: - prefix = funicodeOrNone(c_ns.prefix) - if prefix not in nsmap: - nsmap[prefix] = funicodeOrNone(c_ns.href) - c_ns = c_ns.next - c_node = c_node.parent - return nsmap + self._assertNode() + return _build_nsmap(self._c_node) def get(self, key, default=None): u"""Gets an element attribute. |
