From 7cc619d568a378b50722c4528443cd9c8f7a7715 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Tue, 2 Jul 2024 19:22:32 +0200 Subject: [PATCH] save: Implement save options for indenting Implement XML_SAVE_NO_INDENT to disable and XML_SAVE_INDENT to enable indenting regardless of the global xmlIndentTreeOutput. Implement XML_SAVE_EMPTY to enable empty tags regardless of the global xmlSaveNoEmptyTags. See #736. --- globals.c | 4 ++++ include/libxml/xmlsave.h | 6 +++++- xmlsave.c | 16 +++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/globals.c b/globals.c index 2ba1aab0..fbd4878f 100644 --- a/globals.c +++ b/globals.c @@ -403,6 +403,8 @@ xmlError xmlLastError; /** * xmlIndentTreeOutput: * + * DEPRECATED: Use XML_SAVE_INDENT and XML_SAVE_NO_INDENT. + * * Global setting, asking the serializer to indent the output tree by default * Enabled by default */ @@ -420,6 +422,8 @@ static const char *xmlTreeIndentStringThrDef = " "; /** * xmlSaveNoEmptyTags: * + * DEPRECATED: Use XML_SAVE_EMPTY and XML_SAVE_NO_EMPTY. + * * Global setting, asking the serializer to not output empty tags * as but . those two forms are indistinguishable * once parsed. diff --git a/include/libxml/xmlsave.h b/include/libxml/xmlsave.h index e266e467..e73ed6ae 100644 --- a/include/libxml/xmlsave.h +++ b/include/libxml/xmlsave.h @@ -34,7 +34,11 @@ typedef enum { XML_SAVE_XHTML = 1<<4, /* force XHTML1 specific rules */ XML_SAVE_AS_XML = 1<<5, /* force XML serialization on HTML doc */ XML_SAVE_AS_HTML = 1<<6, /* force HTML serialization on XML doc */ - XML_SAVE_WSNONSIG = 1<<7 /* format with non-significant whitespace */ + XML_SAVE_WSNONSIG = 1<<7, /* format with non-significant whitespace */ + /* Available since 2.14.0 */ + XML_SAVE_EMPTY = 1<<8, /* force empty tags, overriding global */ + XML_SAVE_NO_INDENT = 1<<9, /* disable indenting */ + XML_SAVE_INDENT = 1<<10 /* force indenting, overriding global */ } xmlSaveOption; diff --git a/xmlsave.c b/xmlsave.c index 3df22c4d..213a2fdf 100644 --- a/xmlsave.c +++ b/xmlsave.c @@ -300,16 +300,16 @@ xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt, int options) ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0; } - ctxt->options = options; - - if (xmlSaveNoEmptyTags) { - ctxt->options |= XML_SAVE_NO_EMPTY; - } - if (options & XML_SAVE_FORMAT) ctxt->format = 1; else if (options & XML_SAVE_WSNONSIG) ctxt->format = 2; + + if (((options & XML_SAVE_EMPTY) == 0) && + (xmlSaveNoEmptyTags)) + options |= XML_SAVE_NO_EMPTY; + + ctxt->options = options; } /** @@ -835,7 +835,9 @@ static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur); static void xmlSaveWriteIndent(xmlSaveCtxtPtr ctxt) { - if (!xmlIndentTreeOutput) + if ((ctxt->options & XML_SAVE_NO_INDENT) || + (((ctxt->options & XML_SAVE_INDENT) == 0) && + (xmlIndentTreeOutput == 0))) return; xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size *