XML Formatter QoL improvements (#88)
- Rewritten backend methods for prettifing and minimizing XMLs - Added "Clear" and "Insert default XML" buttons in XML Formatter. - Added place to display error messages in case of any Co-authored-by: Adam Bem <adam.bem@zoho.eu> Reviewed-on: R11/release11-tools-web#88
This commit is contained in:
@@ -14,7 +14,9 @@ def prettify(source: str) -> str:
|
||||
prolog_end = source.find("?>") + 2
|
||||
prolog = source[prolog_start:prolog_end] + "\n"
|
||||
source = source[prolog_end: ]
|
||||
xml = etree.XML(source)
|
||||
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
xml = etree.fromstring(source, parser=parser)
|
||||
return prolog + etree.tostring(xml, pretty_print=True).decode()
|
||||
|
||||
def minimize(source: str) -> str:
|
||||
@@ -23,18 +25,18 @@ def minimize(source: str) -> str:
|
||||
:param source: XML
|
||||
:return: minimized XML
|
||||
"""
|
||||
result = source
|
||||
|
||||
to_remove = [" ", " ", "\t", "\n"]
|
||||
to_substitute = [(" <", "<"), ("> ", ">"), ("</ ", "</"), (" >", ">")]
|
||||
prolog = ""
|
||||
prolog_start = source.find("<?")
|
||||
|
||||
for chars in to_remove:
|
||||
result = result.replace(chars, "")
|
||||
if prolog_start != -1:
|
||||
prolog_end = source.find("?>") + 2
|
||||
prolog = source[prolog_start:prolog_end]
|
||||
source = source[prolog_end: ]
|
||||
|
||||
for e in to_substitute:
|
||||
result = result.replace(e[0], e[1])
|
||||
|
||||
return result
|
||||
parser = etree.XMLParser(remove_blank_text=True)
|
||||
xml = etree.fromstring(source, parser=parser)
|
||||
return prolog + etree.tostring(xml, pretty_print=False).decode()
|
||||
|
||||
|
||||
def xpath(source: str, xpath: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user