Skip to content

Commit 2e687fc

Browse files
Meir017kblok
authored andcommitted
Round the clip dimensions (#896)
* Round the clip dimensions * Update lib/PuppeteerSharp.Tests/ElementHandleTests/ScreenshotTests.cs Co-Authored-By: Meir017 <[email protected]> * Update lib/PuppeteerSharp/Page.cs Co-Authored-By: Meir017 <[email protected]> * Update lib/PuppeteerSharp/Page.cs Co-Authored-By: Meir017 <[email protected]>
1 parent 1f4a71d commit 2e687fc

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

lib/PuppeteerSharp.Tests/ElementHandleTests/ScreenshotTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,23 @@ public async Task ShouldNotHangWithZeroWidthHeightElement()
152152
var exception = await Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotDataAsync);
153153
Assert.Equal("Node has 0 height.", exception.Message);
154154
}
155+
156+
[Fact]
157+
public async Task ShouldWorkForAnElementWithFractionalDimensions()
158+
{
159+
await Page.SetContentAsync("<div style=\"width:48.51px;height:19.8px;border:1px solid black;\"></div>");
160+
var elementHandle = await Page.QuerySelectorAsync("div");
161+
var screenshot = await elementHandle.ScreenshotDataAsync();
162+
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional.png", screenshot));
163+
}
164+
165+
[Fact]
166+
public async Task ShouldWorkForAnElementWithAnOffset()
167+
{
168+
await Page.SetContentAsync("<div style=\"position:absolute; top: 10.3px; left: 20.4px;width:50.3px;height:20.2px;border:1px solid black;\"></div>");
169+
var elementHandle = await Page.QuerySelectorAsync("div");
170+
var screenshot = await elementHandle.ScreenshotDataAsync();
171+
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot));
172+
}
155173
}
156174
}
138 Bytes
Loading
138 Bytes
Loading

lib/PuppeteerSharp/Media/Clip.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using Newtonsoft.Json;
3-
4-
namespace PuppeteerSharp.Media
1+
namespace PuppeteerSharp.Media
52
{
63
/// <summary>
74
/// Clip data.
@@ -35,16 +32,5 @@ public class Clip
3532
/// </summary>
3633
/// <value>The scale.</value>
3734
public int Scale { get; internal set; }
38-
39-
internal Clip Clone()
40-
{
41-
return new Clip
42-
{
43-
X = X,
44-
Y = Y,
45-
Width = Width,
46-
Height = Height
47-
};
48-
}
4935
}
5036
}

lib/PuppeteerSharp/Page.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,11 +1640,7 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
16401640
}).ConfigureAwait(false);
16411641
}
16421642

1643-
var clip = options.Clip?.Clone();
1644-
if (clip != null)
1645-
{
1646-
clip.Scale = 1;
1647-
}
1643+
var clip = options.Clip != null ? ProcessClip(options.Clip) : null;
16481644

16491645
if (!_screenshotBurstModeOn)
16501646
{
@@ -1743,6 +1739,21 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
17431739
return result.Data;
17441740
}
17451741

1742+
private Clip ProcessClip(Clip clip)
1743+
{
1744+
var x = Math.Round(clip.X);
1745+
var y = Math.Round(clip.Y);
1746+
1747+
return new Clip
1748+
{
1749+
X = x,
1750+
Y = y,
1751+
Width = Math.Round(clip.Width + clip.X - x, MidpointRounding.AwayFromZero),
1752+
Height = Math.Round(clip.Height + clip.Y - y, MidpointRounding.AwayFromZero),
1753+
Scale = 1
1754+
};
1755+
}
1756+
17461757
private Task ResetBackgroundColorAndViewportAsync(ScreenshotOptions options)
17471758
{
17481759
var omitBackgroundTask = options?.OmitBackground == true && options.Type == ScreenshotType.Png ?

0 commit comments

Comments
 (0)