From 06240a0b2d1a69cf7edc6d1c1ce4f63982b6156d Mon Sep 17 00:00:00 2001 From: Mathias Mogensen <42929161+Xazin@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:02:55 +0100 Subject: [PATCH] feat: add em and divider support to html converter (#22) --- lib/src/infra/html_converter.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/src/infra/html_converter.dart b/lib/src/infra/html_converter.dart index f20d3d8a9..30369cafe 100644 --- a/lib/src/infra/html_converter.dart +++ b/lib/src/infra/html_converter.dart @@ -21,6 +21,7 @@ class HTMLTag { static const image = "img"; static const anchor = "a"; static const italic = "i"; + static const em = "em"; static const bold = "b"; static const underline = "u"; static const del = "del"; @@ -29,6 +30,7 @@ class HTMLTag { static const code = "code"; static const blockQuote = "blockquote"; static const div = "div"; + static const divider = "hr"; static bool isTopLevel(String tag) { return tag == h1 || @@ -69,6 +71,7 @@ class HTMLToNodesConverter { child.localName == HTMLTag.strong || child.localName == HTMLTag.underline || child.localName == HTMLTag.italic || + child.localName == HTMLTag.em || child.localName == HTMLTag.del) { _handleRichTextElement(delta, child); } else if (child.localName == HTMLTag.bold) { @@ -130,6 +133,8 @@ class HTMLToNodesConverter { return [_handleParagraph(element, attributes)]; } else if (element.localName == HTMLTag.image) { return [_handleImage(element)]; + } else if (element.localName == HTMLTag.divider) { + return [_handleDivider()]; } else { final delta = Delta(); delta.insert(element.text); @@ -137,6 +142,7 @@ class HTMLToNodesConverter { return [TextNode(delta: delta)]; } } + return []; } @@ -148,6 +154,8 @@ class HTMLToNodesConverter { return node; } + Node _handleDivider() => Node(type: 'divider'); + Map _cssStringToMap(String? cssString) { final result = {}; if (cssString == null) { @@ -235,7 +243,7 @@ class HTMLToNodesConverter { } else if (element.localName == HTMLTag.underline) { delta.insert(element.text, attributes: {BuiltInAttributeKey.underline: true}); - } else if (element.localName == HTMLTag.italic) { + } else if ([HTMLTag.italic, HTMLTag.em].contains(element.localName)) { delta .insert(element.text, attributes: {BuiltInAttributeKey.italic: true}); } else if (element.localName == HTMLTag.del) { @@ -301,7 +309,9 @@ class HTMLToNodesConverter { final result = []; for (var child in element.children) { result.addAll(_handleListElement( - child, {"subtype": BuiltInAttributeKey.bulletedList})); + child, + {"subtype": BuiltInAttributeKey.bulletedList}, + )); } return result; }