2008-08-08 Beth Dakin <bdakin@apple.com>
[WebKit-https.git] / WebCore / page / Frame.cpp
index a51f980b41b8852f0836d34a6cf9e13f35e75324..e2b3b4874b4d87b339c8e1cdab909ae527805b9d 100644 (file)
@@ -1538,15 +1538,27 @@ bool Frame::findString(const String& target, bool forward, bool caseFlag, bool w
 
         resultRange = findPlainText(searchRange.get(), target, forward, caseFlag);
     }
+    
+    Editor::Visibility rangeLocation = editor()->rangeVisibility(resultRange.get());
+    if (rangeLocation == Editor::BeforeVisibleArea && forward)
+        resultRange = editor()->firstVisibleRange(resultRange.get(), target, forward, caseFlag);
 
     // If we didn't find anything and we're wrapping, search again in the entire document (this will
     // redundantly re-search the area already searched in some cases).
-    if (resultRange->collapsed(exception) && wrapFlag) {
+    bool disconnectedFrameShouldWrap = rangeLocation == Editor::AfterVisibleArea
+        || (rangeLocation == Editor::BeforeVisibleArea && !forward);
+    if (resultRange->collapsed(exception) && wrapFlag || disconnectedFrameShouldWrap) {
         searchRange = rangeOfContents(document());
         resultRange = findPlainText(searchRange.get(), target, forward, caseFlag);
         // We used to return false here if we ended up with the same range that we started with
         // (e.g., the selection was already the only instance of this text). But we decided that
         // this should be a success case instead, so we'll just fall through in that case.
+        
+        rangeLocation = editor()->rangeVisibility(resultRange.get());
+        if (rangeLocation == Editor::BeforeVisibleArea)
+            resultRange = editor()->firstVisibleRange(resultRange.get(), target, forward, caseFlag);
+        else if (rangeLocation == Editor::AfterVisibleArea)
+            resultRange = editor()->lastVisibleRange(resultRange.get(), target, forward, caseFlag);
     }
 
     if (resultRange->collapsed(exception))
@@ -1582,6 +1594,15 @@ unsigned Frame::markAllMatchesForText(const String& target, bool caseFlag, unsig
         VisiblePosition newStart = endVisiblePosition(resultRange.get(), DOWNSTREAM);
         if (newStart == startVisiblePosition(searchRange.get(), DOWNSTREAM))
             break;
+        
+        Editor::Visibility rangeLocation = editor()->rangeVisibility(resultRange.get());
+        if (rangeLocation == Editor::BeforeVisibleArea) {
+            searchRange = rangeOfContents(document());
+            searchRange->setStartAfter(resultRange->startContainer()->shadowAncestorNode(), exception);
+            continue;
+        }
+        if (rangeLocation == Editor::AfterVisibleArea)
+            break;
 
         ++matchCount;