<!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>[211356] 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/211356">211356</a></dd>
<dt>Author</dt> <dd>n_wang@apple.com</dd>
<dt>Date</dt> <dd>2017-01-29 21:03:11 -0800 (Sun, 29 Jan 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>AX: WKContentView needs to implement UITextInput methods to make speak selection highlighting work
https://bugs.webkit.org/show_bug.cgi?id=166955

Reviewed by Ryosuke Niwa.

Source/WebCore:

Created a new version of Range::collectSelectionRect() that returns rects for each
line, so that Speak Selection doesn't need to handle searching for soft line breaks.
Also added a variant of findPlainText to search for the closest matched range to the given position.

Test: editing/text-iterator/range-of-string-closest-to-position.html

* dom/Range.cpp:
(WebCore::Range::collectSelectionRectsWithoutUnionInteriorLines):
(WebCore::Range::collectSelectionRects):
* dom/Range.h:
* editing/TextIterator.cpp:
(WebCore::findPlainTextMatches):
(WebCore::updateSearchBuffer):
(WebCore::findIteratorOptions):
(WebCore::rangeMatches):
(WebCore::findClosestPlainText):
(WebCore::findPlainText):
(WebCore::findPlainTextOffset): Deleted.
* editing/TextIterator.h:
* editing/htmlediting.h:
* testing/Internals.cpp:
(WebCore::Internals::rangeOfStringNearLocation):
* testing/Internals.h:
* testing/Internals.idl:

Source/WebKit2:

Implemented methods that Speak Selection can use to retrieve the word/sentence highlighting rects.

* Scripts/webkit/messages.py:
(headers_for_type):
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/ios/WKContentViewInteraction.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView webSelectionRectsForSelectionRects:]):
(-[WKContentView webSelectionRects]):
(-[WKContentView _accessibilityRetrieveRectsEnclosingSelectionOffset:withGranularity:]):
(-[WKContentView _accessibilityRetrieveRectsAtSelectionOffset:withText:]):
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::selectionRectsCallback):
(WebKit::WebPageProxy::requestRectsForGranularityWithSelectionOffset):
(WebKit::WebPageProxy::requestRectsAtSelectionOffsetWithText):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::visiblePositionForPositionWithOffset):
(WebKit::WebPage::getRectsForGranularityWithSelectionOffset):
(WebKit::rangeNearPositionMatchesText):
(WebKit::WebPage::getRectsAtSelectionOffsetWithText):

LayoutTests:

* editing/text-iterator/range-of-string-closest-to-position-expected.txt: Added.
* editing/text-iterator/range-of-string-closest-to-position.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomRangecpp">trunk/Source/WebCore/dom/Range.cpp</a></li>
<li><a href="#trunkSourceWebCoredomRangeh">trunk/Source/WebCore/dom/Range.h</a></li>
<li><a href="#trunkSourceWebCoreeditingTextIteratorcpp">trunk/Source/WebCore/editing/TextIterator.cpp</a></li>
<li><a href="#trunkSourceWebCoreeditingTextIteratorh">trunk/Source/WebCore/editing/TextIterator.h</a></li>
<li><a href="#trunkSourceWebCoreeditinghtmleditingh">trunk/Source/WebCore/editing/htmlediting.h</a></li>
<li><a href="#trunkSourceWebCoretestingInternalscpp">trunk/Source/WebCore/testing/Internals.cpp</a></li>
<li><a href="#trunkSourceWebCoretestingInternalsh">trunk/Source/WebCore/testing/Internals.h</a></li>
<li><a href="#trunkSourceWebCoretestingInternalsidl">trunk/Source/WebCore/testing/Internals.idl</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2Scriptswebkitmessagespy">trunk/Source/WebKit2/Scripts/webkit/messages.py</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebPageProxyh">trunk/Source/WebKit2/UIProcess/WebPageProxy.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebPageProxymessagesin">trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWKContentViewInteractionh">trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWKContentViewInteractionmm">trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWebPageProxyIOSmm">trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPageh">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPagemessagesin">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageiosWebPageIOSmm">trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestseditingtextiteratorrangeofstringclosesttopositionexpectedtxt">trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position-expected.txt</a></li>
<li><a href="#trunkLayoutTestseditingtextiteratorrangeofstringclosesttopositionhtml">trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/LayoutTests/ChangeLog        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2017-01-29  Nan Wang  &lt;n_wang@apple.com&gt;
+
+        AX: WKContentView needs to implement UITextInput methods to make speak selection highlighting work
+        https://bugs.webkit.org/show_bug.cgi?id=166955
+
+        Reviewed by Ryosuke Niwa.
+
+        * editing/text-iterator/range-of-string-closest-to-position-expected.txt: Added.
+        * editing/text-iterator/range-of-string-closest-to-position.html: Added.
+
</ins><span class="cx"> 2017-01-29  Yoav Weiss  &lt;yoav@yoav.ws&gt;
</span><span class="cx"> 
</span><span class="cx">         Add invalid value tests to Link header handling.
</span></span></pre></div>
<a id="trunkLayoutTestseditingtextiteratorrangeofstringclosesttopositionexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position-expected.txt (0 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position-expected.txt                                (rev 0)
+++ trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position-expected.txt        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -0,0 +1,11 @@
</span><ins>+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), 'the', 11).toArray() is secondThe
+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), 'the', 9).toArray() is secondThe
+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), 'the', 6).toArray() is firstThe
+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), 'the', 16).toArray() is secondThe
+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), 'The', 11).toArray() is upperThe
+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p2), '中文', 5).toArray() is chineseMatch
+PASS internals.rangeOfStringNearLocation(rangeOfNodeContent(p3), 'the', 5).toArray() is rtlMatch
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestseditingtextiteratorrangeofstringclosesttopositionhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position.html (0 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position.html                                (rev 0)
+++ trunk/LayoutTests/editing/text-iterator/range-of-string-closest-to-position.html        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -0,0 +1,75 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;meta charset=&quot;utf-8&quot;&gt; 
+&lt;script src=&quot;../../resources/js-test-pre.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;div id=&quot;test&quot; contenteditable&gt;
+&lt;p id=&quot;p1&quot;&gt;the car is the good the The world.&lt;/p&gt;
+&lt;p id=&quot;p2&quot;&gt;中文字是中文字&lt;/p&gt;
+&lt;bdo dir=&quot;rtl&quot; id=&quot;p3&quot;&gt;the best&lt;/bdo&gt;
+&lt;/div&gt;
+&lt;div id=&quot;console&quot;&gt;&lt;/div&gt;
+&lt;script&gt;
+
+if (!window.internals)
+    testFailed('This test requires internals object');
+else {
+    var container = document.getElementById('test');
+
+    function range(startContainer, startOffset, endContainer, endOffset) {
+        var range =  document.createRange();
+        range.setStart(startContainer, startOffset);
+        range.setEnd(endContainer, endOffset);
+        return range;
+    }
+
+    function rangeOfNodeContent(referenceNode) {
+        var range = document.createRange();
+        range.selectNodeContents(referenceNode);
+        return range;
+    }
+
+    Range.prototype.toArray = function () {
+        return [this.startContainer, this.startOffset, this.endContainer, this.endOffset];
+    }
+
+    var p1 = document.getElementById(&quot;p1&quot;);
+    var p2 = document.getElementById(&quot;p2&quot;);
+    var p3 = document.getElementById(&quot;p3&quot;);
+
+    var firstThe = [p1.firstChild, 0, p1.firstChild, 3];
+    var secondThe = [p1.firstChild, 11, p1.firstChild, 14];
+    var upperThe = [p1.firstChild, 24, p1.firstChild, 27];
+    var chineseMatch = [p2.firstChild, 4, p2.firstChild, 6];
+    var germanMatch = [p3.firstChild, 5, p3.firstChild, 6];
+    var rtlMatch = [p3.firstChild, 0, p3.firstChild, 3];
+
+    // When the target position is correct, at the beginning of the matched range.
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), \'the\', 11).toArray()', 'secondThe');
+
+    // Cases that the matched range is on either side of the target position.
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), \'the\', 9).toArray()', 'secondThe');    
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), \'the\', 6).toArray()', 'firstThe');
+
+    // In this case, target position is at the beginning of &quot;oog&quot;, make sure it's picking the 
+    // left side &quot;the&quot; as the closest match.
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), \'the\', 16).toArray()', 'secondThe');
+
+    // Upper case.
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p1), \'The\', 11).toArray()', 'upperThe');
+
+    // CJK
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p2), \'中文\', 5).toArray()', 'chineseMatch');
+
+    // RtL
+    shouldBe('internals.rangeOfStringNearLocation(rangeOfNodeContent(p3), \'the\', 5).toArray()', 'rtlMatch');
+
+    container.style.display = 'none';
+}
+
+&lt;/script&gt;
+&lt;script src=&quot;../../resources/js-test-post.js&quot;&gt;&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/ChangeLog        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -1,3 +1,35 @@
</span><ins>+2017-01-29  Nan Wang  &lt;n_wang@apple.com&gt;
+
+        AX: WKContentView needs to implement UITextInput methods to make speak selection highlighting work
+        https://bugs.webkit.org/show_bug.cgi?id=166955
+
+        Reviewed by Ryosuke Niwa.
+
+        Created a new version of Range::collectSelectionRect() that returns rects for each
+        line, so that Speak Selection doesn't need to handle searching for soft line breaks.
+        Also added a variant of findPlainText to search for the closest matched range to the given position.
+
+        Test: editing/text-iterator/range-of-string-closest-to-position.html
+
+        * dom/Range.cpp:
+        (WebCore::Range::collectSelectionRectsWithoutUnionInteriorLines):
+        (WebCore::Range::collectSelectionRects):
+        * dom/Range.h:
+        * editing/TextIterator.cpp:
+        (WebCore::findPlainTextMatches):
+        (WebCore::updateSearchBuffer):
+        (WebCore::findIteratorOptions):
+        (WebCore::rangeMatches):
+        (WebCore::findClosestPlainText):
+        (WebCore::findPlainText):
+        (WebCore::findPlainTextOffset): Deleted.
+        * editing/TextIterator.h:
+        * editing/htmlediting.h:
+        * testing/Internals.cpp:
+        (WebCore::Internals::rangeOfStringNearLocation):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
</ins><span class="cx"> 2017-01-29  Andy Estes  &lt;aestes@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [QuickLook] Add a WebPreference to enable saving QuickLook documents in WebKitLegacy
</span></span></pre></div>
<a id="trunkSourceWebCoredomRangecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Range.cpp (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Range.cpp        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/dom/Range.cpp        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -1266,7 +1266,7 @@
</span><span class="cx"> 
</span><span class="cx"> // This function is similar in spirit to addLineBoxRects, but annotates the returned rectangles
</span><span class="cx"> // with additional state which helps iOS draw selections in its unique way.
</span><del>-void Range::collectSelectionRects(Vector&lt;SelectionRect&gt;&amp; rects)
</del><ins>+int Range::collectSelectionRectsWithoutUnionInteriorLines(Vector&lt;SelectionRect&gt;&amp; rects)
</ins><span class="cx"> {
</span><span class="cx">     auto&amp; startContainer = this-&gt;startContainer();
</span><span class="cx">     auto&amp; endContainer = this-&gt;endContainer();
</span><span class="lines">@@ -1325,7 +1325,7 @@
</span><span class="cx">         VisiblePosition endPosition(createLegacyEditingPosition(&amp;endContainer, endOffset), VP_DEFAULT_AFFINITY);
</span><span class="cx">         VisiblePosition brPosition(createLegacyEditingPosition(stopNode, 0), VP_DEFAULT_AFFINITY);
</span><span class="cx">         if (endPosition == brPosition)
</span><del>-            rects.last().setIsLineBreak(true);    
</del><ins>+            rects.last().setIsLineBreak(true);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     int lineTop = std::numeric_limits&lt;int&gt;::max();
</span><span class="lines">@@ -1422,7 +1422,15 @@
</span><span class="cx">         } else if (selectionRect.direction() == LTR &amp;&amp; selectionRect.isLastOnLine())
</span><span class="cx">             selectionRect.setLogicalWidth(selectionRect.maxX() - selectionRect.logicalLeft());
</span><span class="cx">     }
</span><ins>+    
+    return maxLineNumber;
+}
</ins><span class="cx"> 
</span><ins>+void Range::collectSelectionRects(Vector&lt;SelectionRect&gt;&amp; rects)
+{
+    int maxLineNumber = collectSelectionRectsWithoutUnionInteriorLines(rects);
+    const size_t numberOfRects = rects.size();
+    
</ins><span class="cx">     // Union all the rectangles on interior lines (i.e. not first or last).
</span><span class="cx">     // On first and last lines, just avoid having overlaps by merging intersecting rectangles.
</span><span class="cx">     Vector&lt;SelectionRect&gt; unionedRects;
</span></span></pre></div>
<a id="trunkSourceWebCoredomRangeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Range.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Range.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/dom/Range.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -125,6 +125,7 @@
</span><span class="cx">     WEBCORE_EXPORT FloatRect absoluteBoundingRect() const;
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx">     WEBCORE_EXPORT void collectSelectionRects(Vector&lt;SelectionRect&gt;&amp;);
</span><ins>+    WEBCORE_EXPORT int collectSelectionRectsWithoutUnionInteriorLines(Vector&lt;SelectionRect&gt;&amp;);
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     void nodeChildrenChanged(ContainerNode&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCoreeditingTextIteratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/TextIterator.cpp (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/TextIterator.cpp        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/editing/TextIterator.cpp        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -2656,10 +2656,28 @@
</span><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static std::optional&lt;std::pair&lt;size_t, size_t&gt;&gt; findPlainTextOffset(SearchBuffer&amp; buffer, CharacterIterator&amp; findIterator, bool searchForward)
</del><ins>+static TextIteratorBehavior findIteratorOptions(FindOptions options)
</ins><span class="cx"> {
</span><del>-    size_t matchStart = 0;
-    size_t matchLength = 0;
</del><ins>+    TextIteratorBehavior iteratorOptions = TextIteratorEntersTextControls | TextIteratorClipsToFrameAncestors;
+    if (!(options &amp; DoNotTraverseFlatTree))
+        iteratorOptions |= TextIteratorTraversesFlatTree;
+    return iteratorOptions;
+}
+
+static void findPlainTextMatches(const Range&amp; range, const String&amp; target, FindOptions options, const std::function&lt;bool(size_t, size_t)&gt;&amp; match)
+{
+    SearchBuffer buffer(target, options);
+    if (buffer.needsMoreContext()) {
+        Ref&lt;Range&gt; beforeStartRange = range.ownerDocument().createRange();
+        beforeStartRange-&gt;setEnd(range.startContainer(), range.startOffset());
+        for (SimplifiedBackwardsTextIterator backwardsIterator(beforeStartRange.get()); !backwardsIterator.atEnd(); backwardsIterator.advance()) {
+            buffer.prependContext(backwardsIterator.text());
+            if (!buffer.needsMoreContext())
+                break;
+        }
+    }
+
+    CharacterIterator findIterator(range, findIteratorOptions(options));
</ins><span class="cx">     while (!findIterator.atEnd()) {
</span><span class="cx">         findIterator.advance(buffer.append(findIterator.text()));
</span><span class="cx">         while (1) {
</span><span class="lines">@@ -2674,45 +2692,53 @@
</span><span class="cx">             }
</span><span class="cx">             size_t lastCharacterInBufferOffset = findIterator.characterOffset();
</span><span class="cx">             ASSERT(lastCharacterInBufferOffset &gt;= matchStartOffset);
</span><del>-            matchStart = lastCharacterInBufferOffset - matchStartOffset;
-            matchLength = newMatchLength;
-            if (searchForward) // Look for the last match when searching backwards instead.
-                return std::pair&lt;size_t, size_t&gt; { matchStart, matchLength };
</del><ins>+            if (match(lastCharacterInBufferOffset - matchStartOffset, newMatchLength))
+                return;
</ins><span class="cx">         }
</span><span class="cx">     }
</span><ins>+}
</ins><span class="cx"> 
</span><ins>+static Ref&lt;Range&gt; rangeForMatch(const Range&amp; range, FindOptions options, size_t matchStart, size_t matchLength, bool searchForward)
+{
</ins><span class="cx">     if (!matchLength)
</span><del>-        return std::nullopt;
</del><ins>+        return collapsedToBoundary(range, searchForward);
+    CharacterIterator rangeComputeIterator(range, findIteratorOptions(options));
+    return characterSubrange(range.ownerDocument(), rangeComputeIterator, matchStart, matchLength);
+}
</ins><span class="cx"> 
</span><del>-    return std::pair&lt;size_t, size_t&gt; { matchStart, matchLength };
</del><ins>+Ref&lt;Range&gt; findClosestPlainText(const Range&amp; range, const String&amp; target, FindOptions options, unsigned targetOffset)
+{
+    size_t matchStart = 0;
+    size_t matchLength = 0;
+    size_t distance = std::numeric_limits&lt;size_t&gt;::max();
+    auto match = [targetOffset, &amp;distance, &amp;matchStart, &amp;matchLength] (size_t start, size_t length) {
+        size_t newDistance = std::min(abs(static_cast&lt;signed&gt;(start - targetOffset)), abs(static_cast&lt;signed&gt;(start + length - targetOffset)));
+        if (newDistance &lt; distance) {
+            matchStart = start;
+            matchLength = length;
+            distance = newDistance;
+        }
+        return false;
+    };
+
+    findPlainTextMatches(range, target, options, match);
+    return rangeForMatch(range, options, matchStart, matchLength, !(options &amp; Backwards));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Ref&lt;Range&gt; findPlainText(const Range&amp; range, const String&amp; target, FindOptions options)
</span><span class="cx"> {
</span><del>-    SearchBuffer buffer(target, options);
-
-    if (buffer.needsMoreContext()) {
-        Ref&lt;Range&gt; beforeStartRange = range.ownerDocument().createRange();
-        beforeStartRange-&gt;setEnd(range.startContainer(), range.startOffset());
-        for (SimplifiedBackwardsTextIterator backwardsIterator(beforeStartRange.get()); !backwardsIterator.atEnd(); backwardsIterator.advance()) {
-            buffer.prependContext(backwardsIterator.text());
-            if (!buffer.needsMoreContext())
-                break;
-        }
-    }
-
</del><span class="cx">     bool searchForward = !(options &amp; Backwards);
</span><del>-    TextIteratorBehavior iteratorOptions = TextIteratorEntersTextControls | TextIteratorClipsToFrameAncestors;
-    if (!(options &amp; DoNotTraverseFlatTree))
-        iteratorOptions |= TextIteratorTraversesFlatTree;
</del><ins>+    size_t matchStart = 0;
+    size_t matchLength = 0;
+    auto match = [searchForward, &amp;matchStart, &amp;matchLength] (size_t start, size_t length) {
+        matchStart = start;
+        matchLength = length;
+        // Look for the last match when searching backwards instead.
+        return searchForward;
+    };
</ins><span class="cx"> 
</span><del>-    CharacterIterator findIterator(range, iteratorOptions);
-    auto result = findPlainTextOffset(buffer, findIterator, searchForward);
-    if (!result)
-        return collapsedToBoundary(range, searchForward);
-
-    CharacterIterator rangeComputeIterator(range, iteratorOptions);
-    return characterSubrange(range.ownerDocument(), rangeComputeIterator, result-&gt;first, result-&gt;second);
</del><ins>+    findPlainTextMatches(range, target, options, match);
+    return rangeForMatch(range, options, matchStart, matchLength, searchForward);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreeditingTextIteratorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/TextIterator.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/TextIterator.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/editing/TextIterator.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -45,6 +45,7 @@
</span><span class="cx"> WEBCORE_EXPORT String plainText(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
</span><span class="cx"> WEBCORE_EXPORT String plainTextReplacingNoBreakSpace(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
</span><span class="cx"> Ref&lt;Range&gt; findPlainText(const Range&amp;, const String&amp;, FindOptions);
</span><ins>+WEBCORE_EXPORT Ref&lt;Range&gt; findClosestPlainText(const Range&amp;, const String&amp;, FindOptions, unsigned);
</ins><span class="cx"> 
</span><span class="cx"> // FIXME: Move this somewhere else in the editing directory. It doesn't belong here.
</span><span class="cx"> bool isRendererReplacedElement(RenderObject*);
</span></span></pre></div>
<a id="trunkSourceWebCoreeditinghtmleditingh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/editing/htmlediting.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/editing/htmlediting.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/editing/htmlediting.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -144,9 +144,9 @@
</span><span class="cx"> 
</span><span class="cx"> int comparePositions(const VisiblePosition&amp;, const VisiblePosition&amp;);
</span><span class="cx"> 
</span><del>-int indexForVisiblePosition(const VisiblePosition&amp;, RefPtr&lt;ContainerNode&gt;&amp; scope);
</del><ins>+WEBCORE_EXPORT int indexForVisiblePosition(const VisiblePosition&amp;, RefPtr&lt;ContainerNode&gt;&amp; scope);
</ins><span class="cx"> int indexForVisiblePosition(Node&amp;, const VisiblePosition&amp;, bool forSelectionPreservation);
</span><del>-VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope);
</del><ins>+WEBCORE_EXPORT VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope);
</ins><span class="cx"> VisiblePosition visiblePositionForIndexUsingCharacterIterator(Node&amp;, int index); // FIXME: Why do we need this version?
</span><span class="cx"> 
</span><span class="cx"> // -------------------------------------------------------------------------
</span></span></pre></div>
<a id="trunkSourceWebCoretestingInternalscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/testing/Internals.cpp (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/testing/Internals.cpp        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/testing/Internals.cpp        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -1456,6 +1456,11 @@
</span><span class="cx">     return TextIterator::subrange(&amp;range, rangeLocation, rangeLength);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+RefPtr&lt;Range&gt; Internals::rangeOfStringNearLocation(const Range&amp; searchRange, const String&amp; text, unsigned targetOffset)
+{
+    return findClosestPlainText(searchRange, text, 0, targetOffset);
+}
+
</ins><span class="cx"> ExceptionOr&lt;RefPtr&lt;Range&gt;&gt; Internals::rangeForDictionaryLookupAtLocation(int x, int y)
</span><span class="cx"> {
</span><span class="cx"> #if PLATFORM(MAC)
</span></span></pre></div>
<a id="trunkSourceWebCoretestingInternalsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/testing/Internals.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/testing/Internals.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/testing/Internals.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -201,6 +201,7 @@
</span><span class="cx">     String rangeAsText(const Range&amp;);
</span><span class="cx">     Ref&lt;Range&gt; subrange(Range&amp;, int rangeLocation, int rangeLength);
</span><span class="cx">     ExceptionOr&lt;RefPtr&lt;Range&gt;&gt; rangeForDictionaryLookupAtLocation(int x, int y);
</span><ins>+    RefPtr&lt;Range&gt; rangeOfStringNearLocation(const Range&amp;, const String&amp;, unsigned);
</ins><span class="cx"> 
</span><span class="cx">     ExceptionOr&lt;void&gt; setDelegatesScrolling(bool enabled);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoretestingInternalsidl"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/testing/Internals.idl (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/testing/Internals.idl        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebCore/testing/Internals.idl        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -195,6 +195,7 @@
</span><span class="cx">     DOMString rangeAsText(Range range);
</span><span class="cx">     Range subrange(Range range, long rangeLocation, long rangeLength);
</span><span class="cx">     [MayThrowException] Range? rangeForDictionaryLookupAtLocation(long x, long y);
</span><ins>+    Range? rangeOfStringNearLocation(Range range, DOMString text, long targetOffset);
</ins><span class="cx"> 
</span><span class="cx">     [MayThrowException] void setDelegatesScrolling(boolean enabled);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/ChangeLog        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -1,3 +1,34 @@
</span><ins>+2017-01-29  Nan Wang  &lt;n_wang@apple.com&gt;
+
+        AX: WKContentView needs to implement UITextInput methods to make speak selection highlighting work
+        https://bugs.webkit.org/show_bug.cgi?id=166955
+
+        Reviewed by Ryosuke Niwa.
+
+        Implemented methods that Speak Selection can use to retrieve the word/sentence highlighting rects.
+
+        * Scripts/webkit/messages.py:
+        (headers_for_type):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/ios/WKContentViewInteraction.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView webSelectionRectsForSelectionRects:]):
+        (-[WKContentView webSelectionRects]):
+        (-[WKContentView _accessibilityRetrieveRectsEnclosingSelectionOffset:withGranularity:]):
+        (-[WKContentView _accessibilityRetrieveRectsAtSelectionOffset:withText:]):
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::selectionRectsCallback):
+        (WebKit::WebPageProxy::requestRectsForGranularityWithSelectionOffset):
+        (WebKit::WebPageProxy::requestRectsAtSelectionOffsetWithText):
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::visiblePositionForPositionWithOffset):
+        (WebKit::WebPage::getRectsForGranularityWithSelectionOffset):
+        (WebKit::rangeNearPositionMatchesText):
+        (WebKit::WebPage::getRectsAtSelectionOffsetWithText):
+
</ins><span class="cx"> 2017-01-29  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [iOS] Expose WebCore::DataDetection::detectContentInRange WKWebProcessPlugInRangeHandle
</span></span></pre></div>
<a id="trunkSourceWebKit2Scriptswebkitmessagespy"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Scripts/webkit/messages.py (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Scripts/webkit/messages.py        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/Scripts/webkit/messages.py        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -365,6 +365,7 @@
</span><span class="cx">         'WebCore::TextIndicatorData': ['&lt;WebCore/TextIndicator.h&gt;'],
</span><span class="cx">         'WebCore::TextureMapperAnimations': ['&lt;WebCore/TextureMapperAnimation.h&gt;'],
</span><span class="cx">         'WebCore::ViewportAttributes': ['&lt;WebCore/ViewportArguments.h&gt;'],
</span><ins>+        'WebCore::SelectionRect': ['&quot;EditorState.h&quot;'],
</ins><span class="cx">         'WebKit::BackForwardListItemState': ['&quot;SessionState.h&quot;'],
</span><span class="cx">         'WebKit::LayerHostingMode': ['&quot;LayerTreeContext.h&quot;'],
</span><span class="cx">         'WebKit::PageState': ['&quot;SessionState.h&quot;'],
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebPageProxyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebPageProxy.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -256,6 +256,7 @@
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> typedef GenericCallback&lt;const WebCore::IntPoint&amp;, uint32_t, uint32_t, uint32_t&gt; GestureCallback;
</span><span class="cx"> typedef GenericCallback&lt;const WebCore::IntPoint&amp;, uint32_t, uint32_t&gt; TouchesCallback;
</span><ins>+typedef GenericCallback&lt;const Vector&lt;WebCore::SelectionRect&gt;&amp;&gt; SelectionRectsCallback;
</ins><span class="cx"> struct NodeAssistanceArguments {
</span><span class="cx">     AssistedNodeInformation m_nodeInformation;
</span><span class="cx">     bool m_userIsInteracting;
</span><span class="lines">@@ -545,6 +546,8 @@
</span><span class="cx">     void handleTwoFingerTapAtPoint(const WebCore::IntPoint&amp;, uint64_t requestID);
</span><span class="cx">     void setForceAlwaysUserScalable(bool);
</span><span class="cx">     void setIsScrollingOrZooming(bool);
</span><ins>+    void requestRectsForGranularityWithSelectionOffset(WebCore::TextGranularity, uint32_t offset, std::function&lt;void(const Vector&lt;WebCore::SelectionRect&gt;&amp;, CallbackBase::Error)&gt;);
+    void requestRectsAtSelectionOffsetWithText(int32_t offset, const String&amp;, std::function&lt;void(const Vector&lt;WebCore::SelectionRect&gt;&amp;, CallbackBase::Error)&gt;);
</ins><span class="cx"> #if ENABLE(DATA_INTERACTION)
</span><span class="cx">     void didPerformDataInteractionControllerOperation();
</span><span class="cx">     void requestStartDataInteraction(const WebCore::IntPoint&amp; clientPosition, const WebCore::IntPoint&amp; globalPosition);
</span><span class="lines">@@ -1477,6 +1480,7 @@
</span><span class="cx">     void selectionContextCallback(const String&amp;, const String&amp;, const String&amp;, uint64_t);
</span><span class="cx">     void interpretKeyEvent(const EditorState&amp;, bool isCharEvent, bool&amp; handled);
</span><span class="cx">     void showPlaybackTargetPicker(bool hasVideo, const WebCore::IntRect&amp; elementRect);
</span><ins>+    void selectionRectsCallback(const Vector&lt;WebCore::SelectionRect&gt;&amp;, uint64_t);
</ins><span class="cx"> #endif
</span><span class="cx"> #if PLATFORM(GTK)
</span><span class="cx">     void printFinishedCallback(const WebCore::ResourceError&amp;, uint64_t);
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebPageProxymessagesin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.messages.in        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -195,6 +195,7 @@
</span><span class="cx">     DidNotHandleTapAsClick(WebCore::IntPoint point)
</span><span class="cx">     DisableDoubleTapGesturesDuringTapIfNecessary(uint64_t requestID)
</span><span class="cx">     DrawToPDFCallback(IPC::DataReference pdfData, uint64_t callbackID)
</span><ins>+    SelectionRectsCallback(Vector&lt;WebCore::SelectionRect&gt; selectionRects, uint64_t callbackID);
</ins><span class="cx"> #endif
</span><span class="cx"> #if ENABLE(DATA_DETECTION)
</span><span class="cx">     SetDataDetectionResult(struct WebKit::DataDetectionResult dataDetectionResult)
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWKContentViewInteractionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -247,6 +247,8 @@
</span><span class="cx"> - (NSArray *)_dataDetectionResults;
</span><span class="cx"> - (NSArray&lt;NSValue *&gt; *)_uiTextSelectionRects;
</span><span class="cx"> - (void)accessibilityRetrieveSpeakSelectionContent;
</span><ins>+- (void)_accessibilityRetrieveRectsEnclosingSelectionOffset:(NSInteger)offset withGranularity:(UITextGranularity)granularity;
+- (void)_accessibilityRetrieveRectsAtSelectionOffset:(NSInteger)offset withText:(NSString *)text;
</ins><span class="cx"> 
</span><span class="cx"> #if ENABLE(DATA_INTERACTION)
</span><span class="cx"> - (void)_didPerformDataInteractionControllerOperation;
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWKContentViewInteractionmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -495,6 +495,8 @@
</span><span class="cx"> 
</span><span class="cx"> @interface WKContentView (WKInteractionPrivate)
</span><span class="cx"> - (void)accessibilitySpeakSelectionSetContent:(NSString *)string;
</span><ins>+- (NSArray *)webSelectionRectsForSelectionRects:(const Vector&lt;WebCore::SelectionRect&gt;&amp;)selectionRects;
+- (void)_accessibilityDidGetSelectionRects:(NSArray *)selectionRects withGranularity:(UITextGranularity)granularity atOffset:(NSInteger)offset;
</ins><span class="cx"> @end
</span><span class="cx"> 
</span><span class="cx"> @implementation WKContentView (WKInteraction)
</span><span class="lines">@@ -1496,11 +1498,8 @@
</span><span class="cx">     return _positionInformation.nodeAtPositionIsAssistedNode;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-- (NSArray *)webSelectionRects
</del><ins>+- (NSArray *)webSelectionRectsForSelectionRects:(const Vector&lt;WebCore::SelectionRect&gt;&amp;)selectionRects
</ins><span class="cx"> {
</span><del>-    if (_page-&gt;editorState().selectionIsNone)
-        return nil;
-    const auto&amp; selectionRects = _page-&gt;editorState().postLayoutData().selectionRects;
</del><span class="cx">     unsigned size = selectionRects.size();
</span><span class="cx">     if (!size)
</span><span class="cx">         return nil;
</span><span class="lines">@@ -1524,6 +1523,14 @@
</span><span class="cx">     return webRects;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (NSArray *)webSelectionRects
+{
+    if (_page-&gt;editorState().selectionIsNone)
+        return nil;
+    const auto&amp; selectionRects = _page-&gt;editorState().postLayoutData().selectionRects;
+    return [self webSelectionRectsForSelectionRects:selectionRects];
+}
+
</ins><span class="cx"> - (void)_highlightLongPressRecognized:(UILongPressGestureRecognizer *)gestureRecognizer
</span><span class="cx"> {
</span><span class="cx">     ASSERT(gestureRecognizer == _highlightLongPressGestureRecognizer);
</span><span class="lines">@@ -2197,6 +2204,28 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (void)_accessibilityRetrieveRectsEnclosingSelectionOffset:(NSInteger)offset withGranularity:(UITextGranularity)granularity
+{
+    RetainPtr&lt;WKContentView&gt; view = self;
+    _page-&gt;requestRectsForGranularityWithSelectionOffset(toWKTextGranularity(granularity), offset , [view, offset, granularity](const Vector&lt;WebCore::SelectionRect&gt;&amp; selectionRects, CallbackBase::Error error) {
+        if (error != WebKit::CallbackBase::Error::None)
+            return;
+        if ([view respondsToSelector:@selector(_accessibilityDidGetSelectionRects:withGranularity:atOffset:)])
+            [view _accessibilityDidGetSelectionRects:[view webSelectionRectsForSelectionRects:selectionRects] withGranularity:granularity atOffset:offset];
+    });
+}
+
+- (void)_accessibilityRetrieveRectsAtSelectionOffset:(NSInteger)offset withText:(NSString *)text
+{
+    RetainPtr&lt;WKContentView&gt; view = self;
+    _page-&gt;requestRectsAtSelectionOffsetWithText(offset, text, [view, offset](const Vector&lt;WebCore::SelectionRect&gt;&amp; selectionRects, CallbackBase::Error error) {
+        if (error != WebKit::CallbackBase::Error::None)
+            return;
+        if ([view respondsToSelector:@selector(_accessibilityDidGetSelectionRects:withGranularity:atOffset:)])
+            [view _accessibilityDidGetSelectionRects:[view webSelectionRectsForSelectionRects:selectionRects] withGranularity:UITextGranularityWord atOffset:offset];
+    });
+}
+
</ins><span class="cx"> // UIWKInteractionViewProtocol
</span><span class="cx"> 
</span><span class="cx"> static inline GestureType toGestureType(UIWKGestureType gestureType)
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWebPageProxyIOSmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -186,6 +186,17 @@
</span><span class="cx">     callback-&gt;performCallbackWithReturnValue(beforeText, markedText, selectedText, afterText, location, length);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebPageProxy::selectionRectsCallback(const Vector&lt;WebCore::SelectionRect&gt;&amp; selectionRects, uint64_t callbackID)
+{
+    auto callback = m_callbacks.take&lt;SelectionRectsCallback&gt;(callbackID);
+    if (!callback) {
+        ASSERT_NOT_REACHED();
+        return;
+    }
+    
+    callback-&gt;performCallbackWithReturnValue(selectionRects);
+}
+
</ins><span class="cx"> void WebPageProxy::updateVisibleContentRects(const VisibleContentRectUpdateInfo&amp; visibleContentRectUpdate)
</span><span class="cx"> {
</span><span class="cx">     if (!isValid())
</span><span class="lines">@@ -722,6 +733,28 @@
</span><span class="cx">     m_process-&gt;send(Messages::WebPage::SelectWordBackward(), m_pageID);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebPageProxy::requestRectsForGranularityWithSelectionOffset(WebCore::TextGranularity granularity, uint32_t offset, std::function&lt;void(const Vector&lt;WebCore::SelectionRect&gt;&amp;, CallbackBase::Error)&gt; callbackFunction)
+{
+    if (!isValid()) {
+        callbackFunction(Vector&lt;WebCore::SelectionRect&gt;(), CallbackBase::Error::Unknown);
+        return;
+    }
+    
+    uint64_t callbackID = m_callbacks.put(WTFMove(callbackFunction), m_process-&gt;throttler().backgroundActivityToken());
+    m_process-&gt;send(Messages::WebPage::GetRectsForGranularityWithSelectionOffset(static_cast&lt;uint32_t&gt;(granularity), offset, callbackID), m_pageID);
+}
+
+void WebPageProxy::requestRectsAtSelectionOffsetWithText(int32_t offset, const String&amp; text, std::function&lt;void(const Vector&lt;WebCore::SelectionRect&gt;&amp;, CallbackBase::Error)&gt; callbackFunction)
+{
+    if (!isValid()) {
+        callbackFunction(Vector&lt;WebCore::SelectionRect&gt;(), CallbackBase::Error::Unknown);
+        return;
+    }
+    
+    uint64_t callbackID = m_callbacks.put(WTFMove(callbackFunction), m_process-&gt;throttler().backgroundActivityToken());
+    m_process-&gt;send(Messages::WebPage::GetRectsAtSelectionOffsetWithText(offset, text, callbackID), m_pageID);
+}
+
</ins><span class="cx"> void WebPageProxy::moveSelectionByOffset(int32_t offset, std::function&lt;void (CallbackBase::Error)&gt; callbackFunction)
</span><span class="cx"> {
</span><span class="cx">     if (!isValid()) {
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPageh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -577,6 +577,8 @@
</span><span class="cx">     void updateSelectionAppearance();
</span><span class="cx">     void getSelectionContext(uint64_t callbackID);
</span><span class="cx">     void handleTwoFingerTapAtPoint(const WebCore::IntPoint&amp;, uint64_t requestID);
</span><ins>+    void getRectsForGranularityWithSelectionOffset(uint32_t, int32_t, uint64_t callbackID);
+    void getRectsAtSelectionOffsetWithText(int32_t, const String&amp;, uint64_t callbackID);
</ins><span class="cx"> #if ENABLE(IOS_TOUCH_EVENTS)
</span><span class="cx">     void dispatchAsynchronousTouchEvents(const Vector&lt;WebTouchEvent, 1&gt;&amp; queue);
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPagemessagesin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -96,6 +96,8 @@
</span><span class="cx">     SetAllowsMediaDocumentInlinePlayback(bool allows)
</span><span class="cx">     HandleTwoFingerTapAtPoint(WebCore::IntPoint point, uint64_t requestID)
</span><span class="cx">     SetForceAlwaysUserScalable(bool userScalable)
</span><ins>+    GetRectsForGranularityWithSelectionOffset(uint32_t granularity, int32_t offset, uint64_t callbackID);
+    GetRectsAtSelectionOffsetWithText(int32_t offset, String text, uint64_t callbackID);
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     SetControlledByAutomation(bool controlled)
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageiosWebPageIOSmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm (211355 => 211356)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm        2017-01-30 01:40:29 UTC (rev 211355)
+++ trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm        2017-01-30 05:03:11 UTC (rev 211356)
</span><span class="lines">@@ -105,6 +105,7 @@
</span><span class="cx"> #import &lt;WebCore/VisibleUnits.h&gt;
</span><span class="cx"> #import &lt;WebCore/WKContentObservation.h&gt;
</span><span class="cx"> #import &lt;WebCore/WebEvent.h&gt;
</span><ins>+#import &lt;WebCore/htmlediting.h&gt;
</ins><span class="cx"> #import &lt;wtf/MathExtras.h&gt;
</span><span class="cx"> #import &lt;wtf/SetForScope.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -1819,6 +1820,84 @@
</span><span class="cx">     send(Messages::WebPageProxy::VoidCallback(callbackID));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static VisiblePosition visiblePositionForPositionWithOffset(const VisiblePosition&amp; position, int32_t offset)
+{
+    RefPtr&lt;ContainerNode&gt; root;
+    unsigned startIndex = indexForVisiblePosition(position, root);
+    return visiblePositionForIndex(startIndex + offset, root.get());
+}
+
+void WebPage::getRectsForGranularityWithSelectionOffset(uint32_t granularity, int32_t offset, uint64_t callbackID)
+{
+    Frame&amp; frame = m_page-&gt;focusController().focusedOrMainFrame();
+    VisibleSelection selection = frame.selection().selection();
+    VisiblePosition selectionStart = selection.visibleStart();
+
+    if (selectionStart.isNull()) {
+        send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
+        return;
+    }
+
+    auto position = visiblePositionForPositionWithOffset(selectionStart, offset);
+    SelectionDirection direction = offset &lt; 0 ? DirectionBackward : DirectionForward;
+
+    auto range = enclosingTextUnitOfGranularity(position, static_cast&lt;WebCore::TextGranularity&gt;(granularity), direction);
+    if (!range || range-&gt;collapsed()) {
+        send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
+        return;
+    }
+
+    Vector&lt;WebCore::SelectionRect&gt; selectionRects;
+    range-&gt;collectSelectionRectsWithoutUnionInteriorLines(selectionRects);
+    convertSelectionRectsToRootView(frame.view(), selectionRects);
+    send(Messages::WebPageProxy::SelectionRectsCallback(selectionRects, callbackID));
+}
+
+static RefPtr&lt;Range&gt; rangeNearPositionMatchesText(const VisiblePosition&amp; position, RefPtr&lt;Range&gt; originalRange, const String&amp; matchText, RefPtr&lt;Range&gt; selectionRange)
+{
+    auto range = Range::create(selectionRange-&gt;ownerDocument(), selectionRange-&gt;startPosition(), position.deepEquivalent().parentAnchoredEquivalent());
+    unsigned targetOffset = TextIterator::rangeLength(range.ptr(), true);
+    return findClosestPlainText(*selectionRange.get(), matchText, 0, targetOffset);
+}
+
+void WebPage::getRectsAtSelectionOffsetWithText(int32_t offset, const String&amp; text, uint64_t callbackID)
+{
+    Frame&amp; frame = m_page-&gt;focusController().focusedOrMainFrame();
+    uint32_t length = text.length();
+    VisibleSelection selection = frame.selection().selection();
+    VisiblePosition selectionStart = selection.visibleStart();
+    VisiblePosition selectionEnd = selection.visibleEnd();
+
+    if (selectionStart.isNull() || selectionEnd.isNull()) {
+        send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
+        return;
+    }
+
+    auto startPosition = visiblePositionForPositionWithOffset(selectionStart, offset);
+    auto endPosition = visiblePositionForPositionWithOffset(startPosition, length);
+    auto range = Range::create(*frame.document(), startPosition, endPosition);
+
+    if (range-&gt;collapsed()) {
+        send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
+        return;
+    }
+
+    String rangeText = plainTextReplacingNoBreakSpace(range.ptr(), TextIteratorDefaultBehavior, true);
+    if (rangeText != text) {
+        auto selectionRange = selection.toNormalizedRange();
+        // Try to search for a range which is the closest to the position within the selection range that matches the passed in text.
+        if (auto wordRange = rangeNearPositionMatchesText(startPosition, range.ptr(), text, selectionRange)) {
+            if (!wordRange-&gt;collapsed())
+                range = *wordRange;
+        }
+    }
+
+    Vector&lt;WebCore::SelectionRect&gt; selectionRects;
+    range-&gt;collectSelectionRectsWithoutUnionInteriorLines(selectionRects);
+    convertSelectionRectsToRootView(frame.view(), selectionRects);
+    send(Messages::WebPageProxy::SelectionRectsCallback(selectionRects, callbackID));
+}
+
</ins><span class="cx"> VisiblePosition WebPage::visiblePositionInFocusedNodeForPoint(const Frame&amp; frame, const IntPoint&amp; point, bool isInteractingWithAssistedNode)
</span><span class="cx"> {
</span><span class="cx">     IntPoint adjustedPoint(frame.view()-&gt;rootViewToContents(point));
</span></span></pre>
</div>
</div>

</body>
</html>