AX: Support API: accessibilityReplaceRange:withText
authorcfleizach@apple.com <cfleizach@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 Apr 2019 00:39:50 +0000 (00:39 +0000)
committercfleizach@apple.com <cfleizach@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 9 Apr 2019 00:39:50 +0000 (00:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196636

Reviewed by Daniel Bates.

Source/WebCore:

Support this platform API on mac to provide a way to replace a range of editable text.

Test: accessibility/mac/replace-text-with-range.html

* accessibility/AccessibilityObject.cpp:
(WebCore::AccessibilityObject::replaceTextInRange):
* accessibility/AccessibilityObject.h:
* accessibility/mac/AccessibilityObjectBase.mm:
(WebCore::PlainTextRange::PlainTextRange):
* accessibility/mac/WebAccessibilityObjectWrapperMac.mm:
(-[WebAccessibilityObjectWrapper accessibilityReplaceRange:withText:]):

Tools:

* DumpRenderTree/AccessibilityUIElement.cpp:
(replaceTextInRangeCallback):
(AccessibilityUIElement::replaceTextInRange):
(AccessibilityUIElement::getJSClass):
* DumpRenderTree/AccessibilityUIElement.h:
* DumpRenderTree/ios/AccessibilityUIElementIOS.mm:
(AccessibilityUIElement::replaceTextInRange):
* DumpRenderTree/mac/AccessibilityUIElementMac.mm:
(AccessibilityUIElement::replaceTextInRange):
* WebKitTestRunner/InjectedBundle/AccessibilityUIElement.h:
* WebKitTestRunner/InjectedBundle/Bindings/AccessibilityUIElement.idl:
* WebKitTestRunner/InjectedBundle/ios/AccessibilityUIElementIOS.mm:
(WTR::AccessibilityUIElement::replaceTextInRange):
* WebKitTestRunner/InjectedBundle/mac/AccessibilityUIElementMac.mm:
(WTR::AccessibilityUIElement::replaceTextInRange):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244059 268f45cc-cd09-0410-ab3c-d52691b4dbfc

16 files changed:
LayoutTests/accessibility/mac/replace-text-with-range-expected.txt [new file with mode: 0644]
LayoutTests/accessibility/mac/replace-text-with-range.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/accessibility/AccessibilityObject.cpp
Source/WebCore/accessibility/AccessibilityObject.h
Source/WebCore/accessibility/mac/AccessibilityObjectBase.mm
Source/WebCore/accessibility/mac/WebAccessibilityObjectWrapperMac.mm
Tools/ChangeLog
Tools/DumpRenderTree/AccessibilityUIElement.cpp
Tools/DumpRenderTree/AccessibilityUIElement.h
Tools/DumpRenderTree/ios/AccessibilityUIElementIOS.mm
Tools/DumpRenderTree/mac/AccessibilityUIElementMac.mm
Tools/WebKitTestRunner/InjectedBundle/AccessibilityUIElement.h
Tools/WebKitTestRunner/InjectedBundle/Bindings/AccessibilityUIElement.idl
Tools/WebKitTestRunner/InjectedBundle/ios/AccessibilityUIElementIOS.mm
Tools/WebKitTestRunner/InjectedBundle/mac/AccessibilityUIElementMac.mm

diff --git a/LayoutTests/accessibility/mac/replace-text-with-range-expected.txt b/LayoutTests/accessibility/mac/replace-text-with-range-expected.txt
new file mode 100644 (file)
index 0000000..7940246
--- /dev/null
@@ -0,0 +1,17 @@
+This tests that the replace with range API functions as expected.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS contenteditable.replaceTextInRange("Blurb", 5, 5) is true
+PASS contenteditable.stringValue is 'AXValue: HelloBlurb'
+PASS contenteditable.replaceTextInRange("Blorg", 5, 5) is true
+PASS contenteditable.stringValue is 'AXValue: HelloBlorg'
+PASS text.replaceTextInRange("blurb", 6, 5) is true
+PASS text.stringValue is 'AXValue: Hello blurb'
+PASS textarea.replaceTextInRange("blurb", 6, 5) is true
+PASS textarea.stringValue is 'AXValue: Hello blurb'
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/accessibility/mac/replace-text-with-range.html b/LayoutTests/accessibility/mac/replace-text-with-range.html
new file mode 100644 (file)
index 0000000..e1eb01d
--- /dev/null
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../resources/js-test.js"></script>
+</head>
+<body id="body">
+<div id="content">
+
+<div id="contenteditable" contenteditable="true" role="textbox">HelloWorld</div>
+<input type="text" id="text" value="Hello world">
+<textarea id="textarea">Hello world</textarea>
+</div>
+
+<p id="description"></p>
+<div id="console"></div>
+
+<script>
+    description("This tests that the replace with range API functions as expected.");
+
+    if (window.accessibilityController) {
+        var contenteditable = accessibilityController.accessibleElementById("contenteditable");
+        shouldBeTrue('contenteditable.replaceTextInRange("Blurb", 5, 5)');
+        shouldBe("contenteditable.stringValue", "'AXValue: HelloBlurb'");
+
+        // now test what happens when a control has focus
+        document.getElementById("contenteditable").focus();
+        shouldBeTrue('contenteditable.replaceTextInRange("Blorg", 5, 5)');
+        shouldBe("contenteditable.stringValue", "'AXValue: HelloBlorg'");
+        document.getElementById("contenteditable").blur();
+
+        var text = accessibilityController.accessibleElementById("text");
+        shouldBeTrue('text.replaceTextInRange("blurb", 6, 5)');
+        shouldBe("text.stringValue", "'AXValue: Hello blurb'");
+
+        var textarea = accessibilityController.accessibleElementById("textarea");
+        shouldBeTrue('textarea.replaceTextInRange("blurb", 6, 5)');
+        shouldBe("textarea.stringValue", "'AXValue: Hello blurb'");
+
+        document.getElementById("content").style.visibility = "hidden";
+    }
+
+</script>
+
+</body>
+</html>
index 52ba667..e22a954 100644 (file)
@@ -1,3 +1,22 @@
+2019-04-08  Chris Fleizach  <cfleizach@apple.com>
+
+        AX: Support API: accessibilityReplaceRange:withText
+        https://bugs.webkit.org/show_bug.cgi?id=196636
+
+        Reviewed by Daniel Bates.
+
+        Support this platform API on mac to provide a way to replace a range of editable text.
+
+        Test: accessibility/mac/replace-text-with-range.html
+
+        * accessibility/AccessibilityObject.cpp:
+        (WebCore::AccessibilityObject::replaceTextInRange):
+        * accessibility/AccessibilityObject.h:
+        * accessibility/mac/AccessibilityObjectBase.mm:
+        (WebCore::PlainTextRange::PlainTextRange):
+        * accessibility/mac/WebAccessibilityObjectWrapperMac.mm:
+        (-[WebAccessibilityObjectWrapper accessibilityReplaceRange:withText:]):
+
 2019-04-08  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [iOS] Do not allow starting selection drags when selection views are not visible
index 9043365..7a71ffe 100644 (file)
@@ -2274,7 +2274,35 @@ bool AccessibilityObject::dispatchAccessibilityEventWithType(AccessibilityEventT
     auto event = Event::create(eventName, Event::CanBubble::Yes, Event::IsCancelable::Yes);
     return dispatchAccessibilityEvent(event);
 }
+    
+bool AccessibilityObject::replaceTextInRange(const String& replacementString, const PlainTextRange& range)
+{
+    if (!renderer() || !is<Element>(node()))
+        return false;
+
+    auto& element = downcast<Element>(*renderer()->node());
 
+    // We should use the editor's insertText to mimic typing into the field.
+    // Also only do this when the field is in editing mode.
+    auto& frame = renderer()->frame();
+    if (element.shouldUseInputMethod()) {
+        frame.selection().setSelectedRange(rangeForPlainTextRange(range).get(), DOWNSTREAM, FrameSelection::ShouldCloseTyping::Yes);
+        frame.editor().replaceSelectionWithText(replacementString, Editor::SelectReplacement::No, Editor::SmartReplace::No);
+        return true;
+    }
+    
+    if (is<HTMLInputElement>(element)) {
+        downcast<HTMLInputElement>(element).setRangeText(replacementString, range.start, range.length, "");
+        return true;
+    }
+    if (is<HTMLTextAreaElement>(element)) {
+        downcast<HTMLTextAreaElement>(element).setRangeText(replacementString, range.start, range.length, "");
+        return true;
+    }
+
+    return false;
+}
+    
 bool AccessibilityObject::dispatchAccessibleSetValueEvent(const String& value) const
 {
     if (!canSetValueAttribute())
index cd96968..d4f756a 100644 (file)
@@ -297,6 +297,10 @@ struct PlainTextRange {
         , length(l)
     { }
     
+#if PLATFORM(COCOA)
+    PlainTextRange(NSRange);
+#endif
+    
     bool isNull() const { return !start && !length; }
 };
 
@@ -701,6 +705,8 @@ public:
     virtual void setSelectedText(const String&) { }
     virtual void setSelectedTextRange(const PlainTextRange&) { }
     virtual void setValue(const String&) { }
+    bool replaceTextInRange(const String&, const PlainTextRange&);
+
     virtual void setValue(float) { }
     virtual void setSelected(bool) { }
     virtual void setSelectedRows(AccessibilityChildrenVector&) { }
index aaf2dc9..6b0c93d 100644 (file)
 
 namespace WebCore {
 
+PlainTextRange::PlainTextRange(NSRange r)
+    : start(r.location)
+    , length(r.length)
+{
+}
+    
 String AccessibilityObject::speechHintAttributeValue() const
 {
     auto speak = speakAsProperty();
index 7890608..a03f765 100644 (file)
@@ -3642,6 +3642,14 @@ IGNORE_WARNINGS_END
         [self accessibilityScrollToVisible];
 }
 
+- (BOOL)accessibilityReplaceRange:(NSRange)range withText:(NSString *)string
+{
+    if (![self updateObjectBackingStore])
+        return NO;
+    
+    return m_object->replaceTextInRange(string, PlainTextRange(range));
+}
+
 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
 - (void)accessibilitySetValue:(id)value forAttribute:(NSString*)attributeName
 IGNORE_WARNINGS_END
index 702b2b3..51ab3f6 100644 (file)
@@ -1,3 +1,26 @@
+2019-04-08  Chris Fleizach  <cfleizach@apple.com>
+
+        AX: Support API: accessibilityReplaceRange:withText
+        https://bugs.webkit.org/show_bug.cgi?id=196636
+
+        Reviewed by Daniel Bates.
+
+        * DumpRenderTree/AccessibilityUIElement.cpp:
+        (replaceTextInRangeCallback):
+        (AccessibilityUIElement::replaceTextInRange):
+        (AccessibilityUIElement::getJSClass):
+        * DumpRenderTree/AccessibilityUIElement.h:
+        * DumpRenderTree/ios/AccessibilityUIElementIOS.mm:
+        (AccessibilityUIElement::replaceTextInRange):
+        * DumpRenderTree/mac/AccessibilityUIElementMac.mm:
+        (AccessibilityUIElement::replaceTextInRange):
+        * WebKitTestRunner/InjectedBundle/AccessibilityUIElement.h:
+        * WebKitTestRunner/InjectedBundle/Bindings/AccessibilityUIElement.idl:
+        * WebKitTestRunner/InjectedBundle/ios/AccessibilityUIElementIOS.mm:
+        (WTR::AccessibilityUIElement::replaceTextInRange):
+        * WebKitTestRunner/InjectedBundle/mac/AccessibilityUIElementMac.mm:
+        (WTR::AccessibilityUIElement::replaceTextInRange):
+
 2019-04-08  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [iOS] Do not allow starting selection drags when selection views are not visible
index bd7c454..87e1e96 100644 (file)
@@ -806,6 +806,18 @@ static JSValueRef resetSelectedTextMarkerRangeCallback(JSContextRef context, JSO
     return JSValueMakeUndefined(context);
 }
 
+static JSValueRef replaceTextInRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    if (argumentCount < 3)
+        return JSValueMakeUndefined(context);
+
+    auto text = adopt(JSValueToStringCopy(context, arguments[0], exception));
+    int position = JSValueToNumber(context, arguments[1], exception);
+    int length = JSValueToNumber(context, arguments[2], exception);
+
+    return JSValueMakeBoolean(context, toAXElement(thisObject)->replaceTextInRange(text.get(), position, length));
+}
+
 static JSValueRef attributedStringForTextMarkerRangeContainsAttributeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
 {
     AccessibilityTextMarkerRange* markerRange = 0;
@@ -1609,6 +1621,11 @@ void AccessibilityUIElement::resetSelectedTextMarkerRange()
 {
 }
 
+void AccessibilityUIElement::replaceTextInRange(JSStringRef, int, int)
+{
+    return false;
+}
+
 int AccessibilityUIElement::textMarkerRangeLength(AccessibilityTextMarkerRange*)
 {
     return 0;
@@ -1931,6 +1948,7 @@ JSClassRef AccessibilityUIElement::getJSClass()
         { "textMarkerRangeForElement", textMarkerRangeForElementCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
         { "selectedTextMarkerRange", selectedTextMarkerRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
         { "resetSelectedTextMarkerRange", resetSelectedTextMarkerRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
+        { "replaceTextInRange", replaceTextInRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
         { "attributedStringForTextMarkerRangeContainsAttribute", attributedStringForTextMarkerRangeContainsAttributeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
         { "indexForTextMarker", indexForTextMarkerCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
         { "isTextMarkerValid", isTextMarkerValidCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
index 7f27f23..3a7081f 100644 (file)
@@ -277,7 +277,8 @@ public:
     AccessibilityTextMarkerRange selectedTextMarkerRange();
     void resetSelectedTextMarkerRange();
     bool setSelectedVisibleTextRange(AccessibilityTextMarkerRange*);
-    
+    bool replaceTextInRange(JSStringRef, int position, int length);
+
     JSRetainPtr<JSStringRef> stringForTextMarkerRange(AccessibilityTextMarkerRange*);
     JSRetainPtr<JSStringRef> attributedStringForTextMarkerRange(AccessibilityTextMarkerRange*);
     JSRetainPtr<JSStringRef> attributedStringForTextMarkerRangeWithOptions(AccessibilityTextMarkerRange*, bool includeSpellCheck);
index 9c3e572..e14774b 100644 (file)
@@ -477,6 +477,11 @@ AccessibilityTextMarkerRange AccessibilityUIElement::selectedTextMarkerRange()
     return nullptr;
 }
 
+bool AccessibilityUIElement::replaceTextInRange(JSStringRef, int, int)
+{
+    return false;
+}
+
 void AccessibilityUIElement::resetSelectedTextMarkerRange()
 {
 }
index fd60d40..e956923 100644 (file)
@@ -70,6 +70,7 @@
 typedef void (*AXPostedNotificationCallback)(id element, NSString* notification, void* context);
 
 @interface NSObject (WebKitAccessibilityAdditions)
+- (BOOL)accessibilityReplaceRange:(NSRange)range withText:(NSString *)string;
 - (NSArray *)accessibilityArrayAttributeValues:(NSString *)attribute index:(NSUInteger)index maxCount:(NSUInteger)maxCount;
 - (NSUInteger)accessibilityIndexOfChild:(id)child;
 - (NSUInteger)accessibilityArrayAttributeCount:(NSString *)attribute;
@@ -1633,6 +1634,14 @@ void AccessibilityUIElement::resetSelectedTextMarkerRange()
     END_AX_OBJC_EXCEPTIONS
 }
 
+bool AccessibilityUIElement::replaceTextInRange(JSStringRef string, int location, int length)
+{
+    BEGIN_AX_OBJC_EXCEPTIONS
+    return [m_element accessibilityReplaceRange:NSMakeRange(location, length) withText:[NSString stringWithJSStringRef:string]];
+    END_AX_OBJC_EXCEPTIONS
+    return false;
+}
+
 int AccessibilityUIElement::textMarkerRangeLength(AccessibilityTextMarkerRange* range)
 {
     BEGIN_AX_OBJC_EXCEPTIONS
index 45c31c0..dfa45ca 100644 (file)
@@ -279,6 +279,7 @@ public:
     RefPtr<AccessibilityTextMarkerRange> textMarkerRangeForMarkers(AccessibilityTextMarker* startMarker, AccessibilityTextMarker* endMarker);
     RefPtr<AccessibilityTextMarkerRange> selectedTextMarkerRange();
     void resetSelectedTextMarkerRange();
+    bool replaceTextInRange(JSStringRef, int position, int length);
     RefPtr<AccessibilityTextMarker> startTextMarkerForTextMarkerRange(AccessibilityTextMarkerRange*);
     RefPtr<AccessibilityTextMarker> endTextMarkerForTextMarkerRange(AccessibilityTextMarkerRange*);
     RefPtr<AccessibilityTextMarker> endTextMarkerForBounds(int x, int y, int width, int height);
index 9be0b1c..127e6a7 100644 (file)
@@ -205,6 +205,7 @@ interface AccessibilityUIElement {
     AccessibilityTextMarkerRange textMarkerRangeForMarkers(AccessibilityTextMarker startMarker, AccessibilityTextMarker endMarker);
     AccessibilityTextMarkerRange selectedTextMarkerRange();
     void resetSelectedTextMarkerRange();
+    boolean replaceTextInRange(DOMString string, long position, long length);
     AccessibilityTextMarker startTextMarkerForTextMarkerRange(AccessibilityTextMarkerRange range);
     AccessibilityTextMarker endTextMarkerForTextMarkerRange(AccessibilityTextMarkerRange range);
     AccessibilityTextMarker endTextMarkerForBounds(long x, long y, long width, long height);
index d839112..7ed6cbd 100644 (file)
@@ -1144,6 +1144,11 @@ RefPtr<AccessibilityTextMarker> AccessibilityUIElement::startTextMarkerForBounds
 {
     return nullptr;
 }
+    
+bool AccessibilityUIElement::replaceTextInRange(JSStringRef, int, int)
+{
+    return false;
+}
 
 RefPtr<AccessibilityTextMarker> AccessibilityUIElement::textMarkerForPoint(int x, int y)
 {
index 02e83a6..c9b40d8 100644 (file)
@@ -74,6 +74,7 @@
 typedef void (*AXPostedNotificationCallback)(id element, NSString* notification, void* context);
 
 @interface NSObject (WebKitAccessibilityAdditions)
+- (BOOL)accessibilityReplaceRange:(NSRange)range withText:(NSString *)string;
 - (NSArray *)accessibilityArrayAttributeValues:(NSString *)attribute index:(NSUInteger)index maxCount:(NSUInteger)maxCount;
 - (NSUInteger)accessibilityIndexOfChild:(id)child;
 - (NSUInteger)accessibilityArrayAttributeCount:(NSString *)attribute;
@@ -1806,6 +1807,14 @@ RefPtr<AccessibilityTextMarker> AccessibilityUIElement::endTextMarkerForBounds(i
     return nullptr;
 }
 
+bool AccessibilityUIElement::replaceTextInRange(JSStringRef string, int location, int length)
+{
+    BEGIN_AX_OBJC_EXCEPTIONS
+    return [m_element accessibilityReplaceRange:NSMakeRange(location, length) withText:[NSString stringWithJSStringRef:string]];
+    END_AX_OBJC_EXCEPTIONS
+    return false;
+}
+    
 RefPtr<AccessibilityTextMarker> AccessibilityUIElement::startTextMarkerForBounds(int x, int y, int width, int height)
 {
     BEGIN_AX_OBJC_EXCEPTIONS