-
-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathflutter_html.dart
180 lines (159 loc) · 5.89 KB
/
flutter_html.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
library;
import 'package:flutter/material.dart';
import 'package:flutter_html/src/html_parser.dart';
import 'package:flutter_html/src/extension/html_extension.dart';
import 'package:flutter_html/src/style.dart';
import 'package:html/dom.dart' as dom;
//export render context api
export 'package:flutter_html/src/html_parser.dart';
//export src for advanced custom render uses (e.g. casting context.tree)
export 'package:flutter_html/src/anchor.dart';
export 'package:flutter_html/src/tree/image_element.dart';
export 'package:flutter_html/src/tree/interactable_element.dart';
export 'package:flutter_html/src/tree/replaced_element.dart';
export 'package:flutter_html/src/tree/styled_element.dart';
//export css_box_widget for use in extensions.
export 'package:flutter_html/src/css_box_widget.dart';
//export style api
export 'package:flutter_html/src/style.dart';
//export extension api
export 'package:flutter_html/src/extension/html_extension.dart';
class Html extends StatefulWidget {
/// The `Html` widget takes HTML as input and displays a RichText
/// tree of the parsed HTML content.
///
/// **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).
///
/// **extensions** A list of [Extension]s that add additional capabilities to flutter_html
/// See the [Extension] class for more details.
///
/// **onLinkTap** This function is called whenever a link (`<a href>`)
/// is tapped.
///
/// **shrinkWrap** This makes the Html widget take up only the width it
/// needs and no more.
///
/// **onlyRenderTheseTags** provides an exclusive list of tags to render.
///
/// **doNotRenderTheseTags** provides a short list of tags that the Html
/// widget should completely ignore.
///
/// **style** Pass in the style information for the Html here.
/// See [its wiki page](https://github.com/Sub6Resources/flutter_html/wiki/Style) for more info.
Html({
super.key,
GlobalKey? anchorKey,
required this.data,
this.onLinkTap,
this.onAnchorTap,
this.extensions = const [],
this.onCssParseError,
this.shrinkWrap = false,
this.onlyRenderTheseTags,
this.doNotRenderTheseTags,
this.style = const {},
}) : documentElement = null,
assert(data != null),
_anchorKey = anchorKey ?? GlobalKey();
Html.fromDom({
super.key,
GlobalKey? anchorKey,
required dom.Document? document,
this.onLinkTap,
this.onAnchorTap,
this.extensions = const [],
this.onCssParseError,
this.shrinkWrap = false,
this.doNotRenderTheseTags,
this.onlyRenderTheseTags,
this.style = const {},
}) : data = null,
assert(document != null),
documentElement = document!.documentElement,
_anchorKey = anchorKey ?? GlobalKey();
Html.fromElement({
super.key,
GlobalKey? anchorKey,
required this.documentElement,
this.onLinkTap,
this.onAnchorTap,
this.extensions = const [],
this.onCssParseError,
this.shrinkWrap = false,
this.doNotRenderTheseTags,
this.onlyRenderTheseTags,
this.style = const {},
}) : data = null,
assert(documentElement != null),
_anchorKey = anchorKey ?? GlobalKey();
/// A unique key for this Html widget to ensure uniqueness of anchors
final GlobalKey _anchorKey;
/// 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.Element]
final dom.Element? documentElement;
/// A function that defines what to do when a link is tapped
final OnTap? onLinkTap;
/// A function that defines what to do when an anchor link is tapped. When this value is set,
/// the default anchor behaviour is overwritten.
final OnTap? onAnchorTap;
/// A function that defines what to do when CSS fails to parse
final OnCssParseError? onCssParseError;
/// A parameter that should be set when the HTML widget is expected to be
/// flexible
final bool shrinkWrap;
/// A set of HTML tags to completely ignore in the provided code.
final Set<String>? doNotRenderTheseTags;
/// A set of the only HTML tags that should be rendered by this widget.
///
/// Note that the html parser wraps your html in an `<html>` and `<body>` tag
/// by default, so you should include those in this set if you want any
/// of your html to render.
final Set<String>? onlyRenderTheseTags;
/// A list of [HtmlExtension]s that add additional capabilities to flutter_html
/// See the [HtmlExtension] class for more details.
final List<HtmlExtension> extensions;
/// An API that allows you to override the default style for any HTML element
final Map<String, Style> style;
@override
State<StatefulWidget> createState() => _HtmlState();
}
class _HtmlState extends State<Html> {
late dom.Element documentElement;
@override
void initState() {
super.initState();
documentElement = widget.data != null
? HtmlParser.parseHTML(widget.data!)
: widget.documentElement!;
}
@override
void didUpdateWidget(Html oldWidget) {
super.didUpdateWidget(oldWidget);
if ((widget.data != null && oldWidget.data != widget.data) ||
oldWidget.documentElement != widget.documentElement) {
documentElement = widget.data != null
? HtmlParser.parseHTML(widget.data!)
: widget.documentElement!;
}
}
@override
Widget build(BuildContext context) {
return HtmlParser(
key: widget._anchorKey,
htmlData: documentElement,
onLinkTap: widget.onLinkTap,
onAnchorTap: widget.onAnchorTap,
onCssParseError: widget.onCssParseError,
shrinkWrap: widget.shrinkWrap,
style: widget.style,
extensions: widget.extensions,
doNotRenderTheseTags: widget.doNotRenderTheseTags,
onlyRenderTheseTags: widget.onlyRenderTheseTags,
);
}
}