Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 0b56500

Browse files
committed
html: Fix htmlCreatePushParserCtxt with encoding
Regression from commit ec7be50. Fixes #696.
1 parent 387a952 commit 0b56500

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

HTMLparser.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5987,7 +5987,7 @@ htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
59875987

59885988
xmlInitParser();
59895989

5990-
buf = xmlAllocParserInputBuffer(enc);
5990+
buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
59915991
if (buf == NULL) return(NULL);
59925992

59935993
ctxt = htmlNewSAXParserCtxt(sax, user_data);
@@ -6018,6 +6018,9 @@ htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
60186018

60196019
inputPush(ctxt, inputStream);
60206020

6021+
if (enc != XML_CHAR_ENCODING_NONE)
6022+
xmlSwitchEncoding(ctxt, enc);
6023+
60216024
if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
60226025
(ctxt->input->buf != NULL)) {
60236026
size_t pos = ctxt->input->cur - ctxt->input->base;

testparser.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <libxml/parser.h>
8+
#include <libxml/HTMLparser.h>
89

910
static int
1011
testStandaloneWithEncoding(void) {
@@ -86,6 +87,36 @@ testHugeEncodedChunk(void) {
8687
}
8788
#endif
8889

90+
#if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_PUSH_ENABLED)
91+
static int
92+
testHtmlPushWithEncoding(void) {
93+
htmlParserCtxtPtr ctxt;
94+
htmlDocPtr doc;
95+
htmlNodePtr node;
96+
int err = 0;
97+
98+
ctxt = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL,
99+
XML_CHAR_ENCODING_UTF8);
100+
htmlParseChunk(ctxt, "-\xC3\xA4-", 4, 1);
101+
102+
doc = ctxt->myDoc;
103+
if (!xmlStrEqual(doc->encoding, BAD_CAST "UTF-8")) {
104+
fprintf(stderr, "testHtmlPushWithEncoding failed\n");
105+
err = 1;
106+
}
107+
108+
node = xmlDocGetRootElement(doc)->children->children->children;
109+
if (!xmlStrEqual(node->content, BAD_CAST "-\xC3\xA4-")) {
110+
fprintf(stderr, "testHtmlPushWithEncoding failed\n");
111+
err = 1;
112+
}
113+
114+
xmlFreeDoc(doc);
115+
htmlFreeParserCtxt(ctxt);
116+
return err;
117+
}
118+
#endif
119+
89120
int
90121
main(void) {
91122
int err = 0;
@@ -95,6 +126,9 @@ main(void) {
95126
err |= testHugePush();
96127
err |= testHugeEncodedChunk();
97128
#endif
129+
#if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_PUSH_ENABLED)
130+
err |= testHtmlPushWithEncoding();
131+
#endif
98132

99133
return err;
100134
}

0 commit comments

Comments
 (0)