Skip to content

Commit 3c91d1d

Browse files
authored
Allow for passing IJSHandle as argument to EvaluateFunctionAsync/EvaluateFunctionHandleAsync (#2038)
* Allow for passing IJSHandle as argument to EvaluateFunctionAsync/EvaluateFunctionHandleAsync - Update ExecutionContext.FormatArgument to match IJSHandle instead of JSHandle - Make RemoteObjectHelper class static - Move JSHandle.FormatArgument to RemoteObjectHelper.FormatArgument (extension method) - IJSHandle.RemoteObject fix xml doc * Move FormatArgument from RemoteObjectHelper to PuppeteerHandleExtensions Review feedback
1 parent 1ee8f69 commit 3c91d1d

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

lib/PuppeteerSharp/ExecutionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private object FormatArgument(object arg)
196196

197197
break;
198198

199-
case JSHandle objectHandle:
199+
case IJSHandle objectHandle:
200200
return objectHandle.FormatArgument(this);
201201
}
202202

lib/PuppeteerSharp/IJSHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface IJSHandle : IAsyncDisposable
2424
IExecutionContext ExecutionContext { get; }
2525

2626
/// <summary>
27-
/// Gets or sets the remote object.
27+
/// Gets the remote object.
2828
/// </summary>
2929
/// <value>The remote object.</value>
3030
RemoteObject RemoteObject { get; }

lib/PuppeteerSharp/JSHandle.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -152,34 +152,5 @@ public Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)
152152
list.Insert(0, this);
153153
return ExecutionContext.EvaluateFunctionAsync<T>(script, list.ToArray());
154154
}
155-
156-
internal object FormatArgument(ExecutionContext context)
157-
{
158-
if (ExecutionContext != context)
159-
{
160-
throw new PuppeteerException("JSHandles can be evaluated only in the context they were created!");
161-
}
162-
163-
if (Disposed)
164-
{
165-
throw new PuppeteerException("JSHandle is disposed!");
166-
}
167-
168-
var unserializableValue = RemoteObject.UnserializableValue;
169-
170-
if (unserializableValue != null)
171-
{
172-
return new { unserializableValue };
173-
}
174-
175-
if (RemoteObject.ObjectId == null)
176-
{
177-
return new { RemoteObject.Value };
178-
}
179-
180-
var objectId = RemoteObject.ObjectId;
181-
182-
return new { objectId };
183-
}
184155
}
185156
}

lib/PuppeteerSharp/PuppeteerHandleExtensions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,34 @@ public static async Task<T> EvaluateFunctionAsync<T>(this IJSHandle arrayHandle,
112112
await arrayHandle.DisposeAsync().ConfigureAwait(false);
113113
return result;
114114
}
115+
116+
internal static object FormatArgument(this IJSHandle jSHandle, ExecutionContext context)
117+
{
118+
if (jSHandle.Disposed)
119+
{
120+
throw new PuppeteerException("JSHandle is disposed!");
121+
}
122+
123+
if (jSHandle.ExecutionContext != context)
124+
{
125+
throw new PuppeteerException("JSHandles can be evaluated only in the context they were created!");
126+
}
127+
128+
var unserializableValue = jSHandle.RemoteObject.UnserializableValue;
129+
130+
if (unserializableValue != null)
131+
{
132+
return new { unserializableValue };
133+
}
134+
135+
if (jSHandle.RemoteObject.ObjectId == null)
136+
{
137+
return new { jSHandle.RemoteObject.Value };
138+
}
139+
140+
var objectId = jSHandle.RemoteObject.ObjectId;
141+
142+
return new { objectId };
143+
}
115144
}
116145
}

0 commit comments

Comments
 (0)