diff --git a/dlls/xmllite/tests/writer.c b/dlls/xmllite/tests/writer.c index 577b3307c23..a55957e9d97 100644 --- a/dlls/xmllite/tests/writer.c +++ b/dlls/xmllite/tests/writer.c @@ -755,6 +755,65 @@ static void test_writeenddocument(void) IStream_Release(stream); } +static void test_WriteComment(void) +{ + static const WCHAR closeW[] = {'-','-','>',0}; + static const WCHAR aW[] = {'a',0}; + static const WCHAR bW[] = {'b',0}; + IXmlWriter *writer; + IStream *stream; + HGLOBAL hglobal; + HRESULT hr; + char *ptr; + + hr = CreateStreamOnHGlobal(NULL, TRUE, &stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL); + ok(hr == S_OK, "Expected S_OK, got %08x\n", hr); + + hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteComment(writer, aW); + ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr); + + hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteComment(writer, aW); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, bW, NULL); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteComment(writer, aW); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteComment(writer, NULL); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_WriteComment(writer, closeW); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = IXmlWriter_Flush(writer); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = GetHGlobalFromStream(stream, &hglobal); + ok(hr == S_OK, "got 0x%08x\n", hr); + + ptr = GlobalLock(hglobal); + ok(ptr != NULL, "got %p\n", ptr); + ok(!strncmp(ptr, "", 37), "got %s\n", ptr); + GlobalUnlock(hglobal); + + IXmlWriter_Release(writer); + IStream_Release(stream); +} + START_TEST(writer) { if (!init_pointers()) @@ -769,4 +828,5 @@ START_TEST(writer) test_omitxmldeclaration(); test_bom(); test_writeenddocument(); + test_WriteComment(); } diff --git a/dlls/xmllite/writer.c b/dlls/xmllite/writer.c index fa0155375a1..766d33564cb 100644 --- a/dlls/xmllite/writer.c +++ b/dlls/xmllite/writer.c @@ -43,6 +43,7 @@ static const WCHAR closeelementW[] = {'<','/'}; static const WCHAR closepiW[] = {'?','>'}; static const WCHAR ltW[] = {'<'}; static const WCHAR gtW[] = {'>'}; +static const WCHAR spaceW[] = {' '}; struct output_buffer { @@ -578,9 +579,49 @@ static HRESULT WINAPI xmlwriter_WriteChars(IXmlWriter *iface, const WCHAR *pwch, return E_NOTIMPL; } -static HRESULT WINAPI xmlwriter_WriteComment(IXmlWriter *iface, LPCWSTR pwszComment) +static HRESULT WINAPI xmlwriter_WriteComment(IXmlWriter *iface, LPCWSTR comment) { - return E_NOTIMPL; + static const WCHAR copenW[] = {'<','!','-','-'}; + static const WCHAR ccloseW[] = {'-','-','>'}; + xmlwriter *This = impl_from_IXmlWriter(iface); + + TRACE("%p %s\n", This, debugstr_w(comment)); + + switch (This->state) + { + case XmlWriterState_Initial: + return E_UNEXPECTED; + case XmlWriterState_ElemStarted: + writer_close_starttag(This); + break; + case XmlWriterState_DocClosed: + return WR_E_INVALIDACTION; + default: + ; + } + + write_output_buffer(This->output, copenW, ARRAY_SIZE(copenW)); + if (comment) { + int len = strlenW(comment), i; + + /* Make sure there's no two hyphen sequences in a string, space is used as a separator to produce compliant + comment string */ + if (len > 1) { + for (i = 0; i < len; i++) { + write_output_buffer(This->output, comment + i, 1); + if (comment[i] == '-' && (i + 1 < len) && comment[i+1] == '-') + write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW)); + } + } + else + write_output_buffer(This->output, comment, len); + + if (len && comment[len-1] == '-') + write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW)); + } + write_output_buffer(This->output, ccloseW, ARRAY_SIZE(ccloseW)); + + return S_OK; } static HRESULT WINAPI xmlwriter_WriteDocType(IXmlWriter *iface, LPCWSTR pwszName, LPCWSTR pwszPublicId, @@ -763,7 +804,6 @@ static HRESULT WINAPI xmlwriter_WriteProcessingInstruction(IXmlWriter *iface, LP xmlwriter *This = impl_from_IXmlWriter(iface); static const WCHAR xmlW[] = {'x','m','l',0}; static const WCHAR openpiW[] = {'<','?'}; - static const WCHAR spaceW[] = {' '}; TRACE("(%p)->(%s %s)\n", This, wine_dbgstr_w(name), wine_dbgstr_w(text));