[Libreoffice-commits] core.git: include/svtools svtools/qa svtools/source

Miklos Vajna vmiklos at collabora.co.uk
Thu Mar 8 17:54:18 UTC 2018


 include/svtools/HtmlWriter.hxx       |    4 ++++
 svtools/qa/unit/testHtmlWriter.cxx   |   17 +++++++++++++++++
 svtools/source/svhtml/HtmlWriter.cxx |   11 ++++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

New commits:
commit a8ca72cbcd2c6ea0baa5e3569224c2f08d358f3e
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Thu Mar 8 15:56:08 2018 +0100

    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 at collabora.co.uk>
    Tested-by: Jenkins <ci at libreoffice.org>

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: */


More information about the Libreoffice-commits mailing list