Title: [211491] trunk
Revision
211491
Author
n_w...@apple.com
Date
2017-02-01 10:12:08 -0800 (Wed, 01 Feb 2017)

Log Message

AX: Incorrect range from index and length in text controls when there are newlines
https://bugs.webkit.org/show_bug.cgi?id=167677
<rdar://problem/30300099>

Reviewed by Chris Fleizach.

Source/WebCore:

When there are newlines in text controls, the VisiblePosition that is created
from the index could be wrong. Because the index doesn't include the information
regarding a position's affinity. Fixed it by using CharacterOffset instead, since
it doesn't require that information.

Test: accessibility/mac/range-for-contenteditable-newline.html

* accessibility/AXObjectCache.cpp:
(WebCore::AXObjectCache::traverseToOffsetInRange):
(WebCore::AXObjectCache::nextCharacterOffset):
(WebCore::AXObjectCache::characterOffsetForIndex):

LayoutTests:

* accessibility/mac/range-for-contenteditable-newline-expected.txt: Added.
* accessibility/mac/range-for-contenteditable-newline.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (211490 => 211491)


--- trunk/LayoutTests/ChangeLog	2017-02-01 18:08:57 UTC (rev 211490)
+++ trunk/LayoutTests/ChangeLog	2017-02-01 18:12:08 UTC (rev 211491)
@@ -1,3 +1,14 @@
+2017-02-01  Nan Wang  <n_w...@apple.com>
+
+        AX: Incorrect range from index and length in text controls when there are newlines
+        https://bugs.webkit.org/show_bug.cgi?id=167677
+        <rdar://problem/30300099>
+
+        Reviewed by Chris Fleizach.
+
+        * accessibility/mac/range-for-contenteditable-newline-expected.txt: Added.
+        * accessibility/mac/range-for-contenteditable-newline.html: Added.
+
 2017-02-01  Ryan Haddad  <ryanhad...@apple.com>
 
         ios-simulator TestExpectations gardening.

Added: trunk/LayoutTests/accessibility/mac/range-for-contenteditable-newline-expected.txt (0 => 211491)


--- trunk/LayoutTests/accessibility/mac/range-for-contenteditable-newline-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/accessibility/mac/range-for-contenteditable-newline-expected.txt	2017-02-01 18:12:08 UTC (rev 211491)
@@ -0,0 +1,25 @@
+some
+hello world
+test
+
+abc
+
+
+
+def
+This tests that when there are newline characters within text controls, we can get the correct text range from index and length.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS textSome is 'some'
+PASS textHello is 'hello'
+PASS textWorld is 'world'
+PASS textTest is 'test'
+PASS textareaTextHello is 'hello'
+PASS textareaTextWorld is 'world'
+PASS textDEF is 'def'
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/accessibility/mac/range-for-contenteditable-newline.html (0 => 211491)


--- trunk/LayoutTests/accessibility/mac/range-for-contenteditable-newline.html	                        (rev 0)
+++ trunk/LayoutTests/accessibility/mac/range-for-contenteditable-newline.html	2017-02-01 18:12:08 UTC (rev 211491)
@@ -0,0 +1,51 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src=""
+</head>
+<body id="body">
+
+<div id="content">
+<div id="textcontrol" contenteditable="true">some<div>hello world</div><div>test</div></div>
+<textarea id="textarea" cols='60' rows='8'>hello test
+world</textarea>
+</div>
+<div id="textcontrol2" contenteditable="true">abc<div><br></div<div><br></div><div><br></div><div>def</div></div>
+
+<div role="group" id="console"></div>
+
+<script>
+
+    description("This tests that when there are newline characters within text controls, we can get the correct text range from index and length.");
+
+    // Test contenteditable with newlines.
+    var textControl = accessibilityController.accessibleElementById("textcontrol");
+    var textSome = textControl.stringForRange(0, 4);
+    shouldBe("textSome", "'some'");
+    
+    var textHello = textControl.stringForRange(5, 5);
+    shouldBe("textHello", "'hello'");
+    
+    var textWorld = textControl.stringForRange(11, 5);
+    shouldBe("textWorld", "'world'");
+    
+    var textTest = textControl.stringForRange(17, 4);
+    shouldBe("textTest", "'test'");
+    
+    // Test <textarea> with newlines.
+    var textarea = accessibilityController.accessibleElementById("textarea");
+    var textareaTextHello = textarea.stringForRange(0, 5);
+    var textareaTextWorld = textarea.stringForRange(11, 5);
+    shouldBe("textareaTextHello", "'hello'");
+    shouldBe("textareaTextWorld", "'world'");
+    
+    // Test multiple <br> in contenteditable
+    var textControl2 = accessibilityController.accessibleElementById("textcontrol2");
+    var textDEF = textControl2.stringForRange(7, 3);
+    shouldBe("textDEF", "'def'");
+
+</script>
+
+<script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (211490 => 211491)


--- trunk/Source/WebCore/ChangeLog	2017-02-01 18:08:57 UTC (rev 211490)
+++ trunk/Source/WebCore/ChangeLog	2017-02-01 18:12:08 UTC (rev 211491)
@@ -1,3 +1,23 @@
+2017-02-01  Nan Wang  <n_w...@apple.com>
+
+        AX: Incorrect range from index and length in text controls when there are newlines
+        https://bugs.webkit.org/show_bug.cgi?id=167677
+        <rdar://problem/30300099>
+
+        Reviewed by Chris Fleizach.
+
+        When there are newlines in text controls, the VisiblePosition that is created
+        from the index could be wrong. Because the index doesn't include the information 
+        regarding a position's affinity. Fixed it by using CharacterOffset instead, since
+        it doesn't require that information.
+
+        Test: accessibility/mac/range-for-contenteditable-newline.html
+
+        * accessibility/AXObjectCache.cpp:
+        (WebCore::AXObjectCache::traverseToOffsetInRange):
+        (WebCore::AXObjectCache::nextCharacterOffset):
+        (WebCore::AXObjectCache::characterOffsetForIndex):
+
 2017-02-01  Timothy Hatcher  <timo...@hatcher.name>
 
         AXObjectCache is missing two functions at link time when !HAVE(ACCESSIBILITY)

Modified: trunk/Source/WebCore/accessibility/AXObjectCache.cpp (211490 => 211491)


--- trunk/Source/WebCore/accessibility/AXObjectCache.cpp	2017-02-01 18:08:57 UTC (rev 211490)
+++ trunk/Source/WebCore/accessibility/AXObjectCache.cpp	2017-02-01 18:12:08 UTC (rev 211491)
@@ -1564,7 +1564,7 @@
     bool finished = false;
     int lastStartOffset = 0;
     
-    TextIterator iterator(range.get());
+    TextIterator iterator(range.get(), TextIteratorEntersTextControls);
     
     // When the range has zero length, there might be replaced node or brTag that we need to increment the characterOffset.
     if (iterator.atEnd()) {
@@ -1617,6 +1617,11 @@
                     if (childNode && childNode->renderer() && childNode->renderer()->isBR()) {
                         currentNode = childNode;
                         hasReplacedNodeOrBR = true;
+                    } else if (currentNode->isShadowRoot()) {
+                        // Since we are entering text controls, we should set the currentNode
+                        // to be the shadow host when there's no content.
+                        currentNode = currentNode->shadowHost();
+                        continue;
                     } else if (currentNode != previousNode) {
                         // We should set the start offset and length for the current node in case this is the last iteration.
                         lastStartOffset = 1;
@@ -2125,9 +2130,11 @@
     CharacterOffset next = characterOffsetForNodeAndOffset(*characterOffset.node, nextOffset);
     
     // To be consistent with VisiblePosition, we should consider the case that current node end to next node start counts 1 offset.
-    bool isReplacedOrBR = isReplacedNodeOrBR(characterOffset.node) || isReplacedNodeOrBR(next.node);
-    if (!ignoreNextNodeStart && !next.isNull() && !isReplacedOrBR && next.node != characterOffset.node)
-        next = characterOffsetForNodeAndOffset(*next.node, 0, TraverseOptionIncludeStart);
+    if (!ignoreNextNodeStart && !next.isNull() && !isReplacedNodeOrBR(next.node) && next.node != characterOffset.node) {
+        int length = TextIterator::rangeLength(rangeForUnorderedCharacterOffsets(characterOffset, next).get());
+        if (length > nextOffset - characterOffset.offset)
+            next = characterOffsetForNodeAndOffset(*next.node, 0, TraverseOptionIncludeStart);
+    }
     
     return next;
 }
@@ -2608,12 +2615,16 @@
     if (!obj)
         return CharacterOffset();
     
-    // Since this would only work on rendered nodes, using VisiblePosition to create a collapsed
-    // range should be fine.
-    VisiblePosition vp = obj->visiblePositionForIndex(index);
-    RefPtr<Range> range = makeRange(vp, vp);
-    
-    return startOrEndCharacterOffsetForRange(range, true);
+    RefPtr<Range> range = obj->elementRange();
+    CharacterOffset start = startOrEndCharacterOffsetForRange(range, true);
+    CharacterOffset end = startOrEndCharacterOffsetForRange(range, false);
+    CharacterOffset result = start;
+    for (int i = 0; i < index; i++) {
+        result = nextCharacterOffset(result, false);
+        if (result.isEqual(end))
+            break;
+    }
+    return result;
 }
 
 int AXObjectCache::indexForCharacterOffset(const CharacterOffset& characterOffset, AccessibilityObject* obj)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to