1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 #include <cppunit/TestFixture.h>
12 #include <unotest/bootstrapfixturebase.hxx>
13 #include <svtools/parhtml.hxx>
14 #include <tools/ref.hxx>
15 #include <tools/stream.hxx>
19 /// Subclass of HTMLParser that can sense the import result.
20 class TestHTMLParser
: public HTMLParser
23 TestHTMLParser(SvStream
& rStream
);
24 virtual void NextToken(HtmlTokenId nToken
) override
;
25 /// Make this public for test purposes.
26 using HTMLParser::SetNamespace
;
29 int m_nLineBreakCount
= 0;
33 TestHTMLParser::TestHTMLParser(SvStream
& rStream
)
38 void TestHTMLParser::NextToken(HtmlTokenId nToken
)
40 if (nToken
== HtmlTokenId::TEXTTOKEN
)
41 m_aDocument
+= aToken
;
42 else if (nToken
== HtmlTokenId::LINEBREAK
)
44 else if (nToken
== HtmlTokenId::CDATA
)
49 class Test
: public CppUnit::TestFixture
53 CPPUNIT_TEST_FIXTURE(Test
, testTdf114428
)
55 SvMemoryStream aStream
;
56 OString
aDocument("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<html>hello</html>"_ostr
);
57 aStream
.WriteBytes(aDocument
.getStr(), aDocument
.getLength());
60 tools::SvRef
<TestHTMLParser
> xParser
= new TestHTMLParser(aStream
);
61 xParser
->CallParser();
63 // This was '<?xml version="1.0" encoding="utf-8"?> hello', XML declaration
65 CPPUNIT_ASSERT_EQUAL(u
"hello"_ustr
, xParser
->m_aDocument
.trim());
68 CPPUNIT_TEST_FIXTURE(Test
, testLineBreak
)
70 SvMemoryStream aStream
;
71 OString
aDocument("aaa<br></br>bbb"_ostr
);
72 aStream
.WriteBytes(aDocument
.getStr(), aDocument
.getLength());
75 tools::SvRef
<TestHTMLParser
> xParser
= new TestHTMLParser(aStream
);
76 xParser
->SetNamespace(u
"reqif-xhtml");
77 xParser
->CallParser();
79 // This was 2, <br></br> was interpreted as 2 line breaks in XHTML mode.
80 CPPUNIT_ASSERT_EQUAL(1, xParser
->m_nLineBreakCount
);
83 CPPUNIT_TEST_FIXTURE(Test
, testCdata
)
85 // Given a document with CDATA:
86 SvMemoryStream aStream
;
87 OString
aDocument("A<![CDATA[B ü <]]>C"_ostr
);
88 aStream
.WriteBytes(aDocument
.getStr(), aDocument
.getLength());
91 // When parsing that HTML:
92 tools::SvRef
<TestHTMLParser
> xParser
= new TestHTMLParser(aStream
);
93 xParser
->CallParser();
95 // Then make sure that we get a cdata token with the correct content:
96 // Without the accompanying fix in place, this test would have failed with:
97 // - Expected: B ü <
99 // i.e. the content inside CDATA was lost.
100 CPPUNIT_ASSERT_EQUAL(u
"B ü <"_ustr
, xParser
->m_aCdata
);
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */