Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / svtools / qa / unit / testHtmlReader.cxx
blobd50da0d3cd9786e3897a02b6772b341759d659b3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
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>
17 namespace
19 /// Subclass of HTMLParser that can sense the import result.
20 class TestHTMLParser : public HTMLParser
22 public:
23 TestHTMLParser(SvStream& rStream);
24 virtual void NextToken(HtmlTokenId nToken) override;
25 /// Make this public for test purposes.
26 using HTMLParser::SetNamespace;
28 OUString m_aDocument;
29 int m_nLineBreakCount = 0;
30 OUString m_aCdata;
33 TestHTMLParser::TestHTMLParser(SvStream& rStream)
34 : HTMLParser(rStream)
38 void TestHTMLParser::NextToken(HtmlTokenId nToken)
40 if (nToken == HtmlTokenId::TEXTTOKEN)
41 m_aDocument += aToken;
42 else if (nToken == HtmlTokenId::LINEBREAK)
43 ++m_nLineBreakCount;
44 else if (nToken == HtmlTokenId::CDATA)
45 m_aCdata = aToken;
48 /// Tests HTMLParser.
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());
58 aStream.Seek(0);
60 tools::SvRef<TestHTMLParser> xParser = new TestHTMLParser(aStream);
61 xParser->CallParser();
63 // This was '<?xml version="1.0" encoding="utf-8"?> hello', XML declaration
64 // was not ignored.
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());
73 aStream.Seek(0);
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 &uuml; &lt;]]>C"_ostr);
88 aStream.WriteBytes(aDocument.getStr(), aDocument.getLength());
89 aStream.Seek(0);
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 &uuml; &lt;
98 // - Actual :
99 // i.e. the content inside CDATA was lost.
100 CPPUNIT_ASSERT_EQUAL(u"B &uuml; &lt;"_ustr, xParser->m_aCdata);
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */