mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-24 13:33:01 +03:00
582913 Fix htmlSetMetaEncoding() to be nicer
* HTMLtree.c: htmlSetMetaEncoding should not destroy existing meta encoding elements, plus it should not change things at all if the encoding is the same. Also fixed htmlSaveFileFormat() to ask for change if outputing to UTF-8.
This commit is contained in:
71
HTMLtree.c
71
HTMLtree.c
@@ -160,8 +160,8 @@ found_content:
|
|||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
|
htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
|
||||||
htmlNodePtr cur, meta;
|
htmlNodePtr cur, meta = NULL, head = NULL;
|
||||||
const xmlChar *content;
|
const xmlChar *content = NULL;
|
||||||
char newcontent[100];
|
char newcontent[100];
|
||||||
|
|
||||||
|
|
||||||
@@ -205,39 +205,24 @@ htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
|
|||||||
if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
|
if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
|
||||||
if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
|
if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
|
||||||
break;
|
break;
|
||||||
if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0)
|
if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
|
||||||
|
head = cur->parent;
|
||||||
goto found_meta;
|
goto found_meta;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
if (cur == NULL)
|
if (cur == NULL)
|
||||||
return(-1);
|
return(-1);
|
||||||
found_head:
|
found_head:
|
||||||
if (cur->children == NULL) {
|
head = cur;
|
||||||
if (encoding == NULL)
|
if (cur->children == NULL)
|
||||||
return(0);
|
goto create;
|
||||||
meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
|
|
||||||
xmlAddChild(cur, meta);
|
|
||||||
xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
|
|
||||||
xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
|
|
||||||
return(0);
|
|
||||||
}
|
|
||||||
cur = cur->children;
|
cur = cur->children;
|
||||||
|
|
||||||
found_meta:
|
found_meta:
|
||||||
if (encoding != NULL) {
|
|
||||||
/*
|
|
||||||
* Create a new Meta element with the right attributes
|
|
||||||
*/
|
|
||||||
|
|
||||||
meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
|
|
||||||
xmlAddPrevSibling(cur, meta);
|
|
||||||
xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
|
|
||||||
xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Search and destroy all the remaining the meta elements carrying
|
* Search and update all the remaining the meta elements carrying
|
||||||
* encoding informations
|
* encoding informations
|
||||||
*/
|
*/
|
||||||
while (cur != NULL) {
|
while (cur != NULL) {
|
||||||
@@ -257,11 +242,11 @@ found_meta:
|
|||||||
if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
|
if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
|
||||||
&& (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
|
&& (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
|
||||||
http = 1;
|
http = 1;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ((value != NULL) &&
|
if ((value != NULL) &&
|
||||||
(!xmlStrcasecmp(attr->name, BAD_CAST"content")))
|
(!xmlStrcasecmp(attr->name, BAD_CAST"content")))
|
||||||
content = value;
|
content = value;
|
||||||
}
|
}
|
||||||
if ((http != 0) && (content != NULL))
|
if ((http != 0) && (content != NULL))
|
||||||
break;
|
break;
|
||||||
@@ -270,16 +255,36 @@ found_meta:
|
|||||||
}
|
}
|
||||||
if ((http != 0) && (content != NULL)) {
|
if ((http != 0) && (content != NULL)) {
|
||||||
meta = cur;
|
meta = cur;
|
||||||
cur = cur->next;
|
break;
|
||||||
xmlUnlinkNode(meta);
|
|
||||||
xmlFreeNode(meta);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
create:
|
||||||
|
if (meta == NULL) {
|
||||||
|
if ((encoding != NULL) && (head != NULL)) {
|
||||||
|
/*
|
||||||
|
* Create a new Meta element with the right attributes
|
||||||
|
*/
|
||||||
|
|
||||||
|
meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
|
||||||
|
if (head->children == NULL)
|
||||||
|
xmlAddChild(head, meta);
|
||||||
|
else
|
||||||
|
xmlAddPrevSibling(head->children, meta);
|
||||||
|
xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
|
||||||
|
xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* change the document only if there is a real encoding change */
|
||||||
|
if (xmlStrcasestr(content, encoding) == NULL) {
|
||||||
|
xmlSetProp(meta, BAD_CAST"content", BAD_CAST newcontent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1159,7 +1164,7 @@ htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
|
|||||||
|
|
||||||
if ((cur == NULL) || (filename == NULL))
|
if ((cur == NULL) || (filename == NULL))
|
||||||
return(-1);
|
return(-1);
|
||||||
|
|
||||||
xmlInitParser();
|
xmlInitParser();
|
||||||
|
|
||||||
if (encoding != NULL) {
|
if (encoding != NULL) {
|
||||||
@@ -1177,8 +1182,8 @@ htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
|
|||||||
handler = xmlFindCharEncodingHandler(encoding);
|
handler = xmlFindCharEncodingHandler(encoding);
|
||||||
if (handler == NULL)
|
if (handler == NULL)
|
||||||
return(-1);
|
return(-1);
|
||||||
htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
|
|
||||||
}
|
}
|
||||||
|
htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
|
||||||
} else {
|
} else {
|
||||||
htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
|
htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user