summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2021-05-31 21:14:41 +0900
committerMiklos Vajna <vmiklos@collabora.com>2022-06-27 14:29:07 +0200
commit12248c17f2ec0e61a85b1b2fc1715ffb2868eba9 (patch)
treee20d712234b1bbab7923f495c77cac8d043cfa03
parenteb0cd13590ae93410d831e689922c263ea75face (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. (cherry picked from commit 685c8db519b022f879cf575ec763d00de98906a3) Conflicts: include/svtools/HtmlWriter.hxx svtools/source/svhtml/HtmlWriter.cxx Change-Id: Ib00a4a37354cd1cb1781ba729ed4046a966e1eb1
-rw-r--r--include/svtools/HtmlWriter.hxx4
-rw-r--r--svtools/qa/unit/testHtmlWriter.cxx17
-rw-r--r--svtools/source/svhtml/HtmlWriter.cxx29
3 files changed, 45 insertions, 5 deletions
diff --git a/include/svtools/HtmlWriter.hxx b/include/svtools/HtmlWriter.hxx
index 49f2aa6c02f2..007668b6018f 100644
--- a/include/svtools/HtmlWriter.hxx
+++ b/include/svtools/HtmlWriter.hxx
@@ -39,10 +39,14 @@ public:
void start(const OString& aElement);
+ bool end(const OString& aElement);
void end();
void flushStack();
+ static void writeAttribute(SvStream& rStream, const OString& aAttribute, sal_Int32 aValue);
+ static void writeAttribute(SvStream& rStream, const OString& aAttribute, const OString& aValue);
+
void attribute(const OString& aAttribute, const char* aValue);
void attribute(const OString& aAttribute, sal_Int32 aValue);
void attribute(const OString& aAttribute, const OString& aValue);
diff --git a/svtools/qa/unit/testHtmlWriter.cxx b/svtools/qa/unit/testHtmlWriter.cxx
index 773da64c4891..70045ba8a50b 100644
--- a/svtools/qa/unit/testHtmlWriter.cxx
+++ b/svtools/qa/unit/testHtmlWriter.cxx
@@ -183,6 +183,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 7414b40b15b5..875da0a724ca 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, const OString& 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, const OString& aAttribute, sal_Int32 aValue)
+{
+ writeAttribute(rStream, aAttribute, OString::number(aValue));
+}
+
+void HtmlWriter::writeAttribute(SvStream& rStream, const OString& aAttribute, const OString& aValue)
+{
+ rStream.WriteOString(aAttribute);
+ rStream.WriteChar('=');
+ rStream.WriteChar('"');
+ rStream.WriteOString(aValue);
+ rStream.WriteChar('"');
+}
+
void HtmlWriter::attribute(const OString &aAttribute, const OString& aValue)
{
if (mbElementOpen && !aAttribute.isEmpty() && !aValue.isEmpty())
{
mrStream.WriteChar(' ');
- mrStream.WriteOString(aAttribute);
- mrStream.WriteChar('=');
- mrStream.WriteChar('"');
- mrStream.WriteOString(aValue);
- mrStream.WriteChar('"');
+ writeAttribute(mrStream, aAttribute, aValue);
}
}