[Libreoffice-commits] core.git: include/svtools svtools/qa svtools/source
Tomaž Vajngerl (via logerrit)
logerrit at kemper.freedesktop.org
Thu Jun 3 21:05:52 UTC 2021
include/svtools/HtmlWriter.hxx | 5 +++++
svtools/qa/unit/testHtmlWriter.cxx | 17 +++++++++++++++++
svtools/source/svhtml/HtmlWriter.cxx | 29 ++++++++++++++++++++++++-----
3 files changed, 46 insertions(+), 5 deletions(-)
New commits:
commit 685c8db519b022f879cf575ec763d00de98906a3
Author: Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Mon May 31 21:14:41 2021 +0900
Commit: Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Thu Jun 3 23:05:05 2021 +0200
HtmlWriter: add end tag checking and helper for attribute writing
Added an input variable to end to enter the name of which variable
is ending. This is to check that the expected end tag is actually
written.
Added writeAttribute, which is independent from HtmlWriter and just
writes the HTML attribute format to the stream.
Change-Id: Ib00a4a37354cd1cb1781ba729ed4046a966e1eb1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116629
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>
diff --git a/include/svtools/HtmlWriter.hxx b/include/svtools/HtmlWriter.hxx
index 1821336fb678..21ccad0cd96d 100644
--- a/include/svtools/HtmlWriter.hxx
+++ b/include/svtools/HtmlWriter.hxx
@@ -38,10 +38,15 @@ public:
void start(const OString& aElement);
+ bool end(const OString& aElement);
void end();
void flushStack();
+ static void writeAttribute(SvStream& rStream, std::string_view aAttribute, sal_Int32 aValue);
+ static void writeAttribute(SvStream& rStream, std::string_view aAttribute,
+ std::string_view aValue);
+
void attribute(std::string_view aAttribute, const char* aValue);
void attribute(std::string_view aAttribute, sal_Int32 aValue);
void attribute(std::string_view aAttribute, std::string_view aValue);
diff --git a/svtools/qa/unit/testHtmlWriter.cxx b/svtools/qa/unit/testHtmlWriter.cxx
index 7154b770a682..7016b77257bb 100644
--- a/svtools/qa/unit/testHtmlWriter.cxx
+++ b/svtools/qa/unit/testHtmlWriter.cxx
@@ -181,6 +181,23 @@ CPPUNIT_TEST_FIXTURE(Test, testCharacters)
CPPUNIT_ASSERT_EQUAL(OString("<abc>hello</abc>"), aString);
}
+CPPUNIT_TEST_FIXTURE(Test, testExactElementEnd)
+{
+ SvMemoryStream aStream;
+
+ HtmlWriter aHtml(aStream);
+ aHtml.prettyPrint(false);
+ aHtml.start("start");
+ aHtml.start("a");
+ CPPUNIT_ASSERT(aHtml.end("a"));
+ aHtml.start("b");
+ CPPUNIT_ASSERT(!aHtml.end("c"));
+ CPPUNIT_ASSERT(aHtml.end("start"));
+
+ OString aString = extractFromStream(aStream);
+ CPPUNIT_ASSERT_EQUAL(OString("<start><a/><b/></start>"), aString);
+}
+
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svtools/source/svhtml/HtmlWriter.cxx b/svtools/source/svhtml/HtmlWriter.cxx
index a4d1b0e3b5ea..8cd7dc8705b4 100644
--- a/svtools/source/svhtml/HtmlWriter.cxx
+++ b/svtools/source/svhtml/HtmlWriter.cxx
@@ -10,6 +10,7 @@
#include <svtools/HtmlWriter.hxx>
#include <tools/stream.hxx>
+#include <sal/log.hxx>
HtmlWriter::HtmlWriter(SvStream& rStream, std::string_view rNamespace) :
mrStream(rStream),
@@ -80,6 +81,14 @@ void HtmlWriter::flushStack()
}
}
+bool HtmlWriter::end(const OString& aElement)
+{
+ bool bExpected = maElementStack.back() == aElement;
+ SAL_WARN_IF(!bExpected, "svtools", "HtmlWriter: end element mismatch - '" << aElement << "' expected '" << maElementStack.back() << "'");
+ end();
+ return bExpected;
+}
+
void HtmlWriter::end()
{
if (mbElementOpen && !mbCharactersWritten)
@@ -108,16 +117,26 @@ void HtmlWriter::end()
mbCharactersWritten = false;
}
+void HtmlWriter::writeAttribute(SvStream& rStream, std::string_view aAttribute, sal_Int32 aValue)
+{
+ writeAttribute(rStream, aAttribute, OString::number(aValue));
+}
+
+void HtmlWriter::writeAttribute(SvStream& rStream, std::string_view aAttribute, std::string_view aValue)
+{
+ rStream.WriteOString(aAttribute);
+ rStream.WriteChar('=');
+ rStream.WriteChar('"');
+ rStream.WriteOString(aValue);
+ rStream.WriteChar('"');
+}
+
void HtmlWriter::attribute(std::string_view aAttribute, std::string_view aValue)
{
if (mbElementOpen && !aAttribute.empty() && !aValue.empty())
{
mrStream.WriteChar(' ');
- mrStream.WriteOString(aAttribute);
- mrStream.WriteChar('=');
- mrStream.WriteChar('"');
- mrStream.WriteOString(aValue);
- mrStream.WriteChar('"');
+ writeAttribute(mrStream, aAttribute, aValue);
}
}
More information about the Libreoffice-commits
mailing list