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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import 'dart:math';
import 'package:chewie/chewie.dart';
import 'package:chewie_audio/chewie_audio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_html/html_parser.dart';
import 'package:flutter_html/src/html_elements.dart';
import 'package:flutter_html/style.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:html/dom.dart' as dom;
import 'package:video_player/video_player.dart';
import 'package:webview_flutter/webview_flutter.dart';
/// A [ReplacedElement] is a type of [StyledElement] that does not require its [children] to be rendered.
///
/// A [ReplacedElement] may use its children nodes to determine relevant information
/// (e.g. <video>'s <source> tags), but the children nodes will not be saved as [children].
abstract class ReplacedElement extends StyledElement {
PlaceholderAlignment alignment;
ReplacedElement(
{String name,
Style style,
dom.Element node,
this.alignment = PlaceholderAlignment.aboveBaseline})
: super(name: name, children: null, style: style, node: node);
static List<String> parseMediaSources(List<dom.Element> elements) {
return elements
.where((element) => element.localName == 'source')
.map((element) {
return element.attributes['src'];
}).toList();
}
Widget toWidget(RenderContext context);
}
/// [TextContentElement] is a [ContentElement] with plaintext as its content.
class TextContentElement extends ReplacedElement {
String text;
TextContentElement({
Style style,
this.text,
}) : super(name: "[text]", style: style);
@override
String toString() {
return "\"${text.replaceAll("\n", "\\n")}\"";
}
@override
Widget toWidget(_) => null;
}
/// [ImageContentElement] is a [ReplacedElement] with an image as its content.
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
class ImageContentElement extends ReplacedElement {
final String src;
final String alt;
ImageContentElement({
String name,
Style style,
this.src,
this.alt,
dom.Element node,
}) : super(
name: name,
style: style,
node: node,
alignment: PlaceholderAlignment.middle,
);
@override
Widget toWidget(RenderContext context) {
for (final entry in context.parser.imageRenders.entries) {
if (entry.key.call(attributes, element)) {
final widget = entry.value.call(context, attributes, element);
if (widget != null) {
return widget;
}
}
}
return SizedBox(width: 0, height: 0);
}
}
/// [IframeContentElement is a [ReplacedElement] with web content.
class IframeContentElement extends ReplacedElement {
final String src;
final double width;
final double height;
final NavigationDelegate navigationDelegate;
final UniqueKey key = UniqueKey();
IframeContentElement({
String name,
Style style,
this.src,
this.width,
this.height,
dom.Element node,
this.navigationDelegate,
}) : super(name: name, style: style, node: node);
@override
Widget toWidget(RenderContext context) {
final sandboxMode = attributes["sandbox"];
return Container(
width: width ?? (height ?? 150) * 2,
height: height ?? (width ?? 300) / 2,
child: WebView(
initialUrl: src,
key: key,
javascriptMode: sandboxMode == null || sandboxMode == "allow-scripts"
? JavascriptMode.unrestricted
: JavascriptMode.disabled,
navigationDelegate: navigationDelegate,
gestureRecognizers: {
Factory<VerticalDragGestureRecognizer>(() => VerticalDragGestureRecognizer())
},
),
);
}
}
/// [AudioContentElement] is a [ContentElement] with an audio file as its content.
class AudioContentElement extends ReplacedElement {
final List<String> src;
final bool showControls;
final bool autoplay;
final bool loop;
final bool muted;
AudioContentElement({
String name,
Style style,
this.src,
this.showControls,
this.autoplay,
this.loop,
this.muted,
dom.Element node,
}) : super(name: name, style: style, node: node);
@override
Widget toWidget(RenderContext context) {
return Container(
width: context.style.width ?? 300,
child: ChewieAudio(
controller: ChewieAudioController(
videoPlayerController: VideoPlayerController.network(
src.first ?? "",
),
autoPlay: autoplay,
looping: loop,
showControls: showControls,
autoInitialize: true,
),
),
);
}
}
/// [VideoContentElement] is a [ContentElement] with a video file as its content.
class VideoContentElement extends ReplacedElement {
final List<String> src;
final String poster;
final bool showControls;
final bool autoplay;
final bool loop;
final bool muted;
final double width;
final double height;
VideoContentElement({
String name,
Style style,
this.src,
this.poster,
this.showControls,
this.autoplay,
this.loop,
this.muted,
this.width,
this.height,
dom.Element node,
}) : super(name: name, style: style, node: node);
@override
Widget toWidget(RenderContext context) {
final double _width = width ?? (height ?? 150) * 2;
final double _height = height ?? (width ?? 300) / 2;
return AspectRatio(
aspectRatio: _width / _height,
child: Container(
child: Chewie(
controller: ChewieController(
videoPlayerController: VideoPlayerController.network(
src.first ?? "",
),
placeholder: poster != null
? Image.network(poster)
: Container(color: Colors.black),
autoPlay: autoplay,
looping: loop,
showControls: showControls,
autoInitialize: true,
aspectRatio: _width / _height,
),
),
),
);
}
}
/// [SvgContentElement] is a [ReplacedElement] with an SVG as its contents.
class SvgContentElement extends ReplacedElement {
final String data;
final double width;
final double height;
SvgContentElement({
this.data,
this.width,
this.height,
});
@override
Widget toWidget(RenderContext context) {
return SvgPicture.string(
data,
width: width,
height: height,
);
}
}
class EmptyContentElement extends ReplacedElement {
EmptyContentElement({String name = "empty"}) : super(name: name);
@override
Widget toWidget(_) => null;
}
class RubyElement extends ReplacedElement {
dom.Element element;
RubyElement({@required this.element, String name = "ruby"})
: super(name: name, alignment: PlaceholderAlignment.middle);
@override
Widget toWidget(RenderContext context) {
dom.Node textNode;
List<Widget> widgets = List<Widget>();
//TODO calculate based off of parent font size.
final rubySize = max(9.0, context.style.fontSize.size / 2);
final rubyYPos = rubySize + rubySize / 2;
element.nodes.forEach((c) {
if (c.nodeType == dom.Node.TEXT_NODE) {
textNode = c;
}
if (c is dom.Element) {
if (c.localName == "rt" && textNode != null) {
final widget = Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
alignment: Alignment.bottomCenter,
child: Center(
child: Transform(
transform:
Matrix4.translationValues(0, -(rubyYPos), 0),
child: Text(c.innerHtml,
style: context.style
.generateTextStyle()
.copyWith(fontSize: rubySize))))),
Container(
child: Text(textNode.text.trim(),
style: context.style.generateTextStyle())),
],
);
widgets.add(widget);
}
}
});
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
textBaseline: TextBaseline.alphabetic,
mainAxisSize: MainAxisSize.min,
children: widgets,
);
}
}
ReplacedElement parseReplacedElement(
dom.Element element,
NavigationDelegate navigationDelegateForIframe,
) {
switch (element.localName) {
case "audio":
final sources = <String>[
if (element.attributes['src'] != null) element.attributes['src'],
...ReplacedElement.parseMediaSources(element.children),
];
if (sources == null || sources.isEmpty || sources.first == null) {
return EmptyContentElement();
}
return AudioContentElement(
name: "audio",
src: sources,
showControls: element.attributes['controls'] != null,
loop: element.attributes['loop'] != null,
autoplay: element.attributes['autoplay'] != null,
muted: element.attributes['muted'] != null,
node: element,
);
case "br":
return TextContentElement(
text: "\n",
style: Style(whiteSpace: WhiteSpace.PRE),
);
case "iframe":
return IframeContentElement(
name: "iframe",
src: element.attributes['src'],
width: double.tryParse(element.attributes['width'] ?? ""),
height: double.tryParse(element.attributes['height'] ?? ""),
navigationDelegate: navigationDelegateForIframe,
node: element);
case "img":
return ImageContentElement(
name: "img",
src: element.attributes['src'],
alt: element.attributes['alt'],
node: element,
);
case "video":
final sources = <String>[
if (element.attributes['src'] != null) element.attributes['src'],
...ReplacedElement.parseMediaSources(element.children),
];
if (sources == null || sources.isEmpty || sources.first == null) {
return EmptyContentElement();
}
return VideoContentElement(
name: "video",
src: sources,
poster: element.attributes['poster'],
showControls: element.attributes['controls'] != null,
loop: element.attributes['loop'] != null,
autoplay: element.attributes['autoplay'] != null,
muted: element.attributes['muted'] != null,
width: double.tryParse(element.attributes['width'] ?? ""),
height: double.tryParse(element.attributes['height'] ?? ""),
node: element,
);
case "svg":
return SvgContentElement(
data: element.outerHtml,
width: double.tryParse(element.attributes['width'] ?? ""),
height: double.tryParse(element.attributes['height'] ?? ""),
);
case "ruby":
return RubyElement(
element: element,
);
default:
return EmptyContentElement(name: element.localName);
}
}