From 47cd14e850ebfeff9907ec5fd36a68f628863e1f Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Thu, 4 Feb 2010 18:49:01 +0100 Subject: [PATCH] Fix encoding selection for xmlParseInNodeContext * parser.c: use the encoding from the document to parse the chunk --- parser.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/parser.c b/parser.c index 4d859669..0834d13f 100644 --- a/parser.c +++ b/parser.c @@ -12884,14 +12884,8 @@ xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, if (ctxt == NULL) return(XML_ERR_NO_MEMORY); - fake = xmlNewComment(NULL); - if (fake == NULL) { - xmlFreeParserCtxt(ctxt); - return(XML_ERR_NO_MEMORY); - } - xmlAddChild(node, fake); - /* + /* * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. * We need a dictionary for xmlDetectSAX2, so if there's no doc dict * we must wait until the last moment to free the original one. @@ -12903,10 +12897,32 @@ xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, } else options |= XML_PARSE_NODICT; + if (doc->encoding != NULL) { + xmlCharEncodingHandlerPtr hdlr; + + if (ctxt->encoding != NULL) + xmlFree((xmlChar *) ctxt->encoding); + ctxt->encoding = xmlStrdup((const xmlChar *) doc->encoding); + + hdlr = xmlFindCharEncodingHandler(doc->encoding); + if (hdlr != NULL) { + xmlSwitchToEncoding(ctxt, hdlr); + } else { + return(XML_ERR_UNSUPPORTED_ENCODING); + } + } + xmlCtxtUseOptionsInternal(ctxt, options, NULL); xmlDetectSAX2(ctxt); ctxt->myDoc = doc; + fake = xmlNewComment(NULL); + if (fake == NULL) { + xmlFreeParserCtxt(ctxt); + return(XML_ERR_NO_MEMORY); + } + xmlAddChild(node, fake); + if (node->type == XML_ELEMENT_NODE) { nodePush(ctxt, node); /*