diff --git a/sope-xml/libxmlSAXDriver/libxmlHTMLSAXDriver.m b/sope-xml/libxmlSAXDriver/libxmlHTMLSAXDriver.m
index ca62bc931..8bf8b3747 100644
--- a/sope-xml/libxmlSAXDriver/libxmlHTMLSAXDriver.m
+++ b/sope-xml/libxmlSAXDriver/libxmlHTMLSAXDriver.m
@@ -29,6 +29,7 @@
#include
#include
+#include
@interface NSObject(contentHandlerExtensions)
@@ -248,6 +249,15 @@ - (void)setupParserWithDocumentPath:(NSString *)_path {
0 /* chunklen */,
[_path cString] /* filename */,
charEncoding /* encoding */);
+
+ // The enc parameter of htmlCreatePushParserCtxt is deprecated and ignored
+ // in libxml2 >= 2.12. Use xmlSwitchEncoding() to explicitly set the
+ // encoding on the parser context. This is backward-compatible with older
+ // libxml2 versions where it is a harmless no-op.
+ if (self->ctxt && charEncoding != XML_CHAR_ENCODING_NONE) {
+ xmlSwitchEncoding((xmlParserCtxtPtr)self->ctxt, charEncoding);
+ }
+
self->doc = NULL;
}
- (void)tearDownParser {