updated to more recent libxml2 version (work in progress)

This commit is contained in:
ejcoumans
2006-06-18 23:22:39 +00:00
parent c91d74bfa3
commit f2636f8d4b
70 changed files with 12357 additions and 3518 deletions

View File

@@ -84,8 +84,8 @@ struct _xmlSaveCtxt {
int level;
int format;
char indent[MAX_INDENT + 1]; /* array for indenting output */
intptr_t indent_nr;
intptr_t indent_size;
int indent_nr;
int indent_size;
xmlCharEncodingOutputFunc escape; /* used for element content */
xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
};
@@ -200,9 +200,9 @@ xmlSerializeHexCharRef(unsigned char *out, int val) {
* if the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets consumed.
*/
static intptr_t
xmlEscapeEntities(unsigned char* out, intptr_t* outlen,
const xmlChar* in, intptr_t* inlen) {
static int
xmlEscapeEntities(unsigned char* out, int *outlen,
const xmlChar* in, int *inlen) {
unsigned char* outstart = out;
const unsigned char* base = in;
unsigned char* outend = out + *outlen;
@@ -326,7 +326,8 @@ error:
static void
xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
{
intptr_t i, len;
int i;
int len;
if (ctxt == NULL) return;
if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
@@ -342,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;
}
}
/**
@@ -379,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) {
@@ -398,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);
}
@@ -714,21 +725,26 @@ xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
return;
}
if (cur->type == XML_CDATA_SECTION_NODE) {
start = end = cur->content;
while (*end != '\0') {
if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) {
end = end + 2;
xmlOutputBufferWrite(buf, 9, "<![CDATA[");
xmlOutputBufferWrite(buf, end - start, (const char *)start);
xmlOutputBufferWrite(buf, 3, "]]>");
start = end;
if (cur->content == NULL) {
xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
} else {
start = end = cur->content;
while (*end != '\0') {
if ((*end == ']') && (*(end + 1) == ']') &&
(*(end + 2) == '>')) {
end = end + 2;
xmlOutputBufferWrite(buf, 9, "<![CDATA[");
xmlOutputBufferWrite(buf, end - start, (const char *)start);
xmlOutputBufferWrite(buf, 3, "]]>");
start = end;
}
end++;
}
if (start != end) {
xmlOutputBufferWrite(buf, 9, "<![CDATA[");
xmlOutputBufferWriteString(buf, (const char *)start);
xmlOutputBufferWrite(buf, 3, "]]>");
}
end++;
}
if (start != end) {
xmlOutputBufferWrite(buf, 9, "<![CDATA[");
xmlOutputBufferWriteString(buf, (const char *)start);
xmlOutputBufferWrite(buf, 3, "]]>");
}
return;
}
@@ -767,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;
@@ -820,43 +836,41 @@ xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
cur->encoding = BAD_CAST ctxt->encoding;
buf = ctxt->buf;
xmlOutputBufferWrite(buf, 14, "<?xml version=");
if (cur->version != NULL)
xmlBufferWriteQuotedString(buf->buffer, cur->version);
else
xmlOutputBufferWrite(buf, 5, "\"1.0\"");
if (ctxt->encoding == NULL) {
if (cur->encoding != NULL)
encoding = cur->encoding;
else if (cur->charset != XML_CHAR_ENCODING_UTF8)
encoding = (const xmlChar *)
xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
xmlOutputBufferWrite(buf, 14, "<?xml version=");
if (cur->version != NULL)
xmlBufferWriteQuotedString(buf->buffer, cur->version);
else
xmlOutputBufferWrite(buf, 5, "\"1.0\"");
if (ctxt->encoding == NULL) {
if (cur->encoding != NULL)
encoding = cur->encoding;
else if (cur->charset != XML_CHAR_ENCODING_UTF8)
encoding = (const xmlChar *)
xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
}
if (encoding != NULL) {
xmlOutputBufferWrite(buf, 10, " encoding=");
xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
}
switch (cur->standalone) {
case 0:
xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
break;
case 1:
xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
break;
}
xmlOutputBufferWrite(buf, 3, "?>\n");
}
if (encoding != NULL) {
xmlOutputBufferWrite(buf, 10, " encoding=");
xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
}
switch (cur->standalone) {
case 0:
xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
break;
case 1:
xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
break;
}
xmlOutputBufferWrite(buf, 3, "?>\n");
#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 (is_xhtml) {
if (encoding != NULL)
htmlSetMetaEncoding(cur, (const xmlChar *) ctxt->encoding);
else
htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
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) {
@@ -1078,12 +1092,17 @@ xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
*/
static void
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
int format;
int format, addmeta = 0;
xmlNodePtr tmp;
xmlChar *start, *end;
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)
@@ -1092,6 +1111,10 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
return;
}
if (cur->type == XML_DOCUMENT_FRAG_NODE) {
xhtmlNodeListDumpOutput(ctxt, cur->children);
return;
}
buf = ctxt->buf;
if (cur->type == XML_ELEMENT_DECL) {
xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
@@ -1202,18 +1225,66 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
if (cur->properties != NULL)
xhtmlAttrListDumpOutput(ctxt, cur->properties);
if ((cur->type == XML_ELEMENT_NODE) &&
(cur->parent != NULL) &&
(cur->parent->parent == (xmlNodePtr) cur->doc) &&
xmlStrEqual(cur->name, BAD_CAST"head") &&
xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
tmp = cur->children;
while (tmp != NULL) {
if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
xmlChar *httpequiv;
httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
if (httpequiv != NULL) {
if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
xmlFree(httpequiv);
break;
}
xmlFree(httpequiv);
}
}
tmp = tmp->next;
}
if (tmp == NULL)
addmeta = 1;
}
if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
(xhtmlIsEmpty(cur) == 1)) {
((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
/*
* C.2. Empty Elements
*/
xmlOutputBufferWrite(buf, 3, " />");
} 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) {
xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
} else {
xmlOutputBufferWrite(buf, 5, "UTF-8");
}
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, ":");
@@ -1224,6 +1295,23 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
return;
}
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) {
xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
} else {
xmlOutputBufferWrite(buf, 5, "UTF-8");
}
xmlOutputBufferWrite(buf, 4, "\" />");
}
if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
}
@@ -1239,25 +1327,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 == ']' &&
@@ -1388,13 +1477,36 @@ xmlSaveToFilename(const char *filename, const char *encoding, int options)
* with the encoding and the options given
*
* Returns a new serialization context or NULL in case of error.
*/
xmlSaveCtxtPtr
xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
{
TODO
return(NULL);
xmlSaveCtxtPtr ret;
xmlOutputBufferPtr out_buff;
xmlCharEncodingHandlerPtr handler;
ret = xmlNewSaveCtxt(encoding, options);
if (ret == NULL) return(NULL);
if (encoding != NULL) {
handler = xmlFindCharEncodingHandler(encoding);
if (handler == NULL) {
xmlFree(ret);
return(NULL);
}
} else
handler = NULL;
out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
if (out_buff == NULL) {
xmlFree(ret);
if (handler) xmlCharEncCloseFunc(handler);
return(NULL);
}
ret->buf = out_buff;
return(ret);
}
*/
/**
* xmlSaveToIO:
@@ -1477,7 +1589,7 @@ xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
*
* Returns the number of byte written or -1 in case of error.
*/
intptr_t
int
xmlSaveFlush(xmlSaveCtxtPtr ctxt)
{
if (ctxt == NULL) return(-1);
@@ -1494,10 +1606,10 @@ xmlSaveFlush(xmlSaveCtxtPtr ctxt)
*
* Returns the number of byte written or -1 in case of error.
*/
intptr_t
int
xmlSaveClose(xmlSaveCtxtPtr ctxt)
{
intptr_t ret;
int ret;
if (ctxt == NULL) return(-1);
ret = xmlSaveFlush(ctxt);
@@ -1687,12 +1799,12 @@ xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
*
* Returns the number of bytes written to the buffer or -1 in case of error
*/
intptr_t
int
xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
int format)
{
size_t use;
intptr_t ret;
unsigned int use;
int ret;
xmlOutputBufferPtr outbuf;
xmlInitParser();
@@ -1716,7 +1828,7 @@ xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
xmlSaveErrMemory("creating buffer");
return (-1);
}
memset(outbuf, 0, sizeof(xmlOutputBuffer));
memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
outbuf->buffer = buf;
outbuf->encoder = NULL;
outbuf->writecallback = NULL;
@@ -1728,7 +1840,7 @@ xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
xmlFree(outbuf);
ret = buf->use - use;
return ret;
return (ret);
}
/**
@@ -1821,18 +1933,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;
if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
(cur->type == XML_ELEMENT_NODE) &&
(xmlStrEqual(cur->name, BAD_CAST "html"))) {
if (encoding != NULL)
htmlSetMetaEncoding((htmlDocPtr) doc,
(const xmlChar *) encoding);
else
htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
}
is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
if (is_xhtml < 0)
is_xhtml = 0;
}
if (is_xhtml)
@@ -1859,7 +1962,7 @@ xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
void
xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
intptr_t * doc_txt_len, const char * txt_encoding,
int * doc_txt_len, const char * txt_encoding,
int format) {
xmlSaveCtxt ctxt;
int dummy = 0;
@@ -1978,7 +2081,7 @@ xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
void
xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
intptr_t * doc_txt_len, const char * txt_encoding) {
int * doc_txt_len, const char * txt_encoding) {
xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
txt_encoding, 0);
}
@@ -1995,13 +2098,13 @@ xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
* Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
* or xmlKeepBlanksDefault(0) was called
*/
intptr_t
int
xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
xmlSaveCtxt ctxt;
xmlOutputBufferPtr buf;
const char * encoding;
xmlCharEncodingHandlerPtr handler = NULL;
intptr_t ret;
int ret;
if (cur == NULL) {
#ifdef DEBUG_TREE
@@ -2043,7 +2146,7 @@ xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
*
* returns: the number of bytes written or -1 in case of failure.
*/
intptr_t
int
xmlDocDump(FILE *f, xmlDocPtr cur) {
return(xmlDocFormatDump (f, cur, 0));
}
@@ -2060,10 +2163,10 @@ xmlDocDump(FILE *f, xmlDocPtr cur) {
*
* returns: the number of bytes written or -1 in case of failure.
*/
intptr_t
int
xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
xmlSaveCtxt ctxt;
intptr_t ret;
int ret;
if (buf == NULL) return(-1);
if (cur == NULL) {
@@ -2095,12 +2198,12 @@ xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
*
* returns: the number of bytes written or -1 in case of failure.
*/
intptr_t
int
xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
const char *encoding, int format)
{
xmlSaveCtxt ctxt;
intptr_t ret;
int ret;
if (buf == NULL) return(-1);
if ((cur == NULL) ||
@@ -2134,13 +2237,13 @@ xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
* Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
* or xmlKeepBlanksDefault(0) was called
*/
intptr_t
int
xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
const char * encoding, int format ) {
xmlSaveCtxt ctxt;
xmlOutputBufferPtr buf;
xmlCharEncodingHandlerPtr handler = NULL;
intptr_t ret;
int ret;
if (cur == NULL)
return(-1);
@@ -2188,7 +2291,7 @@ xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
*
* returns: the number of bytes written or -1 in case of failure.
*/
intptr_t
int
xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
}
@@ -2207,7 +2310,7 @@ xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
*
* returns: the number of bytes written or -1 in case of failure.
*/
intptr_t
int
xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
}
@@ -2222,7 +2325,7 @@ xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
* used.
* returns: the number of bytes written or -1 in case of failure.
*/
intptr_t
int
xmlSaveFile(const char *filename, xmlDocPtr cur) {
return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
}