summaryrefslogtreecommitdiff
path: root/xmlsave.c
diff options
context:
space:
mode:
Diffstat (limited to 'xmlsave.c')
-rw-r--r--xmlsave.c92
1 files changed, 64 insertions, 28 deletions
diff --git a/xmlsave.c b/xmlsave.c
index 571427d..ca1d876 100644
--- a/xmlsave.c
+++ b/xmlsave.c
@@ -343,6 +343,10 @@ xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
ctxt->indent_size);
ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
}
+
+ if (xmlSaveNoEmptyTags) {
+ ctxt->options |= XML_SAVE_NO_EMPTY;
+ }
}
/**
@@ -380,13 +384,6 @@ xmlNewSaveCtxt(const char *encoding, int options)
}
memset(ret, 0, sizeof(xmlSaveCtxt));
- /*
- * Use the options
- */
- ret->options = options;
- if (options & XML_SAVE_FORMAT)
- ret->format = 1;
-
if (encoding != NULL) {
ret->handler = xmlFindCharEncodingHandler(encoding);
if (ret->handler == NULL) {
@@ -399,6 +396,19 @@ xmlNewSaveCtxt(const char *encoding, int options)
}
xmlSaveCtxtInit(ret);
+ /*
+ * Use the options
+ */
+
+ /* Re-check this option as it may already have been set */
+ if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
+ options |= XML_SAVE_NO_EMPTY;
+ }
+
+ ret->options = options;
+ if (options & XML_SAVE_FORMAT)
+ ret->format = 1;
+
return(ret);
}
@@ -773,7 +783,7 @@ xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
xmlAttrListDumpOutput(ctxt, cur->properties);
if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
- (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
+ (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
xmlOutputBufferWrite(buf, 2, "/>");
ctxt->format = format;
return;
@@ -855,10 +865,12 @@ xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
}
#ifdef LIBXML_HTML_ENABLED
- dtd = xmlGetIntSubset(cur);
- if (dtd != NULL) {
- is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
- if (is_xhtml < 0) is_xhtml = 0;
+ if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
+ dtd = xmlGetIntSubset(cur);
+ if (dtd != NULL) {
+ is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
+ if (is_xhtml < 0) is_xhtml = 0;
+ }
}
#endif
if (cur->children != NULL) {
@@ -1086,6 +1098,11 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
xmlOutputBufferPtr buf;
if (cur == NULL) return;
+ if ((cur->type == XML_DOCUMENT_NODE) ||
+ (cur->type == XML_HTML_DOCUMENT_NODE)) {
+ xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
+ return;
+ }
if (cur->type == XML_XINCLUDE_START)
return;
if (cur->type == XML_XINCLUDE_END)
@@ -1240,6 +1257,13 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
} else {
if (addmeta == 1) {
xmlOutputBufferWrite(buf, 1, ">");
+ if (ctxt->format) {
+ xmlOutputBufferWrite(buf, 1, "\n");
+ if (xmlIndentTreeOutput)
+ xmlOutputBufferWrite(buf, ctxt->indent_size *
+ (ctxt->level + 1 > ctxt->indent_nr ?
+ ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
+ }
xmlOutputBufferWriteString(buf,
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
if (ctxt->encoding) {
@@ -1247,12 +1271,16 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
} else {
xmlOutputBufferWrite(buf, 5, "UTF-8");
}
- xmlOutputBufferWrite(buf, 3, "\" /");
+ xmlOutputBufferWrite(buf, 4, "\" />");
+ if (ctxt->format)
+ xmlOutputBufferWrite(buf, 1, "\n");
+ } else {
+ xmlOutputBufferWrite(buf, 1, ">");
}
/*
* C.3. Element Minimization and Empty Element Content
*/
- xmlOutputBufferWrite(buf, 3, "></");
+ xmlOutputBufferWrite(buf, 2, "</");
if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
xmlOutputBufferWrite(buf, 1, ":");
@@ -1264,6 +1292,13 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
}
xmlOutputBufferWrite(buf, 1, ">");
if (addmeta == 1) {
+ if (ctxt->format) {
+ xmlOutputBufferWrite(buf, 1, "\n");
+ if (xmlIndentTreeOutput)
+ xmlOutputBufferWrite(buf, ctxt->indent_size *
+ (ctxt->level + 1 > ctxt->indent_nr ?
+ ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
+ }
xmlOutputBufferWriteString(buf,
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
if (ctxt->encoding) {
@@ -1288,25 +1323,26 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
xmlNodePtr child = cur->children;
while (child != NULL) {
- if ((child->type == XML_TEXT_NODE) ||
- (child->type == XML_CDATA_SECTION_NODE)) {
- /*
- * Apparently CDATA escaping for style just break on IE,
- * mozilla and galeon, so ...
- */
- if (xmlStrEqual(cur->name, BAD_CAST "style") &&
- (xmlStrchr(child->content, '<') == NULL) &&
- (xmlStrchr(child->content, '>') == NULL) &&
- (xmlStrchr(child->content, '&') == NULL)) {
+ if (child->type == XML_TEXT_NODE) {
+ if ((xmlStrchr(child->content, '<') == NULL) &&
+ (xmlStrchr(child->content, '&') == NULL) &&
+ (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) {
+ /* Nothing to escape, so just output as is... */
+ /* FIXME: Should we do something about "--" also? */
int level = ctxt->level;
int indent = ctxt->format;
ctxt->level = 0;
ctxt->format = 0;
- xhtmlNodeDumpOutput(ctxt, child);
+ xmlOutputBufferWriteString(buf, (const char *) child->content);
+ /* (We cannot use xhtmlNodeDumpOutput() here because
+ * we wish to leave '>' unescaped!) */
ctxt->level = level;
ctxt->format = indent;
} else {
+ /* We must use a CDATA section. Unfortunately,
+ * this will break CSS and JavaScript when read by
+ * a browser in HTML4-compliant mode. :-( */
start = end = child->content;
while (*end != '\0') {
if (*end == ']' &&
@@ -1870,9 +1906,9 @@ xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
#ifdef LIBXML_HTML_ENABLED
dtd = xmlGetIntSubset(doc);
if (dtd != NULL) {
- is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
- if (is_xhtml < 0)
- is_xhtml = 0;
+ is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
+ if (is_xhtml < 0)
+ is_xhtml = 0;
}
if (is_xhtml)