include/sfx2/AccessibilityIssue.hxx                      |    1 
 sw/inc/AccessibilityCheckStrings.hrc                     |    1 
 sw/qa/core/accessibilitycheck/AccessibilityCheckTest.cxx |   11 ++
 sw/qa/core/accessibilitycheck/data/FakeCaptionTest.odt   |binary
 sw/source/core/access/AccessibilityCheck.cxx             |   66 +++++++++++++++
 5 files changed, 79 insertions(+)

New commits:
commit 766151ff6ec759563583abfa43f5bb3755ee2603
Author:     Paris Oplopoios <paris.oplopo...@collabora.com>
AuthorDate: Fri Oct 28 13:14:36 2022 +0300
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Nov 3 07:43:09 2022 +0100

    a11y: Add check for fake captions
    
    Add accessibility check and relevant test for a document that has
    fake captions
    
    Change-Id: If22999378fd7467151c1d400662858fe1cb0319d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/141964
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/include/sfx2/AccessibilityIssue.hxx 
b/include/sfx2/AccessibilityIssue.hxx
index 9d1023053515..b9e3266c73a2 100644
--- a/include/sfx2/AccessibilityIssue.hxx
+++ b/include/sfx2/AccessibilityIssue.hxx
@@ -32,6 +32,7 @@ enum class AccessibilityIssueID
     HYPERLINK_IS_TEXT,
     HYPERLINK_SHORT,
     FAKE_FOOTNOTE,
+    FAKE_CAPTION,
     MANUAL_NUMBERING
 };
 
diff --git a/sw/inc/AccessibilityCheckStrings.hrc 
b/sw/inc/AccessibilityCheckStrings.hrc
index 86be8b494c90..e63d304d1063 100644
--- a/sw/inc/AccessibilityCheckStrings.hrc
+++ b/sw/inc/AccessibilityCheckStrings.hrc
@@ -22,6 +22,7 @@
 #define STR_TEXT_BLINKING               NC_("STR_TEXT_BLINKING", "Blinking 
text.")
 #define STR_AVOID_FOOTNOTES             NC_("STR_AVOID_FOOTNOTES", "Avoid 
footnotes.")
 #define STR_AVOID_FAKE_FOOTNOTES        NC_("STR_AVOID_FAKE_FOOTNOTES", "Avoid 
fake footnotes.")
+#define STR_AVOID_FAKE_CAPTIONS         NC_("STR_AVOID_FAKE_CAPTIONS", "Avoid 
fake captions.")
 #define STR_AVOID_ENDNOTES              NC_("STR_AVOID_ENDNOTES", "Avoid 
endnotes.")
 #define STR_AVOID_BACKGROUND_IMAGES     NC_("STR_AVOID_BACKGROUND_IMAGES", 
"Avoid background images.")
 #define STR_AVOID_NEWLINES_SPACE        NC_("STR_AVOID_NEWLINES_SPACE", "Avoid 
newlines to create space.")
diff --git a/sw/qa/core/accessibilitycheck/AccessibilityCheckTest.cxx 
b/sw/qa/core/accessibilitycheck/AccessibilityCheckTest.cxx
index 39f094a38c2a..02912e4c9d35 100644
--- a/sw/qa/core/accessibilitycheck/AccessibilityCheckTest.cxx
+++ b/sw/qa/core/accessibilitycheck/AccessibilityCheckTest.cxx
@@ -143,6 +143,17 @@ CPPUNIT_TEST_FIXTURE(AccessibilityCheckTest, 
testCheckFakeFootnote)
     CPPUNIT_ASSERT_EQUAL(sfx::AccessibilityIssueID::FAKE_FOOTNOTE, 
aIssues[1]->m_eIssueID);
 }
 
+CPPUNIT_TEST_FIXTURE(AccessibilityCheckTest, testCheckFakeCaption)
+{
+    SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "FakeCaptionTest.odt");
+    CPPUNIT_ASSERT(pDoc);
+    sw::AccessibilityCheck aCheck(pDoc);
+    aCheck.check();
+    auto& aIssues = aCheck.getIssueCollection().getIssues();
+    CPPUNIT_ASSERT_EQUAL(size_t(1), aIssues.size());
+    CPPUNIT_ASSERT_EQUAL(sfx::AccessibilityIssueID::FAKE_CAPTION, 
aIssues[0]->m_eIssueID);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/qa/core/accessibilitycheck/data/FakeCaptionTest.odt 
b/sw/qa/core/accessibilitycheck/data/FakeCaptionTest.odt
new file mode 100644
index 000000000000..9630bfcf73d7
Binary files /dev/null and 
b/sw/qa/core/accessibilitycheck/data/FakeCaptionTest.odt differ
diff --git a/sw/source/core/access/AccessibilityCheck.cxx 
b/sw/source/core/access/AccessibilityCheck.cxx
index 3860eb8904e0..0170003c4a48 100644
--- a/sw/source/core/access/AccessibilityCheck.cxx
+++ b/sw/source/core/access/AccessibilityCheck.cxx
@@ -11,12 +11,14 @@
 #include <AccessibilityCheck.hxx>
 #include <AccessibilityIssue.hxx>
 #include <AccessibilityCheckStrings.hrc>
+#include <strings.hrc>
 #include <ndnotxt.hxx>
 #include <ndtxt.hxx>
 #include <docsh.hxx>
 #include <IDocumentDrawModelAccess.hxx>
 #include <drawdoc.hxx>
 #include <svx/svdpage.hxx>
+#include <sortedobjs.hxx>
 #include <swtable.hxx>
 #include <com/sun/star/frame/XModel.hpp>
 #include <com/sun/star/text/XTextContent.hpp>
@@ -26,11 +28,13 @@
 #include <unotools/intlwrapper.hxx>
 #include <tools/urlobj.hxx>
 #include <editeng/langitem.hxx>
+#include <calbck.hxx>
 #include <charatr.hxx>
 #include <svx/xfillit0.hxx>
 #include <svx/xflclit.hxx>
 #include <ftnidx.hxx>
 #include <txtftn.hxx>
+#include <txtfrm.hxx>
 #include <svl/itemiter.hxx>
 #include <o3tl/vector_utils.hxx>
 #include <svx/swframetypes.hxx>
@@ -841,6 +845,67 @@ public:
     }
 };
 
+class FakeCaptionCheck : public NodeCheck
+{
+public:
+    FakeCaptionCheck(sfx::AccessibilityIssueCollection& rIssueCollection)
+        : NodeCheck(rIssueCollection)
+    {
+    }
+    void check(SwNode* pCurrent) override
+    {
+        if (!pCurrent->IsTextNode())
+            return;
+
+        SwTextNode* pTextNode = pCurrent->GetTextNode();
+        const OUString& sText = pTextNode->GetText();
+
+        if (sText.getLength() == 0)
+            return;
+
+        // Check if it's a real caption
+        const SwNode* aStartFly = pCurrent->FindFlyStartNode();
+        if (aStartFly
+            && aStartFly->GetFlyFormat()->GetAnchor().GetAnchorId() != 
RndStdIds::FLY_AS_CHAR)
+            return;
+
+        auto aIter = SwIterator<SwTextFrame, SwTextNode, 
sw::IteratorMode::UnwrapMulti>(*pTextNode);
+        auto nCount = 0;
+        for (auto aTextFrame = aIter.First(); aTextFrame; aTextFrame = 
aIter.Next())
+        {
+            auto aObjects = aTextFrame->GetDrawObjs();
+            if (aObjects)
+                nCount += aObjects->size();
+
+            if (nCount > 1)
+                return;
+        }
+
+        // Check that there's exactly 1 image anchored in this node
+        if (nCount == 1)
+        {
+            OString sTemp;
+            sText.convertToString(&sTemp, RTL_TEXTENCODING_ASCII_US, 0);
+            if (sText.startsWith(SwResId(STR_POOLCOLL_LABEL))
+                || sText.startsWith(SwResId(STR_POOLCOLL_LABEL_ABB))
+                || sText.startsWith(SwResId(STR_POOLCOLL_LABEL_TABLE))
+                || sText.startsWith(SwResId(STR_POOLCOLL_LABEL_FRAME))
+                || sText.startsWith(SwResId(STR_POOLCOLL_LABEL_DRAWING))
+                || sText.startsWith(SwResId(STR_POOLCOLL_LABEL_FIGURE)))
+            {
+                auto pIssue = lclAddIssue(m_rIssueCollection, 
SwResId(STR_AVOID_FAKE_CAPTIONS),
+                                          
sfx::AccessibilityIssueID::FAKE_CAPTION);
+                pIssue->setIssueObject(IssueObject::TEXT);
+                pIssue->setNode(pTextNode);
+                SwDoc& rDocument = pTextNode->GetDoc();
+                pIssue->setDoc(rDocument);
+                pIssue->setStart(0);
+                pIssue->setEnd(sText.getLength());
+            }
+        }
+    }
+};
+
 class BlinkingTextCheck : public NodeCheck
 {
 private:
@@ -1266,6 +1331,7 @@ void AccessibilityCheck::check()
     
aNodeChecks.push_back(std::make_unique<NewlineSpacingCheck>(m_aIssueCollection));
     
aNodeChecks.push_back(std::make_unique<SpaceSpacingCheck>(m_aIssueCollection));
     
aNodeChecks.push_back(std::make_unique<FakeFootnoteCheck>(m_aIssueCollection));
+    
aNodeChecks.push_back(std::make_unique<FakeCaptionCheck>(m_aIssueCollection));
 
     auto const& pNodes = m_pDoc->GetNodes();
     SwNode* pNode = nullptr;

Reply via email to