diff --git a/lib/flutter_html.dart b/lib/flutter_html.dart
index 1405471220..f0b33d547d 100644
--- a/lib/flutter_html.dart
+++ b/lib/flutter_html.dart
@@ -60,7 +60,7 @@ class Html extends StatefulWidget {
this.onImageTap,
this.tagsList = const [],
this.style = const {},
- }) : document = null,
+ }) : documentElement = null,
assert (data != null),
_anchorKey = anchorKey ?? GlobalKey(),
super(key: key);
@@ -68,7 +68,7 @@ class Html extends StatefulWidget {
Html.fromDom({
Key? key,
GlobalKey? anchorKey,
- @required this.document,
+ @required dom.Document? document,
this.onLinkTap,
this.onAnchorTap,
this.customRenders = const {},
@@ -80,6 +80,25 @@ class Html extends StatefulWidget {
this.style = const {},
}) : data = null,
assert(document != null),
+ this.documentElement = document!.documentElement,
+ _anchorKey = anchorKey ?? GlobalKey(),
+ super(key: key);
+
+ Html.fromElement({
+ Key? key,
+ GlobalKey? anchorKey,
+ @required this.documentElement,
+ this.onLinkTap,
+ this.onAnchorTap,
+ this.customRenders = const {},
+ this.onCssParseError,
+ this.onImageError,
+ this.shrinkWrap = false,
+ this.onImageTap,
+ this.tagsList = const [],
+ this.style = const {},
+ }) : data = null,
+ assert(documentElement != null),
_anchorKey = anchorKey ?? GlobalKey(),
super(key: key);
@@ -89,8 +108,8 @@ class Html extends StatefulWidget {
/// The HTML data passed to the widget as a String
final String? data;
- /// The HTML data passed to the widget as a pre-processed [dom.Document]
- final dom.Document? document;
+ /// The HTML data passed to the widget as a pre-processed [dom.Element]
+ final dom.Element? documentElement;
/// A function that defines what to do when a link is tapped
final OnTap? onLinkTap;
@@ -135,13 +154,13 @@ class Html extends StatefulWidget {
}
class _HtmlState extends State {
- late final dom.Document doc;
+ late final dom.Element documentElement;
@override
void initState() {
super.initState();
- doc =
- widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.document!;
+ documentElement =
+ widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.documentElement!;
}
@override
@@ -150,7 +169,7 @@ class _HtmlState extends State {
width: widget.shrinkWrap ? null : MediaQuery.of(context).size.width,
child: HtmlParser(
key: widget._anchorKey,
- htmlData: doc,
+ htmlData: documentElement,
onLinkTap: widget.onLinkTap,
onAnchorTap: widget.onAnchorTap,
onImageTap: widget.onImageTap,
@@ -174,7 +193,7 @@ class SelectableHtml extends StatefulWidget {
///
/// **Attributes**
/// **data** *required* takes in a String of HTML data (required only for `Html` constructor).
- /// **document** *required* takes in a Document of HTML data (required only for `Html.fromDom` constructor).
+ /// **documentElement** *required* takes in a Element of HTML data (required only for `Html.fromDom` and `Html.fromElement` constructor).
///
/// **onLinkTap** This function is called whenever a link (``)
/// is tapped.
@@ -213,7 +232,7 @@ class SelectableHtml extends StatefulWidget {
this.tagsList = const [],
this.selectionControls,
this.scrollPhysics,
- }) : document = null,
+ }) : documentElement = null,
assert(data != null),
_anchorKey = anchorKey ?? GlobalKey(),
super(key: key);
@@ -221,7 +240,7 @@ class SelectableHtml extends StatefulWidget {
SelectableHtml.fromDom({
Key? key,
GlobalKey? anchorKey,
- required this.document,
+ @required dom.Document? document,
this.onLinkTap,
this.onAnchorTap,
this.onCssParseError,
@@ -233,6 +252,25 @@ class SelectableHtml extends StatefulWidget {
this.scrollPhysics,
}) : data = null,
assert(document != null),
+ this.documentElement = document!.documentElement,
+ _anchorKey = anchorKey ?? GlobalKey(),
+ super(key: key);
+
+ SelectableHtml.fromElement({
+ Key? key,
+ GlobalKey? anchorKey,
+ @required this.documentElement,
+ this.onLinkTap,
+ this.onAnchorTap,
+ this.onCssParseError,
+ this.shrinkWrap = false,
+ this.style = const {},
+ this.customRenders = const {},
+ this.tagsList = const [],
+ this.selectionControls,
+ this.scrollPhysics,
+ }) : data = null,
+ assert(documentElement != null),
_anchorKey = anchorKey ?? GlobalKey(),
super(key: key);
@@ -242,8 +280,8 @@ class SelectableHtml extends StatefulWidget {
/// The HTML data passed to the widget as a String
final String? data;
- /// The HTML data passed to the widget as a pre-processed [dom.Document]
- final dom.Document? document;
+ /// The HTML data passed to the widget as a pre-processed [dom.Element]
+ final dom.Element? documentElement;
/// A function that defines what to do when a link is tapped
final OnTap? onLinkTap;
@@ -283,13 +321,12 @@ class SelectableHtml extends StatefulWidget {
}
class _SelectableHtmlState extends State {
- late final dom.Document doc;
+ late final dom.Element documentElement;
@override
void initState() {
super.initState();
- doc =
- widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.document!;
+ documentElement = widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.documentElement!;
}
@override
@@ -298,7 +335,7 @@ class _SelectableHtmlState extends State {
width: widget.shrinkWrap ? null : MediaQuery.of(context).size.width,
child: HtmlParser(
key: widget._anchorKey,
- htmlData: doc,
+ htmlData: documentElement,
onLinkTap: widget.onLinkTap,
onAnchorTap: widget.onAnchorTap,
onImageTap: null,
diff --git a/lib/html_parser.dart b/lib/html_parser.dart
index fdfcf4cbd1..e18195a575 100644
--- a/lib/html_parser.dart
+++ b/lib/html_parser.dart
@@ -25,7 +25,7 @@ typedef OnCssParseError = String? Function(
class HtmlParser extends StatelessWidget {
final Key? key;
- final dom.Document htmlData;
+ final dom.Element htmlData;
final OnTap? onLinkTap;
final OnTap? onAnchorTap;
final OnTap? onImageTap;
@@ -127,9 +127,9 @@ class HtmlParser extends StatelessWidget {
);
}
- /// [parseHTML] converts a string of HTML to a DOM document using the dart `html` library.
- static dom.Document parseHTML(String data) {
- return htmlparser.parse(data);
+ /// [parseHTML] converts a string of HTML to a DOM element using the dart `html` library.
+ static dom.Element parseHTML(String data) {
+ return htmlparser.parse(data).documentElement!;
}
/// [parseCss] converts a string of CSS to a CSS stylesheet using the dart `csslib` library.
@@ -139,7 +139,7 @@ class HtmlParser extends StatelessWidget {
/// [lexDomTree] converts a DOM document to a simplified tree of [StyledElement]s.
static StyledElement lexDomTree(
- dom.Document html,
+ dom.Element html,
List customRenderMatchers,
List tagsList,
BuildContext context,
@@ -148,7 +148,7 @@ class HtmlParser extends StatelessWidget {
StyledElement tree = StyledElement(
name: "[Tree Root]",
children: [],
- node: html.documentElement,
+ node: html,
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
);