From 5f4ed88ee8eb162c0e3af5044e0450e927226ab1 Mon Sep 17 00:00:00 2001 From: Nikolay Sivov Date: Sun, 23 Jul 2017 14:55:44 +0300 Subject: [PATCH] xmllite/writer: Fix empty element and state handling in WriteElementString(). Signed-off-by: Nikolay Sivov Signed-off-by: Alexandre Julliard --- dlls/xmllite/tests/writer.c | 82 ++++++++++++++++++++++--------------- dlls/xmllite/writer.c | 26 ++++++------ 2 files changed, 64 insertions(+), 44 deletions(-) diff --git a/dlls/xmllite/tests/writer.c b/dlls/xmllite/tests/writer.c index 89fdb4e49bd..6408b54cfac 100644 --- a/dlls/xmllite/tests/writer.c +++ b/dlls/xmllite/tests/writer.c @@ -67,6 +67,14 @@ static void check_output(IStream *stream, const char *expected, BOOL todo, int l #define CHECK_OUTPUT(stream, expected) check_output(stream, expected, FALSE, __LINE__) #define CHECK_OUTPUT_TODO(stream, expected) check_output(stream, expected, TRUE, __LINE__) +static void writer_set_property(IXmlWriter *writer, XmlWriterProperty property) +{ + HRESULT hr; + + hr = IXmlWriter_SetProperty(writer, property, TRUE); + ok(hr == S_OK, "Failed to set writer property, hr %#x.\n", hr); +} + /* used to test all Write* methods for consistent error state */ static void check_writer_state(IXmlWriter *writer, HRESULT exp_hr) { @@ -475,8 +483,7 @@ static void test_omitxmldeclaration(void) stream = writer_set_output(writer); - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -557,8 +564,7 @@ static void test_bom(void) hr = CreateXmlWriter(&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); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_SetOutput(writer, output); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -664,7 +670,6 @@ static void test_bom(void) static void test_writestartelement(void) { static const WCHAR valueW[] = {'v','a','l','u','e',0}; - static const char *str = "value"; static const WCHAR aW[] = {'a',0}; static const WCHAR bW[] = {'b',0}; IXmlWriter *writer; @@ -726,10 +731,14 @@ static void test_writestartelement(void) hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, valueW); ok(hr == S_OK, "got 0x%08x\n", hr); + hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, NULL); + ok(hr == S_OK, "got 0x%08x\n", hr); + hr = IXmlWriter_Flush(writer); ok(hr == S_OK, "got 0x%08x\n", hr); - CHECK_OUTPUT(stream, str); + CHECK_OUTPUT(stream, + "value"); IStream_Release(stream); IXmlWriter_Release(writer); @@ -840,8 +849,7 @@ static void test_WriteComment(void) hr = CreateXmlWriter(&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); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_WriteComment(writer, aW); ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr); @@ -888,8 +896,7 @@ static void test_WriteCData(void) hr = CreateXmlWriter(&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); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_WriteCData(writer, aW); ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr); @@ -1087,11 +1094,8 @@ static void test_indentation(void) stream = writer_set_output(writer); - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); - - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_Indent, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); + writer_set_property(writer, XmlWriterProperty_Indent); hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -1117,8 +1121,31 @@ static void test_indentation(void) " \r\n" ""); - IXmlWriter_Release(writer); IStream_Release(stream); + + /* WriteElementString */ + stream = writer_set_output(writer); + + hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL); + ok(hr == S_OK, "Unexpected hr %#x.\n", hr); + + hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, NULL); + ok(hr == S_OK, "Unexpected hr %#x.\n", hr); + + hr = IXmlWriter_WriteEndElement(writer); + ok(hr == S_OK, "Unexpected hr %#x.\n", hr); + + hr = IXmlWriter_Flush(writer); + ok(hr == S_OK, "Unexpected hr %#x.\n", hr); + + CHECK_OUTPUT_TODO(stream, + "\r\n" + " \r\n" + ""); + + IStream_Release(stream); + + IXmlWriter_Release(writer); } static void test_WriteAttributeString(void) @@ -1139,8 +1166,7 @@ static void test_WriteAttributeString(void) stream = writer_set_output(writer); - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -1219,11 +1245,8 @@ static void test_WriteFullEndElement(void) /* standalone element */ stream = writer_set_output(writer); - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); - - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_Indent, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); + writer_set_property(writer, XmlWriterProperty_Indent); hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -1247,11 +1270,8 @@ static void test_WriteFullEndElement(void) /* nested elements */ stream = writer_set_output(writer); - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); - - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_Indent, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); + writer_set_property(writer, XmlWriterProperty_Indent); hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -1293,8 +1313,7 @@ static void test_WriteCharEntity(void) /* without indentation */ stream = writer_set_output(writer); - hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE); - ok(hr == S_OK, "got 0x%08x\n", hr); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); ok(hr == S_OK, "got 0x%08x\n", hr); @@ -1334,8 +1353,7 @@ static void test_WriteString(void) hr = CreateXmlWriter(&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); + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); hr = IXmlWriter_WriteString(writer, aW); ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr); diff --git a/dlls/xmllite/writer.c b/dlls/xmllite/writer.c index b0e16c0aaf6..ba0c60ad37d 100644 --- a/dlls/xmllite/writer.c +++ b/dlls/xmllite/writer.c @@ -40,6 +40,7 @@ DEFINE_GUID(IID_IXmlWriterOutput, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0])) static const WCHAR closeelementW[] = {'<','/'}; +static const WCHAR closetagW[] = {' ','/','>'}; static const WCHAR closepiW[] = {'?','>'}; static const WCHAR ltW[] = {'<'}; static const WCHAR gtW[] = {'>'}; @@ -835,26 +836,27 @@ static HRESULT WINAPI xmlwriter_WriteElementString(IXmlWriter *iface, LPCWSTR pr case XmlWriterState_ElemStarted: writer_close_starttag(This); break; - case XmlWriterState_Ready: - case XmlWriterState_DocStarted: - case XmlWriterState_PIDocStarted: - break; - default: - This->state = XmlWriterState_DocClosed; + case XmlWriterState_DocClosed: return WR_E_INVALIDACTION; + default: + ; } write_encoding_bom(This); write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW)); write_output_qname(This->output, prefix, local_name); - write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW)); if (value) + { + write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW)); write_output_buffer(This->output, value, -1); + write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW)); + write_output_qname(This->output, prefix, local_name); + write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW)); + } + else + write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW)); - write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW)); - write_output_qname(This->output, prefix, local_name); - write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW)); This->state = XmlWriterState_Content; return S_OK; @@ -911,8 +913,8 @@ static HRESULT WINAPI xmlwriter_WriteEndElement(IXmlWriter *iface) writer_dec_indent(This); - if (This->starttagopen) { - static WCHAR closetagW[] = {' ','/','>'}; + if (This->starttagopen) + { write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW)); This->starttagopen = FALSE; }