AX: Select text mechanism returns results outside of text control regions
authorcfleizach@apple.com <cfleizach@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 19 May 2015 15:48:17 +0000 (15:48 +0000)
committercfleizach@apple.com <cfleizach@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 19 May 2015 15:48:17 +0000 (15:48 +0000)
https://bugs.webkit.org/show_bug.cgi?id=145041

Reviewed by Mario Sanchez Prada.

Source/WebCore:

The behavior of the select text mechanism (inside accessibility) should make sure that results
returned stay within text control elements if the search was initiated inside a text control element.

Test: platform/mac/accessibility/select-text-stays-inside-text-controls.html

* accessibility/AccessibilityObject.cpp:
(WebCore::rangeClosestToRange):
(WebCore::AccessibilityObject::selectText):

LayoutTests:

* platform/mac/accessibility/select-text-stays-inside-text-controls-expected.txt: Added.
* platform/mac/accessibility/select-text-stays-inside-text-controls.html: Added.

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

LayoutTests/ChangeLog
LayoutTests/platform/mac/accessibility/select-text-stays-inside-text-controls-expected.txt [new file with mode: 0644]
LayoutTests/platform/mac/accessibility/select-text-stays-inside-text-controls.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/accessibility/AccessibilityObject.cpp

index 149bf04..3030c1c 100644 (file)
@@ -1,3 +1,13 @@
+2015-05-14  Chris Fleizach  <cfleizach@apple.com>
+
+        AX: Select text mechanism returns results outside of text control regions
+        https://bugs.webkit.org/show_bug.cgi?id=145041
+
+        Reviewed by Mario Sanchez Prada.
+
+        * platform/mac/accessibility/select-text-stays-inside-text-controls-expected.txt: Added.
+        * platform/mac/accessibility/select-text-stays-inside-text-controls.html: Added.
+
 2015-05-19  Marcos Chavarría Teijeiro  <mchavarria@igalia.com>
 
         [GTK] Gardening 19th May
diff --git a/LayoutTests/platform/mac/accessibility/select-text-stays-inside-text-controls-expected.txt b/LayoutTests/platform/mac/accessibility/select-text-stays-inside-text-controls-expected.txt
new file mode 100644 (file)
index 0000000..cc8a2ee
--- /dev/null
@@ -0,0 +1,23 @@
+ outside text
+This tests that the select text mechanism will not return results outside of a text input control.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS result is 'textfield'
+PASS document.getElementById('textfield').selectionStart is 0
+PASS document.getElementById('textfield').selectionEnd is 9
+PASS result is ''
+PASS document.getElementById('textfield').selectionStart is 0
+PASS document.getElementById('textfield').selectionEnd is 0
+PASS result is 'text area'
+PASS document.getElementById('textarea').selectionStart is 0
+PASS document.getElementById('textarea').selectionEnd is 9
+PASS result is ''
+PASS document.getElementById('textarea').selectionStart is 0
+PASS document.getElementById('textarea').selectionEnd is 0
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/platform/mac/accessibility/select-text-stays-inside-text-controls.html b/LayoutTests/platform/mac/accessibility/select-text-stays-inside-text-controls.html
new file mode 100644 (file)
index 0000000..34e47b1
--- /dev/null
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../../resources/js-test-pre.js"></script>
+</head>
+<body>
+
+<input type="text" id="textfield" value="textfield text">
+<br>
+<textarea id="textarea">text area text</textarea>
+outside text
+
+<p id="description"></p>
+<div id="console"></div>
+
+<script>
+    description("This tests that the select text mechanism will not return results outside of a text input control.");
+    
+    if (window.accessibilityController) {
+        var textfield = accessibilityController.accessibleElementById("textfield");
+        var windowSelection = window.getSelection();
+
+        document.getElementById("textfield").focus();
+        document.getElementById("textfield").setSelectionRange(0, 0);
+
+        // Try to select text inside the text field.
+        result = textfield.selectTextWithCriteria("AXSelectTextAmbiguityResolutionClosestToSelection", "textfield");
+        shouldBe("result", "'textfield'");
+        shouldBe("document.getElementById('textfield').selectionStart", "0");
+        shouldBe("document.getElementById('textfield').selectionEnd", "9");
+
+        document.getElementById("textfield").setSelectionRange(0, 0);
+
+        // Try to select text outside the focused text field. It should fail.
+        result = textfield.selectTextWithCriteria("AXSelectTextAmbiguityResolutionClosestToSelection", "outside");
+        shouldBe("result", "''");
+        shouldBe("document.getElementById('textfield').selectionStart", "0");
+        shouldBe("document.getElementById('textfield').selectionEnd", "0");
+
+        // Do the same for text area
+        document.getElementById("textarea").focus();
+        document.getElementById("textarea").setSelectionRange(0, 0);
+
+        var textarea = accessibilityController.accessibleElementById("textarea");
+
+        // Try to select text inside the text field.
+        result = textarea.selectTextWithCriteria("AXSelectTextAmbiguityResolutionClosestToSelection", "text area");
+        shouldBe("result", "'text area'");
+        shouldBe("document.getElementById('textarea').selectionStart", "0");
+        shouldBe("document.getElementById('textarea').selectionEnd", "9");
+
+        document.getElementById("textarea").setSelectionRange(0, 0);
+
+        // Try to select text outside the focused text field. It should fail.
+        result = textarea.selectTextWithCriteria("AXSelectTextAmbiguityResolutionClosestToSelection", "outside");
+        shouldBe("result", "''");
+        shouldBe("document.getElementById('textarea').selectionStart", "0");
+        shouldBe("document.getElementById('textarea').selectionEnd", "0");
+    }
+</script>
+
+<script src="../../../resources/js-test-post.js"></script>
+</body>
+</html>
index 9f8d6ce..536bb9a 100644 (file)
@@ -1,3 +1,19 @@
+2015-05-14  Chris Fleizach  <cfleizach@apple.com>
+
+        AX: Select text mechanism returns results outside of text control regions
+        https://bugs.webkit.org/show_bug.cgi?id=145041
+
+        Reviewed by Mario Sanchez Prada.
+
+        The behavior of the select text mechanism (inside accessibility) should make sure that results
+        returned stay within text control elements if the search was initiated inside a text control element.
+
+        Test: platform/mac/accessibility/select-text-stays-inside-text-controls.html
+
+        * accessibility/AccessibilityObject.cpp:
+        (WebCore::rangeClosestToRange):
+        (WebCore::AccessibilityObject::selectText):
+
 2015-05-19  Chris Dumez  <cdumez@apple.com>
 
         Mark static variables as const when possible
index cb33809..0b4e115 100644 (file)
@@ -610,7 +610,15 @@ void AccessibilityObject::findMatchingObjects(AccessibilitySearchCriteria* crite
 static PassRefPtr<Range> rangeClosestToRange(Range* referenceRange, PassRefPtr<Range> afterRange, PassRefPtr<Range> beforeRange)
 {
     ASSERT(referenceRange);
+    
+    // The treeScope for shadow nodes may not be the same scope as another element in a document.
+    // Comparisons may fail in that case, which are expected behavior and should not assert.
+    if (afterRange && ((afterRange->startPosition().anchorNode()->compareDocumentPosition(referenceRange->endPosition().anchorNode()) & Node::DOCUMENT_POSITION_DISCONNECTED) == Node::DOCUMENT_POSITION_DISCONNECTED))
+        return nullptr;
     ASSERT(!afterRange || afterRange->startPosition() >= referenceRange->endPosition());
+    
+    if (beforeRange && ((beforeRange->endPosition().anchorNode()->compareDocumentPosition(referenceRange->startPosition().anchorNode()) & Node::DOCUMENT_POSITION_DISCONNECTED) == Node::DOCUMENT_POSITION_DISCONNECTED))
+        return nullptr;
     ASSERT(!beforeRange || beforeRange->endPosition() <= referenceRange->startPosition());
     
     if (!referenceRange || (!afterRange && !beforeRange))
@@ -710,6 +718,12 @@ String AccessibilityObject::selectText(AccessibilitySelectTextCriteria* criteria
     
     // Determine which candidate is closest to the selection and perform the activity.
     if (RefPtr<Range> closestStringRange = rangeClosestToRange(selectedStringRange.get(), closestAfterStringRange, closestBeforeStringRange)) {
+        // If the search started within a text control, ensure that the result is inside that element.
+        if (element() && element()->isTextFormControl()) {
+            if (!closestStringRange->startContainer()->isDescendantOrShadowDescendantOf(element()) || !closestStringRange->endContainer()->isDescendantOrShadowDescendantOf(element()))
+                return String();
+        }
+        
         String closestString = closestStringRange->text();
         bool replaceSelection = false;
         if (frame->selection().setSelectedRange(closestStringRange.get(), DOWNSTREAM, true)) {