- HTMLtree.[ch]: more work on the HTML serialization routnes,
  cleanup, encoding support.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 3350a96..ca6ac3e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Jun 15 07:08:57 CEST 2001 Daniel Veillard <Daniel.Veillard@imag.fr>
+
+	* HTMLtree.[ch]: more work on the HTML serialization routnes,
+	  cleanup, encoding support.
+
 Thu Jun 14 10:31:17 CEST 2001 Daniel Veillard <Daniel.Veillard@imag.fr>
 
 	* xpath.c: Thomas Broyer suggested a better patch for the / arg
diff --git a/HTMLtree.c b/HTMLtree.c
index 7554d17..ae0374c 100644
--- a/HTMLtree.c
+++ b/HTMLtree.c
@@ -294,6 +294,9 @@
  *									*
  ************************************************************************/
 
+void htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
+	                xmlNodePtr cur, const char *encoding, int format);
+
 static void
 htmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur, int format);
 
@@ -616,18 +619,46 @@
  *
  * Dump an HTML node, recursive behaviour,children are printed too.
  *
- * TODO: handle the encoding not used yet
+ * TODO: if encoding == NULL try to save in the doc encoding
+ *
+ * returns: the number of byte written or -1 in case of failure.
  */
-void
-htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc, xmlNodePtr cur,
-	               const char *encoding ATTRIBUTE_UNUSED, int format) {
-    xmlBufferPtr buf;
+int
+htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
+	               xmlNodePtr cur, const char *encoding, int format) {
+    xmlOutputBufferPtr buf;
+    xmlCharEncodingHandlerPtr handler = NULL;
+    int ret;
 
-    buf = xmlBufferCreate();
-    if (buf == NULL) return;
-    htmlNodeDumpFormat(buf, doc, cur, format);
-    xmlBufferDump(out, buf);
-    xmlBufferFree(buf);
+    if (encoding != NULL) {
+	xmlCharEncoding enc;
+
+	enc = xmlParseCharEncoding(encoding);
+	if (enc != XML_CHAR_ENCODING_UTF8) {
+	    handler = xmlFindCharEncodingHandler(encoding);
+	    if (handler == NULL)
+		return(-1);
+	}
+    }
+
+    /*
+     * Fallback to HTML or ASCII when the encoding is unspecified
+     */
+    if (handler == NULL)
+	handler = xmlFindCharEncodingHandler("HTML");
+    if (handler == NULL)
+	handler = xmlFindCharEncodingHandler("ascii");
+
+    /* 
+     * save the content to a temp buffer.
+     */
+    buf = xmlOutputBufferCreateFile(out, handler);
+    if (buf == NULL) return(0);
+
+    htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
+
+    ret = xmlOutputBufferClose(buf);
+    return(ret);
 }
 
 /**
@@ -862,9 +893,6 @@
 void htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
 	                xmlNodePtr cur, const char *encoding);
 
-void htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
-	                xmlNodePtr cur, const char *encoding, int format);
-
 /**
  * htmlNodeListDumpOutput:
  * @buf:  the HTML buffer output
diff --git a/HTMLtree.h b/HTMLtree.h
index b55f91e..bd3a2fb 100644
--- a/HTMLtree.h
+++ b/HTMLtree.h
@@ -75,7 +75,7 @@
 void		htmlNodeDumpFile	(FILE *out,
 					 xmlDocPtr doc,
 					 xmlNodePtr cur);
-void		htmlNodeDumpFileFormat	(FILE *out,
+int		htmlNodeDumpFileFormat	(FILE *out,
 					 xmlDocPtr doc,
 					 xmlNodePtr cur,
 					 const char *encoding,
diff --git a/include/libxml/HTMLtree.h b/include/libxml/HTMLtree.h
index b55f91e..bd3a2fb 100644
--- a/include/libxml/HTMLtree.h
+++ b/include/libxml/HTMLtree.h
@@ -75,7 +75,7 @@
 void		htmlNodeDumpFile	(FILE *out,
 					 xmlDocPtr doc,
 					 xmlNodePtr cur);
-void		htmlNodeDumpFileFormat	(FILE *out,
+int		htmlNodeDumpFileFormat	(FILE *out,
 					 xmlDocPtr doc,
 					 xmlNodePtr cur,
 					 const char *encoding,