forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalValue.cs
326 lines (252 loc) · 10.8 KB
/
LocalValue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// <copyright file="LocalValue.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.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace OpenQA.Selenium.BiDi.Modules.Script;
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(NumberLocalValue), "number")]
[JsonDerivedType(typeof(StringLocalValue), "string")]
[JsonDerivedType(typeof(NullLocalValue), "null")]
[JsonDerivedType(typeof(UndefinedLocalValue), "undefined")]
[JsonDerivedType(typeof(BooleanLocalValue), "boolean")]
[JsonDerivedType(typeof(BigIntLocalValue), "bigint")]
[JsonDerivedType(typeof(ChannelLocalValue), "channel")]
[JsonDerivedType(typeof(ArrayLocalValue), "array")]
[JsonDerivedType(typeof(DateLocalValue), "date")]
[JsonDerivedType(typeof(MapLocalValue), "map")]
[JsonDerivedType(typeof(ObjectLocalValue), "object")]
[JsonDerivedType(typeof(RegExpLocalValue), "regexp")]
[JsonDerivedType(typeof(SetLocalValue), "set")]
public abstract record LocalValue
{
public static implicit operator LocalValue(bool? value) { return value is bool b ? (b ? True : False) : Null; }
public static implicit operator LocalValue(int? value) { return value is int i ? Number(i) : Null; }
public static implicit operator LocalValue(double? value) { return value is double d ? Number(d) : Null; }
public static implicit operator LocalValue(string? value) { return value is null ? Null : String(value); }
// TODO: Extend converting from types
public static LocalValue ConvertFrom(object? value)
{
switch (value)
{
case LocalValue localValue:
return localValue;
case null:
return Null;
case bool b:
return b ? True : False;
case int i:
return Number(i);
case double d:
return Number(d);
case string str:
return String(str);
case IEnumerable<object?> list:
return Array(list.Select(ConvertFrom).ToList());
case object:
{
var type = value.GetType();
var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
List<List<LocalValue>> values = [];
foreach (var property in properties)
{
values.Add([property.Name, ConvertFrom(property.GetValue(value))]);
}
return Object(values);
}
}
}
private static readonly BigInteger MaxDouble = new BigInteger(double.MaxValue);
private static readonly BigInteger MinDouble = new BigInteger(double.MinValue);
public static LocalValue ConvertFrom(JsonNode? node)
{
if (node is null)
{
return Null;
}
switch (node.GetValueKind())
{
case System.Text.Json.JsonValueKind.Null:
return Null;
case System.Text.Json.JsonValueKind.True:
return True;
case System.Text.Json.JsonValueKind.False:
return False;
case System.Text.Json.JsonValueKind.String:
return String(node.ToString());
case System.Text.Json.JsonValueKind.Number:
{
var numberString = node.ToString();
var bigNumber = BigInteger.Parse(numberString);
if (bigNumber > MaxDouble || bigNumber < MinDouble)
{
return BigInt(bigNumber);
}
return Number(double.Parse(numberString));
}
case System.Text.Json.JsonValueKind.Array:
return Array(node.AsArray().Select(ConvertFrom));
case System.Text.Json.JsonValueKind.Object:
var convertedToListForm = node.AsObject().Select(property => new LocalValue[] { String(property.Key), ConvertFrom(property.Value) }).ToList();
return Object(convertedToListForm);
default:
throw new InvalidOperationException("Invalid JSON node");
}
}
public static ChannelLocalValue Channel(ChannelLocalValue.ChannelProperties options)
{
return new ChannelLocalValue(options);
}
public static ArrayLocalValue Array(IEnumerable<LocalValue> values)
{
return new ArrayLocalValue(values);
}
public static SetLocalValue Set(HashSet<LocalValue> values)
{
return new SetLocalValue(values);
}
public static ObjectLocalValue Object(IEnumerable<IEnumerable<LocalValue>> values)
{
return new ObjectLocalValue(values);
}
public static ObjectLocalValue Object(IDictionary<string, LocalValue> values)
{
var convertedValues = values.Select(pair => new LocalValue[] { new StringLocalValue(pair.Key), pair.Value }).ToList();
return new ObjectLocalValue(convertedValues);
}
public static MapLocalValue Map(IEnumerable<IEnumerable<LocalValue>> values)
{
return new MapLocalValue(values);
}
public static MapLocalValue Map(IDictionary<LocalValue, LocalValue> values)
{
var convertedValues = values.Select(PairToList).ToList();
return new MapLocalValue(convertedValues);
}
private static LocalValue[] PairToList(KeyValuePair<LocalValue, LocalValue> pair)
{
return [pair.Key, pair.Value];
}
public static BigIntLocalValue BigInt(BigInteger value)
{
return new BigIntLocalValue(value.ToString());
}
public static DateLocalValue Date(DateTime value)
{
return new DateLocalValue(value.ToString("o"));
}
public static StringLocalValue String(string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value), $"string value cannot be null, use a {nameof(NullLocalValue)} value instead");
}
return new StringLocalValue(value);
}
public static NumberLocalValue Number(double value)
{
return new NumberLocalValue(value);
}
public static BooleanLocalValue True { get; } = new BooleanLocalValue(true);
public static BooleanLocalValue False { get; } = new BooleanLocalValue(false);
public static NullLocalValue Null { get; } = new NullLocalValue();
public static UndefinedLocalValue Undefined { get; } = new UndefinedLocalValue();
/// <summary>
/// Converts a .NET Regex into a BiDi Regex
/// </summary>
/// <param name="regex">A .NET Regex.</param>
/// <returns>A BiDi Regex.</returns>
/// <remarks>
/// Note that the .NET regular expression engine does not work the same as the JavaScript engine.
/// To minimize the differences between the two engines, it is recommended to enabled the <see cref="RegexOptions.ECMAScript"/> option.
/// </remarks>
public static RegExpLocalValue Regex(Regex regex)
{
RegexOptions options = regex.Options;
if (options == RegexOptions.None)
{
return new RegExpLocalValue(new RegExpValue(regex.ToString()));
}
string flags = string.Empty;
const RegexOptions NonBacktracking = (RegexOptions)1024;
#if NET8_0_OR_GREATER
Debug.Assert(NonBacktracking == RegexOptions.NonBacktracking);
#endif
const RegexOptions NonApplicableOptions = RegexOptions.Compiled | NonBacktracking;
const RegexOptions UnsupportedOptions =
RegexOptions.ExplicitCapture |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.RightToLeft |
RegexOptions.CultureInvariant;
options &= ~NonApplicableOptions;
if ((options & UnsupportedOptions) != 0)
{
throw new NotSupportedException($"The selected RegEx options are not supported in BiDi: {options & UnsupportedOptions}");
}
if ((options & RegexOptions.IgnoreCase) != 0)
{
flags += "i";
options = options & ~RegexOptions.IgnoreCase;
}
if ((options & RegexOptions.Multiline) != 0)
{
options = options & ~RegexOptions.Multiline;
flags += "m";
}
if ((options & RegexOptions.Singleline) != 0)
{
options = options & ~RegexOptions.Singleline;
flags += "s";
}
Debug.Assert(options == RegexOptions.None);
return new RegExpLocalValue(new RegExpValue(regex.ToString()) { Flags = flags });
}
}
public abstract record PrimitiveProtocolLocalValue : LocalValue;
public record NumberLocalValue(double Value) : PrimitiveProtocolLocalValue
{
public static explicit operator NumberLocalValue(double n) => new NumberLocalValue(n);
}
public record StringLocalValue(string Value) : PrimitiveProtocolLocalValue;
public record NullLocalValue : PrimitiveProtocolLocalValue;
public record UndefinedLocalValue : PrimitiveProtocolLocalValue;
public record BooleanLocalValue(bool Value) : PrimitiveProtocolLocalValue;
public record BigIntLocalValue(string Value) : PrimitiveProtocolLocalValue;
public record ChannelLocalValue(ChannelLocalValue.ChannelProperties Value) : LocalValue
{
// TODO: Revise why we need it
[JsonInclude]
internal string type = "channel";
public record ChannelProperties(Channel Channel)
{
public SerializationOptions? SerializationOptions { get; set; }
public ResultOwnership? Ownership { get; set; }
}
}
public record ArrayLocalValue(IEnumerable<LocalValue> Value) : LocalValue;
public record DateLocalValue(string Value) : LocalValue;
public record MapLocalValue(IEnumerable<IEnumerable<LocalValue>> Value) : LocalValue;
public record ObjectLocalValue(IEnumerable<IEnumerable<LocalValue>> Value) : LocalValue;
public record RegExpLocalValue(RegExpValue Value) : LocalValue;
public record SetLocalValue(IEnumerable<LocalValue> Value) : LocalValue;