using System.Globalization; using AngleSharp.Dom; namespace EvilEngine.Html; public static class Defaults { public const double MonospaceRatio = 12.0 / 16.0; public static readonly Rgba ThreeDFace = new(233, 233, 237); private static readonly HashSet Hidden = [ "head", "script", "style", "template", "title", "meta", "link", "base", "noscript", "area", "datalist", "param", "rp", "source", "track", "col", "colgroup", "dialog", "option", "optgroup", "embed", "object", "iframe", "video", "audio", "canvas", "svg", "math", "map" ]; private static readonly HashSet Blocks = [ "html", "body", "address", "blockquote", "center", "div", "figure", "figcaption", "footer", "form", "header", "hr", "legend", "listing", "main", "p", "plaintext", "pre", "search", "xmp", "article", "aside", "h1", "h2", "h3", "h4", "h5", "h6", "hgroup", "nav", "section", "dir", "dd", "dl", "dt", "menu", "ol", "ul", "fieldset", "details", "summary" ]; private static readonly HashSet Lists = ["dir", "menu", "ol", "ul"]; private static readonly HashSet NestedListMargins = ["dir", "dl", "menu", "ol", "ul"]; private static readonly HashSet Monospace = ["code", "kbd", "samp", "tt", "pre", "listing", "plaintext", "xmp"]; private static readonly HashSet Italic = ["address", "cite", "dfn", "em", "i", "var"]; private static readonly HashSet Bold = ["b", "strong", "th"]; private static readonly HashSet OneEmBlocks = ["p", "dl", "dir", "menu", "ol", "ul", "pre", "listing", "plaintext", "xmp", "blockquote", "figure"]; public static ComputedStyle For(IElement element, ComputedStyle parent) { var name = element.LocalName; var style = parent.Inherit(); if (Hidden.Contains(name) || element.HasAttribute("hidden")) return style with { Display = Display.None }; if (name == "input" && string.Equals(element.GetAttribute("type"), "hidden", StringComparison.OrdinalIgnoreCase)) return style with { Display = Display.None }; if (Blocks.Contains(name)) style = style with { Display = Display.Block }; style = name switch { "li" => style with { Display = Display.ListItem }, "table" => style with { Display = Display.Table }, "caption" => style with { Display = Display.TableCaption }, "thead" => style with { Display = Display.TableHeaderGroup, VerticalAlign = VerticalAlign.Middle }, "tbody" => style with { Display = Display.TableRowGroup, VerticalAlign = VerticalAlign.Middle }, "tfoot" => style with { Display = Display.TableFooterGroup, VerticalAlign = VerticalAlign.Middle }, "tr" => style with { Display = Display.TableRow, VerticalAlign = element.ParentElement?.LocalName == "table" ? VerticalAlign.Middle : parent.VerticalAlign }, "td" or "th" => style with { Display = Display.TableCell, VerticalAlign = parent.VerticalAlign }, _ => style }; style = Font(name, style, parent); var em = style.FontSize; if (OneEmBlocks.Contains(name)) style = style with { Margin = Edges.Vertical(em) }; style = name switch { "body" => style with { Margin = Edges.All(8) }, "h1" => style with { Margin = Edges.Vertical(0.67 * em) }, "h2" => style with { Margin = Edges.Vertical(0.83 * em) }, "h3" => style with { Margin = Edges.Vertical(1.0 * em) }, "h4" => style with { Margin = Edges.Vertical(1.33 * em) }, "h5" => style with { Margin = Edges.Vertical(1.67 * em) }, "h6" => style with { Margin = Edges.Vertical(2.33 * em) }, "blockquote" or "figure" => style with { Margin = new Edges(em, 40, em, 40) }, "dd" => style with { Margin = new Edges(0, 0, 0, 40) }, "fieldset" => style with { Margin = new Edges(0, 2, 0, 2), Padding = new Edges(0.35 * em, 0.75 * em, 0.625 * em, 0.75 * em), Border = Edges.All(2), BorderStyle = BorderStyle.Groove, BorderColor = ThreeDFace }, "legend" => style with { Padding = new Edges(0, 2, 0, 2) }, "hr" => style with { Margin = Edges.Vertical(0.5 * em), Border = Edges.All(1), BorderStyle = BorderStyle.Inset, BorderColor = Rgba.Gray }, "center" or "caption" => style with { TextAlign = TextAlign.Center }, "th" when parent.TextAlign == TextAlign.Start => style with { TextAlign = TextAlign.Center }, "pre" or "listing" or "plaintext" or "xmp" => style with { WhiteSpace = WhiteSpace.Pre }, "nobr" => style with { WhiteSpace = WhiteSpace.NoWrap }, "u" or "ins" => style with { Decoration = style.Decoration | Decoration.Underline }, "s" or "strike" or "del" => style with { Decoration = style.Decoration | Decoration.LineThrough }, "mark" => style with { Background = Rgba.Yellow, Color = Rgba.Black }, "sub" => style with { VerticalAlign = VerticalAlign.Sub }, "sup" => style with { VerticalAlign = VerticalAlign.Super }, _ => style }; if (name == "summary" && element.ParentElement is { LocalName: "details" } details && details.Children.FirstOrDefault(child => child.LocalName == "summary") == element) style = style with { ListStyle = details.HasAttribute("open") ? ListStyle.DisclosureOpen : ListStyle.DisclosureClosed }; if (name is "a" or "area" && element.HasAttribute("href")) style = style with { Color = Rgba.Link, Decoration = style.Decoration | Decoration.Underline }; if (Lists.Contains(name)) style = style with { Padding = new Edges(0, 0, 0, 40), ListStyle = name == "ol" ? ListStyle.Decimal : ListDepthStyle(element) }; if (NestedListMargins.Contains(name) && HasListAncestor(element)) style = style with { Margin = style.Margin with { Top = 0, Bottom = 0 } }; if (name is "ol" or "ul" or "li" && ListType(element.GetAttribute("type")) is { } type) style = style with { ListStyle = type }; if (name is "p" or "div" or "h1" or "h2" or "h3" or "h4" or "h5" or "h6" or "caption" && Align(element.GetAttribute("align")) is { } align) style = style with { TextAlign = align }; if (name is "td" or "th" && element.HasAttribute("nowrap")) style = style with { WhiteSpace = WhiteSpace.NoWrap }; return name switch { "table" => Table(element, style), "td" or "th" => Cell(element, style), "tr" or "thead" or "tbody" or "tfoot" => Aligned(element, style), _ => style }; } private static ComputedStyle Table(IElement table, ComputedStyle style) { style = style with { BorderSpacing = ParseInteger(table.GetAttribute("cellspacing")) ?? 2, Width = ParseDimension(table.GetAttribute("width")), CenteredByMargins = string.Equals(table.GetAttribute("align")?.Trim(), "center", StringComparison.OrdinalIgnoreCase) }; if (table.GetAttribute("border") is { } border) style = style with { Border = Edges.All(ParseInteger(border) ?? 1), BorderStyle = BorderStyle.Outset }; return style; } private static ComputedStyle Cell(IElement cell, ComputedStyle style) { var table = cell.Closest("table"); style = Aligned(cell, style with { Padding = Edges.All(ParseInteger(table?.GetAttribute("cellpadding")) ?? 1), Width = ParseDimension(cell.GetAttribute("width")), Height = ParseDimension(cell.GetAttribute("height")) }); if (table?.GetAttribute("border") is { } border && ParseInteger(border) != 0) style = style with { Border = Edges.All(1), BorderStyle = BorderStyle.Inset }; return style; } private static ComputedStyle Aligned(IElement element, ComputedStyle style) { if (Align(element.GetAttribute("align")) is { } align) style = style with { TextAlign = align }; return element.GetAttribute("valign")?.Trim().ToLowerInvariant() switch { "top" => style with { VerticalAlign = VerticalAlign.Top }, "middle" or "center" => style with { VerticalAlign = VerticalAlign.Middle }, "bottom" => style with { VerticalAlign = VerticalAlign.Bottom }, "baseline" => style with { VerticalAlign = VerticalAlign.Baseline }, _ => style }; } public static int? ParseInteger(string? value) { if (value == null) return null; var i = 0; while (i < value.Length && value[i] is ' ' or '\t' or '\n' or '\f' or '\r') i++; if (i < value.Length && value[i] == '+') i++; var start = i; long number = 0; for (; i < value.Length && char.IsAsciiDigit(value[i]); i++) number = Math.Min(number * 10 + (value[i] - '0'), int.MaxValue); return i == start ? null : (int)number; } public static Length? ParseDimension(string? value) { if (value == null) return null; var i = 0; while (i < value.Length && value[i] is ' ' or '\t' or '\n' or '\f' or '\r') i++; var start = i; while (i < value.Length && char.IsAsciiDigit(value[i])) i++; if (i == start) return null; var end = i; if (i < value.Length && value[i] == '.') { i++; while (i < value.Length && char.IsAsciiDigit(value[i])) i++; if (i > end + 1) end = i; } var number = double.Parse(value[start..end], CultureInfo.InvariantCulture); if (number == 0) return null; return new Length(number, end < value.Length && value[end..].TrimStart('.').StartsWith('%')); } private static ComputedStyle Font(string name, ComputedStyle style, ComputedStyle parent) { var size = parent.FontSize; style = name switch { "h1" => style with { FontSize = size * 2, Bold = true }, "h2" => style with { FontSize = size * 1.5, Bold = true }, "h3" => style with { FontSize = size * 1.17, Bold = true }, "h4" => style with { FontSize = size, Bold = true }, "h5" => style with { FontSize = size * 0.83, Bold = true }, "h6" => style with { FontSize = size * 0.67, Bold = true }, "big" => style with { FontSize = size * 1.2 }, "small" or "sub" or "sup" => style with { FontSize = size / 1.2 }, _ => style }; if (Bold.Contains(name)) style = style with { Bold = true }; if (Italic.Contains(name)) style = style with { Italic = true }; if (Monospace.Contains(name) && style.Family != GenericFamily.Monospace) style = style with { Family = GenericFamily.Monospace, FontSize = style.FontSize * MonospaceRatio }; return style; } private static bool HasListAncestor(IElement element) { for (var parent = element.ParentElement; parent != null; parent = parent.ParentElement) if (Lists.Contains(parent.LocalName)) return true; return false; } private static ListStyle ListDepthStyle(IElement element) { var depth = 0; for (var parent = element.ParentElement; parent != null; parent = parent.ParentElement) if (Lists.Contains(parent.LocalName)) depth++; return depth switch { 0 => ListStyle.Disc, 1 => ListStyle.Circle, _ => ListStyle.Square }; } private static ListStyle? ListType(string? type) { return type switch { null => null, "1" => ListStyle.Decimal, "a" => ListStyle.LowerAlpha, "A" => ListStyle.UpperAlpha, "i" => ListStyle.LowerRoman, "I" => ListStyle.UpperRoman, _ => type.ToLowerInvariant() switch { "disc" => ListStyle.Disc, "circle" => ListStyle.Circle, "square" => ListStyle.Square, "none" => ListStyle.None, _ => null } }; } private static TextAlign? Align(string? align) { return align?.ToLowerInvariant() switch { "left" or "justify" => TextAlign.Start, "center" or "middle" => TextAlign.Center, "right" => TextAlign.Right, _ => null }; } }