-
-
Notifications
You must be signed in to change notification settings - Fork 902
/
Copy pathimage_builtin.dart
228 lines (196 loc) · 6.49 KB
/
image_builtin.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
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
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
class ImageBuiltIn extends HtmlExtension {
final String? dataEncoding;
final Set<String>? mimeTypes;
final Map<String, String>? networkHeaders;
final Set<String> networkSchemas;
final Set<String>? networkDomains;
final Set<String>? fileExtensions;
final String assetSchema;
final AssetBundle? assetBundle;
final String? assetPackage;
final bool handleNetworkImages;
final bool handleAssetImages;
final bool handleDataImages;
const ImageBuiltIn({
this.networkHeaders,
this.networkDomains,
this.networkSchemas = const {"http", "https"},
this.fileExtensions,
this.assetSchema = "asset:",
this.assetBundle,
this.assetPackage,
this.mimeTypes,
this.dataEncoding,
this.handleNetworkImages = true,
this.handleAssetImages = true,
this.handleDataImages = true,
});
@override
Set<String> get supportedTags => {
"img",
};
@override
bool matches(ExtensionContext context) {
if (context.elementName != "img") {
return false;
}
return (_matchesNetworkImage(context) && handleNetworkImages) ||
(_matchesAssetImage(context) && handleAssetImages) ||
(_matchesBase64Image(context) && handleDataImages);
}
@override
StyledElement prepare(
ExtensionContext context, List<StyledElement> children) {
final parsedWidth = double.tryParse(context.attributes["width"] ?? "");
final parsedHeight = double.tryParse(context.attributes["height"] ?? "");
return ImageElement(
name: context.elementName,
children: children,
style: Style(),
node: context.node,
elementId: context.id,
src: context.attributes["src"]!,
alt: context.attributes["alt"],
width: parsedWidth != null ? Width(parsedWidth) : null,
height: parsedHeight != null ? Height(parsedHeight) : null,
);
}
@override
InlineSpan build(ExtensionContext context) {
final element = context.styledElement as ImageElement;
final imageStyle = Style(
width: element.width,
height: element.height,
).merge(context.styledElement!.style);
late Widget child;
if (_matchesBase64Image(context)) {
child = _base64ImageRender(context, imageStyle);
} else if (_matchesAssetImage(context)) {
child = _assetImageRender(context, imageStyle);
} else if (_matchesNetworkImage(context)) {
child = _networkImageRender(context, imageStyle);
} else {
// Our matcher went a little overboard and matched
// something we can't render
return TextSpan(text: element.alt);
}
return WidgetSpan(
alignment: context.style!.verticalAlign
.toPlaceholderAlignment(context.style!.display),
baseline: TextBaseline.alphabetic,
child: CssBoxWidget(
style: imageStyle,
childIsReplaced: true,
child: child,
),
);
}
static RegExp get dataUriFormat => RegExp(
r"^(?<scheme>data):(?<mime>image/[\w+\-.]+);*(?<encoding>base64)?,\s*(?<data>.*)");
bool _matchesBase64Image(ExtensionContext context) {
final attributes = context.attributes;
if (attributes['src'] == null) {
return false;
}
final dataUri = dataUriFormat.firstMatch(attributes['src']!);
return context.elementName == "img" &&
dataUri != null &&
(mimeTypes == null ||
mimeTypes!.contains(dataUri.namedGroup('mime'))) &&
dataUri.namedGroup('mime') != 'image/svg+xml' &&
(dataEncoding == null ||
dataUri.namedGroup('encoding') == dataEncoding);
}
bool _matchesAssetImage(ExtensionContext context) {
final attributes = context.attributes;
return context.elementName == "img" &&
attributes['src'] != null &&
!attributes['src']!.endsWith(".svg") &&
attributes['src']!.startsWith(assetSchema) &&
(fileExtensions == null ||
attributes['src']!.endsWithAnyFileExtension(fileExtensions!));
}
bool _matchesNetworkImage(ExtensionContext context) {
final attributes = context.attributes;
if (attributes['src'] == null) {
return false;
}
final src = Uri.tryParse(attributes['src']!);
if (src == null) {
return false;
}
return context.elementName == "img" &&
networkSchemas.contains(src.scheme) &&
!src.path.endsWith(".svg") &&
(networkDomains == null || networkDomains!.contains(src.host)) &&
(fileExtensions == null ||
src.path.endsWithAnyFileExtension(fileExtensions!));
}
Widget _base64ImageRender(ExtensionContext context, Style imageStyle) {
final element = context.styledElement as ImageElement;
final decodedImage = base64.decode(element.src.split("base64,")[1].trim());
return Image.memory(
decodedImage,
width: imageStyle.width?.value,
height: imageStyle.height?.value,
fit: BoxFit.fill,
errorBuilder: (ctx, error, stackTrace) {
return Text(
element.alt ?? "",
style: context.styledElement!.style.generateTextStyle(),
);
},
);
}
Widget _assetImageRender(ExtensionContext context, Style imageStyle) {
final element = context.styledElement as ImageElement;
final assetPath = element.src.replaceFirst('asset:', '');
return Image.asset(
assetPath,
width: imageStyle.width?.value,
height: imageStyle.height?.value,
fit: BoxFit.fill,
bundle: assetBundle,
package: assetPackage,
errorBuilder: (ctx, error, stackTrace) {
return Text(
element.alt ?? "",
style: context.styledElement!.style.generateTextStyle(),
);
},
);
}
Widget _networkImageRender(ExtensionContext context, Style imageStyle) {
final element = context.styledElement as ImageElement;
return CssBoxWidget(
style: imageStyle,
childIsReplaced: true,
child: Image.network(
element.src,
width: imageStyle.width?.value,
height: imageStyle.height?.value,
fit: BoxFit.contain,
headers: networkHeaders,
errorBuilder: (ctx, error, stackTrace) {
return Text(
element.alt ?? "",
style: context.styledElement!.style.generateTextStyle(),
);
},
),
);
}
}
extension _SetFolding on String {
bool endsWithAnyFileExtension(Iterable<String> endings) {
for (final element in endings) {
if (endsWith(".$element")) {
return true;
}
}
return false;
}
}