diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2023-11-24 18:16:11 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2023-11-24 17:51:34 +0100 |
commit | b09cf69f7afa6946a9a4dde6bfa130d382ab1f81 (patch) | |
tree | 46ddb38c9be558b2b02be9186d5da54b9d44a7c5 /svtools | |
parent | cb70626ccb63e09f3a4130fd115a29ac79d823f2 (diff) |
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>
Diffstat (limited to 'svtools')
-rw-r--r-- | svtools/qa/unit/testHtmlWriter.cxx | 6 | ||||
-rw-r--r-- | svtools/source/svhtml/HtmlWriter.cxx | 25 |
2 files changed, 17 insertions, 14 deletions
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("<abc>hello</abc>"_ostr, aString); + CPPUNIT_ASSERT_EQUAL("<abc>hello</abc> <abc>world</abc>"_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: */ |