FindController::findString always updates foundStringMatchIndex even if match is the same as before
https://bugs.webkit.org/show_bug.cgi?id=201775
<rdar://problem/55352425>

Patch by Matt Mokary <mmokary@apple.com> on 2019-10-14
Reviewed by Tim Horton.

Source/WebKit:

Allow an update to a find string without changing current match index, as is often the desired behavior when
modifying a query rather than moving forward or backward through a match set.

* Shared/WebFindOptions.h:
* UIProcess/API/Cocoa/WKWebView.mm:
(toFindOptions):
* UIProcess/API/Cocoa/_WKFindOptions.h:
* WebProcess/WebPage/FindController.cpp:
(WebKit::FindController::findString):
Do not change match index if NoIndexChange bit is set. Otherwise, no change in behavior.

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/WKWebViewFindString.mm:
(TestWebKitAPI::TEST):
_WKFindOptionsNoIndexChange test

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@251111 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index 29d4ba1..1b40757 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,22 @@
+2019-10-14  Matt Mokary  <mmokary@apple.com>
+
+        FindController::findString always updates foundStringMatchIndex even if match is the same as before
+        https://bugs.webkit.org/show_bug.cgi?id=201775
+        <rdar://problem/55352425>
+
+        Reviewed by Tim Horton.
+
+        Allow an update to a find string without changing current match index, as is often the desired behavior when
+        modifying a query rather than moving forward or backward through a match set.
+
+        * Shared/WebFindOptions.h:
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (toFindOptions):
+        * UIProcess/API/Cocoa/_WKFindOptions.h:
+        * WebProcess/WebPage/FindController.cpp:
+        (WebKit::FindController::findString):
+        Do not change match index if NoIndexChange bit is set. Otherwise, no change in behavior.
+
 2019-10-14  David Quesada  <david_quesada@apple.com>
 
         Remove WebCore::IOSApplication::isWebApp()
diff --git a/Source/WebKit/Shared/WebFindOptions.h b/Source/WebKit/Shared/WebFindOptions.h
index b0bf643..b576ec4 100644
--- a/Source/WebKit/Shared/WebFindOptions.h
+++ b/Source/WebKit/Shared/WebFindOptions.h
@@ -37,6 +37,7 @@
     FindOptionsShowFindIndicator = 1 << 6,
     FindOptionsShowHighlight = 1 << 7,
     FindOptionsDetermineMatchIndex = 1 << 8,
+    FindOptionsNoIndexChange = 1 << 9,
 };
 
 } // namespace WebKit
diff --git a/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm b/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
index ba14c27..7e04826 100644
--- a/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
+++ b/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
@@ -5660,6 +5660,8 @@
         findOptions |= WebKit::FindOptionsShowFindIndicator;
     if (wkFindOptions & _WKFindOptionsShowHighlight)
         findOptions |= WebKit::FindOptionsShowHighlight;
+    if (wkFindOptions & _WKFindOptionsNoIndexChange)
+        findOptions |= WebKit::FindOptionsNoIndexChange;
     if (wkFindOptions & _WKFindOptionsDetermineMatchIndex)
         findOptions |= WebKit::FindOptionsDetermineMatchIndex;
 
diff --git a/Source/WebKit/UIProcess/API/Cocoa/_WKFindOptions.h b/Source/WebKit/UIProcess/API/Cocoa/_WKFindOptions.h
index d2a1fde..474d865 100644
--- a/Source/WebKit/UIProcess/API/Cocoa/_WKFindOptions.h
+++ b/Source/WebKit/UIProcess/API/Cocoa/_WKFindOptions.h
@@ -34,7 +34,8 @@
     _WKFindOptionsShowOverlay = 1 << 5,
     _WKFindOptionsShowFindIndicator = 1 << 6,
     _WKFindOptionsShowHighlight = 1 << 7,
-    _WKFindOptionsDetermineMatchIndex = 1 << 8,
+    _WKFindOptionsNoIndexChange = 1 << 8,
+    _WKFindOptionsDetermineMatchIndex = 1 << 9,
 
     _WKFindOptionsIrrelevantForIncrementalResults = _WKFindOptionsShowOverlay | _WKFindOptionsShowFindIndicator | _WKFindOptionsShowHighlight | _WKFindOptionsDetermineMatchIndex,
     _WKFindOptionsIrrelevantForBatchResults = _WKFindOptionsBackwards | _WKFindOptionsWrapAround | _WKFindOptionsIrrelevantForIncrementalResults
diff --git a/Source/WebKit/WebProcess/WebPage/FindController.cpp b/Source/WebKit/WebProcess/WebPage/FindController.cpp
index 32db3e6..0762d00 100644
--- a/Source/WebKit/WebProcess/WebPage/FindController.cpp
+++ b/Source/WebKit/WebProcess/WebPage/FindController.cpp
@@ -268,7 +268,7 @@
         if (!foundStringStartsAfterSelection) {
             if (options & FindOptionsBackwards)
                 m_foundStringMatchIndex--;
-            else
+            else if (!(options & FindOptionsNoIndexChange))
                 m_foundStringMatchIndex++;
         }
     }
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index 39fc344..9db76ae 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,15 @@
+2019-10-14  Matt Mokary  <mmokary@apple.com>
+
+        FindController::findString always updates foundStringMatchIndex even if match is the same as before
+        https://bugs.webkit.org/show_bug.cgi?id=201775
+        <rdar://problem/55352425>
+
+        Reviewed by Tim Horton.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKWebViewFindString.mm:
+        (TestWebKitAPI::TEST):
+        _WKFindOptionsNoIndexChange test
+
 2019-10-14  Andy Estes  <aestes@apple.com>
 
         REGRESSION (r243682): Quick Look previews loaded from the memory cache render with the wrong content type
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewFindString.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewFindString.mm
index 159bd5e..82e50aa 100644
--- a/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewFindString.mm
+++ b/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewFindString.mm
@@ -170,4 +170,23 @@
 }
 #endif
 
+TEST(WKWebViewFindString, DoNotUpdateMatchIndexWhenGivenNoIndexChangeOption)
+{
+    auto findDelegate = adoptNS([[WKWebViewFindStringFindDelegate alloc] init]);
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    auto firstWebView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 200) configuration:configuration.get() addToWindow:YES]);
+    [firstWebView synchronouslyLoadHTMLString:@"<p>hello</p><p>hello</p>"];
+    [firstWebView _setFindDelegate:findDelegate.get()];
+
+    [firstWebView _findString:@"hello" options:0 maxCount:maxCount];
+    Util::run(&isDone);
+
+    EXPECT_EQ(0, [findDelegate matchIndex]);
+
+    [firstWebView _findString:@"hello" options:_WKFindOptionsNoIndexChange maxCount:maxCount];
+    Util::run(&isDone);
+
+    EXPECT_EQ(0, [findDelegate matchIndex]);
+}
+
 } // namespace TestWebKitAPI