sw/qa/core/tox/tox.cxx        |   43 ++++++++++++++++++++++++++++++++++++++++++
 sw/source/core/tox/txmsrt.cxx |   14 ++++++++++++-
 2 files changed, 56 insertions(+), 1 deletion(-)

New commits:
commit 107638884395a193133613e2a464a4a9a870c0e3
Author:     Miklos Vajna <[email protected]>
AuthorDate: Fri Feb 26 17:32:17 2021 +0100
Commit:     Miklos Vajna <[email protected]>
CommitDate: Sat Feb 27 15:44:35 2021 +0100

    sw bibliography, refer to a page: strip URLs in the bibliography table
    
    In case multiple bibliography entries refer to the same source, just
    different page numbers, then it's pointless to list those sources
    multiple times in the bibliography table.
    
    As a first step, strip the page fragment of those URLs, so later we can
    merge these entries.
    
    Change-Id: I02991d3d19742092198309174aa3d662622fa505
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111653
    Reviewed-by: Miklos Vajna <[email protected]>
    Tested-by: Jenkins

diff --git a/sw/qa/core/tox/tox.cxx b/sw/qa/core/tox/tox.cxx
index b9a075119293..22c0bd3cafa1 100644
--- a/sw/qa/core/tox/tox.cxx
+++ b/sw/qa/core/tox/tox.cxx
@@ -64,6 +64,49 @@ CPPUNIT_TEST_FIXTURE(Test, testAuthorityLinkClick)
     // i.e. the URL was not clickable and the table row was a single text 
portion.
     CPPUNIT_ASSERT_EQUAL(OUString("http://www.example.com/test.pdf";), aActual);
 }
+
+CPPUNIT_TEST_FIXTURE(Test, testAuthorityTableEntryURL)
+{
+    // Given a document with a bibliography reference (of type WWW) in it:
+    createSwDoc();
+    uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xField(
+        xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), 
uno::UNO_QUERY);
+    uno::Sequence<beans::PropertyValue> 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("http://www.example.com/test.pdf#page=1";)),
+    };
+    xField->setPropertyValue("Fields", uno::makeAny(aFields));
+    uno::Reference<text::XTextDocument> xTextDocument(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XText> xText = xTextDocument->getText();
+    uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+    uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
+    xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
+    // Create a bibliography table.
+    uno::Reference<text::XTextContent> 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);
+
+    // When updating that table:
+    uno::Reference<text::XDocumentIndex> xTableIndex(xTable, uno::UNO_QUERY);
+    xTableIndex->update();
+
+    // Then the page number from the source's URL should be stripped:
+    // Paragraph index: Reference, table header, table row.
+    // Portion index: ID, etc; then the URL.
+    auto aActual = getProperty<OUString>(getRun(getParagraph(3), 2), 
"HyperLinkURL");
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: http://www.example.com/test.pdf
+    // - Actual  : http://www.example.com/test.pdf#page=1
+    // i.e. the page number was still part of the bibliography table.
+    CPPUNIT_ASSERT_EQUAL(OUString("http://www.example.com/test.pdf";), aActual);
+}
 }
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/core/tox/txmsrt.cxx b/sw/source/core/tox/txmsrt.cxx
index 25456a292169..7c2748c82bd2 100644
--- a/sw/source/core/tox/txmsrt.cxx
+++ b/sw/source/core/tox/txmsrt.cxx
@@ -19,6 +19,7 @@
 
 #include <unotools/charclass.hxx>
 #include <osl/diagnose.h>
+#include <tools/urlobj.hxx>
 #include <txtfld.hxx>
 #include <doc.hxx>
 #include <IDocumentLayoutAccess.hxx>
@@ -844,7 +845,18 @@ OUString SwTOXAuthority::GetText(sal_uInt16 nAuthField, 
const SwRootFrame* pLayo
 void SwTOXAuthority::FillText(SwTextNode& rNd, const SwIndex& rInsPos, 
sal_uInt16 nAuthField,
                               SwRootFrame const* const pLayout) const
 {
-    rNd.InsertText(GetText(nAuthField, pLayout), rInsPos);
+    OUString aText = GetText(nAuthField, pLayout);
+    if (nAuthField == AUTH_FIELD_URL)
+    {
+        INetURLObject aObject(aText);
+        if (aObject.GetMark().startsWith("page="))
+        {
+            aObject.SetMark(OUString());
+            aText = aObject.GetMainURL(INetURLObject::DecodeMechanism::NONE);
+        }
+    }
+
+    rNd.InsertText(aText, rInsPos);
 }
 
 bool SwTOXAuthority::equivalent(const SwTOXSortTabBase& rCmp)
_______________________________________________
Libreoffice-commits mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to