66from lxml .html import fragment_fromstring
77
88
9- def dump_xml_node (node ):
9+ def lxml_dump_node (node ):
1010 return etree .tostring (node , method = "xml" ).decode ("utf8" )
1111
1212
1313# This code is taken from Python 3.7. The addition is escaping of the tab
1414# character.
15- def my_escape (string : str ) -> str :
15+ def lxml_escape_for_html (string : str ) -> str :
1616 """
1717 Replace special characters "&", "<" and ">" to HTML-safe sequences.
1818 If the optional flag quote is true (the default), the quotation mark
@@ -29,7 +29,7 @@ def my_escape(string: str) -> str:
2929 return string
3030
3131
32- def my_escape_title (string : str ) -> str :
32+ def lxml_escape_title (string : str ) -> str :
3333 # The only known reason for this method is the presence of & in the
3434 # HEADER title of ReqIF files found at the ci.eclipse.org.
3535 string = string .replace ("&" , "&" )
@@ -43,47 +43,47 @@ def my_escape_title(string: str) -> str:
4343# when the etree.tostring(...) method is used:
4444# <reqif-xhtml:div xmlns:reqif-xhtml="http://www.w3.org/1999/xhtml">--</reqif-xhtml:div> # noqa: E501
4545# FIXME: Would be great to find a better solution for this.
46- def stringify_namespaced_children (node , namespace_tag = None ) -> str :
46+ def lxml_stringify_namespaced_children (node , namespace_tag = None ) -> str :
4747 if namespace_tag is None :
4848 assert (
4949 len (node .nsmap ) > 0
50- ), f"This method must be called on a namespaced tag:\n { dump_xml_node (node )} " # noqa: E501
50+ ), f"This method must be called on a namespaced tag:\n { lxml_dump_node (node )} " # noqa: E501
5151 nskey = next (iter (node .nsmap .keys ()))
5252 else :
5353 nskey = namespace_tag
5454
55- def _stringify_reqif_ns_node (node ):
55+ def _lxml_stringify_reqif_ns_node (node ):
5656 assert node is not None
5757
5858 output = ""
5959 node_no_ns_tag = etree .QName (node ).localname
6060 output += f"<{ nskey } :{ node_no_ns_tag } "
6161 for attribute , attribute_value in node .attrib .items ():
62- output += f' { attribute } ="{ my_escape (attribute_value )} "'
62+ output += f' { attribute } ="{ lxml_escape_for_html (attribute_value )} "'
6363 if node .text is not None or len (node .getchildren ()) > 0 :
6464 output += ">"
6565 if node .text is not None :
66- output += my_escape (node .text )
66+ output += lxml_escape_for_html (node .text )
6767 for child in node .getchildren ():
68- output += _stringify_reqif_ns_node (child )
68+ output += _lxml_stringify_reqif_ns_node (child )
6969 output += f"</{ nskey } :{ node_no_ns_tag } >"
7070 else :
7171 output += "/>"
7272
7373 if node .tail is not None :
74- output += my_escape (node .tail )
74+ output += lxml_escape_for_html (node .tail )
7575 return output
7676
7777 string = ""
7878 if node .text is not None :
79- string += my_escape (node .text )
79+ string += lxml_escape_for_html (node .text )
8080 for child in node .getchildren ():
81- string += _stringify_reqif_ns_node (child )
81+ string += _lxml_stringify_reqif_ns_node (child )
8282 return string
8383
8484
8585# https://stackoverflow.com/a/28173933/598057
86- def stringify_children (node ):
86+ def lxml_stringify_children (node ):
8787 return "" .join (
8888 chunk
8989 for chunk in chain (
@@ -114,7 +114,7 @@ def stringify_children(node):
114114def lxml_convert_to_reqif_ns_xhtml_string (string , reqif_xhtml = True ) -> str :
115115 namespace_tag = "reqif-xhtml" if reqif_xhtml else "xhtml"
116116 node = fragment_fromstring (string , create_parent = "NOT-USED" )
117- return stringify_namespaced_children (node , namespace_tag = namespace_tag )
117+ return lxml_stringify_namespaced_children (node , namespace_tag = namespace_tag )
118118
119119
120120def lxml_convert_from_reqif_ns_xhtml_string (lxml_node ) -> str :
@@ -128,10 +128,10 @@ def lxml_convert_from_reqif_ns_xhtml_string(lxml_node) -> str:
128128def lxml_convert_children_from_reqif_ns_xhtml_string (lxml_node ) -> str :
129129 lxml_node_deep_copy = deepcopy (lxml_node )
130130 lxml_strip_namespace_from_xml (lxml_node_deep_copy , full = True )
131- return stringify_children (lxml_node_deep_copy )
131+ return lxml_stringify_children (lxml_node_deep_copy )
132132
133133
134- def is_self_closed_tag (xml ):
134+ def lxml_is_self_closed_tag (xml ):
135135 # The tag cannot be closed if it has children or has a non-None text.
136136 if len (xml .getchildren ()) > 0 :
137137 return False
0 commit comments