better handling of conditional features more testing on parser contexts

* gentest.py testapi.c: better handling of conditional features
* HTMLparser.c SAX2.c parserInternals.c xmlwriter.c: more testing
  on parser contexts closed leaks, error messages
Daniel
diff --git a/xmlwriter.c b/xmlwriter.c
index 4b2c3e1..3255c67 100644
--- a/xmlwriter.c
+++ b/xmlwriter.c
@@ -71,6 +71,7 @@
     xmlChar *ichar;             /* indent character */
     char qchar;                 /* character used for quoting attribute values */
     xmlParserCtxtPtr ctxt;
+    int no_doc_free;
 };
 
 static void xmlFreeTextWriterStackEntry(xmlLinkPtr lk);
@@ -198,6 +199,7 @@
                         "xmlNewTextWriter : out of memory!\n");
         return NULL;
     }
+    ret->no_doc_free = 0;
 
     return ret;
 }
@@ -376,8 +378,10 @@
 
     xmlSetDocCompressMode(ctxt->myDoc, compression);
 
-    if (doc != NULL)
+    if (doc != NULL) {
         *doc = ctxt->myDoc;
+	ret->no_doc_free = 1;
+    }
 
     return ret;
 }
@@ -434,6 +438,7 @@
 
     ctxt->myDoc = doc;
     ctxt->node = node;
+    ret->no_doc_free = 1;
 
     xmlSetDocCompressMode(doc, compression);
 
@@ -461,8 +466,13 @@
     if (writer->nsstack != NULL)
         xmlListDelete(writer->nsstack);
 
-    if (writer->ctxt != NULL)
+    if (writer->ctxt != NULL) {
+        if ((writer->ctxt->myDoc != NULL) && (writer->no_doc_free == 0)) {
+	    xmlFreeDoc(writer->ctxt->myDoc);
+	    writer->ctxt->myDoc = NULL;
+	}
         xmlFreeParserCtxt(writer->ctxt);
+    }
 
     if (writer->ichar != NULL)
         xmlFree(writer->ichar);