<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[286055] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/286055">286055</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2021-11-19 05:35:55 -0800 (Fri, 19 Nov 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>[GTK][a11y] Add implementation of hypertext interface when building with ATSPI
https://bugs.webkit.org/show_bug.cgi?id=232708

Reviewed by Adrian Perez de Castro.

Source/WebCore:

* SourcesGTK.txt:
* accessibility/atspi/AccessibilityAtspi.cpp:
(WebCore::AccessibilityAtspi::unregisterObject):
(WebCore::AccessibilityAtspi::registerHyperlink):
* accessibility/atspi/AccessibilityAtspi.h:
* accessibility/atspi/AccessibilityObjectAtspi.cpp:
(WebCore::AccessibilityObjectAtspi::interfacesForObject):
(WebCore::AccessibilityObjectAtspi::path):
(WebCore::AccessibilityObjectAtspi::hyperlinkReference):
(WebCore::AccessibilityObjectAtspi::buildInterfaces const):
* accessibility/atspi/AccessibilityObjectAtspi.h:
* accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp: Added.
(WebCore::AccessibilityObjectAtspi::hyperlinkCount const):
(WebCore::AccessibilityObjectAtspi::hyperlink const):
(WebCore::AccessibilityObjectAtspi::hyperlinkIndex const):
* accessibility/atspi/AccessibilityObjectTextAtspi.cpp:
(WebCore::AccessibilityObjectAtspi::characterIndex const):

Tools:

Add unit tests to check hypertext interface.

* TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
(testHypertextBasic):
(beforeAll):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreSourcesGTKtxt">trunk/Source/WebCore/SourcesGTK.txt</a></li>
<li><a href="#trunkSourceWebCoreaccessibilityatspiAccessibilityAtspicpp">trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.cpp</a></li>
<li><a href="#trunkSourceWebCoreaccessibilityatspiAccessibilityAtspih">trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.h</a></li>
<li><a href="#trunkSourceWebCoreaccessibilityatspiAccessibilityObjectAtspicpp">trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp</a></li>
<li><a href="#trunkSourceWebCoreaccessibilityatspiAccessibilityObjectAtspih">trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h</a></li>
<li><a href="#trunkSourceWebCoreaccessibilityatspiAccessibilityObjectTextAtspicpp">trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectTextAtspi.cpp</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitGtkTestWebKitAccessibilitycpp">trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreaccessibilityatspiAccessibilityObjectHypertextAtspicpp">trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/ChangeLog      2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -1,5 +1,30 @@
</span><span class="cx"> 2021-11-19  Carlos Garcia Campos  <cgarcia@igalia.com>
</span><span class="cx"> 
</span><ins>+        [GTK][a11y] Add implementation of hypertext interface when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=232708
+
+        Reviewed by Adrian Perez de Castro.
+
+        * SourcesGTK.txt:
+        * accessibility/atspi/AccessibilityAtspi.cpp:
+        (WebCore::AccessibilityAtspi::unregisterObject):
+        (WebCore::AccessibilityAtspi::registerHyperlink):
+        * accessibility/atspi/AccessibilityAtspi.h:
+        * accessibility/atspi/AccessibilityObjectAtspi.cpp:
+        (WebCore::AccessibilityObjectAtspi::interfacesForObject):
+        (WebCore::AccessibilityObjectAtspi::path):
+        (WebCore::AccessibilityObjectAtspi::hyperlinkReference):
+        (WebCore::AccessibilityObjectAtspi::buildInterfaces const):
+        * accessibility/atspi/AccessibilityObjectAtspi.h:
+        * accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp: Added.
+        (WebCore::AccessibilityObjectAtspi::hyperlinkCount const):
+        (WebCore::AccessibilityObjectAtspi::hyperlink const):
+        (WebCore::AccessibilityObjectAtspi::hyperlinkIndex const):
+        * accessibility/atspi/AccessibilityObjectTextAtspi.cpp:
+        (WebCore::AccessibilityObjectAtspi::characterIndex const):
+
+2021-11-19  Carlos Garcia Campos  <cgarcia@igalia.com>
+
</ins><span class="cx">         [GTK][a11y] Add implementation of hyperlink interface when building with ATSPI
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=232707
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreSourcesGTKtxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/SourcesGTK.txt (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/SourcesGTK.txt      2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/SourcesGTK.txt 2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -43,6 +43,7 @@
</span><span class="cx"> accessibility/atspi/AccessibilityObjectAtspi.cpp
</span><span class="cx"> accessibility/atspi/AccessibilityObjectComponentAtspi.cpp
</span><span class="cx"> accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp
</span><ins>+accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp
</ins><span class="cx"> accessibility/atspi/AccessibilityObjectTextAtspi.cpp
</span><span class="cx"> accessibility/atspi/AccessibilityObjectValueAtspi.cpp
</span><span class="cx"> accessibility/atspi/AccessibilityRootAtspi.cpp
</span></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityAtspicpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.cpp (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.cpp  2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.cpp     2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -135,6 +135,12 @@
</span><span class="cx">         if (!m_connection)
</span><span class="cx">             return;
</span><span class="cx"> 
</span><ins>+        if (m_atspiHyperlinks.contains(atspiObject.ptr())) {
+            auto registeredObjects = m_atspiHyperlinks.take(atspiObject.ptr());
+            for (auto id : registeredObjects)
+                g_dbus_connection_unregister_object(m_connection.get(), id);
+        }
+
</ins><span class="cx">         g_dbus_connection_emit_signal(m_connection.get(), nullptr, atspiObject->path().utf8().data(), "org.a11y.atspi.Event.Object", "StateChanged",
</span><span class="cx">             g_variant_new("(siiva{sv})", "defunct", TRUE, 0, g_variant_new_string("0"), nullptr), nullptr);
</span><span class="cx"> 
</span><span class="lines">@@ -146,6 +152,24 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+String AccessibilityAtspi::registerHyperlink(AccessibilityObjectAtspi& atspiObject, Vector<std::pair<GDBusInterfaceInfo*, GDBusInterfaceVTable*>>&& interfaces)
+{
+    RELEASE_ASSERT(!isMainThread());
+    if (!m_connection)
+        return { };
+
+    String path = makeString("/org/a11y/atspi/accessible/", createCanonicalUUIDString().replace('-', '_'));
+    Vector<unsigned, 1> registeredObjects;
+    registeredObjects.reserveInitialCapacity(interfaces.size());
+    for (const auto& interface : interfaces) {
+        auto id = g_dbus_connection_register_object(m_connection.get(), path.utf8().data(), interface.first, interface.second, &atspiObject, nullptr, nullptr);
+        registeredObjects.uncheckedAppend(id);
+    }
+    m_atspiHyperlinks.add(&atspiObject, WTFMove(registeredObjects));
+
+    return path;
+}
+
</ins><span class="cx"> void AccessibilityAtspi::childrenChanged(AccessibilityObjectAtspi& atspiObject, AccessibilityObjectAtspi& child, ChildrenChanged change)
</span><span class="cx"> {
</span><span class="cx">     RELEASE_ASSERT(isMainThread());
</span></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityAtspih"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.h (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.h    2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityAtspi.h       2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -52,6 +52,7 @@
</span><span class="cx">     void unregisterRoot(AccessibilityRootAtspi&);
</span><span class="cx">     String registerObject(AccessibilityObjectAtspi&, Vector<std::pair<GDBusInterfaceInfo*, GDBusInterfaceVTable*>>&&);
</span><span class="cx">     void unregisterObject(AccessibilityObjectAtspi&);
</span><ins>+    String registerHyperlink(AccessibilityObjectAtspi&, Vector<std::pair<GDBusInterfaceInfo*, GDBusInterfaceVTable*>>&&);
</ins><span class="cx"> 
</span><span class="cx">     enum class ChildrenChanged { Added, Removed };
</span><span class="cx">     void childrenChanged(AccessibilityObjectAtspi&, AccessibilityObjectAtspi&, ChildrenChanged);
</span><span class="lines">@@ -78,6 +79,7 @@
</span><span class="cx">     GRefPtr<GDBusConnection> m_connection;
</span><span class="cx">     HashMap<AccessibilityRootAtspi*, Vector<unsigned, 2>> m_rootObjects;
</span><span class="cx">     HashMap<AccessibilityObjectAtspi*, Vector<unsigned, 20>> m_atspiObjects;
</span><ins>+    HashMap<AccessibilityObjectAtspi*, Vector<unsigned, 20>> m_atspiHyperlinks;
</ins><span class="cx">     unsigned m_cacheID { 0 };
</span><span class="cx">     HashMap<String, AccessibilityObjectAtspi*> m_cache;
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityObjectAtspicpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp    2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp       2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -69,6 +69,7 @@
</span><span class="cx">         interfaces.add(Interface::Text);
</span><span class="cx">     else if (!coreObject.isWebArea()) {
</span><span class="cx">         if (coreObject.roleValue() != AccessibilityRole::Table) {
</span><ins>+            interfaces.add(Interface::Hypertext);
</ins><span class="cx">             if ((renderer && renderer->childrenInline()) || roleIsTextType(coreObject.roleValue()) || coreObject.isMathToken())
</span><span class="cx">                 interfaces.add(Interface::Text);
</span><span class="cx">         }
</span><span class="lines">@@ -480,6 +481,8 @@
</span><span class="cx">             interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_value_interface), &s_valueFunctions });
</span><span class="cx">         if (m_interfaces.contains(Interface::Hyperlink))
</span><span class="cx">             interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_hyperlink_interface), &s_hyperlinkFunctions });
</span><ins>+        if (m_interfaces.contains(Interface::Hypertext))
+            interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_hypertext_interface), &s_hypertextFunctions });
</ins><span class="cx">         m_path = atspiRoot->atspi().registerObject(*this, WTFMove(interfaces));
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -492,6 +495,17 @@
</span><span class="cx">     return g_variant_new("(so)", root()->atspi().uniqueName(), path().utf8().data());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+GVariant* AccessibilityObjectAtspi::hyperlinkReference()
+{
+    RELEASE_ASSERT(!isMainThread());
+    if (m_hyperlinkPath.isNull()) {
+        path();
+        m_hyperlinkPath = root()->atspi().registerHyperlink(*this, { { const_cast<GDBusInterfaceInfo*>(&webkit_hyperlink_interface), &s_hyperlinkFunctions } });
+    }
+
+    return g_variant_new("(so)", root()->atspi().uniqueName(), m_hyperlinkPath.utf8().data());
+}
+
</ins><span class="cx"> void AccessibilityObjectAtspi::setRoot(AccessibilityRootAtspi* root)
</span><span class="cx"> {
</span><span class="cx">     Locker locker { m_rootLock };
</span><span class="lines">@@ -1099,6 +1113,8 @@
</span><span class="cx">         g_variant_builder_add(builder, "s", webkit_value_interface.name);
</span><span class="cx">     if (m_interfaces.contains(Interface::Hyperlink))
</span><span class="cx">         g_variant_builder_add(builder, "s", webkit_hyperlink_interface.name);
</span><ins>+    if (m_interfaces.contains(Interface::Hypertext))
+        g_variant_builder_add(builder, "s", webkit_hypertext_interface.name);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AccessibilityObjectAtspi::serialize(GVariantBuilder* builder) const
</span></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityObjectAtspih"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h      2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h 2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -47,7 +47,8 @@
</span><span class="cx">         Component = 1 << 1,
</span><span class="cx">         Text = 1 << 2,
</span><span class="cx">         Value = 1 << 3,
</span><del>-        Hyperlink = 1 << 4
</del><ins>+        Hyperlink = 1 << 4,
+        Hypertext = 1 << 5
</ins><span class="cx">     };
</span><span class="cx">     const OptionSet<Interface>& interfaces() const { return m_interfaces; }
</span><span class="cx"> 
</span><span class="lines">@@ -66,6 +67,7 @@
</span><span class="cx"> 
</span><span class="cx">     const String& path();
</span><span class="cx">     GVariant* reference();
</span><ins>+    GVariant* hyperlinkReference();
</ins><span class="cx">     void serialize(GVariantBuilder*) const;
</span><span class="cx"> 
</span><span class="cx">     WEBCORE_EXPORT String id() const;
</span><span class="lines">@@ -143,6 +145,7 @@
</span><span class="cx">     CString textAtOffset(int, TextGranularity, int&, int&) const;
</span><span class="cx">     int characterAtOffset(int) const;
</span><span class="cx">     std::optional<unsigned> characterOffset(UChar, int) const;
</span><ins>+    std::optional<unsigned> characterIndex(UChar, unsigned) const;
</ins><span class="cx">     IntRect textExtents(int, int, uint32_t) const;
</span><span class="cx">     int offsetAtPoint(const IntPoint&, uint32_t) const;
</span><span class="cx">     IntPoint boundsForSelection(const VisibleSelection&) const;
</span><span class="lines">@@ -154,6 +157,10 @@
</span><span class="cx"> 
</span><span class="cx">     unsigned offsetInParent() const;
</span><span class="cx"> 
</span><ins>+    unsigned hyperlinkCount() const;
+    AccessibilityObjectAtspi* hyperlink(unsigned) const;
+    std::optional<unsigned> hyperlinkIndex(unsigned) const;
+
</ins><span class="cx">     static OptionSet<Interface> interfacesForObject(AXCoreObject&);
</span><span class="cx"> 
</span><span class="cx">     static GDBusInterfaceVTable s_accessibleFunctions;
</span><span class="lines">@@ -161,6 +168,7 @@
</span><span class="cx">     static GDBusInterfaceVTable s_textFunctions;
</span><span class="cx">     static GDBusInterfaceVTable s_valueFunctions;
</span><span class="cx">     static GDBusInterfaceVTable s_hyperlinkFunctions;
</span><ins>+    static GDBusInterfaceVTable s_hypertextFunctions;
</ins><span class="cx"> 
</span><span class="cx">     AXCoreObject* m_axObject { nullptr };
</span><span class="cx">     AXCoreObject* m_coreObject { nullptr };
</span><span class="lines">@@ -169,6 +177,7 @@
</span><span class="cx">     std::optional<AccessibilityObjectAtspi*> m_parent;
</span><span class="cx">     Atomic<bool> m_isRegistered { false };
</span><span class="cx">     String m_path;
</span><ins>+    String m_hyperlinkPath;
</ins><span class="cx">     mutable int m_indexInParent { -1 };
</span><span class="cx">     mutable Lock m_rootLock;
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityObjectHypertextAtspicpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp (0 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp                           (rev 0)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp      2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -0,0 +1,122 @@
</span><ins>+/*
+ * Copyright (C) 2021 Igalia S.L.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+#include "AccessibilityObjectAtspi.h"
+
+#if ENABLE(ACCESSIBILITY) && USE(ATSPI)
+
+#include "AccessibilityRootAtspi.h"
+#include <gio/gio.h>
+#include <wtf/unicode/CharacterNames.h>
+
+namespace WebCore {
+
+GDBusInterfaceVTable AccessibilityObjectAtspi::s_hypertextFunctions = {
+    // method_call
+    [](GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData) {
+        RELEASE_ASSERT(!isMainThread());
+        auto atspiObject = Ref { *static_cast<AccessibilityObjectAtspi*>(userData) };
+        atspiObject->updateBackingStore();
+
+        if (!g_strcmp0(methodName, "GetNLinks")) {
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", atspiObject->hyperlinkCount()));
+        } else if (!g_strcmp0(methodName, "GetLink")) {
+            int index;
+            g_variant_get(parameters, "(i)", &index);
+            auto* wrapper = index >= 0 ? atspiObject->hyperlink(index) : nullptr;
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(@(so))", wrapper ? wrapper->hyperlinkReference() : atspiObject->root()->atspi().nullReference()));
+        } else if (!g_strcmp0(methodName, "GetLinkIndex")) {
+            int offset;
+            g_variant_get(parameters, "(i)", &offset);
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", offset >= 0 ? atspiObject->hyperlinkIndex(offset).value_or(-1) : -1));
+        }
+    },
+    // get_property
+    nullptr,
+    // set_property,
+    nullptr,
+    // padding
+    nullptr
+};
+
+unsigned AccessibilityObjectAtspi::hyperlinkCount() const
+{
+    return Accessibility::retrieveValueFromMainThread<unsigned>([this]() -> unsigned {
+        if (m_coreObject)
+            m_coreObject->updateBackingStore();
+
+        if (!m_coreObject)
+            return 0;
+
+        unsigned linkCount = 0;
+        const auto& children = m_coreObject->children();
+        for (const auto& child : children) {
+            if (child->accessibilityIsIgnored())
+                continue;
+
+            auto* wrapper = child->wrapper();
+            if (wrapper && wrapper->interfaces().contains(Interface::Hyperlink))
+                linkCount++;
+        }
+
+        return linkCount;
+    });
+}
+
+AccessibilityObjectAtspi* AccessibilityObjectAtspi::hyperlink(unsigned index) const
+{
+    return Accessibility::retrieveValueFromMainThread<AccessibilityObjectAtspi*>([this, index]() -> AccessibilityObjectAtspi* {
+        if (m_coreObject)
+            m_coreObject->updateBackingStore();
+
+        if (!m_coreObject)
+            return nullptr;
+
+        const auto& children = m_coreObject->children();
+        if (index >= children.size())
+            return nullptr;
+
+        int linkIndex = -1;
+        for (const auto& child : children) {
+            if (child->accessibilityIsIgnored())
+                continue;
+
+            auto* wrapper = child->wrapper();
+            if (!wrapper || !wrapper->interfaces().contains(Interface::Hyperlink))
+                continue;
+
+            if (static_cast<unsigned>(++linkIndex) == index) {
+                wrapper->setRoot(root());
+                return wrapper;
+            }
+        }
+
+        return nullptr;
+    });
+}
+
+std::optional<unsigned> AccessibilityObjectAtspi::hyperlinkIndex(unsigned offset) const
+{
+    return characterIndex(objectReplacementCharacter, offset);
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(ACCESSIBILITY) && USE(ATSPI)
</ins></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityObjectTextAtspicpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectTextAtspi.cpp (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectTextAtspi.cpp        2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectTextAtspi.cpp   2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -508,6 +508,38 @@
</span><span class="cx">     return UTF16OffsetToUTF8(mapping, offset);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+std::optional<unsigned> AccessibilityObjectAtspi::characterIndex(UChar character, unsigned offset) const
+{
+    auto utf16Text = text();
+    auto utf8Text = utf16Text.utf8();
+    if (utf8Text.isNull())
+        return std::nullopt;
+
+    auto length = g_utf8_strlen(utf8Text.data(), -1);
+    if (offset >= length)
+        return std::nullopt;
+
+    auto mapping = offsetMapping(utf16Text);
+    auto utf16Offset = UTF8OffsetToUTF16(mapping, offset);
+    if (utf16Text[utf16Offset] != character)
+        return std::nullopt;
+
+    unsigned start = 0;
+    int index = -1;
+    size_t position;
+    while ((position = utf16Text.find(character, start)) != notFound) {
+        index++;
+        if (static_cast<unsigned>(position) == utf16Offset)
+            break;
+        start = position + 1;
+    }
+
+    if (index == -1)
+        return std::nullopt;
+
+    return index;
+}
+
</ins><span class="cx"> IntRect AccessibilityObjectAtspi::boundsForRange(unsigned utf16Offset, unsigned length, uint32_t coordinateType) const
</span><span class="cx"> {
</span><span class="cx">     return Accessibility::retrieveValueFromMainThread<IntRect>([this, utf16Offset, length, coordinateType]() -> IntRect {
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Tools/ChangeLog       2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -1,5 +1,18 @@
</span><span class="cx"> 2021-11-19  Carlos Garcia Campos  <cgarcia@igalia.com>
</span><span class="cx"> 
</span><ins>+        [GTK][a11y] Add implementation of hypertext interface when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=232708
+
+        Reviewed by Adrian Perez de Castro.
+
+        Add unit tests to check hypertext interface.
+
+        * TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
+        (testHypertextBasic):
+        (beforeAll):
+
+2021-11-19  Carlos Garcia Campos  <cgarcia@igalia.com>
+
</ins><span class="cx">         Unreviewed. [GLIB] Add new test case to /jsc/class
</span><span class="cx"> 
</span><span class="cx">         Add a test case to check using JSC_TYPE_VALUE for a JSCClass property.
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitGtkTestWebKitAccessibilitycpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp (286054 => 286055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp    2021-11-19 09:52:50 UTC (rev 286054)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp       2021-11-19 13:35:55 UTC (rev 286055)
</span><span class="lines">@@ -1817,6 +1817,73 @@
</span><span class="cx">     g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == link2.get());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void testHypertextBasic(AccessibilityTest* test, gconstpointer)
+{
+    test->showInWindow(800, 600);
+    test->loadHtml(
+        "<html>"
+        "  <body>"
+        "    <p>This is <button>button</button> and <a href='https://www.webkitgtk.org'>link</a> in a paragraph</p>"
+        "  </body>"
+        "</html>",
+        nullptr);
+    test->waitUntilLoadFinished();
+
+    auto testApp = test->findTestApplication();
+    g_assert_true(ATSPI_IS_ACCESSIBLE(testApp.get()));
+
+    auto documentWeb = test->findDocumentWeb(testApp.get());
+    g_assert_true(ATSPI_IS_ACCESSIBLE(documentWeb.get()));
+    g_assert_cmpint(atspi_accessible_get_child_count(documentWeb.get(), nullptr), ==, 1);
+
+    auto p = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_HYPERTEXT(p.get()));
+    g_assert_cmpint(atspi_hypertext_get_n_links(ATSPI_HYPERTEXT(p.get()), nullptr), ==, 2);
+
+    auto link = adoptGRef(atspi_hypertext_get_link(ATSPI_HYPERTEXT(p.get()), 0, nullptr));
+    g_assert_true(ATSPI_IS_HYPERLINK(link.get()));
+    g_assert_cmpint(atspi_hyperlink_get_n_anchors(ATSPI_HYPERLINK(link.get()), nullptr), ==, 1);
+    g_assert_true(atspi_hyperlink_is_valid(ATSPI_HYPERLINK(link.get()), nullptr));
+    g_assert_cmpint(atspi_hyperlink_get_start_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 8);
+#if USE(ATSPI)
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 9);
+#else
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 15);
+#endif
+    GUniquePtr<char> uri(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "");
+    auto button = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 0, nullptr));
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == button.get());
+
+    link = adoptGRef(atspi_hypertext_get_link(ATSPI_HYPERTEXT(p.get()), 1, nullptr));
+    g_assert_true(ATSPI_IS_HYPERLINK(link.get()));
+    g_assert_cmpint(atspi_hyperlink_get_n_anchors(ATSPI_HYPERLINK(link.get()), nullptr), ==, 1);
+    g_assert_true(atspi_hyperlink_is_valid(ATSPI_HYPERLINK(link.get()), nullptr));
+#if USE(ATSPI)
+    g_assert_cmpint(atspi_hyperlink_get_start_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 14);
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 15);
+#else
+    g_assert_cmpint(atspi_hyperlink_get_start_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 20);
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 24);
+#endif
+    uri.reset(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "https://www.webkitgtk.org/");
+    auto a = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 1, nullptr));
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == a.get());
+
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 0, nullptr), ==, -1);
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 8, nullptr), ==, 0);
+#if USE(ATSPI)
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 9, nullptr), ==, -1);
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 14, nullptr), ==, 1);
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 15, nullptr), ==, -1);
+#else
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 15, nullptr), ==, -1);
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 20, nullptr), ==, 1);
+    g_assert_cmpint(atspi_hypertext_get_link_index(ATSPI_HYPERTEXT(p.get()), 24, nullptr), ==, -1);
+#endif
+}
+
</ins><span class="cx"> void beforeAll()
</span><span class="cx"> {
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "accessible/basic-hierarchy", testAccessibleBasicHierarchy);
</span><span class="lines">@@ -1839,6 +1906,7 @@
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "text/replaced-objects", testTextReplacedObjects);
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "value/basic", testValueBasic);
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "hyperlink/basic", testHyperlinkBasic);
</span><ins>+    AccessibilityTest::add("WebKitAccessibility", "hypertext/basic", testHypertextBasic);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void afterAll()
</span></span></pre>
</div>
</div>

</body>
</html>