Skip to content

Commit

Permalink
Intersect content quads with viewport (#1099)
Browse files Browse the repository at this point in the history
* Intersect content quads with viewport

* Improve error handling

* ops
  • Loading branch information
kblok authored and Meir017 committed Apr 30, 2019
1 parent 66056c8 commit 9444c9c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
17 changes: 17 additions & 0 deletions lib/PuppeteerSharp.Tests/InputTests/ClickTests.cs
Expand Up @@ -68,6 +68,23 @@ public async Task ShouldClickWithDisabledJavascript()
Assert.Equal(TestConstants.ServerUrl + "/wrappedlink.html#clicked", Page.Url);
}

[Fact]
public async Task ShouldClickWhenOneOfInlineBoxChildrenIsOutsideOfViewport()
{
await Page.SetContentAsync($@"
<style>
i {{
position: absolute;
top: -1000px;
}}
</style>
<span onclick='javascript:window.CLICKED = 42;'><i>woof</i><b>doggo</b></span>
");

await Page.ClickAsync("span");
Assert.Equal(42, await Page.EvaluateFunctionAsync<int>("() => window.CLICKED"));
}

[Fact]
public async Task ShouldSelectTheTextByTripleClicking()
{
Expand Down
28 changes: 22 additions & 6 deletions lib/PuppeteerSharp/ElementHandle.cs
Expand Up @@ -5,6 +5,7 @@
using PuppeteerSharp.Input;
using PuppeteerSharp.Messaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -426,16 +427,20 @@ private async Task<(decimal x, decimal y)> ClickablePointAsync()
{
GetContentQuadsResponse result = null;

var contentQuadsTask = Client.SendAsync<GetContentQuadsResponse>("DOM.getContentQuads", new DomGetContentQuadsRequest
{
ObjectId = RemoteObject.ObjectId
});
var layoutTask = Client.SendAsync<PageGetLayoutMetricsResponse>("Page.getLayoutMetrics");

try
{
result = await Client.SendAsync<GetContentQuadsResponse>("DOM.getContentQuads", new DomGetContentQuadsRequest
{
ObjectId = RemoteObject.ObjectId
}).ConfigureAwait(false);
await Task.WhenAll(contentQuadsTask, layoutTask).ConfigureAwait(false);
result = contentQuadsTask.Result;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
_logger.LogError(ex, "Unable to get content quads");
}

if (result == null || result.Quads.Length == 0)
Expand All @@ -444,7 +449,11 @@ private async Task<(decimal x, decimal y)> ClickablePointAsync()
}

// Filter out quads that have too small area to click into.
var quads = result.Quads.Select(FromProtocolQuad).Where(q => ComputeQuadArea(q) > 1);
var quads = result.Quads
.Select(FromProtocolQuad)
.Select(q => IntersectQuadWithViewport(q, layoutTask.Result))
.Where(q => ComputeQuadArea(q.ToArray()) > 1);

if (!quads.Any())
{
throw new PuppeteerException("Node is either not visible or not an HTMLElement");
Expand All @@ -466,6 +475,13 @@ private async Task<(decimal x, decimal y)> ClickablePointAsync()
);
}

private IEnumerable<BoxModelPoint> IntersectQuadWithViewport(IEnumerable<BoxModelPoint> quad, PageGetLayoutMetricsResponse viewport)
=> quad.Select(point => new BoxModelPoint
{
X = Math.Min(Math.Max(point.X, 0), viewport.ContentSize.Width),
Y = Math.Min(Math.Max(point.Y, 0), viewport.ContentSize.Height),
});

private async Task ScrollIntoViewIfNeededAsync()
{
var errorMessage = await ExecutionContext.EvaluateFunctionAsync<string>(@"async(element, pageJavascriptEnabled) => {
Expand Down

0 comments on commit 9444c9c

Please sign in to comment.