From 7b99871635cd48c2a8a1d0afbd7afc60a45cc2ff Mon Sep 17 00:00:00 2001 From: Vojtěch Doležal Date: Mon, 27 Feb 2023 08:52:35 +0100 Subject: tdf#153396 - Bibliography marks improvements Adds option to separate function of "URL" into (listed) "URL" and "Target URL" to allow for more flexibility (in that case if target URL is empty, bibliography mark hyperlink leads to bibliography table row if possible) When writing tests also found and fixed bug where exporting new file with anchor link bibliography mark crashes LO. Change-Id: Ic1b5c8c9590c0338dcfc4fa3a981142bddae0113 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147868 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- sw/qa/core/text/text.cxx | 154 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) (limited to 'sw/qa/core/text/text.cxx') diff --git a/sw/qa/core/text/text.cxx b/sw/qa/core/text/text.cxx index d16e7b324656..7ace4df111c6 100644 --- a/sw/qa/core/text/text.cxx +++ b/sw/qa/core/text/text.cxx @@ -12,7 +12,9 @@ #include #include +#include #include +#include #include #include @@ -135,6 +137,158 @@ CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testBibliographyUrlPdfExport) CPPUNIT_ASSERT(pPdfPage->hasLinks()); } +CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testBibliographyUrlPdfExport2) +{ + // Given a document with a bibliography entry field: + std::shared_ptr pPDFium = vcl::pdf::PDFiumLibrary::get(); + if (!pPDFium) + { + return; + } + createSwDoc(); + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY); + uno::Sequence aFields = { + comphelper::makePropertyValue("BibiliographicType", text::BibliographyDataType::WWW), + comphelper::makePropertyValue("Identifier", OUString("AT")), + comphelper::makePropertyValue("Author", OUString("Author")), + comphelper::makePropertyValue("Title", OUString("Title")), + comphelper::makePropertyValue("URL", OUString("#page=1")), + }; + xField->setPropertyValue("Fields", uno::Any(aFields)); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + uno::Reference xContent(xField, uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); + + // When exporting to PDF: + save("writer_pdf_Export"); + + // Then make sure the field links when the Target URL is set + // (this test is important, isn't the same as the one above) + std::unique_ptr pPdfDocument = parsePDFExport(); + std::unique_ptr pPdfPage = pPdfDocument->openPage(/*nIndex=*/0); + CPPUNIT_ASSERT(pPdfPage->hasLinks()); +} + +CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testBibliographyUrlPdfExport3) +{ + // Given a document with a bibliography entry field: + std::shared_ptr pPDFium = vcl::pdf::PDFiumLibrary::get(); + if (!pPDFium) + { + return; + } + createSwDoc(); + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY); + uno::Sequence aFields = { + comphelper::makePropertyValue("BibiliographicType", text::BibliographyDataType::WWW), + comphelper::makePropertyValue("Identifier", OUString("AT")), + comphelper::makePropertyValue("Author", OUString("Author")), + comphelper::makePropertyValue("Title", OUString("Title")), + comphelper::makePropertyValue("TargetURL", OUString("#page=1")), + }; + xField->setPropertyValue("Fields", uno::Any(aFields)); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + uno::Reference xContent(xField, uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); + + // When exporting to PDF: + save("writer_pdf_Export"); + + // Then make sure there are no links since UseTargetURL is not set + std::unique_ptr pPdfDocument = parsePDFExport(); + std::unique_ptr pPdfPage = pPdfDocument->openPage(/*nIndex=*/0); + CPPUNIT_ASSERT(!pPdfPage->hasLinks()); +} + +CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testBibliographyUrlPdfExport4) +{ + // Given a document with a bibliography entry field: + std::shared_ptr pPDFium = vcl::pdf::PDFiumLibrary::get(); + if (!pPDFium) + { + return; + } + createSwDoc(); + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY); + uno::Sequence aFields = { + comphelper::makePropertyValue("BibiliographicType", text::BibliographyDataType::WWW), + comphelper::makePropertyValue("Identifier", OUString("AT")), + comphelper::makePropertyValue("Author", OUString("Author")), + comphelper::makePropertyValue("Title", OUString("Title")), + comphelper::makePropertyValue("TargetURL", OUString("#page=1")), + comphelper::makePropertyValue("UseTargetURL", OUString("true")), + }; + xField->setPropertyValue("Fields", uno::Any(aFields)); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + uno::Reference xContent(xField, uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); + + // When exporting to PDF: + save("writer_pdf_Export"); + + // Then make sure the field links when the Target URL is set + std::unique_ptr pPdfDocument = parsePDFExport(); + std::unique_ptr pPdfPage = pPdfDocument->openPage(/*nIndex=*/0); + CPPUNIT_ASSERT(pPdfPage->hasLinks()); +} + +CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testBibliographyUrlPdfExport5) +{ + // Given a document with a bibliography entry field: + std::shared_ptr pPDFium = vcl::pdf::PDFiumLibrary::get(); + if (!pPDFium) + { + return; + } + createSwDoc(); + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY); + uno::Sequence aFields = { + comphelper::makePropertyValue("BibiliographicType", text::BibliographyDataType::WWW), + comphelper::makePropertyValue("Identifier", OUString("AT")), + comphelper::makePropertyValue("Author", OUString("Author")), + comphelper::makePropertyValue("Title", OUString("Title")), + comphelper::makePropertyValue("UseTargetURL", OUString("true")), + }; + xField->setPropertyValue("Fields", uno::Any(aFields)); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + uno::Reference xContent(xField, uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); + // Create a bibliography table. + uno::Reference xTable( + xFactory->createInstance("com.sun.star.text.Bibliography"), uno::UNO_QUERY); + xCursor->gotoEnd(/*bExpand=*/false); + xText->insertControlCharacter(xCursor, text::ControlCharacter::APPEND_PARAGRAPH, + /*bAbsorb=*/false); + xText->insertTextContent(xCursor, xTable, /*bAbsorb=*/false); + // Update the table + uno::Reference xTableIndex(xTable, uno::UNO_QUERY); + xTableIndex->update(); + + // When exporting to PDF: + save("writer_pdf_Export"); + + // Then make sure the mark links to the table when table is present + std::unique_ptr pPdfDocument = parsePDFExport(); + std::unique_ptr pPdfPage = pPdfDocument->openPage(/*nIndex=*/0); + CPPUNIT_ASSERT(pPdfPage->hasLinks()); +} + CPPUNIT_TEST_FIXTURE(SwCoreTextTest, testTabOverMarginSection) { createSwDoc("tabovermargin-section.fodt"); -- cgit v1.2.3