Skip to content

Commit 9adc596

Browse files
authored
Merge pull request Sub6Resources#827 from andy1xx8/contribute_change_doc_to_element
change DOM Document to Element for more flexible
2 parents d13ca75 + 1e0d9fd commit 9adc596

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

lib/flutter_html.dart

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ class Html extends StatefulWidget {
6060
this.onImageTap,
6161
this.tagsList = const [],
6262
this.style = const {},
63-
}) : document = null,
63+
}) : documentElement = null,
6464
assert (data != null),
6565
_anchorKey = anchorKey ?? GlobalKey(),
6666
super(key: key);
6767

6868
Html.fromDom({
6969
Key? key,
7070
GlobalKey? anchorKey,
71-
@required this.document,
71+
@required dom.Document? document,
7272
this.onLinkTap,
7373
this.onAnchorTap,
7474
this.customRenders = const {},
@@ -80,6 +80,25 @@ class Html extends StatefulWidget {
8080
this.style = const {},
8181
}) : data = null,
8282
assert(document != null),
83+
this.documentElement = document!.documentElement,
84+
_anchorKey = anchorKey ?? GlobalKey(),
85+
super(key: key);
86+
87+
Html.fromElement({
88+
Key? key,
89+
GlobalKey? anchorKey,
90+
@required this.documentElement,
91+
this.onLinkTap,
92+
this.onAnchorTap,
93+
this.customRenders = const {},
94+
this.onCssParseError,
95+
this.onImageError,
96+
this.shrinkWrap = false,
97+
this.onImageTap,
98+
this.tagsList = const [],
99+
this.style = const {},
100+
}) : data = null,
101+
assert(documentElement != null),
83102
_anchorKey = anchorKey ?? GlobalKey(),
84103
super(key: key);
85104

@@ -89,8 +108,8 @@ class Html extends StatefulWidget {
89108
/// The HTML data passed to the widget as a String
90109
final String? data;
91110

92-
/// The HTML data passed to the widget as a pre-processed [dom.Document]
93-
final dom.Document? document;
111+
/// The HTML data passed to the widget as a pre-processed [dom.Element]
112+
final dom.Element? documentElement;
94113

95114
/// A function that defines what to do when a link is tapped
96115
final OnTap? onLinkTap;
@@ -135,13 +154,13 @@ class Html extends StatefulWidget {
135154
}
136155

137156
class _HtmlState extends State<Html> {
138-
late final dom.Document doc;
157+
late final dom.Element documentElement;
139158

140159
@override
141160
void initState() {
142161
super.initState();
143-
doc =
144-
widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.document!;
162+
documentElement =
163+
widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.documentElement!;
145164
}
146165

147166
@override
@@ -150,7 +169,7 @@ class _HtmlState extends State<Html> {
150169
width: widget.shrinkWrap ? null : MediaQuery.of(context).size.width,
151170
child: HtmlParser(
152171
key: widget._anchorKey,
153-
htmlData: doc,
172+
htmlData: documentElement,
154173
onLinkTap: widget.onLinkTap,
155174
onAnchorTap: widget.onAnchorTap,
156175
onImageTap: widget.onImageTap,
@@ -174,7 +193,7 @@ class SelectableHtml extends StatefulWidget {
174193
///
175194
/// **Attributes**
176195
/// **data** *required* takes in a String of HTML data (required only for `Html` constructor).
177-
/// **document** *required* takes in a Document of HTML data (required only for `Html.fromDom` constructor).
196+
/// **documentElement** *required* takes in a Element of HTML data (required only for `Html.fromDom` and `Html.fromElement` constructor).
178197
///
179198
/// **onLinkTap** This function is called whenever a link (`<a href>`)
180199
/// is tapped.
@@ -213,15 +232,15 @@ class SelectableHtml extends StatefulWidget {
213232
this.tagsList = const [],
214233
this.selectionControls,
215234
this.scrollPhysics,
216-
}) : document = null,
235+
}) : documentElement = null,
217236
assert(data != null),
218237
_anchorKey = anchorKey ?? GlobalKey(),
219238
super(key: key);
220239

221240
SelectableHtml.fromDom({
222241
Key? key,
223242
GlobalKey? anchorKey,
224-
required this.document,
243+
@required dom.Document? document,
225244
this.onLinkTap,
226245
this.onAnchorTap,
227246
this.onCssParseError,
@@ -233,6 +252,25 @@ class SelectableHtml extends StatefulWidget {
233252
this.scrollPhysics,
234253
}) : data = null,
235254
assert(document != null),
255+
this.documentElement = document!.documentElement,
256+
_anchorKey = anchorKey ?? GlobalKey(),
257+
super(key: key);
258+
259+
SelectableHtml.fromElement({
260+
Key? key,
261+
GlobalKey? anchorKey,
262+
@required this.documentElement,
263+
this.onLinkTap,
264+
this.onAnchorTap,
265+
this.onCssParseError,
266+
this.shrinkWrap = false,
267+
this.style = const {},
268+
this.customRenders = const {},
269+
this.tagsList = const [],
270+
this.selectionControls,
271+
this.scrollPhysics,
272+
}) : data = null,
273+
assert(documentElement != null),
236274
_anchorKey = anchorKey ?? GlobalKey(),
237275
super(key: key);
238276

@@ -242,8 +280,8 @@ class SelectableHtml extends StatefulWidget {
242280
/// The HTML data passed to the widget as a String
243281
final String? data;
244282

245-
/// The HTML data passed to the widget as a pre-processed [dom.Document]
246-
final dom.Document? document;
283+
/// The HTML data passed to the widget as a pre-processed [dom.Element]
284+
final dom.Element? documentElement;
247285

248286
/// A function that defines what to do when a link is tapped
249287
final OnTap? onLinkTap;
@@ -283,13 +321,12 @@ class SelectableHtml extends StatefulWidget {
283321
}
284322

285323
class _SelectableHtmlState extends State<SelectableHtml> {
286-
late final dom.Document doc;
324+
late final dom.Element documentElement;
287325

288326
@override
289327
void initState() {
290328
super.initState();
291-
doc =
292-
widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.document!;
329+
documentElement = widget.data != null ? HtmlParser.parseHTML(widget.data!) : widget.documentElement!;
293330
}
294331

295332
@override
@@ -298,7 +335,7 @@ class _SelectableHtmlState extends State<SelectableHtml> {
298335
width: widget.shrinkWrap ? null : MediaQuery.of(context).size.width,
299336
child: HtmlParser(
300337
key: widget._anchorKey,
301-
htmlData: doc,
338+
htmlData: documentElement,
302339
onLinkTap: widget.onLinkTap,
303340
onAnchorTap: widget.onAnchorTap,
304341
onImageTap: null,

lib/html_parser.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef OnCssParseError = String? Function(
2525

2626
class HtmlParser extends StatelessWidget {
2727
final Key? key;
28-
final dom.Document htmlData;
28+
final dom.Element htmlData;
2929
final OnTap? onLinkTap;
3030
final OnTap? onAnchorTap;
3131
final OnTap? onImageTap;
@@ -127,9 +127,9 @@ class HtmlParser extends StatelessWidget {
127127
);
128128
}
129129

130-
/// [parseHTML] converts a string of HTML to a DOM document using the dart `html` library.
131-
static dom.Document parseHTML(String data) {
132-
return htmlparser.parse(data);
130+
/// [parseHTML] converts a string of HTML to a DOM element using the dart `html` library.
131+
static dom.Element parseHTML(String data) {
132+
return htmlparser.parse(data).documentElement!;
133133
}
134134

135135
/// [parseCss] converts a string of CSS to a CSS stylesheet using the dart `csslib` library.
@@ -139,7 +139,7 @@ class HtmlParser extends StatelessWidget {
139139

140140
/// [lexDomTree] converts a DOM document to a simplified tree of [StyledElement]s.
141141
static StyledElement lexDomTree(
142-
dom.Document html,
142+
dom.Element html,
143143
List<CustomRenderMatcher> customRenderMatchers,
144144
List<String> tagsList,
145145
BuildContext context,
@@ -148,7 +148,7 @@ class HtmlParser extends StatelessWidget {
148148
StyledElement tree = StyledElement(
149149
name: "[Tree Root]",
150150
children: <StyledElement>[],
151-
node: html.documentElement,
151+
node: html,
152152
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
153153
);
154154

0 commit comments

Comments
 (0)