From: David Kahurani k.kahurani@gmail.com
Validate a name then write it.
Signed-off-by: David Kahurani k.kahurani@gmail.com --- dlls/xmllite/tests/writer.c | 77 ++++++++++++++++++++++++++++++++++++- dlls/xmllite/writer.c | 33 +++++++++++++--- 2 files changed, 104 insertions(+), 6 deletions(-)
diff --git a/dlls/xmllite/tests/writer.c b/dlls/xmllite/tests/writer.c index 90226b2eb4d..7896392cafb 100644 --- a/dlls/xmllite/tests/writer.c +++ b/dlls/xmllite/tests/writer.c @@ -1842,7 +1842,7 @@ static void test_WriteName(void) hr = IXmlWriter_WriteName(writer, L"name"); ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
- hr = IXmlWriter_WriteFullEndElement(writer); + hr = IXmlWriter_WriteEndElement(writer); ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
hr = IXmlWriter_WriteStartElement(writer, NULL, L"b", NULL); @@ -1882,6 +1882,80 @@ static void test_WriteName(void) IXmlWriter_Release(writer); }
+static void test_WriteEntityRef(void) +{ + IXmlWriter *writer; + IStream *stream; + HRESULT hr; + + hr = CreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, L""); + ok(hr == E_INVALIDARG, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, NULL); + ok(hr == E_INVALIDARG, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, L"name"); + ok(hr == E_UNEXPECTED, "Unexpected hr %#lx.\n", hr); + + stream = writer_set_output(writer); + + writer_set_property(writer, XmlWriterProperty_OmitXmlDeclaration); + + hr = IXmlWriter_WriteEntityRef(writer, L"name"); + ok(hr == WR_E_INVALIDACTION, "Unexpected hr %#lx.\n", hr); + + IStream_Release(stream); + stream = writer_set_output(writer); + + hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, L"a", NULL); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, L"na:me"); + ok(hr == WC_E_NAMECHARACTER, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_Flush(writer); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + CHECK_OUTPUT(stream, + "<a"); + + IStream_Release(stream); + stream = writer_set_output(writer); + + hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteStartElement(writer, NULL, L"a", NULL); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, L"name"); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, L".name"); + ok(hr == WC_E_NAMECHARACTER, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEndDocument(writer); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_WriteEntityRef(writer, L"name"); + ok(hr == WR_E_INVALIDACTION, "Unexpected hr %#lx.\n", hr); + + hr = IXmlWriter_Flush(writer); + ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); + + CHECK_OUTPUT(stream, + "<a>&name;</a>"); + + IStream_Release(stream); + IXmlWriter_Release(writer); +} + static void test_WriteFullEndElement(void) { IXmlWriter *writer; @@ -3069,4 +3143,5 @@ START_TEST(writer) test_WriteNode(); test_WriteNodeShallow(); test_WriteName(); + test_WriteEntityRef(); } diff --git a/dlls/xmllite/writer.c b/dlls/xmllite/writer.c index f4d1a474232..5696e09782b 100644 --- a/dlls/xmllite/writer.c +++ b/dlls/xmllite/writer.c @@ -1471,25 +1471,48 @@ static HRESULT WINAPI xmlwriter_WriteEndElement(IXmlWriter *iface) return S_OK; }
-static HRESULT WINAPI xmlwriter_WriteEntityRef(IXmlWriter *iface, LPCWSTR pwszName) +static HRESULT WINAPI xmlwriter_WriteEntityRef(IXmlWriter *iface, LPCWSTR name) { - xmlwriter *This = impl_from_IXmlWriter(iface); + xmlwriter *writer = impl_from_IXmlWriter(iface); + const WCHAR *p = name;
- FIXME("%p %s\n", This, wine_dbgstr_w(pwszName)); + TRACE("%p %s\n", iface, wine_dbgstr_w(name));
- switch (This->state) + if (is_empty_string(name)) + return E_INVALIDARG; + + if (!is_namestartchar(*p++)) + return WC_E_NAMECHARACTER; + + while (*p) + { + if (!is_ncnamechar(*p)) + return WC_E_NAMECHARACTER; + p++; + } + + switch (writer->state) { case XmlWriterState_Initial: return E_UNEXPECTED; case XmlWriterState_InvalidEncoding: return MX_E_ENCODING; case XmlWriterState_DocClosed: + case XmlWriterState_Ready: + writer->state = XmlWriterState_DocClosed; return WR_E_INVALIDACTION; + case XmlWriterState_ElemStarted: + writer_close_starttag(writer); + break; default: ; }
- return E_NOTIMPL; + write_output_buffer_char(writer->output, '&'); + write_output_buffer(writer->output, name, -1); + write_output_buffer_char(writer->output, ';'); + + return S_OK; }
static HRESULT WINAPI xmlwriter_WriteFullEndElement(IXmlWriter *iface)