|
@@ -7,6 +7,18 @@ import 'package:flutter/foundation.dart';
|
|
import 'package:html/parser.dart' show parse;
|
|
import 'package:html/parser.dart' show parse;
|
|
import 'package:html/dom.dart' as html;
|
|
import 'package:html/dom.dart' as html;
|
|
|
|
|
|
|
|
+const String tagH1 = "h1";
|
|
|
|
+const String tagH2 = "h2";
|
|
|
|
+const String tagH3 = "h3";
|
|
|
|
+const String tagUnorderedList = "ul";
|
|
|
|
+const String tagList = "li";
|
|
|
|
+const String tagParagraph = "p";
|
|
|
|
+const String tagImage = "img";
|
|
|
|
+const String tagAnchor = "a";
|
|
|
|
+const String tagBold = "b";
|
|
|
|
+const String tagStrong = "strong";
|
|
|
|
+const String tagSpan = "span";
|
|
|
|
+
|
|
class HTMLConverter {
|
|
class HTMLConverter {
|
|
final html.Document _document;
|
|
final html.Document _document;
|
|
|
|
|
|
@@ -19,10 +31,10 @@ class HTMLConverter {
|
|
final childNodes = _document.body?.nodes.toList() ?? <html.Node>[];
|
|
final childNodes = _document.body?.nodes.toList() ?? <html.Node>[];
|
|
for (final child in childNodes) {
|
|
for (final child in childNodes) {
|
|
if (child is html.Element) {
|
|
if (child is html.Element) {
|
|
- if (child.localName == "a" ||
|
|
|
|
- child.localName == "span" ||
|
|
|
|
- child.localName == "strong" ||
|
|
|
|
- child.localName == "b") {
|
|
|
|
|
|
+ if (child.localName == tagAnchor ||
|
|
|
|
+ child.localName == tagSpan ||
|
|
|
|
+ child.localName == tagStrong ||
|
|
|
|
+ child.localName == tagBold) {
|
|
_handleRichTextElement(delta, child);
|
|
_handleRichTextElement(delta, child);
|
|
} else {
|
|
} else {
|
|
_handleElement(result, child);
|
|
_handleElement(result, child);
|
|
@@ -40,17 +52,17 @@ class HTMLConverter {
|
|
}
|
|
}
|
|
|
|
|
|
_handleElement(List<Node> nodes, html.Element element) {
|
|
_handleElement(List<Node> nodes, html.Element element) {
|
|
- if (element.localName == "h1") {
|
|
|
|
- _handleHeadingElement(nodes, element, "h1");
|
|
|
|
- } else if (element.localName == "h2") {
|
|
|
|
- _handleHeadingElement(nodes, element, "h2");
|
|
|
|
- } else if (element.localName == "h3") {
|
|
|
|
- _handleHeadingElement(nodes, element, "h3");
|
|
|
|
- } else if (element.localName == "ul") {
|
|
|
|
|
|
+ if (element.localName == tagH1) {
|
|
|
|
+ _handleHeadingElement(nodes, element, tagH1);
|
|
|
|
+ } else if (element.localName == tagH2) {
|
|
|
|
+ _handleHeadingElement(nodes, element, tagH2);
|
|
|
|
+ } else if (element.localName == tagH3) {
|
|
|
|
+ _handleHeadingElement(nodes, element, tagH3);
|
|
|
|
+ } else if (element.localName == tagUnorderedList) {
|
|
_handleUnorderedList(nodes, element);
|
|
_handleUnorderedList(nodes, element);
|
|
- } else if (element.localName == "li") {
|
|
|
|
|
|
+ } else if (element.localName == tagList) {
|
|
_handleListElement(nodes, element);
|
|
_handleListElement(nodes, element);
|
|
- } else if (element.localName == "p") {
|
|
|
|
|
|
+ } else if (element.localName == tagParagraph) {
|
|
_handleParagraph(nodes, element);
|
|
_handleParagraph(nodes, element);
|
|
} else {
|
|
} else {
|
|
final delta = Delta();
|
|
final delta = Delta();
|
|
@@ -89,17 +101,17 @@ class HTMLConverter {
|
|
}
|
|
}
|
|
|
|
|
|
_handleRichTextElement(Delta delta, html.Element element) {
|
|
_handleRichTextElement(Delta delta, html.Element element) {
|
|
- if (element.localName == "span") {
|
|
|
|
|
|
+ if (element.localName == tagSpan) {
|
|
delta.insert(element.text,
|
|
delta.insert(element.text,
|
|
_getDeltaAttributesFromHtmlAttributes(element.attributes));
|
|
_getDeltaAttributesFromHtmlAttributes(element.attributes));
|
|
- } else if (element.localName == "a") {
|
|
|
|
|
|
+ } else if (element.localName == tagAnchor) {
|
|
final hyperLink = element.attributes["href"];
|
|
final hyperLink = element.attributes["href"];
|
|
Map<String, dynamic>? attributes;
|
|
Map<String, dynamic>? attributes;
|
|
if (hyperLink != null) {
|
|
if (hyperLink != null) {
|
|
attributes = {"href": hyperLink};
|
|
attributes = {"href": hyperLink};
|
|
}
|
|
}
|
|
delta.insert(element.text, attributes);
|
|
delta.insert(element.text, attributes);
|
|
- } else if (element.localName == "strong" || element.localName == "b") {
|
|
|
|
|
|
+ } else if (element.localName == tagStrong || element.localName == tagBold) {
|
|
delta.insert(element.text, {"bold": true});
|
|
delta.insert(element.text, {"bold": true});
|
|
} else {
|
|
} else {
|
|
delta.insert(element.text);
|
|
delta.insert(element.text);
|
|
@@ -107,7 +119,7 @@ class HTMLConverter {
|
|
}
|
|
}
|
|
|
|
|
|
_handleRichText(List<Node> nodes, html.Element element) {
|
|
_handleRichText(List<Node> nodes, html.Element element) {
|
|
- final image = element.querySelector("img");
|
|
|
|
|
|
+ final image = element.querySelector(tagImage);
|
|
if (image != null) {
|
|
if (image != null) {
|
|
_handleImage(nodes, image);
|
|
_handleImage(nodes, image);
|
|
return;
|
|
return;
|