Skip to content

Conversation

@symbiogenesis
Copy link
Contributor

Before:

Method Mean Error StdDev Gen0 Allocated
EscapeJsStringBenchmark 1.258 ms 0.0216 ms 0.0202 ms 255.8594 2.3 MB

After:

Method Mean Error StdDev Median Allocated
EscapeJsStringBenchmark 3.111 us 0.3148 us 0.9281 us 2.434 us -

@symbiogenesis symbiogenesis requested a review from a team as a code owner February 3, 2025 06:29
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Feb 3, 2025
@jsuarezruiz
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@symbiogenesis
Copy link
Contributor Author

Looks like I need some conditional compilation to make the Regex source generation work.

@symbiogenesis
Copy link
Contributor Author

Hopefully it is good now

Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

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

I see this code is duplicated. Could we move it to a webview utils and then both controls use it? I think there will be a couple of other things I will add in the future. I already noticed some with hybrid web view. So a separate class will be useful.

@symbiogenesis
Copy link
Contributor Author

ok, it is now using a common helper class

@bronteq
Copy link

bronteq commented Apr 18, 2025

@mattleibow it would be nice to see this merged, all the platforms could benefit

@mattleibow
Copy link
Member

/rebase

@github-actions github-actions bot force-pushed the optimize-js-escaping branch from 25ec409 to 1390efe Compare May 6, 2025 10:22
mattleibow
mattleibow previously approved these changes May 6, 2025
@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen changed the base branch from main to inflight/current May 8, 2025 16:15
@PureWeen PureWeen dismissed mattleibow’s stale review May 8, 2025 16:15

The base branch was changed.

@PureWeen PureWeen changed the base branch from inflight/current to main May 8, 2025 16:15
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

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

Build failing

@bronteq
Copy link

bronteq commented Jun 16, 2025

@mattleibow @PureWeen friendly ping, @symbiogenesis committed with a fix, please run the pipeline again

@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@mattleibow
Copy link
Member

@copilot can you fix this error?

D:\a\1\s\src\Core\src\Handlers\WebView\WebViewHelper.cs(16,20): error CS1503: Argument 1: cannot convert from 'char' to 'string' [D:\a\1\s\src\Core\src\Core.csproj::TargetFramework=netstandard2.0]
D:\a\1\s\src\Core\src\Handlers\WebView\WebViewHelper.cs(16,8): error CA1307: 'string.Contains(char)' has a method overload that takes a 'StringComparison' parameter. Replace this call in 'Microsoft.Maui.Handlers.WebViewHelper.EscapeJsString(string)' with a call to 'string.Contains(char, System.StringComparison)' for clarity of intent. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1307) [D:\a\1\s\src\Core\src\Core.csproj::TargetFramework=netstandard2.1]

@mattleibow mattleibow requested a review from Copilot June 16, 2025 16:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes the JavaScript escaping logic to improve performance and reduce allocations.

  • Introduces a centralized helper (WebViewHelper.EscapeJsString) that leverages conditional compilation and generated regex on .NET 6+
  • Removes duplicate and less efficient JS escaping implementations from HybridWebViewHandler and WebView
  • Updates and adds unit tests to validate the new escaping behavior

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/Core/src/Handlers/WebView/WebViewHelper.cs New implementation using generated regex to optimize JS escaping
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.cs Replaces local JS escape logic with a call to WebViewHelper.EscapeJsString
src/Controls/tests/Core.UnitTests/WebViewTests.cs Unit tests updated/added to cover the new JS escaping behavior
src/Controls/src/Core/WebView/WebView.cs Removed duplicate local JS escaping and now reuses WebViewHelper.EscapeJsString
Comments suppressed due to low confidence (2)

src/Controls/tests/Core.UnitTests/WebViewTests.cs:59

  • Consider adding a test case for strings with multiple consecutive backslashes preceding a single quote to ensure the escaping logic handles these edge cases correctly.
var result = WebViewHelper.EscapeJsString(input);

src/Controls/src/Core/WebView/WebView.cs:293

  • The removal of the local JS escaping logic in favor of a centralized helper improves consistency; please ensure that all use cases formerly handled by the local implementation are covered by the unit tests.
internal static string EscapeJsString(string js) => WebViewHelper.EscapeJsString(js);

@mattleibow mattleibow force-pushed the optimize-js-escaping branch from 4dc6683 to b98fdc4 Compare June 24, 2025 21:13
@mattleibow
Copy link
Member

/azp run MAUI-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mattleibow mattleibow requested review from MartyIX and PureWeen June 24, 2025 21:21
@PureWeen PureWeen changed the base branch from main to inflight/current August 15, 2025 23:15
@PureWeen PureWeen merged commit 4d7e83b into dotnet:inflight/current Aug 15, 2025
17 checks passed
github-actions bot pushed a commit that referenced this pull request Aug 15, 2025
* optimize JS escaping

* add tests

* copy to HybridWebView

* use conditional compilation

* Update src/Controls/tests/DeviceTests/Elements/WebView/WebViewTests.cs

Co-authored-by: MartyIX <[email protected]>

* Move tests

* use common WebViewHelper class

* add missing using

* fix build

* Fix CI

* More tests

---------

Co-authored-by: Edward Miller <[email protected]>
Co-authored-by: MartyIX <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
github-actions bot pushed a commit that referenced this pull request Aug 15, 2025
* optimize JS escaping

* add tests

* copy to HybridWebView

* use conditional compilation

* Update src/Controls/tests/DeviceTests/Elements/WebView/WebViewTests.cs

Co-authored-by: MartyIX <[email protected]>

* Move tests

* use common WebViewHelper class

* add missing using

* fix build

* Fix CI

* More tests

---------

Co-authored-by: Edward Miller <[email protected]>
Co-authored-by: MartyIX <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
github-actions bot pushed a commit that referenced this pull request Aug 19, 2025
* optimize JS escaping

* add tests

* copy to HybridWebView

* use conditional compilation

* Update src/Controls/tests/DeviceTests/Elements/WebView/WebViewTests.cs

Co-authored-by: MartyIX <[email protected]>

* Move tests

* use common WebViewHelper class

* add missing using

* fix build

* Fix CI

* More tests

---------

Co-authored-by: Edward Miller <[email protected]>
Co-authored-by: MartyIX <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
github-actions bot pushed a commit that referenced this pull request Aug 22, 2025
* optimize JS escaping

* add tests

* copy to HybridWebView

* use conditional compilation

* Update src/Controls/tests/DeviceTests/Elements/WebView/WebViewTests.cs

Co-authored-by: MartyIX <[email protected]>

* Move tests

* use common WebViewHelper class

* add missing using

* fix build

* Fix CI

* More tests

---------

Co-authored-by: Edward Miller <[email protected]>
Co-authored-by: MartyIX <[email protected]>
Co-authored-by: Matthew Leibowitz <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-webview WebView community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants