EPUB export: fix double escaped NBSP

The EPUB package interface already XML-escapes characters, avoid a double
escape.

And once that works, handle NBSP/tabs.

Change-Id: I8b7bbdc2592096bdd46fbdb29b48b723ef5cf990
Reviewed-on: https://gerrit.libreoffice.org/42098
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
diff --git a/external/libepubgen/libepubgen-epub3.patch.1 b/external/libepubgen/libepubgen-epub3.patch.1
index 0415bf3..8e62660 100644
--- a/external/libepubgen/libepubgen-epub3.patch.1
+++ b/external/libepubgen/libepubgen-epub3.patch.1
@@ -1906,3 +1906,32 @@ index 1661064..3340643 100644
-- 
2.12.3

From bce7c05a18a4c5089d5ac77bc61b9f6978e7224b Mon Sep 17 00:00:00 2001
From: Miklos Vajna <vmiklos@collabora.co.uk>
Date: Fri, 8 Sep 2017 11:21:32 +0200
Subject: [PATCH] EPUBHTMLGenerator: write un-escaped NBSP

Package implementations are supposed to take care of escaping, like it
was already a requirement for normal text.
---
 src/lib/EPUBHTMLGenerator.cpp      |  3 ++-
 src/test/EPUBTextGeneratorTest.cpp | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/lib/EPUBHTMLGenerator.cpp b/src/lib/EPUBHTMLGenerator.cpp
index aa09332..ed968bf 100644
--- a/src/lib/EPUBHTMLGenerator.cpp
+++ b/src/lib/EPUBHTMLGenerator.cpp
@@ -681,7 +681,8 @@ void EPUBHTMLGenerator::insertSpace()
 {
   if (m_impl->m_ignore)
     return;
-  m_impl->output().insertCharacters("&nbsp;");
+  // NBSP.
+  m_impl->output().insertCharacters("\xc2\xa0");
 }
 
 void EPUBHTMLGenerator::openOrderedListLevel(const RVNGPropertyList &propList)
-- 
2.12.3

diff --git a/writerperfect/qa/unit/EPUBExportTest.cxx b/writerperfect/qa/unit/EPUBExportTest.cxx
index 0d86973..10fd5eb 100644
--- a/writerperfect/qa/unit/EPUBExportTest.cxx
+++ b/writerperfect/qa/unit/EPUBExportTest.cxx
@@ -63,6 +63,7 @@ public:
    void testNamedStyleInheritance();
    void testNestedSpan();
    void testLineBreak();
    void testEscape();

    CPPUNIT_TEST_SUITE(EPUBExportTest);
    CPPUNIT_TEST(testOutlineLevel);
@@ -77,6 +78,7 @@ public:
    CPPUNIT_TEST(testNamedStyleInheritance);
    CPPUNIT_TEST(testNestedSpan);
    CPPUNIT_TEST(testLineBreak);
    CPPUNIT_TEST(testEscape);
    CPPUNIT_TEST_SUITE_END();
};

@@ -330,6 +332,19 @@ void EPUBExportTest::testLineBreak()
    assertXPath(mpXmlDoc, "//xhtml:p[2]/xhtml:br", 1);
}

void EPUBExportTest::testEscape()
{
    createDoc("escape.fodt", {});

    mpXmlDoc = parseExport("OEBPS/sections/section0001.xhtml");
    // This was lost.
    assertXPathContent(mpXmlDoc, "//xhtml:p[1]/xhtml:span[1]", OUString::fromUtf8("\xc2\xa0"));
    // Make sure escaping happens only once.
    assertXPathContent(mpXmlDoc, "//xhtml:p[1]/xhtml:span[2]", "a&b");
    // This was also lost.
    assertXPathContent(mpXmlDoc, "//xhtml:p[1]/xhtml:span[3]", "\t");
}

CPPUNIT_TEST_SUITE_REGISTRATION(EPUBExportTest);

}
diff --git a/writerperfect/qa/unit/data/writer/epubexport/escape.fodt b/writerperfect/qa/unit/data/writer/epubexport/escape.fodt
new file mode 100644
index 0000000..8d23fb3
--- /dev/null
+++ b/writerperfect/qa/unit/data/writer/epubexport/escape.fodt
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:officeooo="http://openoffice.org/2009/office" office:version="1.2" office:mimetype="application/vnd.oasis.opendocument.text">
  <office:automatic-styles>
    <style:style style:name="T1" style:family="text">
      <style:text-properties officeooo:rsid="0006b966"/>
    </style:style>
    <style:style style:name="T2" style:family="text">
      <style:text-properties fo:font-weight="bold" officeooo:rsid="0006b966" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
    </style:style>
  </office:automatic-styles>
  <office:body>
    <office:text>
      <text:p><text:s/><text:span text:style-name="T1">a&amp;b</text:span><text:span text:style-name="T2"><text:tab/></text:span></text:p>
    </office:text>
  </office:body>
</office:document>
diff --git a/writerperfect/source/writer/exp/txtparai.cxx b/writerperfect/source/writer/exp/txtparai.cxx
index b085537..e660a34 100644
--- a/writerperfect/source/writer/exp/txtparai.cxx
+++ b/writerperfect/source/writer/exp/txtparai.cxx
@@ -152,6 +152,48 @@ void XMLLineBreakContext::startElement(const OUString &/*rName*/, const css::uno
    mrImport.GetGenerator().insertLineBreak();
}

/// Handler for <text:s>.
class XMLSpaceContext : public XMLImportContext
{
public:
    XMLSpaceContext(XMLImport &rImport);

    void SAL_CALL startElement(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &xAttribs) override;
};

XMLSpaceContext::XMLSpaceContext(XMLImport &rImport)
    : XMLImportContext(rImport)
{
}

void XMLSpaceContext::startElement(const OUString &/*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/)
{
    mrImport.GetGenerator().openSpan(librevenge::RVNGPropertyList());
    mrImport.GetGenerator().insertSpace();
    mrImport.GetGenerator().closeSpan();
}

/// Handler for <text:tab>.
class XMLTabContext : public XMLImportContext
{
public:
    XMLTabContext(XMLImport &rImport);

    void SAL_CALL startElement(const OUString &rName, const css::uno::Reference<css::xml::sax::XAttributeList> &xAttribs) override;
};

XMLTabContext::XMLTabContext(XMLImport &rImport)
    : XMLImportContext(rImport)
{
}

void XMLTabContext::startElement(const OUString &/*rName*/, const css::uno::Reference<css::xml::sax::XAttributeList> &/*xAttribs*/)
{
    mrImport.GetGenerator().openSpan(librevenge::RVNGPropertyList());
    mrImport.GetGenerator().insertTab();
    mrImport.GetGenerator().closeSpan();
}

/// Handler for <text:a>.
class XMLHyperlinkContext : public XMLImportContext
{
@@ -255,6 +297,10 @@ XMLImportContext *CreateChildContext(XMLImport &rImport, const OUString &rName)
{
    if (rName == "text:line-break")
        return new XMLLineBreakContext(rImport);
    if (rName == "text:s")
        return new XMLSpaceContext(rImport);
    if (rName == "text:tab")
        return new XMLTabContext(rImport);
    return nullptr;
}