summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.co.uk>2018-03-08 15:56:08 +0100
committerMiklos Vajna <vmiklos@collabora.co.uk>2018-03-08 18:53:53 +0100
commita8ca72cbcd2c6ea0baa5e3569224c2f08d358f3e (patch)
treeca322bef8cf508041f7f83791df8528d6c692f41
parentad628045f62b0656c58d2e195e33186c055c2196 (diff)
svtools: allow writing characters with HtmlWriter
The sw HTML image export already uses HtmlWriter, so need to extend this to support XHTML alternate text, writing the character data directly is not an option. Change-Id: Iac3f2b9a3828175a5f1d0fb14ab505f8de389e52 Reviewed-on: https://gerrit.libreoffice.org/50965 Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk> Tested-by: Jenkins <ci@libreoffice.org>
-rw-r--r--include/svtools/HtmlWriter.hxx4
-rw-r--r--svtools/qa/unit/testHtmlWriter.cxx17
-rw-r--r--svtools/source/svhtml/HtmlWriter.cxx11
3 files changed, 31 insertions, 1 deletions
diff --git a/include/svtools/HtmlWriter.hxx b/include/svtools/HtmlWriter.hxx
index a842df4870a9..25030f6dffe8 100644
--- a/include/svtools/HtmlWriter.hxx
+++ b/include/svtools/HtmlWriter.hxx
@@ -27,6 +27,7 @@ private:
bool mbElementOpen;
bool mbContentWritten;
+ bool mbCharactersWritten;
bool mbPrettyPrint;
/// XML namespace, in case of XHTML.
OString maNamespace;
@@ -52,6 +53,9 @@ public:
void single(const OString& aContent);
void endAttribute();
+
+ /// Writes character data.
+ void characters(const OString& rChars);
};
#endif
diff --git a/svtools/qa/unit/testHtmlWriter.cxx b/svtools/qa/unit/testHtmlWriter.cxx
index bdb5c5814fd7..18cb409ff37b 100644
--- a/svtools/qa/unit/testHtmlWriter.cxx
+++ b/svtools/qa/unit/testHtmlWriter.cxx
@@ -41,6 +41,7 @@ public:
void testNested();
void testNamespace();
void testAttributeValues();
+ void testCharacters();
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testSingleElement);
@@ -50,6 +51,7 @@ public:
CPPUNIT_TEST(testNested);
CPPUNIT_TEST(testNamespace);
CPPUNIT_TEST(testAttributeValues);
+ CPPUNIT_TEST(testCharacters);
CPPUNIT_TEST_SUITE_END();
};
@@ -190,6 +192,21 @@ void Test::testAttributeValues()
CPPUNIT_ASSERT_EQUAL(OString("<abc one=\"one\" two=\"two\" three=\"12\"/>"), aString);
}
+void Test::testCharacters()
+{
+ SvMemoryStream aStream;
+
+ HtmlWriter aHtml(aStream);
+ aHtml.prettyPrint(false);
+ aHtml.start("abc");
+ aHtml.characters("hello");
+ aHtml.end();
+
+ OString aString = extractFromStream(aStream);
+
+ CPPUNIT_ASSERT_EQUAL(OString("<abc>hello</abc>"), aString);
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/svtools/source/svhtml/HtmlWriter.cxx b/svtools/source/svhtml/HtmlWriter.cxx
index 499aa7ed8ab0..631a56e3e1f1 100644
--- a/svtools/source/svhtml/HtmlWriter.cxx
+++ b/svtools/source/svhtml/HtmlWriter.cxx
@@ -15,6 +15,7 @@ HtmlWriter::HtmlWriter(SvStream& rStream, const OString& rNamespace) :
mrStream(rStream),
mbElementOpen(false),
mbContentWritten(false),
+ mbCharactersWritten(false),
mbPrettyPrint(true)
{
if (!rNamespace.isEmpty())
@@ -83,7 +84,7 @@ void HtmlWriter::flushStack()
void HtmlWriter::end()
{
- if (mbElementOpen)
+ if (mbElementOpen && !mbCharactersWritten)
{
mrStream.WriteCharPtr("/>");
if (mbPrettyPrint)
@@ -107,6 +108,7 @@ void HtmlWriter::end()
maElementStack.pop_back();
mbElementOpen = false;
mbContentWritten = false;
+ mbCharactersWritten = false;
}
void HtmlWriter::attribute(const OString &aAttribute, const OString& aValue)
@@ -146,5 +148,12 @@ void HtmlWriter::attribute(const OString& aAttribute)
}
}
+void HtmlWriter::characters(const OString& rChars)
+{
+ if (!mbCharactersWritten)
+ mrStream.WriteCharPtr(">");
+ mrStream.WriteOString(rChars);
+ mbCharactersWritten = true;
+}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */