mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-23 01:52:48 +03:00
xmlreader: Fix xmlTextReaderConstEncoding
Regression from commit f1c1f5c6
.
Fixes #697.
This commit is contained in:
12
SAX2.c
12
SAX2.c
@@ -846,17 +846,7 @@ xmlSAX2EndDocument(void *ctx)
|
|||||||
|
|
||||||
doc = ctxt->myDoc;
|
doc = ctxt->myDoc;
|
||||||
if ((doc != NULL) && (doc->encoding == NULL)) {
|
if ((doc != NULL) && (doc->encoding == NULL)) {
|
||||||
const xmlChar *encoding = NULL;
|
const xmlChar *encoding = xmlGetActualEncoding(ctxt);
|
||||||
|
|
||||||
if ((ctxt->input->flags & XML_INPUT_USES_ENC_DECL) ||
|
|
||||||
(ctxt->input->flags & XML_INPUT_AUTO_ENCODING)) {
|
|
||||||
/* Preserve encoding exactly */
|
|
||||||
encoding = ctxt->encoding;
|
|
||||||
} else if ((ctxt->input->buf) && (ctxt->input->buf->encoder)) {
|
|
||||||
encoding = BAD_CAST ctxt->input->buf->encoder->name;
|
|
||||||
} else if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) {
|
|
||||||
encoding = BAD_CAST "UTF-8";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (encoding != NULL) {
|
if (encoding != NULL) {
|
||||||
doc->encoding = xmlStrdup(encoding);
|
doc->encoding = xmlStrdup(encoding);
|
||||||
|
@@ -68,6 +68,8 @@ XML_HIDDEN void
|
|||||||
xmlDetectEncoding(xmlParserCtxtPtr ctxt);
|
xmlDetectEncoding(xmlParserCtxtPtr ctxt);
|
||||||
XML_HIDDEN void
|
XML_HIDDEN void
|
||||||
xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding);
|
xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding);
|
||||||
|
XML_HIDDEN const xmlChar *
|
||||||
|
xmlGetActualEncoding(xmlParserCtxtPtr ctxt);
|
||||||
|
|
||||||
XML_HIDDEN xmlParserNsData *
|
XML_HIDDEN xmlParserNsData *
|
||||||
xmlParserNsCreate(void);
|
xmlParserNsCreate(void);
|
||||||
|
@@ -1398,6 +1398,30 @@ xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) {
|
|||||||
ctxt->encoding = encoding;
|
ctxt->encoding = encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xmlGetActualEncoding:
|
||||||
|
* @ctxt: the parser context
|
||||||
|
*
|
||||||
|
* Returns the actual used to parse the document. This can differ from
|
||||||
|
* the declared encoding.
|
||||||
|
*/
|
||||||
|
const xmlChar *
|
||||||
|
xmlGetActualEncoding(xmlParserCtxtPtr ctxt) {
|
||||||
|
const xmlChar *encoding = NULL;
|
||||||
|
|
||||||
|
if ((ctxt->input->flags & XML_INPUT_USES_ENC_DECL) ||
|
||||||
|
(ctxt->input->flags & XML_INPUT_AUTO_ENCODING)) {
|
||||||
|
/* Preserve encoding exactly */
|
||||||
|
encoding = ctxt->encoding;
|
||||||
|
} else if ((ctxt->input->buf) && (ctxt->input->buf->encoder)) {
|
||||||
|
encoding = BAD_CAST ctxt->input->buf->encoder->name;
|
||||||
|
} else if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) {
|
||||||
|
encoding = BAD_CAST "UTF-8";
|
||||||
|
}
|
||||||
|
|
||||||
|
return(encoding);
|
||||||
|
}
|
||||||
|
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
* *
|
* *
|
||||||
* Commodity functions to handle entities processing *
|
* Commodity functions to handle entities processing *
|
||||||
|
41
testparser.c
41
testparser.c
@@ -176,7 +176,40 @@ testHtmlPushWithEncoding(void) {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_XINCLUDE_ENABLED)
|
#ifdef LIBXML_READER_ENABLED
|
||||||
|
static int
|
||||||
|
testReaderEncoding(void) {
|
||||||
|
xmlBuffer *buf;
|
||||||
|
xmlTextReader *reader;
|
||||||
|
xmlChar *xml;
|
||||||
|
const xmlChar *encoding;
|
||||||
|
int err = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
buf = xmlBufferCreate();
|
||||||
|
xmlBufferCCat(buf, "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
|
||||||
|
xmlBufferCCat(buf, "<doc>");
|
||||||
|
for (i = 0; i < 8192; i++)
|
||||||
|
xmlBufferCCat(buf, "x");
|
||||||
|
xmlBufferCCat(buf, "</doc>");
|
||||||
|
xml = xmlBufferDetach(buf);
|
||||||
|
xmlBufferFree(buf);
|
||||||
|
|
||||||
|
reader = xmlReaderForDoc(BAD_CAST xml, NULL, NULL, 0);
|
||||||
|
xmlTextReaderRead(reader);
|
||||||
|
encoding = xmlTextReaderConstEncoding(reader);
|
||||||
|
|
||||||
|
if (!xmlStrEqual(encoding, BAD_CAST "ISO-8859-1")) {
|
||||||
|
fprintf(stderr, "testReaderEncoding failed\n");
|
||||||
|
err = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
xmlFreeTextReader(reader);
|
||||||
|
xmlFree(xml);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef LIBXML_XINCLUDE_ENABLED
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *message;
|
char *message;
|
||||||
int code;
|
int code;
|
||||||
@@ -254,6 +287,7 @@ testReaderXIncludeError(void) {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef LIBXML_WRITER_ENABLED
|
#ifdef LIBXML_WRITER_ENABLED
|
||||||
static int
|
static int
|
||||||
@@ -314,9 +348,12 @@ main(void) {
|
|||||||
err |= testHtmlPushWithEncoding();
|
err |= testHtmlPushWithEncoding();
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_XINCLUDE_ENABLED)
|
#ifdef LIBXML_READER_ENABLED
|
||||||
|
err |= testReaderEncoding();
|
||||||
|
#ifdef LIBXML_XINCLUDE_ENABLED
|
||||||
err |= testReaderXIncludeError();
|
err |= testReaderXIncludeError();
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
#ifdef LIBXML_WRITER_ENABLED
|
#ifdef LIBXML_WRITER_ENABLED
|
||||||
err |= testWriterClose();
|
err |= testWriterClose();
|
||||||
#endif
|
#endif
|
||||||
|
23
xmlreader.c
23
xmlreader.c
@@ -2866,20 +2866,17 @@ xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
|
|||||||
*/
|
*/
|
||||||
const xmlChar *
|
const xmlChar *
|
||||||
xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
|
xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
|
||||||
xmlDocPtr doc = NULL;
|
const xmlChar *encoding = NULL;
|
||||||
if (reader == NULL)
|
|
||||||
return(NULL);
|
|
||||||
if (reader->doc != NULL)
|
|
||||||
doc = reader->doc;
|
|
||||||
else if (reader->ctxt != NULL)
|
|
||||||
doc = reader->ctxt->myDoc;
|
|
||||||
if (doc == NULL)
|
|
||||||
return(NULL);
|
|
||||||
|
|
||||||
if (doc->encoding == NULL)
|
if (reader == NULL)
|
||||||
return(NULL);
|
return(NULL);
|
||||||
else
|
|
||||||
return(CONSTSTR(doc->encoding));
|
if (reader->ctxt != NULL)
|
||||||
|
encoding = xmlGetActualEncoding(reader->ctxt);
|
||||||
|
else if (reader->doc != NULL)
|
||||||
|
encoding = reader->doc->encoding;
|
||||||
|
|
||||||
|
return(CONSTSTR(encoding));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user