Skip to content

Commit

Permalink
[dotnet] [bidi] Enable implicit ways to specify page ranges for printing
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Sep 13, 2024
1 parent 6c0df70 commit e031b8d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 19 deletions.
1 change: 1 addition & 0 deletions dotnet/src/webdriver/BiDi/Communication/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Broker(BiDi bidi, ITransport transport)
new RealmConverter(_bidi),
new RealmTypeConverter(),
new DateTimeOffsetConverter(),
new PrintPageRangeConverter(),
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),

// https://github.com/dotnet/runtime/issues/72604
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

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

internal class PrintPageRangeConverter : JsonConverter<PrintPageRange>
{
public override PrintPageRange Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, PrintPageRange value, JsonSerializerOptions options)
{
// 5, "5-6", "-2", "2-"

if (value.Start.HasValue && value.End.HasValue && value.Start == value.End)
{
writer.WriteNumberValue(value.Start.Value);
}
else
{
writer.WriteStringValue($"{value.Start}-{value.End}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ public Task SetViewportAsync(SetViewportOptions? options = null)
return _bidi.BrowsingContextModule.SetViewportAsync(this, options);
}

public async Task<string> PrintAsync(PrintOptions? options = null)
public Task<PrintResult> PrintAsync(PrintOptions? options = null)
{
var result = await _bidi.BrowsingContextModule.PrintAsync(this, options).ConfigureAwait(false);

return result.Data;
return _bidi.BrowsingContextModule.PrintAsync(this, options);
}

public Task HandleUserPromptAsync(HandleUserPromptOptions? options = null)
Expand Down
75 changes: 60 additions & 15 deletions dotnet/src/webdriver/BiDi/Modules/BrowsingContext/PrintCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenQA.Selenium.BiDi.Communication;
using OpenQA.Selenium.BiDi.Communication;
using System;
using System.Collections.Generic;

namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;
Expand All @@ -9,14 +10,13 @@ internal record PrintCommandParameters(BrowsingContext Context) : CommandParamet
{
public bool? Background { get; set; }

public Margin? Margin { get; set; }
public PrintMargin? Margin { get; set; }

public Orientation? Orientation { get; set; }
public PrintOrientation? Orientation { get; set; }

public Page? Page { get; set; }
public PrintPage? Page { get; set; }

// TODO: It also supports strings
public IEnumerable<long>? PageRanges { get; set; }
public IEnumerable<PrintPageRange>? PageRanges { get; set; }

public double? Scale { get; set; }

Expand All @@ -27,21 +27,20 @@ public record PrintOptions : CommandOptions
{
public bool? Background { get; set; }

public Margin? Margin { get; set; }
public PrintMargin? Margin { get; set; }

public Orientation? Orientation { get; set; }
public PrintOrientation? Orientation { get; set; }

public Page? Page { get; set; }
public PrintPage? Page { get; set; }

// TODO: It also supports strings
public IEnumerable<long>? PageRanges { get; set; }
public IEnumerable<PrintPageRange>? PageRanges { get; set; }

public double? Scale { get; set; }

public bool? ShrinkToFit { get; set; }
}

public struct Margin
public struct PrintMargin
{
public double? Bottom { get; set; }

Expand All @@ -52,17 +51,63 @@ public struct Margin
public double? Top { get; set; }
}

public enum Orientation
public enum PrintOrientation
{
Portrait,
Landscape
}

public struct Page
public struct PrintPage
{
public double? Height { get; set; }

public double? Width { get; set; }
}

public record PrintResult(string Data);
public readonly record struct PrintPageRange(int? Start, int? End)
{
public static implicit operator PrintPageRange(int index) { return new PrintPageRange(index, index); }

#if NET8_0_OR_GREATER
public static implicit operator PrintPageRange(Range range)
{
int? start;
int? end;

if (range.Start.IsFromEnd && range.Start.Value == 0)
{
start = null;
}
else
{
if (range.Start.IsFromEnd)
{
throw new NotSupportedException($"Page index from end ({range.Start}) is not supported in page range for printing.");
}

start = range.Start.Value;
}

if (range.End.IsFromEnd && range.End.Value == 0)
{
end = null;
}
else
{
if (range.End.IsFromEnd)
{
throw new NotSupportedException($"Page index from end ({range.End}) is not supported in page range for printing.");
}

end = range.End.Value;
}

return new PrintPageRange(start, end);
}
#endif
}

public record PrintResult(string Data)
{
public byte[] ToByteArray() => Convert.FromBase64String(Data);
}

0 comments on commit e031b8d

Please sign in to comment.