diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2021-05-31 21:14:41 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2021-06-03 23:05:05 +0200 |
commit | 685c8db519b022f879cf575ec763d00de98906a3 (patch) | |
tree | 6bbc64ed7c19c4fed31212c82341ea03153b8b37 /svtools | |
parent | 774be6d793203183fe1856ffb8b720f00b48c2bb (diff) |
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@gmail.com>
Diffstat (limited to 'svtools')
-rw-r--r-- | svtools/qa/unit/testHtmlWriter.cxx | 17 | ||||
-rw-r--r-- | svtools/source/svhtml/HtmlWriter.cxx | 29 |
2 files changed, 41 insertions, 5 deletions
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); } } |