From a8ca72cbcd2c6ea0baa5e3569224c2f08d358f3e Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 8 Mar 2018 15:56:08 +0100 Subject: 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 Tested-by: Jenkins --- include/svtools/HtmlWriter.hxx | 4 ++++ svtools/qa/unit/testHtmlWriter.cxx | 17 +++++++++++++++++ svtools/source/svhtml/HtmlWriter.cxx | 11 ++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) 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(""), 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("hello"), 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: */ -- cgit v1.2.3