diff --git a/include/svtools/HtmlWriter.hxx b/include/svtools/HtmlWriter.hxx
index 21ccad0cd96d..9fe66a7d9ad2 100644
--- a/include/svtools/HtmlWriter.hxx
+++ b/include/svtools/HtmlWriter.hxx
@@ -24,8 +24,7 @@ private:
SvStream& mrStream;
- bool mbElementOpen;
- bool mbCharactersWritten;
+ bool mbOpeningTagOpen = false;
bool mbPrettyPrint;
/// XML namespace, in case of XHTML.
OString maNamespace;
diff --git a/svtools/qa/unit/testHtmlWriter.cxx b/svtools/qa/unit/testHtmlWriter.cxx
index 19e5b6078947..3e7e10d34c12 100644
--- a/svtools/qa/unit/testHtmlWriter.cxx
+++ b/svtools/qa/unit/testHtmlWriter.cxx
@@ -175,10 +175,14 @@ CPPUNIT_TEST_FIXTURE(Test, testCharacters)
aHtml.start("abc"_ostr);
aHtml.characters("hello");
aHtml.end();
+ aHtml.characters(" "); // Should not try to close a not opened tag
+ aHtml.start("abc"_ostr);
+ aHtml.characters("world"); // Should close opening tag
+ aHtml.end();
OString aString = extractFromStream(aStream);
- CPPUNIT_ASSERT_EQUAL("hello"_ostr, aString);
+ CPPUNIT_ASSERT_EQUAL("hello world"_ostr, aString);
}
CPPUNIT_TEST_FIXTURE(Test, testExactElementEnd)
diff --git a/svtools/source/svhtml/HtmlWriter.cxx b/svtools/source/svhtml/HtmlWriter.cxx
index 3409461d363c..28a86bbce12a 100644
--- a/svtools/source/svhtml/HtmlWriter.cxx
+++ b/svtools/source/svhtml/HtmlWriter.cxx
@@ -15,8 +15,6 @@
HtmlWriter::HtmlWriter(SvStream& rStream, std::string_view rNamespace) :
mrStream(rStream),
- mbElementOpen(false),
- mbCharactersWritten(false),
mbPrettyPrint(true)
{
if (!rNamespace.empty())
@@ -36,7 +34,7 @@ void HtmlWriter::prettyPrint(bool b)
void HtmlWriter::start(const OString& aElement)
{
- if (mbElementOpen)
+ if (mbOpeningTagOpen)
{
mrStream.WriteChar('>');
if (mbPrettyPrint)
@@ -54,7 +52,7 @@ void HtmlWriter::start(const OString& aElement)
mrStream.WriteChar('<');
mrStream.WriteOString(Concat2View(maNamespace + aElement));
- mbElementOpen = true;
+ mbOpeningTagOpen = true;
}
void HtmlWriter::single(const OString &aContent)
@@ -65,12 +63,12 @@ void HtmlWriter::single(const OString &aContent)
void HtmlWriter::endAttribute()
{
- if (mbElementOpen)
+ if (mbOpeningTagOpen)
{
mrStream.WriteOString("/>");
if (mbPrettyPrint)
mrStream.WriteOString("\n");
- mbElementOpen = false;
+ mbOpeningTagOpen = false;
}
}
@@ -92,11 +90,12 @@ bool HtmlWriter::end(const OString& aElement)
void HtmlWriter::end()
{
- if (mbElementOpen && !mbCharactersWritten)
+ if (mbOpeningTagOpen)
{
mrStream.WriteOString("/>");
if (mbPrettyPrint)
mrStream.WriteOString("\n");
+ mbOpeningTagOpen = false;
}
else
{
@@ -114,8 +113,6 @@ void HtmlWriter::end()
mrStream.WriteOString("\n");
}
maElementStack.pop_back();
- mbElementOpen = false;
- mbCharactersWritten = false;
}
void HtmlWriter::writeAttribute(SvStream& rStream, std::string_view aAttribute, sal_Int32 aValue)
@@ -134,7 +131,7 @@ void HtmlWriter::writeAttribute(SvStream& rStream, std::string_view aAttribute,
void HtmlWriter::attribute(std::string_view aAttribute, std::string_view aValue)
{
- if (mbElementOpen && !aAttribute.empty() && !aValue.empty())
+ if (mbOpeningTagOpen && !aAttribute.empty() && !aValue.empty())
{
mrStream.WriteChar(' ');
writeAttribute(mrStream, aAttribute, aValue);
@@ -158,7 +155,7 @@ void HtmlWriter::attribute(std::string_view aAttribute, std::u16string_view aVal
void HtmlWriter::attribute(std::string_view aAttribute)
{
- if (mbElementOpen && !aAttribute.empty())
+ if (mbOpeningTagOpen && !aAttribute.empty())
{
mrStream.WriteChar(' ');
mrStream.WriteOString(aAttribute);
@@ -167,10 +164,12 @@ void HtmlWriter::attribute(std::string_view aAttribute)
void HtmlWriter::characters(std::string_view rChars)
{
- if (!mbCharactersWritten)
+ if (mbOpeningTagOpen)
+ {
mrStream.WriteOString(">");
+ mbOpeningTagOpen = false;
+ }
mrStream.WriteOString(rChars);
- mbCharactersWritten = true;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */