1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-12-23 17:33:50 +03:00

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.
This commit is contained in:
Nick Wellnhofer 2024-07-02 19:22:32 +02:00
parent 2c4204ecee
commit 7cc619d568
3 changed files with 18 additions and 8 deletions

View File

@ -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 <empty/> but <empty></empty>. those two forms are indistinguishable
* once parsed.

View File

@ -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;

View File

@ -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 *