Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Playwright/Core/APIRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async Task<IAPIRequestContext> IAPIRequest.NewContextAsync(APIRequestNewContextO

storageState = File.ReadAllText(options?.StorageStatePath);
}
if (!string.IsNullOrEmpty(storageState) && storageState != null)
if (!storageState.IsNullOrEmpty())
{
args.Add("storageState", JsonSerializer.Deserialize<object>(storageState, Helpers.JsonExtensions.DefaultJsonSerializerOptions));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright/Core/APIRequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal async Task<IAPIResponse> InnerFetchAsync(IRequest request, string? urlO
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<IAPIResponse> FetchAsync(string url, APIRequestContextOptions? options = null)
{
if (!string.IsNullOrEmpty(_closeReason) && _closeReason != null)
if (!_closeReason.IsNullOrEmpty())
{
throw new PlaywrightException(_closeReason);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright/Core/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public async Task<IBrowserContext> NewContextAsync(BrowserNewContextOptions? opt
storageState = File.ReadAllText(options.StorageStatePath);
}

if (!string.IsNullOrEmpty(storageState) && storageState != null)
if (!storageState.IsNullOrEmpty())
{
args.Add("storageState", JsonSerializer.Deserialize<object>(storageState, Helpers.JsonExtensions.DefaultJsonSerializerOptions));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright/Core/HarRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private HarRouter(LocalUtils localUtils, string harId, HarNotFound notFoundActio
internal static async Task<HarRouter> CreateAsync(LocalUtils localUtils, string file, HarNotFound notFoundAction, HarRouterOptions options)
{
var (harId, error) = await localUtils.HarOpenAsync(file).ConfigureAwait(false);
if (!string.IsNullOrEmpty(error) && error != null)
if (!error.IsNullOrEmpty())
{
throw new PlaywrightException(error);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Playwright/Core/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private async Task RaceWithTargetCloseAsync(Task task)
isBase64 = true;
length = resultBody.Length;
}
else if (!string.IsNullOrEmpty(body) && body != null)
else if (!body.IsNullOrEmpty())
{
resultBody = body;
isBase64 = false;
Expand All @@ -220,15 +220,15 @@ private async Task RaceWithTargetCloseAsync(Task task)
}
}

if (!string.IsNullOrEmpty(contentType) && contentType != null)
if (!contentType.IsNullOrEmpty())
{
resultHeaders["content-type"] = contentType;
}
else if (json != null)
{
resultHeaders["content-type"] = "application/json";
}
else if (!string.IsNullOrEmpty(path) && path != null)
else if (!path.IsNullOrEmpty())
{
resultHeaders["content-type"] = path.GetContentType();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Playwright/Core/ScriptsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Microsoft.Playwright.Helpers;
using Microsoft.Playwright.Transport.Converters;

namespace Microsoft.Playwright.Core;
Expand Down Expand Up @@ -76,11 +77,11 @@ internal static object SerializedArgument(object? arg)

internal static string EvaluationScript(string? content, string? path, bool addSourceUrl)
{
if (!string.IsNullOrEmpty(content) && content != null)
if (!content.IsNullOrEmpty())
{
return content;
}
else if (!string.IsNullOrEmpty(path) && path != null)
else if (!path.IsNullOrEmpty())
{
var source = File.ReadAllText(path);
return addSourceUrl ? AddSourceUrlToScript(source, path) : source;
Expand Down
4 changes: 2 additions & 2 deletions src/Playwright/Core/Tracing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private async Task DoStopChunkAsync(string? filePath)
{
// Not interested in artifacts.
await _TracingStopChunkAsync("discard").ConfigureAwait(false);
if (!string.IsNullOrEmpty(_stacksId) && _stacksId != null)
if (!_stacksId.IsNullOrEmpty())
{
if (_connection.LocalUtils != null)
{
Expand All @@ -136,7 +136,7 @@ private async Task DoStopChunkAsync(string? filePath)
// The artifact may be missing if the browser closed while stopping tracing.
if (artifact == null)
{
if (!string.IsNullOrEmpty(_stacksId) && _stacksId != null)
if (!_stacksId.IsNullOrEmpty())
{
if (_connection.LocalUtils != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright/Core/Waiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Dispose()
["waitId"] = _waitId,
["phase"] = "after",
};
if (!string.IsNullOrEmpty(_error) && _error != null)
if (!_error.IsNullOrEmpty())
{
info["error"] = _error;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Playwright/Helpers/NotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace System.Diagnostics.CodeAnalysis;

// This can be dropped when we drop netstandard2.0
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class NotNullWhenAttribute(bool returnValue) : Attribute
{
public bool ReturnValue { get; } = returnValue;
}
13 changes: 12 additions & 1 deletion src/Playwright/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -604,7 +605,7 @@ internal static class StringExtensions
/// </summary>
/// <param name="query">Query string.</param>
/// <returns>A <see cref="Dictionary{TKey, TValue}"/> containing the parsed QueryString.</returns>
public static Dictionary<string, string> ParseQueryString(this string query)
internal static Dictionary<string, string> ParseQueryString(this string query)
{
if (query is null)
{
Expand Down Expand Up @@ -657,4 +658,14 @@ internal static string MimeType(this string file)

return path.Substring(index);
}

/// <summary>
/// Checks if the string is null or empty. This should be used instead of string.IsNullOrEmpty to avoid nullability warnings.
/// If we drop netstandard2.0 support, we can migrate to string.IsNullOrEmpty().
/// Relates https://stackoverflow.com/a/64066801.
/// </summary>
internal static bool IsNullOrEmpty([NotNullWhen(false)] this string? data)
{
return string.IsNullOrEmpty(data);
}
}