Skip to content

[Swift Testing] Make it easy to allow a WebPage to get and set selection, and to evaluate JS in general#63656

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
rr-codes:eng/Swift-Testing-Make-it-easy-to-allow-a-WebPage-to-get-and-set-selection-and-to-evaluate-JS-in-general
Apr 27, 2026
Merged

[Swift Testing] Make it easy to allow a WebPage to get and set selection, and to evaluate JS in general#63656
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
rr-codes:eng/Swift-Testing-Make-it-easy-to-allow-a-WebPage-to-get-and-set-selection-and-to-evaluate-JS-in-general

Conversation

@rr-codes
Copy link
Copy Markdown
Contributor

@rr-codes rr-codes commented Apr 27, 2026

5c4b430

[Swift Testing] Make it easy to allow a WebPage to get and set selection, and to evaluate JS in general
https://bugs.webkit.org/show_bug.cgi?id=313383
rdar://175649972

Reviewed by Dan Glastonbury.

Implement a generalized way to create semi-type-safe, re-usable JS expressions to be evaluated
by a WebPage in TestWebKitAPI.

Use this new mechanism and implement some basic expressions to get and set selections.

* Tools/TestWebKitAPI/Helpers/cocoa/JavaScriptMessages.swift: Added.
(SelectionBoundingClientRect.expression):
(SelectionBoundingClientRect.encoded):
(SetSelection.expression):
(SetSelection.encoded):
(GetSelection.expression):
(GetSelection.encoded):
* Tools/TestWebKitAPI/Helpers/cocoa/JavaScriptTypes.swift: Added.
(JavaScriptSelection.encoded):
* Tools/TestWebKitAPI/Helpers/cocoa/WebPage+JavaScriptExpression.swift: Added.
(JavaScriptEncodable.encoded):
(JavaScriptExpression.expression):
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/AppKitGesturesTests.swift:
(AppKitGesturesTests.clickingChangesSelection):
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/JavaScriptExpressionTests.swift: Added.
(JavaScriptExpressionTests.setAndGetSelectionWithCollapsedPositionRoundTrip):
(JavaScriptExpressionTests.setSelectionAppliesRangeSelection):
(JavaScriptExpressionTests.getSelectionReturnsCollapsedAfterCollapsedIsSet):
(JavaScriptExpressionTests.getSelectionReturnsRangeAfterRangeIsSet):
(JavaScriptExpressionTests.setAndGetSelectionRangeRoundTrip):
(JavaScriptExpressionTests.selectionBoundingClientRectReturnsNonEmptyRectForRangeSelection):
(JavaScriptExpressionTests.selectionBoundingClientRectReturnsZeroWidthRectForCollapsedSelection):

Canonical link: https://commits.webkit.org/312070@main

d50ce15

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win loading 🛠 ios-apple
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 🧪 win-tests ✅ 🛠 mac-apple
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe ✅ 🛠 vision-apple
🧪 ios-wk2-wpt 🧪 api-mac-debug ✅ 🛠 gtk3-libwebrtc
✅ 🧪 api-ios ✅ 🧪 mac-wk1 ✅ 🛠 gtk
loading 🛠 ios-safer-cpp ✅ 🧪 mac-wk2 ✅ 🧪 gtk-wk2
✅ 🛠 vision 🧪 mac-AS-debug-wk2 ✅ 🧪 api-gtk
✅ 🛠 🧪 merge ✅ 🛠 vision-sim ✅ 🧪 mac-wk2-stress ✅ 🛠 playstation
✅ 🧪 vision-wk2 ✅ 🧪 mac-intel-wk2
✅ 🛠 tv
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@rr-codes rr-codes self-assigned this Apr 27, 2026
@rr-codes rr-codes added the WebKit Misc. For miscellaneous bugs in the WebKit framework (and not JavaScriptCore or WebCore). label Apr 27, 2026
@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-allow-a-WebPage-to-get-and-set-selection-and-to-evaluate-JS-in-general branch from 7b3eedf to 7b15755 Compare April 27, 2026 05:05
public func callJavaScript<Expression>(
_ expression: Expression
) async throws where Expression: JavaScriptExpression, Expression.Output == Void {
let arguments = expression.encoded() as [String: Any]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when expression.encoded() returns ["foo": nil]?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rr-codes pointed out on slack that:

2> let z: [String: Any?] = ["hi": nil]
z: [String : Any?] = 1 key/value pair {
  [0] = {
    key = "hi"
    value = nil
  }
}
  3> let q: [String: Any] = z
q: [String : Any] = 1 key/value pair {
  [0] = {
    key = "hi"
    value = nil
  }
}

}
}

extension JavaScriptSelection.Position: WebPage.JavaScriptDecodable {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a separate extension to the one above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's usually conventional to make each protocol conformance a separate extension since each extension is "about" a different thing

Copy link
Copy Markdown
Contributor

@djg djg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@rr-codes rr-codes force-pushed the eng/Swift-Testing-Make-it-easy-to-allow-a-WebPage-to-get-and-set-selection-and-to-evaluate-JS-in-general branch from 7b15755 to d50ce15 Compare April 27, 2026 06:17
@rr-codes rr-codes added the merge-queue Applied to send a pull request to merge-queue label Apr 27, 2026
…ion, and to evaluate JS in general

https://bugs.webkit.org/show_bug.cgi?id=313383
rdar://175649972

Reviewed by Dan Glastonbury.

Implement a generalized way to create semi-type-safe, re-usable JS expressions to be evaluated
by a WebPage in TestWebKitAPI.

Use this new mechanism and implement some basic expressions to get and set selections.

* Tools/TestWebKitAPI/Helpers/cocoa/JavaScriptMessages.swift: Added.
(SelectionBoundingClientRect.expression):
(SelectionBoundingClientRect.encoded):
(SetSelection.expression):
(SetSelection.encoded):
(GetSelection.expression):
(GetSelection.encoded):
* Tools/TestWebKitAPI/Helpers/cocoa/JavaScriptTypes.swift: Added.
(JavaScriptSelection.encoded):
* Tools/TestWebKitAPI/Helpers/cocoa/WebPage+JavaScriptExpression.swift: Added.
(JavaScriptEncodable.encoded):
(JavaScriptExpression.expression):
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/AppKitGesturesTests.swift:
(AppKitGesturesTests.clickingChangesSelection):
* Tools/TestWebKitAPI/Tests/WebKit/WebPage/JavaScriptExpressionTests.swift: Added.
(JavaScriptExpressionTests.setAndGetSelectionWithCollapsedPositionRoundTrip):
(JavaScriptExpressionTests.setSelectionAppliesRangeSelection):
(JavaScriptExpressionTests.getSelectionReturnsCollapsedAfterCollapsedIsSet):
(JavaScriptExpressionTests.getSelectionReturnsRangeAfterRangeIsSet):
(JavaScriptExpressionTests.setAndGetSelectionRangeRoundTrip):
(JavaScriptExpressionTests.selectionBoundingClientRectReturnsNonEmptyRectForRangeSelection):
(JavaScriptExpressionTests.selectionBoundingClientRectReturnsZeroWidthRectForCollapsedSelection):

Canonical link: https://commits.webkit.org/312070@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/Swift-Testing-Make-it-easy-to-allow-a-WebPage-to-get-and-set-selection-and-to-evaluate-JS-in-general branch from d50ce15 to 5c4b430 Compare April 27, 2026 07:18
@webkit-commit-queue
Copy link
Copy Markdown
Collaborator

Committed 312070@main (5c4b430): https://commits.webkit.org/312070@main

Reviewed commits have been landed. Closing PR #63656 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit 5c4b430 into WebKit:main Apr 27, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WebKit Misc. For miscellaneous bugs in the WebKit framework (and not JavaScriptCore or WebCore).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants