Skip to content

Commit e3ebd0f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into feature/200
2 parents 6618e65 + b785705 commit e3ebd0f

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ A Flutter widget for rendering HTML and CSS as Flutter widgets.
3636
- [Constructors](#constructors)
3737

3838
- [Parameters Table](#parameters)
39+
40+
- [Getters](#getters)
3941

4042
- [Data](#data)
4143

@@ -51,7 +53,7 @@ A Flutter widget for rendering HTML and CSS as Flutter widgets.
5153

5254
- [onImageTap](#onimagetap)
5355

54-
- [blacklistedElements](#blacklistedelements)
56+
- [tagsList](#tagslist)
5557

5658
- [style](#style)
5759

@@ -161,11 +163,15 @@ If you would like to modify or sanitize the HTML before rendering it, then `Html
161163
| `omMathError` | A function that defines what the widget should do when a math fails to render. The function exposes the parsed Tex `String`, as well as the error and error with type from `flutter_math` as a `String`. |
162164
| `shrinkWrap` | A `bool` used while rendering different widgets to specify whether they should be shrink-wrapped or not, like `ContainerSpan` |
163165
| `onImageTap` | A function that defines what the widget should do when an image is tapped. The function exposes the `src` of the image as a `String` to use in your implementation. |
164-
| `blacklistedElements` | A list of elements the `Html` widget should not render. The list should contain the tags of the HTML elements you wish to blacklist. |
166+
| `tagsList` | A list of elements the `Html` widget should render. The list should contain the tags of the HTML elements you wish to include. |
165167
| `style` | A powerful API that allows you to customize the style that should be used when rendering a specific HTMl tag. |
166168
| `navigationDelegateForIframe` | Allows you to set the `NavigationDelegate` for the `WebView`s of all the iframes rendered by the `Html` widget. |
167169
| `customImageRender` | A powerful API that allows you to fully customize how images are loaded. |
168170

171+
### Getters:
172+
173+
Currently the only getter is `Html.tags`. This provides a list of all the tags the package renders. The main use case is to assist in blacklisting elements using `tagsList`. See an [example](#example-usage---tagslist---excluding-tags) below.
174+
169175
### Data:
170176

171177
The HTML data passed to the `Html` widget as a `String`. This is required and cannot be null when using `Html`.
@@ -399,25 +405,43 @@ Widget html = Html(
399405
);
400406
```
401407

402-
### blacklistedElements:
408+
### tagsList:
403409

404-
A list of elements the `Html` widget should not render. The list should contain the tags of the HTML elements you wish to blacklist.
410+
A list of elements the `Html` widget should render. The list should contain the tags of the HTML elements you wish to whitelist.
405411

406-
#### Example Usage - blacklistedElements:
412+
#### Example Usage - tagsList - Excluding Tags:
407413
You may have instances where you can choose between two different types of HTML tags to display the same content. In the example below, the `<video>` and `<iframe>` elements are going to display the same content.
408414

409415
The `blacklistedElements` parameter allows you to change which element is rendered. Iframes can be advantageous because they allow parallel loading - Flutter just has to wait for the webview to be initialized before rendering the page, possibly cutting down on load time. Video can be advantageous because it provides a 100% native experience with Flutter widgets, but it may take more time to render the page. You may know that Flutter webview is a little janky in its current state on Android, so using `blacklistedElements` and a simple condition, you can get the best of both worlds - choose the video widget to render on Android and the iframe webview to render on iOS.
416+
410417
```dart
411418
Widget html = Html(
412419
data: """
413420
<video controls>
414421
<source src="https://www.w3schools.com/html/mov_bbb.mp4" />
415422
</video>
416423
<iframe src="https://www.w3schools.com/html/mov_bbb.mp4"></iframe>""",
417-
blacklistedElements: [Platform.isAndroid ? "iframe" : "video"]
424+
tagsList: Html.tags..remove(Platform.isAndroid ? "iframe" : "video")
418425
);
419426
```
420427

428+
`Html.tags` provides easy access to a list of all the tags the package can render, and you can remove specific tags from this list to blacklist them.
429+
430+
#### Example Usage - tagsList - Allowing Tags:
431+
You may also have instances where you would only like the package to render a handful of html tags. You can do that like so:
432+
```dart
433+
Widget html = Html(
434+
data: """
435+
<p>Render this item</p>
436+
<span>Do not render this item or any other item</span>
437+
<img src='https://flutter.dev/images/flutter-mono-81x100.png'/>
438+
""",
439+
tagsList: ['p']
440+
);
441+
```
442+
443+
Here, the package will only ever render `<p>` and ignore all other tags.
444+
421445
### style:
422446

423447
A powerful API that allows you to customize the style that should be used when rendering a specific HTMl tag.

example/lib/generated_plugin_registrant.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
// ignore_for_file: lines_longer_than_80_chars
66

7+
import 'package:url_launcher_web/url_launcher_web.dart';
78
import 'package:video_player_web/video_player_web.dart';
89
import 'package:wakelock_web/wakelock_web.dart';
910

1011
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
1112

1213
// ignore: public_member_api_docs
1314
void registerPlugins(Registrar registrar) {
15+
UrlLauncherPlugin.registerWith(registrar);
1416
VideoPlayerPlugin.registerWith(registrar);
1517
WakelockWeb.registerWith(registrar);
1618
registrar.registerMessageHandler();

lib/flutter_html.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export 'package:flutter_html/src/interactable_element.dart';
1515
import 'package:flutter/material.dart';
1616
import 'package:flutter_html/html_parser.dart';
1717
import 'package:flutter_html/image_render.dart';
18+
import 'package:flutter_html/src/html_elements.dart';
1819
import 'package:flutter_html/style.dart';
1920
import 'package:webview_flutter/webview_flutter.dart';
2021
import 'package:html/dom.dart' as dom;
@@ -41,7 +42,7 @@ class Html extends StatelessWidget {
4142
///
4243
/// **onImageTap** This is called whenever an image is tapped.
4344
///
44-
/// **blacklistedElements** Tag names in this array are ignored during parsing and rendering.
45+
/// **tagsList** Tag names in this array will be the only tags rendered. By default all tags are rendered.
4546
///
4647
/// **style** Pass in the style information for the Html here.
4748
/// See [its wiki page](https://github.com/Sub6Resources/flutter_html/wiki/Style) for more info.
@@ -55,7 +56,7 @@ class Html extends StatelessWidget {
5556
this.onMathError,
5657
this.shrinkWrap = false,
5758
this.onImageTap,
58-
this.blacklistedElements = const [],
59+
this.tagsList = const [],
5960
this.style = const {},
6061
this.navigationDelegateForIframe,
6162
}) : document = null,
@@ -72,7 +73,7 @@ class Html extends StatelessWidget {
7273
this.onMathError,
7374
this.shrinkWrap = false,
7475
this.onImageTap,
75-
this.blacklistedElements = const [],
76+
this.tagsList = const [],
7677
this.style = const {},
7778
this.navigationDelegateForIframe,
7879
}) : data = null,
@@ -108,7 +109,7 @@ class Html extends StatelessWidget {
108109
final OnTap? onImageTap;
109110

110111
/// A list of HTML tags that defines what elements are not rendered
111-
final List<String> blacklistedElements;
112+
final List<String> tagsList;
112113

113114
/// Either return a custom widget for specific node types or return null to
114115
/// fallback to the default rendering.
@@ -122,6 +123,13 @@ class Html extends StatelessWidget {
122123
/// to use NavigationDelegate.
123124
final NavigationDelegate? navigationDelegateForIframe;
124125

126+
static List<String> get tags => new List<String>.from(STYLED_ELEMENTS)
127+
..addAll(INTERACTABLE_ELEMENTS)
128+
..addAll(REPLACED_ELEMENTS)
129+
..addAll(LAYOUT_ELEMENTS)
130+
..addAll(TABLE_CELL_ELEMENTS)
131+
..addAll(TABLE_DEFINITION_ELEMENTS);
132+
125133
@override
126134
Widget build(BuildContext context) {
127135
final dom.Document doc = data != null ? HtmlParser.parseHTML(data!) : document!;
@@ -141,7 +149,7 @@ class Html extends StatelessWidget {
141149
imageRenders: {}
142150
..addAll(customImageRenders)
143151
..addAll(defaultImageRenders),
144-
blacklistedElements: blacklistedElements,
152+
tagsList: tagsList.isEmpty ? Html.tags : tagsList,
145153
navigationDelegateForIframe: navigationDelegateForIframe,
146154
),
147155
);

lib/html_parser.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class HtmlParser extends StatelessWidget {
4343
final Map<String, Style> style;
4444
final Map<String, CustomRender> customRender;
4545
final Map<ImageSourceMatcher, ImageRender> imageRenders;
46-
final List<String> blacklistedElements;
46+
final List<String> tagsList;
4747
final NavigationDelegate? navigationDelegateForIframe;
4848

4949
HtmlParser({
@@ -56,7 +56,7 @@ class HtmlParser extends StatelessWidget {
5656
required this.style,
5757
required this.customRender,
5858
required this.imageRenders,
59-
required this.blacklistedElements,
59+
required this.tagsList,
6060
required this.navigationDelegateForIframe,
6161
});
6262

@@ -65,7 +65,7 @@ class HtmlParser extends StatelessWidget {
6565
StyledElement lexedTree = lexDomTree(
6666
htmlData,
6767
customRender.keys.toList(),
68-
blacklistedElements,
68+
tagsList,
6969
navigationDelegateForIframe,
7070
);
7171
StyledElement inlineStyledTree = applyInlineStyles(lexedTree);
@@ -113,7 +113,7 @@ class HtmlParser extends StatelessWidget {
113113
static StyledElement lexDomTree(
114114
dom.Document html,
115115
List<String> customRenderTags,
116-
List<String> blacklistedElements,
116+
List<String> tagsList,
117117
NavigationDelegate? navigationDelegateForIframe,
118118
) {
119119
StyledElement tree = StyledElement(
@@ -127,7 +127,7 @@ class HtmlParser extends StatelessWidget {
127127
tree.children.add(_recursiveLexer(
128128
node,
129129
customRenderTags,
130-
blacklistedElements,
130+
tagsList,
131131
navigationDelegateForIframe,
132132
));
133133
});
@@ -142,7 +142,7 @@ class HtmlParser extends StatelessWidget {
142142
static StyledElement _recursiveLexer(
143143
dom.Node node,
144144
List<String> customRenderTags,
145-
List<String> blacklistedElements,
145+
List<String> tagsList,
146146
NavigationDelegate? navigationDelegateForIframe,
147147
) {
148148
List<StyledElement> children = <StyledElement>[];
@@ -151,14 +151,14 @@ class HtmlParser extends StatelessWidget {
151151
children.add(_recursiveLexer(
152152
childNode,
153153
customRenderTags,
154-
blacklistedElements,
154+
tagsList,
155155
navigationDelegateForIframe,
156156
));
157157
});
158158

159159
//TODO(Sub6Resources): There's probably a more efficient way to look this up.
160160
if (node is dom.Element) {
161-
if (blacklistedElements.contains(node.localName)) {
161+
if (!tagsList.contains(node.localName)) {
162162
return EmptyContentElement();
163163
}
164164
if (STYLED_ELEMENTS.contains(node.localName)) {

0 commit comments

Comments
 (0)