Skip to content

Commit e031b8d

Browse files
committed
[dotnet] [bidi] Enable implicit ways to specify page ranges for printing
1 parent 6c0df70 commit e031b8d

File tree

4 files changed

+91
-19
lines changed

4 files changed

+91
-19
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public Broker(BiDi bidi, ITransport transport)
5858
new RealmConverter(_bidi),
5959
new RealmTypeConverter(),
6060
new DateTimeOffsetConverter(),
61+
new PrintPageRangeConverter(),
6162
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
6263

6364
// https://github.com/dotnet/runtime/issues/72604
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
2+
using System;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
6+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
7+
8+
internal class PrintPageRangeConverter : JsonConverter<PrintPageRange>
9+
{
10+
public override PrintPageRange Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
15+
public override void Write(Utf8JsonWriter writer, PrintPageRange value, JsonSerializerOptions options)
16+
{
17+
// 5, "5-6", "-2", "2-"
18+
19+
if (value.Start.HasValue && value.End.HasValue && value.Start == value.End)
20+
{
21+
writer.WriteNumberValue(value.Start.Value);
22+
}
23+
else
24+
{
25+
writer.WriteStringValue($"{value.Start}-{value.End}");
26+
}
27+
}
28+
}

dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContext.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,9 @@ public Task SetViewportAsync(SetViewportOptions? options = null)
8888
return _bidi.BrowsingContextModule.SetViewportAsync(this, options);
8989
}
9090

91-
public async Task<string> PrintAsync(PrintOptions? options = null)
91+
public Task<PrintResult> PrintAsync(PrintOptions? options = null)
9292
{
93-
var result = await _bidi.BrowsingContextModule.PrintAsync(this, options).ConfigureAwait(false);
94-
95-
return result.Data;
93+
return _bidi.BrowsingContextModule.PrintAsync(this, options);
9694
}
9795

9896
public Task HandleUserPromptAsync(HandleUserPromptOptions? options = null)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OpenQA.Selenium.BiDi.Communication;
1+
using OpenQA.Selenium.BiDi.Communication;
2+
using System;
23
using System.Collections.Generic;
34

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

12-
public Margin? Margin { get; set; }
13+
public PrintMargin? Margin { get; set; }
1314

14-
public Orientation? Orientation { get; set; }
15+
public PrintOrientation? Orientation { get; set; }
1516

16-
public Page? Page { get; set; }
17+
public PrintPage? Page { get; set; }
1718

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

2121
public double? Scale { get; set; }
2222

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

30-
public Margin? Margin { get; set; }
30+
public PrintMargin? Margin { get; set; }
3131

32-
public Orientation? Orientation { get; set; }
32+
public PrintOrientation? Orientation { get; set; }
3333

34-
public Page? Page { get; set; }
34+
public PrintPage? Page { get; set; }
3535

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

3938
public double? Scale { get; set; }
4039

4140
public bool? ShrinkToFit { get; set; }
4241
}
4342

44-
public struct Margin
43+
public struct PrintMargin
4544
{
4645
public double? Bottom { get; set; }
4746

@@ -52,17 +51,63 @@ public struct Margin
5251
public double? Top { get; set; }
5352
}
5453

55-
public enum Orientation
54+
public enum PrintOrientation
5655
{
5756
Portrait,
5857
Landscape
5958
}
6059

61-
public struct Page
60+
public struct PrintPage
6261
{
6362
public double? Height { get; set; }
6463

6564
public double? Width { get; set; }
6665
}
6766

68-
public record PrintResult(string Data);
67+
public readonly record struct PrintPageRange(int? Start, int? End)
68+
{
69+
public static implicit operator PrintPageRange(int index) { return new PrintPageRange(index, index); }
70+
71+
#if NET8_0_OR_GREATER
72+
public static implicit operator PrintPageRange(Range range)
73+
{
74+
int? start;
75+
int? end;
76+
77+
if (range.Start.IsFromEnd && range.Start.Value == 0)
78+
{
79+
start = null;
80+
}
81+
else
82+
{
83+
if (range.Start.IsFromEnd)
84+
{
85+
throw new NotSupportedException($"Page index from end ({range.Start}) is not supported in page range for printing.");
86+
}
87+
88+
start = range.Start.Value;
89+
}
90+
91+
if (range.End.IsFromEnd && range.End.Value == 0)
92+
{
93+
end = null;
94+
}
95+
else
96+
{
97+
if (range.End.IsFromEnd)
98+
{
99+
throw new NotSupportedException($"Page index from end ({range.End}) is not supported in page range for printing.");
100+
}
101+
102+
end = range.End.Value;
103+
}
104+
105+
return new PrintPageRange(start, end);
106+
}
107+
#endif
108+
}
109+
110+
public record PrintResult(string Data)
111+
{
112+
public byte[] ToByteArray() => Convert.FromBase64String(Data);
113+
}

0 commit comments

Comments
 (0)