-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add FXIOS-8087 [v124] find in page web engine #18622
Merged
lmarceau
merged 10 commits into
mozilla-mobile:main
from
lmarceau:lm/FXIOS-8087-#17999-find-in-page-web-engine
Feb 14, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a936aea
Add find in page in engine session
lmarceau fc006b9
Add tests
lmarceau c9eab04
Integrate find in page, add missing functionalities
lmarceau 3746144
Add find in page content script tests
lmarceau b68cd6f
Revert change
lmarceau f16764a
Swiftlint
lmarceau 069fc47
Add sanitization input tests
lmarceau 03e1ef8
Add find in page delegate test
lmarceau 76312b2
Revert package change
lmarceau 1dbaf6a
Swiftlint
lmarceau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
BrowserKit/Sources/WebEngine/WKWebview/Scripts/FindInPageContentScript.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Common | ||
import Foundation | ||
import WebKit | ||
|
||
public enum FindInPageFunction: String { | ||
/// Find all the occurences of this text in the page | ||
case find | ||
|
||
/// Find the next occurence of this text in the page | ||
case findNext | ||
|
||
/// Find the previous occurence of this text in the page | ||
case findPrevious | ||
} | ||
|
||
public protocol FindInPageHelperDelegate: AnyObject { | ||
func findInPageHelper(didUpdateCurrentResult currentResult: Int) | ||
func findInPageHelper(didUpdateTotalResults totalResults: Int) | ||
} | ||
|
||
class FindInPageContentScript: WKContentScript { | ||
weak var delegate: FindInPageHelperDelegate? | ||
private var logger: Logger | ||
|
||
init(logger: Logger = DefaultLogger.shared) { | ||
self.logger = logger | ||
} | ||
|
||
class func name() -> String { | ||
return "FindInPage" | ||
} | ||
|
||
func scriptMessageHandlerNames() -> [String] { | ||
return ["findInPageHandler"] | ||
} | ||
|
||
func userContentController( | ||
didReceiveMessage message: Any | ||
) { | ||
guard let parameters = message as? [String: Int] else { | ||
// TODO: FXIOS-6463 - Integrate message handler check | ||
logger.log("FindInPage.js sent wrong type of message", | ||
level: .warning, | ||
category: .webview) | ||
return | ||
} | ||
|
||
if let currentResult = parameters["currentResult"] { | ||
delegate?.findInPageHelper(didUpdateCurrentResult: currentResult) | ||
} | ||
|
||
if let totalResults = parameters["totalResults"] { | ||
delegate?.findInPageHelper(didUpdateTotalResults: totalResults) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
BrowserKit/Tests/WebEngineTests/FindInPageContentScriptTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import XCTest | ||
@testable import WebEngine | ||
|
||
final class FindInPageContentScriptTests: XCTestCase { | ||
private var findInPageDelegate: MockFindInPageHelperDelegate! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
findInPageDelegate = MockFindInPageHelperDelegate() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
findInPageDelegate = nil | ||
} | ||
|
||
func testDidReceiveMessageGivenEmptyMessageThenNoDelegateCalled() { | ||
let subject = FindInPageContentScript() | ||
subject.delegate = findInPageDelegate | ||
|
||
subject.userContentController(didReceiveMessage: []) | ||
|
||
XCTAssertEqual(findInPageDelegate.didUpdateCurrentResultCalled, 0) | ||
XCTAssertEqual(findInPageDelegate.didUpdateTotalResultsCalled, 0) | ||
} | ||
|
||
func testDidReceiveMessageGivenStringMessageThenNoDelegateCalled() { | ||
let subject = FindInPageContentScript() | ||
subject.delegate = findInPageDelegate | ||
|
||
subject.userContentController(didReceiveMessage: ["": ""]) | ||
|
||
XCTAssertEqual(findInPageDelegate.didUpdateCurrentResultCalled, 0) | ||
XCTAssertEqual(findInPageDelegate.didUpdateTotalResultsCalled, 0) | ||
} | ||
|
||
func testDidReceiveMessageGivenCurrentResultMessageThenDelegateCalled() { | ||
let subject = FindInPageContentScript() | ||
subject.delegate = findInPageDelegate | ||
let currentResult = 1 | ||
|
||
subject.userContentController(didReceiveMessage: ["currentResult": currentResult]) | ||
|
||
XCTAssertEqual(findInPageDelegate.didUpdateCurrentResultCalled, 1) | ||
XCTAssertEqual(findInPageDelegate.savedCurrentResult, currentResult) | ||
XCTAssertEqual(findInPageDelegate.didUpdateTotalResultsCalled, 0) | ||
} | ||
|
||
func testDidReceiveMessageGivenTotalResultMessageThenDelegateCalled() { | ||
let subject = FindInPageContentScript() | ||
subject.delegate = findInPageDelegate | ||
let totalResult = 10 | ||
|
||
subject.userContentController(didReceiveMessage: ["totalResults": totalResult]) | ||
|
||
XCTAssertEqual(findInPageDelegate.didUpdateCurrentResultCalled, 0) | ||
XCTAssertEqual(findInPageDelegate.didUpdateTotalResultsCalled, 1) | ||
XCTAssertEqual(findInPageDelegate.savedTotalResults, totalResult) | ||
} | ||
|
||
func testDidReceiveMessageGivenTotalAndCurrentResultsMessageThenDelegateCalled() { | ||
let subject = FindInPageContentScript() | ||
subject.delegate = findInPageDelegate | ||
let totalResult = 15 | ||
let currentResult = 20 | ||
|
||
subject.userContentController(didReceiveMessage: ["totalResults": totalResult, "currentResult": currentResult]) | ||
|
||
XCTAssertEqual(findInPageDelegate.didUpdateCurrentResultCalled, 1) | ||
XCTAssertEqual(findInPageDelegate.savedCurrentResult, currentResult) | ||
XCTAssertEqual(findInPageDelegate.didUpdateTotalResultsCalled, 1) | ||
XCTAssertEqual(findInPageDelegate.savedTotalResults, totalResult) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
BrowserKit/Tests/WebEngineTests/Mock/MockFindInPageDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
@testable import WebEngine | ||
|
||
class MockFindInPageHelperDelegate: FindInPageHelperDelegate { | ||
var didUpdateCurrentResultCalled = 0 | ||
var didUpdateTotalResultsCalled = 0 | ||
var savedCurrentResult = 0 | ||
var savedTotalResults = 0 | ||
|
||
func findInPageHelper(didUpdateCurrentResult currentResult: Int) { | ||
savedCurrentResult = currentResult | ||
didUpdateCurrentResultCalled += 1 | ||
} | ||
|
||
func findInPageHelper(didUpdateTotalResults totalResults: Int) { | ||
savedTotalResults = totalResults | ||
didUpdateTotalResultsCalled += 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just OOC is this something being brought over from the existing client? Wondering if there is a more elegant way for us to avoid how we're escaping these slashes but I'm not super-familiar with this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's brought from the existing code 🤔 It's from here exactly