Skip to content

Commit 8a350cc

Browse files
committed
change DOM Document to Element for more flexible
1 parent f88dcfb commit 8a350cc

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

lib/flutter_html.dart

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import 'package:webview_flutter/webview_flutter.dart';
1212
//export render context api
1313
export 'package:flutter_html/html_parser.dart';
1414
export 'package:flutter_html/image_render.dart';
15+
1516
//export src for advanced custom render uses (e.g. casting context.tree)
1617
export 'package:flutter_html/src/anchor.dart';
1718
export 'package:flutter_html/src/interactable_element.dart';
1819
export 'package:flutter_html/src/layout_element.dart';
1920
export 'package:flutter_html/src/replaced_element.dart';
2021
export 'package:flutter_html/src/styled_element.dart';
22+
2123
//export style api
2224
export 'package:flutter_html/style.dart';
2325

@@ -95,8 +97,8 @@ class Html extends StatelessWidget {
9597
/// The HTML data passed to the widget as a String
9698
final String? data;
9799

98-
/// The HTML data passed to the widget as a pre-processed [dom.Document]
99-
final dom.Document? document;
100+
/// The HTML data passed to the widget as a pre-processed [dom.Element]
101+
final dom.Element? document;
100102

101103
/// A function that defines what to do when a link is tapped
102104
final OnTap? onLinkTap;
@@ -150,8 +152,7 @@ class Html extends StatelessWidget {
150152

151153
@override
152154
Widget build(BuildContext context) {
153-
final dom.Document doc =
154-
data != null ? HtmlParser.parseHTML(data!) : document!;
155+
final dom.Element doc = data != null ? HtmlParser.parseHTML(data!): document!;
155156
final double? width = shrinkWrap ? null : MediaQuery.of(context).size.width;
156157

157158
return Container(
@@ -169,9 +170,7 @@ class Html extends StatelessWidget {
169170
selectable: false,
170171
style: style,
171172
customRender: customRender,
172-
imageRenders: {}
173-
..addAll(customImageRenders)
174-
..addAll(defaultImageRenders),
173+
imageRenders: {}..addAll(customImageRenders)..addAll(defaultImageRenders),
175174
tagsList: tagsList.isEmpty ? Html.tags : tagsList,
176175
navigationDelegateForIframe: navigationDelegateForIframe,
177176
),
@@ -211,34 +210,34 @@ class SelectableHtml extends StatelessWidget {
211210
/// (e.g. bold or italic), while container related styling (e.g. borders or padding/margin)
212211
/// do not work because we can't use the `ContainerSpan` class (it needs an enclosing `WidgetSpan`).
213212
214-
SelectableHtml({
215-
Key? key,
216-
GlobalKey? anchorKey,
217-
required this.data,
218-
this.onLinkTap,
219-
this.onAnchorTap,
220-
this.onCssParseError,
221-
this.shrinkWrap = false,
222-
this.style = const {},
223-
this.tagsList = const [],
224-
this.selectionControls
225-
}) : document = null,
213+
SelectableHtml(
214+
{Key? key,
215+
GlobalKey? anchorKey,
216+
required this.data,
217+
this.onLinkTap,
218+
this.onAnchorTap,
219+
this.onCssParseError,
220+
this.shrinkWrap = false,
221+
this.style = const {},
222+
this.tagsList = const [],
223+
this.selectionControls})
224+
: document = null,
226225
assert(data != null),
227226
_anchorKey = anchorKey ?? GlobalKey(),
228227
super(key: key);
229228

230-
SelectableHtml.fromDom({
231-
Key? key,
232-
GlobalKey? anchorKey,
233-
required this.document,
234-
this.onLinkTap,
235-
this.onAnchorTap,
236-
this.onCssParseError,
237-
this.shrinkWrap = false,
238-
this.style = const {},
239-
this.tagsList = const [],
240-
this.selectionControls
241-
}) : data = null,
229+
SelectableHtml.fromDom(
230+
{Key? key,
231+
GlobalKey? anchorKey,
232+
required this.document,
233+
this.onLinkTap,
234+
this.onAnchorTap,
235+
this.onCssParseError,
236+
this.shrinkWrap = false,
237+
this.style = const {},
238+
this.tagsList = const [],
239+
this.selectionControls})
240+
: data = null,
242241
assert(document != null),
243242
_anchorKey = anchorKey ?? GlobalKey(),
244243
super(key: key);
@@ -249,8 +248,8 @@ class SelectableHtml extends StatelessWidget {
249248
/// The HTML data passed to the widget as a String
250249
final String? data;
251250

252-
/// The HTML data passed to the widget as a pre-processed [dom.Document]
253-
final dom.Document? document;
251+
/// The HTML data passed to the widget as a pre-processed [dom.Element]
252+
final dom.Element? document;
254253

255254
/// A function that defines what to do when a link is tapped
256255
final OnTap? onLinkTap;
@@ -280,7 +279,7 @@ class SelectableHtml extends StatelessWidget {
280279

281280
@override
282281
Widget build(BuildContext context) {
283-
final dom.Document doc = data != null ? HtmlParser.parseHTML(data!) : document!;
282+
final dom.Element doc = data != null ? HtmlParser.parseHTML(data!) : document!;
284283
final double? width = shrinkWrap ? null : MediaQuery.of(context).size.width;
285284

286285
return Container(

lib/html_parser.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef CustomRender = dynamic Function(
4242

4343
class HtmlParser extends StatelessWidget {
4444
final Key? key;
45-
final dom.Document htmlData;
45+
final dom.Element htmlData;
4646
final OnTap? onLinkTap;
4747
final OnTap? onAnchorTap;
4848
final OnTap? onImageTap;
@@ -143,9 +143,9 @@ class HtmlParser extends StatelessWidget {
143143
);
144144
}
145145

146-
/// [parseHTML] converts a string of HTML to a DOM document using the dart `html` library.
147-
static dom.Document parseHTML(String data) {
148-
return htmlparser.parse(data);
146+
/// [parseHTML] converts a string of HTML to a DOM element using the dart `html` library.
147+
static dom.Element parseHTML(String data) {
148+
return htmlparser.parse(data).documentElement!;
149149
}
150150

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

156156
/// [lexDomTree] converts a DOM document to a simplified tree of [StyledElement]s.
157157
static StyledElement lexDomTree(
158-
dom.Document html,
158+
dom.Element html,
159159
List<String> customRenderTags,
160160
List<String> tagsList,
161161
NavigationDelegate? navigationDelegateForIframe,
@@ -164,7 +164,7 @@ class HtmlParser extends StatelessWidget {
164164
StyledElement tree = StyledElement(
165165
name: "[Tree Root]",
166166
children: <StyledElement>[],
167-
node: html.documentElement,
167+
node: html,
168168
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
169169
);
170170

0 commit comments

Comments
 (0)