Fix HtmlWriter::characters

This method may be called after an end(), and then it shouldn't try
to close a tag again. Also in this case the characters shouldn't be
considered in the next start()/characters()/end() run.

Clarify the meaning of the previous mbElementOpen, by renaming it
to mbOpeningTagOpen. The old mbCharactersWritten flag was redundant.

Change-Id: Ie0ac6ddd88be774853a1fc152742b51793af798b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159932
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski 2023-11-24 18:16:11 +03:00
parent cb70626ccb
commit b09cf69f7a
3 changed files with 18 additions and 16 deletions

View File

@ -24,8 +24,7 @@ private:
SvStream& mrStream; SvStream& mrStream;
bool mbElementOpen; bool mbOpeningTagOpen = false;
bool mbCharactersWritten;
bool mbPrettyPrint; bool mbPrettyPrint;
/// XML namespace, in case of XHTML. /// XML namespace, in case of XHTML.
OString maNamespace; OString maNamespace;

View File

@ -175,10 +175,14 @@ CPPUNIT_TEST_FIXTURE(Test, testCharacters)
aHtml.start("abc"_ostr); aHtml.start("abc"_ostr);
aHtml.characters("hello"); aHtml.characters("hello");
aHtml.end(); 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); OString aString = extractFromStream(aStream);
CPPUNIT_ASSERT_EQUAL("<abc>hello</abc>"_ostr, aString); CPPUNIT_ASSERT_EQUAL("<abc>hello</abc> <abc>world</abc>"_ostr, aString);
} }
CPPUNIT_TEST_FIXTURE(Test, testExactElementEnd) CPPUNIT_TEST_FIXTURE(Test, testExactElementEnd)

View File

@ -15,8 +15,6 @@
HtmlWriter::HtmlWriter(SvStream& rStream, std::string_view rNamespace) : HtmlWriter::HtmlWriter(SvStream& rStream, std::string_view rNamespace) :
mrStream(rStream), mrStream(rStream),
mbElementOpen(false),
mbCharactersWritten(false),
mbPrettyPrint(true) mbPrettyPrint(true)
{ {
if (!rNamespace.empty()) if (!rNamespace.empty())
@ -36,7 +34,7 @@ void HtmlWriter::prettyPrint(bool b)
void HtmlWriter::start(const OString& aElement) void HtmlWriter::start(const OString& aElement)
{ {
if (mbElementOpen) if (mbOpeningTagOpen)
{ {
mrStream.WriteChar('>'); mrStream.WriteChar('>');
if (mbPrettyPrint) if (mbPrettyPrint)
@ -54,7 +52,7 @@ void HtmlWriter::start(const OString& aElement)
mrStream.WriteChar('<'); mrStream.WriteChar('<');
mrStream.WriteOString(Concat2View(maNamespace + aElement)); mrStream.WriteOString(Concat2View(maNamespace + aElement));
mbElementOpen = true; mbOpeningTagOpen = true;
} }
void HtmlWriter::single(const OString &aContent) void HtmlWriter::single(const OString &aContent)
@ -65,12 +63,12 @@ void HtmlWriter::single(const OString &aContent)
void HtmlWriter::endAttribute() void HtmlWriter::endAttribute()
{ {
if (mbElementOpen) if (mbOpeningTagOpen)
{ {
mrStream.WriteOString("/>"); mrStream.WriteOString("/>");
if (mbPrettyPrint) if (mbPrettyPrint)
mrStream.WriteOString("\n"); mrStream.WriteOString("\n");
mbElementOpen = false; mbOpeningTagOpen = false;
} }
} }
@ -92,11 +90,12 @@ bool HtmlWriter::end(const OString& aElement)
void HtmlWriter::end() void HtmlWriter::end()
{ {
if (mbElementOpen && !mbCharactersWritten) if (mbOpeningTagOpen)
{ {
mrStream.WriteOString("/>"); mrStream.WriteOString("/>");
if (mbPrettyPrint) if (mbPrettyPrint)
mrStream.WriteOString("\n"); mrStream.WriteOString("\n");
mbOpeningTagOpen = false;
} }
else else
{ {
@ -114,8 +113,6 @@ void HtmlWriter::end()
mrStream.WriteOString("\n"); mrStream.WriteOString("\n");
} }
maElementStack.pop_back(); maElementStack.pop_back();
mbElementOpen = false;
mbCharactersWritten = false;
} }
void HtmlWriter::writeAttribute(SvStream& rStream, std::string_view aAttribute, sal_Int32 aValue) 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) 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(' '); mrStream.WriteChar(' ');
writeAttribute(mrStream, aAttribute, aValue); 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) void HtmlWriter::attribute(std::string_view aAttribute)
{ {
if (mbElementOpen && !aAttribute.empty()) if (mbOpeningTagOpen && !aAttribute.empty())
{ {
mrStream.WriteChar(' '); mrStream.WriteChar(' ');
mrStream.WriteOString(aAttribute); mrStream.WriteOString(aAttribute);
@ -167,10 +164,12 @@ void HtmlWriter::attribute(std::string_view aAttribute)
void HtmlWriter::characters(std::string_view rChars) void HtmlWriter::characters(std::string_view rChars)
{ {
if (!mbCharactersWritten) if (mbOpeningTagOpen)
{
mrStream.WriteOString(">"); mrStream.WriteOString(">");
mbOpeningTagOpen = false;
}
mrStream.WriteOString(rChars); mrStream.WriteOString(rChars);
mbCharactersWritten = true;
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */