Kaydet (Commit) 9df0ce08 authored tarafından Miklos Vajna's avatar Miklos Vajna

sw HTML export: allow custom XHTML namespace alias

This helps in case the HTML filter expected to produce an XHTML fragment that
has an explicit namespace alias for <http://www.w3.org/1999/xhtml>.

This only has an effect when HtmlWriter is used to write elements,
though.

Change-Id: I161caf4bb6bc6d0c21a7a67433b08bb060b447f1
Reviewed-on: https://gerrit.libreoffice.org/50448Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMiklos Vajna <vmiklos@collabora.co.uk>
üst 81260c44
......@@ -28,9 +28,11 @@ private:
bool mbElementOpen;
bool mbContentWritten;
bool mbPrettyPrint;
/// XML namespace, in case of XHTML.
OString maNamespace;
public:
HtmlWriter(SvStream& rStream);
HtmlWriter(SvStream& rStream, const OString& rNamespace = OString());
~HtmlWriter();
void prettyPrint(bool b);
......
......@@ -39,6 +39,7 @@ public:
void testSingleElementWithContent();
void testSingleElementWithContentAndAttributes();
void testNested();
void testNamespace();
void testAttributeValues();
CPPUNIT_TEST_SUITE(Test);
......@@ -47,6 +48,7 @@ public:
CPPUNIT_TEST(testSingleElementWithContent);
CPPUNIT_TEST(testSingleElementWithContentAndAttributes);
CPPUNIT_TEST(testNested);
CPPUNIT_TEST(testNamespace);
CPPUNIT_TEST(testAttributeValues);
CPPUNIT_TEST_SUITE_END();
......@@ -157,6 +159,20 @@ void Test::testNested()
CPPUNIT_ASSERT_EQUAL(OString("<abc><xyz/></abc>"), aString);
}
void Test::testNamespace()
{
SvMemoryStream aStream;
HtmlWriter aHtml(aStream, "reqif-xhtml");
aHtml.prettyPrint(false);
aHtml.single("br");
OString aString = extractFromStream(aStream);
// This was <br/>, namespace request was ignored.
CPPUNIT_ASSERT_EQUAL(OString("<reqif-xhtml:br/>"), aString);
}
void Test::testAttributeValues()
{
SvMemoryStream aStream;
......
......@@ -11,12 +11,18 @@
#include <svtools/HtmlWriter.hxx>
#include <tools/stream.hxx>
HtmlWriter::HtmlWriter(SvStream& rStream) :
HtmlWriter::HtmlWriter(SvStream& rStream, const OString& rNamespace) :
mrStream(rStream),
mbElementOpen(false),
mbContentWritten(false),
mbPrettyPrint(true)
{}
{
if (!rNamespace.isEmpty())
{
// Convert namespace alias to a prefix.
maNamespace = rNamespace + ":";
}
}
HtmlWriter::~HtmlWriter()
{}
......@@ -46,7 +52,7 @@ void HtmlWriter::start(const OString& aElement)
}
mrStream.WriteChar('<');
mrStream.WriteOString(aElement);
mrStream.WriteOString(maNamespace + aElement);
mbElementOpen = true;
}
......@@ -93,7 +99,7 @@ void HtmlWriter::end()
}
}
mrStream.WriteCharPtr("</");
mrStream.WriteOString(maElementStack.back());
mrStream.WriteOString(maNamespace + maElementStack.back());
mrStream.WriteCharPtr(">");
if (mbPrettyPrint)
mrStream.WriteCharPtr("\n");
......
......@@ -753,7 +753,7 @@ void OutHTML_SwFormat( Writer& rWrt, const SwFormat& rFormat,
// if necessary, start a new list item
if( rInfo.bInNumBulList && bNumbered )
{
HtmlWriter html(rWrt.Strm());
HtmlWriter html(rWrt.Strm(), rHWrt.maNamespace);
html.start(OOO_STRING_SVTOOLS_HTML_li);
if( USHRT_MAX != nNumStart )
html.attribute(OOO_STRING_SVTOOLS_HTML_O_value, OString::number(nNumStart));
......@@ -2017,7 +2017,7 @@ Writer& OutHTML_SwTextNode( Writer& rWrt, const SwContentNode& rNode )
rHTMLWrt.m_bLFPossible = true;
HtmlWriter aHtml(rWrt.Strm());
HtmlWriter aHtml(rWrt.Strm(), rHTMLWrt.maNamespace);
aHtml.start(OOO_STRING_SVTOOLS_HTML_horzrule);
const SfxItemSet* pItemSet = pNd->GetpSwAttrSet();
......@@ -2461,7 +2461,7 @@ Writer& OutHTML_SwTextNode( Writer& rWrt, const SwContentNode& rNode )
if( 0x0a == c )
{
HTMLOutFuncs::FlushToAscii( rWrt.Strm(), aContext );
HtmlWriter aHtml(rWrt.Strm());
HtmlWriter aHtml(rWrt.Strm(), rHTMLWrt.maNamespace);
aHtml.single(OOO_STRING_SVTOOLS_HTML_linebreak);
}
else if (c == CH_TXT_ATR_FORMELEMENT)
......@@ -2511,7 +2511,7 @@ Writer& OutHTML_SwTextNode( Writer& rWrt, const SwContentNode& rNode )
}
else
{
HtmlWriter aHtml(rHTMLWrt.Strm());
HtmlWriter aHtml(rHTMLWrt.Strm(), rHTMLWrt.maNamespace);
aHtml.single(OOO_STRING_SVTOOLS_HTML_linebreak);
const SvxULSpaceItem& rULSpace = pNd->GetSwAttrSet().Get(RES_UL_SPACE);
if (rULSpace.GetLower() > 0 &&
......@@ -2539,7 +2539,7 @@ Writer& OutHTML_SwTextNode( Writer& rWrt, const SwContentNode& rNode )
pString = OOO_STRING_SVTOOLS_HTML_AL_right;
}
HtmlWriter aHtml(rHTMLWrt.Strm());
HtmlWriter aHtml(rHTMLWrt.Strm(), rHTMLWrt.maNamespace);
aHtml.start(OOO_STRING_SVTOOLS_HTML_linebreak);
aHtml.attribute(OOO_STRING_SVTOOLS_HTML_O_clear, pString);
aHtml.end();
......
......@@ -1230,7 +1230,7 @@ Writer& OutHTML_Image( Writer& rWrt, const SwFrameFormat &rFrameFormat,
if( rHTMLWrt.m_bLFPossible )
rHTMLWrt.OutNewLine( true );
HtmlWriter aHtml(rWrt.Strm());
HtmlWriter aHtml(rWrt.Strm(), rHTMLWrt.maNamespace);
// <a name=...></a>...<img ...>
if( pMarkType && !rFrameFormat.GetName().isEmpty() )
......
......@@ -183,8 +183,16 @@ void SwHTMLWriter::SetupFilterOptions(SfxMedium& rMedium)
{
mbEmbedImages = true;
}
else if (sFilterOptions == "XHTML")
mbXHTML = true;
uno::Sequence<OUString> aOptionSeq = comphelper::string::convertCommaSeparated(sFilterOptions);
const OUString aXhtmlNsKey("xhtmlns=");
for (const auto& rOption : aOptionSeq)
{
if (rOption == "XHTML")
mbXHTML = true;
else if (rOption.startsWith(aXhtmlNsKey))
maNamespace = rOption.copy(aXhtmlNsKey.getLength()).toUtf8();
}
}
ErrCode SwHTMLWriter::WriteStream()
......
......@@ -386,6 +386,8 @@ public:
bool mbEmbedImages : 1;
/// If XHTML markup should be written instead of HTML.
bool mbXHTML = false;
/// XML namespace, in case of XHTML.
OString maNamespace;
#define sCSS2_P_CLASS_leaders "leaders"
bool m_bCfgPrintLayout : 1; // PrintLayout option for TOC dot leaders
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment