Skip to content
Merged
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
71 changes: 54 additions & 17 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ 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);

Html.fromDom({
Key? key,
GlobalKey? anchorKey,
@required this.document,
@required dom.Document? document,
this.onLinkTap,
this.onAnchorTap,
this.customRenders = const {},
Expand All @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -135,13 +154,13 @@ class Html extends StatefulWidget {
}

class _HtmlState extends State<Html> {
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
Expand All @@ -150,7 +169,7 @@ class _HtmlState extends State<Html> {
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,
Expand All @@ -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 (`<a href>`)
/// is tapped.
Expand Down Expand Up @@ -213,15 +232,15 @@ class SelectableHtml extends StatefulWidget {
this.tagsList = const [],
this.selectionControls,
this.scrollPhysics,
}) : document = null,
}) : documentElement = null,
assert(data != null),
_anchorKey = anchorKey ?? GlobalKey(),
super(key: key);

SelectableHtml.fromDom({
Key? key,
GlobalKey? anchorKey,
required this.document,
@required dom.Document? document,
this.onLinkTap,
this.onAnchorTap,
this.onCssParseError,
Expand All @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -283,13 +321,12 @@ class SelectableHtml extends StatefulWidget {
}

class _SelectableHtmlState extends State<SelectableHtml> {
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
Expand All @@ -298,7 +335,7 @@ class _SelectableHtmlState extends State<SelectableHtml> {
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,
Expand Down
12 changes: 6 additions & 6 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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<CustomRenderMatcher> customRenderMatchers,
List<String> tagsList,
BuildContext context,
Expand All @@ -148,7 +148,7 @@ class HtmlParser extends StatelessWidget {
StyledElement tree = StyledElement(
name: "[Tree Root]",
children: <StyledElement>[],
node: html.documentElement,
node: html,
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
);

Expand Down