>From 6710d56353c1dc54ce5f46564cfdfc93f911270d Mon Sep 17 00:00:00 2001
From: Nikolay Sivov <nsivov@codeweavers.com>
Date: Fri, 27 Jan 2012 15:13:54 +0300
Subject: [PATCH 2/5] Support comment nodes in MXWriter

---
 dlls/msxml3/mxwriter.c        |   19 ++++++++++++--
 dlls/msxml3/tests/saxreader.c |   55 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/dlls/msxml3/mxwriter.c b/dlls/msxml3/mxwriter.c
index 82f7ebd..a94fa51 100644
--- a/dlls/msxml3/mxwriter.c
+++ b/dlls/msxml3/mxwriter.c
@@ -1155,11 +1155,24 @@ static HRESULT WINAPI SAXLexicalHandler_endCDATA(ISAXLexicalHandler *iface)
     return E_NOTIMPL;
 }
 
-static HRESULT WINAPI SAXLexicalHandler_comment(ISAXLexicalHandler *iface, const WCHAR *chars, int len)
+static HRESULT WINAPI SAXLexicalHandler_comment(ISAXLexicalHandler *iface, const WCHAR *chars, int nchars)
 {
     mxwriter *This = impl_from_ISAXLexicalHandler( iface );
-    FIXME("(%p)->(%s): stub\n", This, debugstr_wn(chars, len));
-    return E_NOTIMPL;
+    static const WCHAR copenW[] = {'<','!','-','-'};
+    static const WCHAR ccloseW[] = {'-','-','>','\r','\n'};
+
+    TRACE("(%p)->(%s:%d)\n", This, debugstr_wn(chars, nchars), nchars);
+
+    if (!chars) return E_INVALIDARG;
+
+    close_element_starttag(This);
+
+    write_output_buffer(This->buffer, copenW, sizeof(copenW)/sizeof(WCHAR));
+    if (nchars)
+        write_output_buffer(This->buffer, chars, nchars);
+    write_output_buffer(This->buffer, ccloseW, sizeof(ccloseW)/sizeof(WCHAR));
+
+    return S_OK;
 }
 
 static const struct ISAXLexicalHandlerVtbl SAXLexicalHandlerVtbl =
diff --git a/dlls/msxml3/tests/saxreader.c b/dlls/msxml3/tests/saxreader.c
index 33eefff..d05596e 100644
--- a/dlls/msxml3/tests/saxreader.c
+++ b/dlls/msxml3/tests/saxreader.c
@@ -2851,6 +2851,60 @@ static void test_mxwriter_dispex(void)
     IMXWriter_Release(writer);
 }
 
+static void test_mxwriter_comment(void)
+{
+    static const WCHAR commentW[] = {'c','o','m','m','e','n','t',0};
+    ISAXContentHandler *content;
+    ISAXLexicalHandler *lexical;
+    IMXWriter *writer;
+    VARIANT dest;
+    HRESULT hr;
+
+    hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
+            &IID_IMXWriter, (void**)&writer);
+    EXPECT_HR(hr, S_OK);
+
+    hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
+    EXPECT_HR(hr, S_OK);
+
+    hr = IMXWriter_QueryInterface(writer, &IID_ISAXLexicalHandler, (void**)&lexical);
+    EXPECT_HR(hr, S_OK);
+
+    hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
+    EXPECT_HR(hr, S_OK);
+
+    hr = ISAXContentHandler_startDocument(content);
+    EXPECT_HR(hr, S_OK);
+
+    hr = ISAXLexicalHandler_comment(lexical, NULL, 0);
+    EXPECT_HR(hr, E_INVALIDARG);
+
+    hr = ISAXLexicalHandler_comment(lexical, commentW, 0);
+    EXPECT_HR(hr, S_OK);
+
+    V_VT(&dest) = VT_EMPTY;
+    hr = IMXWriter_get_output(writer, &dest);
+    EXPECT_HR(hr, S_OK);
+    ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
+    ok(!lstrcmpW(_bstr_("<!---->\r\n"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
+    VariantClear(&dest);
+
+    hr = ISAXLexicalHandler_comment(lexical, commentW, sizeof(commentW)/sizeof(WCHAR)-1);
+    EXPECT_HR(hr, S_OK);
+
+    V_VT(&dest) = VT_EMPTY;
+    hr = IMXWriter_get_output(writer, &dest);
+    EXPECT_HR(hr, S_OK);
+    ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
+    ok(!lstrcmpW(_bstr_("<!---->\r\n<!--comment-->\r\n"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
+    VariantClear(&dest);
+
+    ISAXContentHandler_Release(content);
+    ISAXLexicalHandler_Release(lexical);
+    IMXWriter_Release(writer);
+    free_bstrs();
+}
+
 START_TEST(saxreader)
 {
     ISAXXMLReader *reader;
@@ -2886,6 +2940,7 @@ START_TEST(saxreader)
         test_mxwriter_startenddocument();
         test_mxwriter_startendelement();
         test_mxwriter_characters();
+        test_mxwriter_comment();
         test_mxwriter_properties();
         test_mxwriter_flush();
         test_mxwriter_stream();
-- 
1.5.6.5


