mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-12-06 15:54:41 +01:00
Parsing HTML belongs before the data reaches the data layer, not in it. Move the flattening to AudibleUtilities, where the rest of the Item translation lives, so Book.Description holds plain text and DataLayer no longer references HtmlAgilityPack. Import now updates a description as well as creating one, which it never did before: books imported while descriptions were stored as raw HTML are still holding markup, and a re-scan is what clears it. A scan that reports no summary leaves the stored one alone. This also cleans up the CSV/Excel export, which was emitting raw HTML too. Co-authored-by: rmcrackan <rmcrackan@gmail.com>
63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using AssertionHelper;
|
|
using AudibleApi.Common;
|
|
using AudibleUtilities;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace AudibleUtilities.Tests;
|
|
|
|
/// <summary>
|
|
/// Audible sends a book's summary as HTML. It is flattened here, at the API boundary, so nothing
|
|
/// downstream has to know that. Reported in issue #2002, where the markup reached a file's metadata
|
|
/// tags because the only stripping Libation did was for the library grid.
|
|
/// </summary>
|
|
[TestClass]
|
|
public class PlainTextDescriptionTests
|
|
{
|
|
[TestMethod]
|
|
[DataRow(null, "")]
|
|
[DataRow("", "")]
|
|
[DataRow(" ", "")]
|
|
[DataRow("Already plain text.", "Already plain text.")]
|
|
[DataRow(" Padded plain text. ", "Padded plain text.")]
|
|
[DataRow("<p>One paragraph.</p>", "One paragraph.")]
|
|
[DataRow("<p>First.</p> <p>Second.</p>", "First.\nSecond.")]
|
|
[DataRow("<p>First.</p><p>Second.</p>", "First.\nSecond.")]
|
|
[DataRow("<p><b>Bold</b> and <i>italic</i>.</p>", "Bold and italic.")]
|
|
[DataRow("Line one.<br>Line two.", "Line one.\nLine two.")]
|
|
[DataRow("Line one.<br />Line two.", "Line one.\nLine two.")]
|
|
[DataRow("<ul><li>One</li><li>Two</li></ul>", "One\nTwo")]
|
|
[DataRow("<p>Tom & Jerry</p>", "Tom & Jerry")]
|
|
[DataRow("<p>©2024 Someone</p>", "\u00a92024 Someone")]
|
|
[DataRow("<p>Blank</p><p></p><p>lines dropped</p>", "Blank\nlines dropped")]
|
|
public void Flattens(string? html, string expected)
|
|
=> HtmlText.ToPlainText(html).Should().Be(expected);
|
|
|
|
[TestMethod]
|
|
public void SeparatorIsConfigurable()
|
|
=> HtmlText.ToPlainText("<p>First.</p> <p>Second.</p>", paragraphSeparator: "\r\n\r\n")
|
|
.Should().Be("First.\r\n\r\nSecond.");
|
|
|
|
/// <summary>
|
|
/// Unmarked summaries skip the parser, so a stray angle bracket cannot swallow the sentence after
|
|
/// it. Entities are still decoded, since Audible writes those into plain text too.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public void TextWithoutTagsIsNotParsed()
|
|
{
|
|
HtmlText.ToPlainText("5 > 3 and 2 < 4").Should().Be("5 > 3 and 2 < 4");
|
|
HtmlText.ToPlainText("Tom & Jerry").Should().Be("Tom & Jerry");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ReadsTheItemsPublisherSummary()
|
|
{
|
|
var item = new Item { PublisherSummary = "<p>Hello.</p> <p>World.</p>" };
|
|
|
|
item.PlainTextDescription().Should().Be("Hello.\nWorld.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AnItemWithNoSummaryFlattensToEmpty()
|
|
=> new Item().PlainTextDescription().Should().Be("");
|
|
}
|