1 | /* |
2 | * Copyright (C) 2009 Igalia S.L. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "FrameSelection.h" |
22 | |
23 | #if HAVE(ACCESSIBILITY) |
24 | |
25 | #include "AXObjectCache.h" |
26 | #include "Document.h" |
27 | #include "Frame.h" |
28 | #include "RenderListItem.h" |
29 | #include "WebKitAccessible.h" |
30 | #include "WebKitAccessibleUtil.h" |
31 | #include <glib.h> |
32 | #include <wtf/NeverDestroyed.h> |
33 | #include <wtf/RefPtr.h> |
34 | |
35 | namespace WebCore { |
36 | |
37 | static void emitTextSelectionChange(AccessibilityObject* object, VisibleSelection selection, int offset) |
38 | { |
39 | auto* axObject = object->wrapper(); |
40 | if (!axObject || !ATK_IS_TEXT(axObject)) |
41 | return; |
42 | |
43 | // We need to adjust the offset for the list item marker in Left-To-Right text because |
44 | // the list item marker is exposed through the text of the accessible list item rather |
45 | // than through a separate accessible object. |
46 | RenderObject* renderer = object->renderer(); |
47 | if (is<RenderListItem>(renderer) && renderer->style().direction() == TextDirection::LTR) |
48 | offset += downcast<RenderListItem>(*renderer).markerTextWithSuffix().length(); |
49 | |
50 | g_signal_emit_by_name(axObject, "text-caret-moved" , offset); |
51 | if (selection.isRange()) |
52 | g_signal_emit_by_name(axObject, "text-selection-changed" ); |
53 | } |
54 | |
55 | static void maybeEmitTextFocusChange(RefPtr<AccessibilityObject>&& object) |
56 | { |
57 | // This static variable is needed to keep track of the old object |
58 | // as per previous calls to this function, in order to properly |
59 | // decide whether to emit some signals or not. |
60 | static NeverDestroyed<RefPtr<AccessibilityObject>> oldObject; |
61 | |
62 | // Ensure the oldObject belongs to the same document that the |
63 | // current object so further comparisons make sense. Otherwise, |
64 | // just reset oldObject to 0 so it won't be taken into account in |
65 | // the immediately following call to this function. |
66 | if (object && oldObject.get() && oldObject.get()->document() != object->document()) |
67 | oldObject.get() = nullptr; |
68 | |
69 | auto* axObject = object ? object->wrapper() : nullptr; |
70 | auto* oldAxObject = oldObject.get() ? oldObject.get()->wrapper() : nullptr; |
71 | |
72 | if (axObject != oldAxObject) { |
73 | if (oldAxObject && ATK_IS_TEXT(oldAxObject)) { |
74 | g_signal_emit_by_name(oldAxObject, "focus-event" , FALSE); |
75 | atk_object_notify_state_change(ATK_OBJECT(oldAxObject), ATK_STATE_FOCUSED, FALSE); |
76 | } |
77 | if (axObject && ATK_IS_TEXT(axObject)) { |
78 | g_signal_emit_by_name(axObject, "focus-event" , TRUE); |
79 | atk_object_notify_state_change(ATK_OBJECT(axObject), ATK_STATE_FOCUSED, TRUE); |
80 | } |
81 | } |
82 | |
83 | // Update pointer to last focused object. |
84 | oldObject.get() = WTFMove(object); |
85 | } |
86 | |
87 | |
88 | void FrameSelection::notifyAccessibilityForSelectionChange(const AXTextStateChangeIntent&) |
89 | { |
90 | if (!AXObjectCache::accessibilityEnabled()) |
91 | return; |
92 | |
93 | if (!m_selection.start().isNotNull() || !m_selection.end().isNotNull()) |
94 | return; |
95 | |
96 | Node* focusedNode = m_selection.end().containerNode(); |
97 | if (!focusedNode) |
98 | return; |
99 | |
100 | AXObjectCache* cache = m_frame->document()->existingAXObjectCache(); |
101 | if (!cache) |
102 | return; |
103 | |
104 | AccessibilityObject* accessibilityObject = cache->getOrCreate(focusedNode->renderer()); |
105 | if (!accessibilityObject) |
106 | return; |
107 | |
108 | int offset; |
109 | RefPtr<AccessibilityObject> object = objectFocusedAndCaretOffsetUnignored(accessibilityObject, offset); |
110 | if (!object) |
111 | return; |
112 | |
113 | emitTextSelectionChange(object.get(), m_selection, offset); |
114 | maybeEmitTextFocusChange(WTFMove(object)); |
115 | } |
116 | |
117 | } // namespace WebCore |
118 | |
119 | #endif // HAVE(ACCESSIBILITY) |
120 | |