<!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>[286050] 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/286050">286050</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2021-11-19 00:51:53 -0800 (Fri, 19 Nov 2021)</dd>
</dl>

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

Reviewed by Adrian Perez de Castro.

Source/WebCore:

Add hyperlink implementation and make links be handled as replaced objects too, to ensure all hyperlinks are
handled the same way. That's consistent with chromium.

* SourcesGTK.txt:
* accessibility/atspi/AccessibilityObjectAtspi.cpp:
(WebCore::AccessibilityObjectAtspi::interfacesForObject):
(WebCore::AccessibilityObjectAtspi::path):
(WebCore::AccessibilityObjectAtspi::buildInterfaces const):
* accessibility/atspi/AccessibilityObjectAtspi.h:
* accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp: Added.
(WebCore::AccessibilityObjectAtspi::url const):
(WebCore::AccessibilityObjectAtspi::offsetInParent const):
* accessibility/atspi/AccessibilityObjectTextAtspi.cpp:
(WebCore::AccessibilityObjectAtspi::characterOffset const):
* editing/TextIterator.cpp:
(WebCore::isRendererReplacedElement):
(WebCore::TextIterator::handleReplacedElement):

Tools:

Add unit tests to check hyperlink interface and implement AccessibilityUIElement::url() in WTR.

* TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
(testTextReplacedObjects):
(testHyperlinkBasic):
(beforeAll):
* WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp:
(WTR::AccessibilityUIElement::url):</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="#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="#trunkSourceWebCoreeditingTextIteratorcpp">trunk/Source/WebCore/editing/TextIterator.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>
<li><a href="#trunkToolsWebKitTestRunnerInjectedBundleatspiAccessibilityUIElementAtspicpp">trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp</a></li>
</ul>

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Source/WebCore/ChangeLog      2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -1,3 +1,28 @@
</span><ins>+2021-11-19  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK][a11y] Add implementation of hyperlink interface when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=232707
+
+        Reviewed by Adrian Perez de Castro.
+
+        Add hyperlink implementation and make links be handled as replaced objects too, to ensure all hyperlinks are
+        handled the same way. That's consistent with chromium.
+
+        * SourcesGTK.txt:
+        * accessibility/atspi/AccessibilityObjectAtspi.cpp:
+        (WebCore::AccessibilityObjectAtspi::interfacesForObject):
+        (WebCore::AccessibilityObjectAtspi::path):
+        (WebCore::AccessibilityObjectAtspi::buildInterfaces const):
+        * accessibility/atspi/AccessibilityObjectAtspi.h:
+        * accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp: Added.
+        (WebCore::AccessibilityObjectAtspi::url const):
+        (WebCore::AccessibilityObjectAtspi::offsetInParent const):
+        * accessibility/atspi/AccessibilityObjectTextAtspi.cpp:
+        (WebCore::AccessibilityObjectAtspi::characterOffset const):
+        * editing/TextIterator.cpp:
+        (WebCore::isRendererReplacedElement):
+        (WebCore::TextIterator::handleReplacedElement):
+
</ins><span class="cx"> 2021-11-18  Frédéric Wang  <fwang@igalia.com>
</span><span class="cx"> 
</span><span class="cx">         Nullptr crash in SimplifiedBackwardsTextIterator::range() via previousSentencePosition
</span></span></pre></div>
<a id="trunkSourceWebCoreSourcesGTKtxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/SourcesGTK.txt (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/SourcesGTK.txt      2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Source/WebCore/SourcesGTK.txt 2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -42,6 +42,7 @@
</span><span class="cx"> accessibility/atspi/AccessibilityAtspi.cpp
</span><span class="cx"> accessibility/atspi/AccessibilityObjectAtspi.cpp
</span><span class="cx"> accessibility/atspi/AccessibilityObjectComponentAtspi.cpp
</span><ins>+accessibility/atspi/AccessibilityObjectHyperlinkAtspi.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="trunkSourceWebCoreaccessibilityatspiAccessibilityObjectAtspicpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp    2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp       2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> #include "RenderAncestorIterator.h"
</span><span class="cx"> #include "RenderBlock.h"
</span><span class="cx"> #include "RenderObject.h"
</span><ins>+#include "TextIterator.h"
</ins><span class="cx"> #include <glib/gi18n-lib.h>
</span><span class="cx"> #include <wtf/MainThread.h>
</span><span class="cx"> #include <wtf/UUID.h>
</span><span class="lines">@@ -86,6 +87,9 @@
</span><span class="cx">     if (coreObject.supportsRangeValue())
</span><span class="cx">         interfaces.add(Interface::Value);
</span><span class="cx"> 
</span><ins>+    if (coreObject.isLink() || (isRendererReplacedElement(renderer)))
+        interfaces.add(Interface::Hyperlink);
+
</ins><span class="cx">     return interfaces;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -474,6 +478,8 @@
</span><span class="cx">             interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_text_interface), &s_textFunctions });
</span><span class="cx">         if (m_interfaces.contains(Interface::Value))
</span><span class="cx">             interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_value_interface), &s_valueFunctions });
</span><ins>+        if (m_interfaces.contains(Interface::Hyperlink))
+            interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_hyperlink_interface), &s_hyperlinkFunctions });
</ins><span class="cx">         m_path = atspiRoot->atspi().registerObject(*this, WTFMove(interfaces));
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -1091,6 +1097,8 @@
</span><span class="cx">         g_variant_builder_add(builder, "s", webkit_text_interface.name);
</span><span class="cx">     if (m_interfaces.contains(Interface::Value))
</span><span class="cx">         g_variant_builder_add(builder, "s", webkit_value_interface.name);
</span><ins>+    if (m_interfaces.contains(Interface::Hyperlink))
+        g_variant_builder_add(builder, "s", webkit_hyperlink_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 (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h      2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h 2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -46,7 +46,8 @@
</span><span class="cx">         Accessible = 1 << 0,
</span><span class="cx">         Component = 1 << 1,
</span><span class="cx">         Text = 1 << 2,
</span><del>-        Value = 1 << 3
</del><ins>+        Value = 1 << 3,
+        Hyperlink = 1 << 4
</ins><span class="cx">     };
</span><span class="cx">     const OptionSet<Interface>& interfaces() const { return m_interfaces; }
</span><span class="cx"> 
</span><span class="lines">@@ -117,6 +118,8 @@
</span><span class="cx">     WEBCORE_EXPORT double minimumIncrement() const;
</span><span class="cx">     void valueChanged(double);
</span><span class="cx"> 
</span><ins>+    WEBCORE_EXPORT URL url() const;
+
</ins><span class="cx"> private:
</span><span class="cx">     explicit AccessibilityObjectAtspi(AXCoreObject*);
</span><span class="cx"> 
</span><span class="lines">@@ -139,6 +142,7 @@
</span><span class="cx">     CString text(int, int) const;
</span><span class="cx">     CString textAtOffset(int, TextGranularity, int&, int&) const;
</span><span class="cx">     int characterAtOffset(int) const;
</span><ins>+    std::optional<unsigned> characterOffset(UChar, int) 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">@@ -148,6 +152,8 @@
</span><span class="cx">     bool scrollToMakeVisible(int, int, uint32_t) const;
</span><span class="cx">     bool scrollToPoint(int, int, uint32_t, int, int) const;
</span><span class="cx"> 
</span><ins>+    unsigned offsetInParent() 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">@@ -154,6 +160,7 @@
</span><span class="cx">     static GDBusInterfaceVTable s_componentFunctions;
</span><span class="cx">     static GDBusInterfaceVTable s_textFunctions;
</span><span class="cx">     static GDBusInterfaceVTable s_valueFunctions;
</span><ins>+    static GDBusInterfaceVTable s_hyperlinkFunctions;
</ins><span class="cx"> 
</span><span class="cx">     AXCoreObject* m_axObject { nullptr };
</span><span class="cx">     AXCoreObject* m_coreObject { nullptr };
</span></span></pre></div>
<a id="trunkSourceWebCoreaccessibilityatspiAccessibilityObjectHyperlinkAtspicpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp (0 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp                           (rev 0)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp      2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -0,0 +1,115 @@
</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/URL.h>
+#include <wtf/unicode/CharacterNames.h>
+
+namespace WebCore {
+
+GDBusInterfaceVTable AccessibilityObjectAtspi::s_hyperlinkFunctions = {
+    // 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, "GetObject")) {
+            int index;
+            g_variant_get(parameters, "(i)", &index);
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(@(so))", !index ? atspiObject->reference() : atspiObject->root()->atspi().nullReference()));
+        } else if (!g_strcmp0(methodName, "GetURI")) {
+            int index;
+            g_variant_get(parameters, "(i)", &index);
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", !index ? atspiObject->url().string().utf8().data() : ""));
+        } else if (!g_strcmp0(methodName, "IsValid"))
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", atspiObject->m_axObject ? TRUE : FALSE));
+    },
+    // get_property
+    [](GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* propertyName, GError** error, gpointer userData) -> GVariant* {
+        RELEASE_ASSERT(!isMainThread());
+        auto atspiObject = Ref { *static_cast<AccessibilityObjectAtspi*>(userData) };
+        atspiObject->updateBackingStore();
+
+        if (!g_strcmp0(propertyName, "NAnchors"))
+            return g_variant_new_int32(1);
+        if (!g_strcmp0(propertyName, "StartIndex"))
+            return g_variant_new_int32(atspiObject->offsetInParent());
+        if (!g_strcmp0(propertyName, "EndIndex"))
+            return g_variant_new_int32(atspiObject->offsetInParent() + 1);
+
+        g_set_error(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Unknown property '%s'", propertyName);
+        return nullptr;
+    },
+    // set_property,
+    nullptr,
+    // padding
+    nullptr
+};
+
+URL AccessibilityObjectAtspi::url() const
+{
+    AXCoreObject* axObject = isMainThread() ? m_coreObject : m_axObject;
+    return axObject ? axObject->url() : URL();
+}
+
+unsigned AccessibilityObjectAtspi::offsetInParent() const
+{
+    return Accessibility::retrieveValueFromMainThread<unsigned>([this]() -> unsigned {
+        if (m_coreObject)
+            m_coreObject->updateBackingStore();
+
+        if (!m_coreObject)
+            return 0;
+
+        auto* parent = m_coreObject->parentObjectUnignored();
+        if (!parent || !parent->wrapper())
+            return 0;
+
+        int index = -1;
+        const auto& children = parent->children();
+        for (const auto& child : children) {
+            if (child->accessibilityIsIgnored())
+                continue;
+
+            auto* wrapper = child->wrapper();
+            if (!wrapper || !wrapper->interfaces().contains(Interface::Hyperlink))
+                continue;
+
+            index++;
+            if (wrapper == this)
+                break;
+        }
+
+        if (index == -1)
+            return 0;
+
+        return parent->wrapper()->characterOffset(objectReplacementCharacter, index).value_or(0);
+    });
+}
+
+} // 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 (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectTextAtspi.cpp        2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectTextAtspi.cpp   2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -21,13 +21,18 @@
</span><span class="cx"> #include "AccessibilityObjectAtspi.h"
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(ACCESSIBILITY) && USE(ATSPI)
</span><ins>+#include "AXObjectCache.h"
</ins><span class="cx"> #include "AccessibilityAtspiEnums.h"
</span><ins>+#include "AccessibilityObject.h"
</ins><span class="cx"> #include "AccessibilityObjectInterface.h"
</span><ins>+#include "AccessibilityRootAtspi.h"
</ins><span class="cx"> #include "Editing.h"
</span><span class="cx"> #include "PlatformScreen.h"
</span><ins>+#include "RenderLayer.h"
</ins><span class="cx"> #include "SurrogatePairAwareTextIterator.h"
</span><span class="cx"> #include "TextIterator.h"
</span><span class="cx"> #include "VisibleUnits.h"
</span><ins>+#include <gio/gio.h>
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -484,6 +489,25 @@
</span><span class="cx">     return g_utf8_get_char(g_utf8_offset_to_pointer(utf8Text.data(), offset));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+std::optional<unsigned> AccessibilityObjectAtspi::characterOffset(UChar character, int index) const
+{
+    auto utf16Text = text();
+    unsigned start = 0;
+    size_t offset;
+    while ((offset = utf16Text.find(character, start)) != notFound) {
+        start = offset + 1;
+        if (!index)
+            break;
+        index--;
+    }
+
+    if (offset == notFound)
+        return std::nullopt;
+
+    auto mapping = offsetMapping(utf16Text);
+    return UTF16OffsetToUTF8(mapping, offset);
+}
+
</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="trunkSourceWebCoreeditingTextIteratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/TextIterator.cpp (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/TextIterator.cpp    2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Source/WebCore/editing/TextIterator.cpp       2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -277,6 +277,11 @@
</span><span class="cx">             return true;
</span><span class="cx">         if (equalLettersIgnoringASCIICase(element.attributeWithoutSynchronization(roleAttr), "img"))
</span><span class="cx">             return true;
</span><ins>+#if USE(ATSPI)
+        // Links are also replaced with object replacement character in ATSPI.
+        if (element.isLink())
+            return true;
+#endif
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     return false;
</span><span class="lines">@@ -750,7 +755,7 @@
</span><span class="cx"> 
</span><span class="cx">     m_hasEmitted = true;
</span><span class="cx"> 
</span><del>-    if (m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharacters) && renderer.isReplaced()) {
</del><ins>+    if (m_behaviors.contains(TextIteratorBehavior::EmitsObjectReplacementCharacters)) {
</ins><span class="cx">         emitCharacter(objectReplacementCharacter, *m_node->parentNode(), m_node, 0, 1);
</span><span class="cx">         // Don't process subtrees for embedded objects. If the text there is required,
</span><span class="cx">         // it must be explicitly asked by specifying a range falling inside its boundaries.
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Tools/ChangeLog       2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -1,3 +1,19 @@
</span><ins>+2021-11-19  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK][a11y] Add implementation of hyperlink interface when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=232707
+
+        Reviewed by Adrian Perez de Castro.
+
+        Add unit tests to check hyperlink interface and implement AccessibilityUIElement::url() in WTR.
+
+        * TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
+        (testTextReplacedObjects):
+        (testHyperlinkBasic):
+        (beforeAll):
+        * WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp:
+        (WTR::AccessibilityUIElement::url):
+
</ins><span class="cx"> 2021-11-18  Alex Christensen  <achristensen@webkit.org>
</span><span class="cx"> 
</span><span class="cx">         Allow all redirect schemes when compiling a content rule list
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitGtkTestWebKitAccessibilitycpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp    2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp       2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -1533,6 +1533,7 @@
</span><span class="cx">         "<html>"
</span><span class="cx">         "  <body>"
</span><span class="cx">         "    <p>This is <button>button1</button> and <button>button2</button> in paragraph</p>"
</span><ins>+        "    <p>This is <a href='#'>link1</a> and <a href='#'>link2</a> in paragraph</p>"
</ins><span class="cx">         "  </body>"
</span><span class="cx">         "</html>",
</span><span class="cx">         nullptr);
</span><span class="lines">@@ -1543,7 +1544,7 @@
</span><span class="cx"> 
</span><span class="cx">     auto documentWeb = test->findDocumentWeb(testApp.get());
</span><span class="cx">     g_assert_true(ATSPI_IS_ACCESSIBLE(documentWeb.get()));
</span><del>-    g_assert_cmpint(atspi_accessible_get_child_count(documentWeb.get(), nullptr), ==, 1);
</del><ins>+    g_assert_cmpint(atspi_accessible_get_child_count(documentWeb.get(), nullptr), ==, 2);
</ins><span class="cx"> 
</span><span class="cx">     auto p = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr));
</span><span class="cx">     g_assert_true(ATSPI_IS_TEXT(p.get()));
</span><span class="lines">@@ -1611,6 +1612,24 @@
</span><span class="cx">     g_assert_cmpuint(g_hash_table_size(attributes.get()), ==, 0);
</span><span class="cx">     g_assert_cmpint(startOffset, ==, 15);
</span><span class="cx">     g_assert_cmpint(endOffset, ==, 28);
</span><ins>+
+    // Links are also replaced elements.
+    p = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_TEXT(p.get()));
+    g_assert_cmpint(atspi_text_get_character_count(ATSPI_TEXT(p.get()), nullptr), ==, 28);
+    text.reset(atspi_text_get_text(ATSPI_TEXT(p.get()), 0, -1, nullptr));
+    g_assert_cmpstr(text.get(), ==, "This is \357\277\274 and \357\277\274 in paragraph");
+    g_assert_cmpint(atspi_accessible_get_child_count(p.get(), nullptr), ==, 2);
+
+    auto link1 = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_TEXT(link1.get()));
+    text.reset(atspi_text_get_text(ATSPI_TEXT(link1.get()), 0, -1, nullptr));
+    g_assert_cmpstr(text.get(), ==, "link1");
+
+    auto link2 = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_TEXT(link2.get()));
+    text.reset(atspi_text_get_text(ATSPI_TEXT(link2.get()), 0, -1, nullptr));
+    g_assert_cmpstr(text.get(), ==, "link2");
</ins><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -1665,6 +1684,139 @@
</span><span class="cx">     g_assert_cmpfloat(atspi_value_get_current_value(ATSPI_VALUE(slider.get()), nullptr), ==, 0);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void testHyperlinkBasic(AccessibilityTest* test, gconstpointer)
+{
+    test->showInWindow(800, 600);
+    test->loadHtml(
+        "<html>"
+        "  <body>"
+        "    <a href='https://www.webkitgtk.org'>WebKitGTK</a>"
+        "    <div role='link'>Link</div>"
+        "    <p>This is <button>button1</button> and <button>button2</button> in a paragraph</p>"
+        "    <p>This is <a href='https://www.webkitgtk.org'>link1</a> and <a href='https://www.gnome.org'>link2</a> in 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), ==, 4);
+
+    auto section = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(section.get()));
+    g_assert_cmpint(atspi_accessible_get_role(section.get(), nullptr), ==, ATSPI_ROLE_SECTION);
+    g_assert_cmpint(atspi_accessible_get_child_count(section.get(), nullptr), ==, 1);
+
+    auto a = adoptGRef(atspi_accessible_get_child_at_index(section.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(a.get()));
+    g_assert_cmpint(atspi_accessible_get_role(a.get(), nullptr), ==, ATSPI_ROLE_LINK);
+    auto link = adoptGRef(atspi_accessible_get_hyperlink(a.get()));
+    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), ==, 0);
+#if USE(ATSPI)
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 1);
+#else
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 0);
+#endif
+    GUniquePtr<char> uri(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "https://www.webkitgtk.org/");
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == a.get());
+
+    auto div = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(div.get()));
+    g_assert_cmpint(atspi_accessible_get_role(div.get(), nullptr), ==, ATSPI_ROLE_LINK);
+    link = adoptGRef(atspi_accessible_get_hyperlink(div.get()));
+    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), ==, 0);
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 1);
+#else
+    g_assert_cmpint(atspi_hyperlink_get_start_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 10);
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 14);
+#endif
+    uri.reset(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "");
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == div.get());
+
+    auto p = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 2, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(p.get()));
+    g_assert_cmpint(atspi_accessible_get_child_count(p.get(), nullptr), ==, 2);
+    auto button1 = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(button1.get()));
+    link = adoptGRef(atspi_accessible_get_hyperlink(button1.get()));
+    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), ==, 16);
+#endif
+    uri.reset(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "");
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == button1.get());
+    auto button2 = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(button2.get()));
+    link = adoptGRef(atspi_accessible_get_hyperlink(button2.get()));
+    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), ==, 21);
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 29);
+#endif
+    uri.reset(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "");
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == button2.get());
+
+    p = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 3, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(p.get()));
+    g_assert_cmpint(atspi_accessible_get_child_count(p.get(), nullptr), ==, 2);
+    auto link1 = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(link1.get()));
+    link = adoptGRef(atspi_accessible_get_hyperlink(link1.get()));
+    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), ==, 13);
+#endif
+    uri.reset(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "https://www.webkitgtk.org/");
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == link1.get());
+    auto link2 = adoptGRef(atspi_accessible_get_child_at_index(p.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(link2.get()));
+    link = adoptGRef(atspi_accessible_get_hyperlink(link2.get()));
+    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), ==, 18);
+    g_assert_cmpint(atspi_hyperlink_get_end_index(ATSPI_HYPERLINK(link.get()), nullptr), ==, 23);
+#endif
+    uri.reset(atspi_hyperlink_get_uri(ATSPI_HYPERLINK(link.get()), 0, nullptr));
+    g_assert_cmpstr(uri.get(), ==, "https://www.gnome.org/");
+    g_assert_true(atspi_hyperlink_get_object(ATSPI_HYPERLINK(link.get()), 0, nullptr) == link2.get());
+}
+
</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">@@ -1686,6 +1838,7 @@
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "text/state-changed", testTextStateChanged);
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "text/replaced-objects", testTextReplacedObjects);
</span><span class="cx">     AccessibilityTest::add("WebKitAccessibility", "value/basic", testValueBasic);
</span><ins>+    AccessibilityTest::add("WebKitAccessibility", "hyperlink/basic", testHyperlinkBasic);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void afterAll()
</span></span></pre></div>
<a id="trunkToolsWebKitTestRunnerInjectedBundleatspiAccessibilityUIElementAtspicpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp (286049 => 286050)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp        2021-11-19 07:30:40 UTC (rev 286049)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp   2021-11-19 08:51:53 UTC (rev 286050)
</span><span class="lines">@@ -34,6 +34,7 @@
</span><span class="cx"> #include <WebCore/AccessibilityAtspiEnums.h>
</span><span class="cx"> #include <WebCore/AccessibilityObjectAtspi.h>
</span><span class="cx"> #include <WebKit/WKBundleFrame.h>
</span><ins>+#include <wtf/URL.h>
</ins><span class="cx"> #include <wtf/text/CString.h>
</span><span class="cx"> #include <wtf/text/StringBuilder.h>
</span><span class="cx"> #include <wtf/text/StringToIntegerConversion.h>
</span><span class="lines">@@ -1128,7 +1129,24 @@
</span><span class="cx"> 
</span><span class="cx"> JSRetainPtr<JSStringRef> AccessibilityUIElement::url()
</span><span class="cx"> {
</span><del>-    return JSStringCreateWithCharacters(0, 0);
</del><ins>+    if (!m_element->interfaces().contains(WebCore::AccessibilityObjectAtspi::Interface::Hyperlink))
+        return JSStringCreateWithCharacters(0, 0);
+
+    m_element->updateBackingStore();
+    auto axURL = m_element->url();
+    if (axURL.isNull())
+        return JSStringCreateWithUTF8CString("AXURL: (null)");
+
+    if (axURL.isLocalFile()) {
+        // Do not expose absolute paths.
+        auto path = axURL.fileSystemPath();
+        auto index = path.find("LayoutTests");
+        if (index != notFound)
+            path = path.substring(index);
+        return OpaqueJSString::tryCreate(makeString("AXURL: ", path)).leakRef();
+    }
+
+    return OpaqueJSString::tryCreate(makeString("AXURL: ", axURL.string())).leakRef();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool AccessibilityUIElement::addNotificationListener(JSValueRef functionCallback)
</span></span></pre>
</div>
</div>

</body>
</html>