Skip to content

Commit

Permalink
Merge pull request #2741 from hardkoded/support-margin-with-no-units
Browse files Browse the repository at this point in the history
Fix Pdf Margin with no units
  • Loading branch information
kblok authored Aug 16, 2024
2 parents a3cca3a + 188e298 commit 5cc4e17
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
"parameters": ["firefox", "headful"],
"expectations": ["FAIL"]
},
{
"comment": "This is flaky on CI/CD",
"testIdPattern": "[oopif.spec] OOPIF should provide access to elements",
"platforms": ["win32"],
"parameters": ["chrome"],
"expectations": ["FAIL"]
},
{
"comment": "",
"testIdPattern": "[puppeteer-sharp] *",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task ShouldWorkWithBothDomcontentloadedAndLoad()
}
}).ContinueWith(_ => bothFired = true);

await waitForRequestTask.WithTimeout();
await waitForRequestTask.WithTimeout(5_000);
await domContentLoadedTask.WithTimeout();
Assert.That(bothFired, Is.False);
responseCompleted.SetResult(true);
Expand Down
22 changes: 22 additions & 0 deletions lib/PuppeteerSharp.Tests/PageTests/PdfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,27 @@ public void PdfOptionsShouldBeSerializable()
var newPdfOptions = JsonSerializer.Deserialize<PdfOptions>(serialized);
Assert.That(newPdfOptions, Is.EqualTo(pdfOptions));
}

[Test]
public void PdfOptionsShouldWorkWithMarginWithNoUnits()
{
var pdfOptions = new PdfOptions
{
Format = PaperFormat.A4,
DisplayHeaderFooter = true,
MarginOptions = new MarginOptions
{
Top = "0",
Right = "0",
Bottom = "0",
Left = "0"
},
FooterTemplate = "<div id=\"footer-template\" style=\"font-size:10px !important; color:#808080; padding-left:10px\">- <span class=\"pageNumber\"></span> - </div>"
};

var serialized = JsonSerializer.Serialize(pdfOptions);
var newPdfOptions = JsonSerializer.Deserialize<PdfOptions>(serialized);
Assert.That(newPdfOptions, Is.EqualTo(pdfOptions));
}
}
}
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/Cdp/CdpPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,14 +1221,14 @@ private decimal ConvertPrintParameterToInches(object parameter)
}

decimal pixels;
if (parameter is decimal || parameter is int)
if (parameter is decimal or int)
{
pixels = Convert.ToDecimal(parameter, CultureInfo.CurrentCulture);
}
else
{
var text = parameter.ToString();
var unit = text.Substring(text.Length - 2).ToLower(CultureInfo.CurrentCulture);
var unit = text.Length > 2 ? text.Substring(text.Length - 2).ToLower(CultureInfo.CurrentCulture) : string.Empty;
string valueText;
if (_unitToPixels.ContainsKey(unit))
{
Expand Down

0 comments on commit 5cc4e17

Please sign in to comment.