Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// <copyright file="SpecialNumberConverter.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;

// Serializes and deserializes double into a BiDi spec-compliant number value.
// See https://w3c.github.io/webdriver-bidi/#type-script-PrimitiveProtocolValue
internal sealed class SpecialNumberConverter : JsonConverter<double>
{
public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.Number:
return reader.GetDouble();

case JsonTokenType.String:
var str = reader.GetString()!;
if (str.Equals("-0", StringComparison.Ordinal))
{
return -0.0;
}

if (str.Equals("NaN", StringComparison.Ordinal))
{
return double.NaN;
}

if (str.Equals("Infinity", StringComparison.Ordinal))
{
return double.PositiveInfinity;
}

if (str.Equals("-Infinity", StringComparison.Ordinal))
{
return double.NegativeInfinity;
}

throw new JsonException($"JSON '{str}' string could not be parsed to a special number");

default:
throw new JsonException($"JSON type not a number or string: {reader.TokenType}");
}
}

public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
{
if (double.IsNaN(value))
{
writer.WriteStringValue("NaN");
}
else if (double.IsPositiveInfinity(value))
{
writer.WriteStringValue("Infinity");
}
else if (double.IsNegativeInfinity(value))
{
writer.WriteStringValue("-Infinity");
}
else if (IsNegativeZero(value))
{
writer.WriteStringValue("-0");
}
else
{
writer.WriteNumberValue(value);
}

static bool IsNegativeZero(double x)
{
// Negative zero is less trivial to test, because 0 == -0 is true
// We need to do a bit pattern comparison

return BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(-0.0);
}
}
}
3 changes: 2 additions & 1 deletion dotnet/src/webdriver/BiDi/Script/LocalValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Numerics;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using OpenQA.Selenium.BiDi.Communication.Json.Converters;

namespace OpenQA.Selenium.BiDi.Script;

Expand Down Expand Up @@ -282,7 +283,7 @@ private static LocalValue ReflectionBasedConvertFrom(object? value)

public abstract record PrimitiveProtocolLocalValue : LocalValue;

public sealed record NumberLocalValue(double Value) : PrimitiveProtocolLocalValue
public sealed record NumberLocalValue([property: JsonConverter(typeof(SpecialNumberConverter))] double Value) : PrimitiveProtocolLocalValue
{
public static explicit operator NumberLocalValue(double n) => new NumberLocalValue(n);
}
Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/webdriver/BiDi/Script/RemoteValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Numerics;
using System.Text.Json;
using System.Text.Json.Serialization;
using OpenQA.Selenium.BiDi.Communication.Json.Converters;

namespace OpenQA.Selenium.BiDi.Script;

Expand Down Expand Up @@ -100,7 +101,7 @@ public abstract record RemoteValue

public abstract record PrimitiveProtocolRemoteValue : RemoteValue;

public sealed record NumberRemoteValue(double Value) : PrimitiveProtocolRemoteValue;
public sealed record NumberRemoteValue([property: JsonConverter(typeof(SpecialNumberConverter))] double Value) : PrimitiveProtocolRemoteValue;

public sealed record BooleanRemoteValue(bool Value) : PrimitiveProtocolRemoteValue;

Expand Down
2 changes: 0 additions & 2 deletions dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ public async Task CanCallFunctionWithArgumentNumberZero()
}

[Test]
[IgnoreBrowser(Selenium.Browser.Edge, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")]
[IgnoreBrowser(Selenium.Browser.Chrome, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")]
public async Task CanCallFunctionWithArgumentNumberNegativeZero()
{
var arg = new NumberLocalValue(double.NegativeZero);
Expand Down