Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/iTextSharp.LGPLv2.Core.FunctionalTests/HtmlWorkerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.util;
using iTextSharp.LGPLv2.Core.FunctionalTests.iTextExamples;
using iTextSharp.text;
Expand Down Expand Up @@ -181,6 +182,35 @@ public void Verify_Html_To_Pdf_With_colspan_CanBeCreated()
TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
}

[TestMethod]
public void Verify_BackgroundColor_Style_Is_Applied_To_Chunks()
{
var styles = new StyleSheet();

using var reader =
new StringReader(
"<span style='background-color:#ffff00'>styled</span> <font bgcolor='red'>attributed</font>");

var objects = HtmlWorker.ParseToList(reader, styles);

var chunks = new List<Chunk>();

foreach (var element in objects)
{
chunks.AddRange(element.Chunks);
}

var styledChunk = chunks.Find(c => c.Content == "styled");
Assert.IsNotNull(styledChunk);
var styledBackground = (object[])styledChunk.Attributes[Chunk.BACKGROUND];
Assert.AreEqual(expected: 0xFFFF00, ((BaseColor)styledBackground[0]).ToArgb() & 0xFFFFFF);

var attributedChunk = chunks.Find(c => c.Content == "attributed");
Assert.IsNotNull(attributedChunk);
var attributedBackground = (object[])attributedChunk.Attributes[Chunk.BACKGROUND];
Assert.AreEqual(expected: 0xFF0000, ((BaseColor)attributedBackground[0]).ToArgb() & 0xFFFFFF);
}

private void applyRtlRunDirection(IElement htmlElement)
{
if (htmlElement is not PdfPTable table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ public static void InsertStyle(INullValueDictionary<string, string> h)
h[key: "color"] = hs;
}
}
else if (key.Equals(Markup.CSS_KEY_BGCOLOR, StringComparison.OrdinalIgnoreCase))
{
h[key: "bgcolor"] = prop[key];
}
else if (key.Equals(Markup.CSS_KEY_LINEHEIGHT, StringComparison.OrdinalIgnoreCase))
{
var ss = prop[key].Trim();
Expand Down Expand Up @@ -387,6 +391,10 @@ public static void InsertStyle(INullValueDictionary<string, string> h, ChainedPr
h[key: "color"] = hs;
}
}
else if (key.Equals(Markup.CSS_KEY_BGCOLOR, StringComparison.OrdinalIgnoreCase))
{
h[key: "bgcolor"] = prop[key];
}
else if (key.Equals(Markup.CSS_KEY_LINEHEIGHT, StringComparison.OrdinalIgnoreCase))
{
var ss = prop[key].Trim();
Expand Down Expand Up @@ -447,6 +455,13 @@ public static Chunk CreateChunk(string text, ChainedProperties props)
ck.SetTextRise(size);
}

var bgColor = Markup.DecodeColor(props[key: "bgcolor"]);

if (bgColor != null)
{
ck.SetBackground(bgColor);
}

ck.SetHyphenation(GetHyphenation(props));

return ck;
Expand Down