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:
parent
cb70626ccb
commit
b09cf69f7a
@ -24,8 +24,7 @@ private:
|
||||
|
||||
SvStream& mrStream;
|
||||
|
||||
bool mbElementOpen;
|
||||
bool mbCharactersWritten;
|
||||
bool mbOpeningTagOpen = false;
|
||||
bool mbPrettyPrint;
|
||||
/// XML namespace, in case of XHTML.
|
||||
OString maNamespace;
|
||||
|
@ -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("<abc>hello</abc>"_ostr, aString);
|
||||
CPPUNIT_ASSERT_EQUAL("<abc>hello</abc> <abc>world</abc>"_ostr, aString);
|
||||
}
|
||||
|
||||
CPPUNIT_TEST_FIXTURE(Test, testExactElementEnd)
|
||||
|
@ -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: */
|
||||
|
Loading…
x
Reference in New Issue
Block a user