Skip to content

Commit d86fc77

Browse files
committed
WIP: Tables
1 parent 9d6e597 commit d86fc77

File tree

7 files changed

+106
-106
lines changed

7 files changed

+106
-106
lines changed

example/lib/main.dart

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,6 @@ const htmlData = """
5656
<circle r="32" cx="65" cy="65" fill="#0F0" opacity="0.5"/>
5757
<circle r="32" cx="50" cy="35" fill="#00F" opacity="0.5"/>
5858
</svg>
59-
<ol>
60-
<li>This</li>
61-
<li><p>is</p></li>
62-
<li>an</li>
63-
<li>
64-
ordered
65-
<ul>
66-
<li>With<br /><br />...</li>
67-
<li>a</li>
68-
<li>nested</li>
69-
<li>unordered
70-
<ol>
71-
<li>With a nested</li>
72-
<li>ordered list.</li>
73-
</ol>
74-
</li>
75-
<li>list</li>
76-
</ul>
77-
</li>
78-
<li>list! Lorem ipsum dolor sit <b>amet cale aaihg aie a gama eia aai aia ia af a</b></li>
79-
<li><h2>Header 2</h2></li>
80-
<h2><li>Header 2</li></h2>
81-
</ol>
8259
""";
8360

8461
class _MyHomePageState extends State<MyHomePage> {
@@ -111,15 +88,15 @@ class _MyHomePageState extends State<MyHomePage> {
11188
// alignment: Alignment.center,
11289
),
11390
"#whitespace": Style(
114-
backgroundColor: Colors.purple,
91+
backgroundColor: Colors.deepPurple,
11592
),
11693
},
11794
customRender: {
11895
"flutter": (RenderContext context, Widget child, attributes) {
11996
return FlutterLogo(
12097
style: (attributes['horizontal'] != null)? FlutterLogoStyle.horizontal: FlutterLogoStyle.markOnly,
12198
textColor: context.style.color,
122-
size: context.style.fontSize * 5,
99+
size: context.style.fontSize * 8,
123100
);
124101
}
125102
},

lib/html_parser.dart

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:collection';
22
import 'dart:math';
33

44
import 'package:flutter_html/flutter_html.dart';
5-
import 'package:flutter_html/src/layout_element.dart';
65
import 'package:flutter_html/src/utils.dart';
76
import 'package:flutter_html/style.dart';
87
import 'package:flutter/material.dart';
@@ -97,8 +96,6 @@ class HtmlParser extends StatelessWidget {
9796
return parseInteractableElement(node, children);
9897
} else if (REPLACED_ELEMENTS.contains(node.localName)) {
9998
return parseReplacedElement(node);
100-
} else if (LAYOUT_ELEMENTS.contains(node.localName)) {
101-
return parseLayoutElement(node, children);
10299
} else if (customRenderTags.contains(node.localName)) {
103100
return parseStyledElement(node, children);
104101
} else {
@@ -238,6 +235,47 @@ class HtmlParser extends StatelessWidget {
238235
),
239236
),
240237
);
238+
} else if(tree.style?.display == Display.TABLE) {
239+
return WidgetSpan(
240+
child: DefaultTextStyle(
241+
style: TextStyle(color: Colors.white),
242+
child: IntrinsicWidth(
243+
child: ContainerSpan(
244+
style: Style(border: Border.all(color: Colors.white)),
245+
child: Column(
246+
crossAxisAlignment: CrossAxisAlignment.stretch,
247+
children: [
248+
IntrinsicHeight(
249+
child: Row(
250+
children: [Expanded(child: Placeholder()), Expanded(child: FlutterLogo())],
251+
mainAxisAlignment: MainAxisAlignment.center,
252+
crossAxisAlignment: CrossAxisAlignment.stretch,
253+
),
254+
),
255+
IntrinsicHeight(
256+
child: Row(
257+
children: [Expanded(child: Text('Hello\nWorld!')), Expanded(child: ContainerSpan(child: Placeholder()))],
258+
mainAxisAlignment: MainAxisAlignment.center,
259+
crossAxisAlignment: CrossAxisAlignment.stretch,
260+
),
261+
),
262+
IntrinsicHeight(
263+
child: Row(
264+
children: [Expanded(child: Placeholder()), Expanded(child: Text('Hi'))],
265+
mainAxisAlignment: MainAxisAlignment.center,
266+
crossAxisAlignment: CrossAxisAlignment.stretch,
267+
),
268+
),
269+
],
270+
),
271+
),
272+
),
273+
),
274+
);
275+
// } else if(tree.style?.display == Display.TABLE_CELL) {
276+
// return TextSpan();
277+
// } else if(tree.style?.display == Display.TABLE_ROW) {
278+
//
241279
} else if (tree is ReplacedElement) {
242280
if (tree is TextContentElement) {
243281
return TextSpan(text: tree.text);
@@ -260,10 +298,6 @@ class HtmlParser extends StatelessWidget {
260298
),
261299
),
262300
);
263-
} else if (tree is LayoutElement) {
264-
return WidgetSpan(
265-
child: tree.toWidget(context),
266-
);
267301
} else {
268302
///[tree] is an inline element.
269303
return TextSpan(
@@ -499,7 +533,14 @@ class HtmlParser extends StatelessWidget {
499533
toRemove.add(child);
500534
} else if (child is TextContentElement &&
501535
child.style.whiteSpace != WhiteSpace.PRE &&
502-
tree.style.display == Display.BLOCK &&
536+
(
537+
tree.style.display == Display.BLOCK ||
538+
tree.style.display == Display.TABLE ||
539+
tree.style.display == Display.TABLE_ROW_GROUP ||
540+
tree.style.display == Display.TABLE_ROW ||
541+
tree.style.display == Display.TABLE_FOOTER_GROUP ||
542+
tree.style.display == Display.TABLE_HEADER_GROUP
543+
) &&
503544
child.text.trim().isEmpty) {
504545
//TODO should this be moved to the whitespace functions?
505546
toRemove.add(child);

lib/src/html_elements.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ const STYLED_ELEMENTS = [
7070
"pre",
7171
"section",
7272
"ul",
73+
74+
//TABLE ELEMENTS
75+
"table",
76+
"td",
77+
"th",
78+
"tr",
79+
"tbody",
80+
"tfoot",
81+
"thead",
7382
];
7483

7584
const INTERACTABLE_ELEMENTS = [
@@ -87,14 +96,6 @@ const REPLACED_ELEMENTS = [
8796
"video",
8897
];
8998

90-
const LAYOUT_ELEMENTS = [
91-
"table",
92-
"tr",
93-
"tbody",
94-
"tfoot",
95-
"thead",
96-
];
97-
9899
/**
99100
Here is a list of elements with planned support:
100101
a - i [x]

lib/src/layout_element.dart

Lines changed: 0 additions & 65 deletions
This file was deleted.

lib/src/replaced_element.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ class VideoContentElement extends ReplacedElement {
202202

203203
/// [SvgContentElement] is a [ReplacedElement] with an SVG as its contents.
204204
class SvgContentElement extends ReplacedElement {
205+
final String name;
205206
final String data;
206207
final double width;
207208
final double height;
208209

209210
SvgContentElement({
211+
this.name,
210212
this.data,
211213
this.width,
212214
this.height,
@@ -283,6 +285,7 @@ ReplacedElement parseReplacedElement(dom.Element element) {
283285
);
284286
case "svg":
285287
return SvgContentElement(
288+
name: "svg",
286289
data: element.outerHtml,
287290
width: double.tryParse(element.attributes['width'] ?? ""),
288291
height: double.tryParse(element.attributes['height'] ?? ""),

lib/src/styled_element.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,42 @@ StyledElement parseStyledElement(
343343
baselineOffset: 1,
344344
);
345345
break;
346+
case "table":
347+
styledElement.style = Style(
348+
display: Display.TABLE,
349+
);
350+
break;
351+
case "tbody":
352+
styledElement.style = Style(
353+
display: Display.TABLE_ROW_GROUP,
354+
);
355+
break;
356+
case "td":
357+
styledElement.style = Style(
358+
display: Display.TABLE_CELL,
359+
);
360+
break;
361+
case "th":
362+
styledElement.style = Style(
363+
display: Display.TABLE_CELL,
364+
fontWeight: FontWeight.bold,
365+
);
366+
break;
367+
case "thead":
368+
styledElement.style = Style(
369+
display: Display.TABLE_HEADER_GROUP,
370+
);
371+
break;
372+
case "tfoot":
373+
styledElement.style = Style(
374+
display: Display.TABLE_FOOTER_GROUP,
375+
);
376+
break;
377+
case "tr":
378+
styledElement.style = Style(
379+
display: Display.TABLE_ROW,
380+
);
381+
break;
346382
case "tt":
347383
continue monospace;
348384
underline:

lib/style.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ enum Display {
215215
INLINE,
216216
INLINE_BLOCK,
217217
LIST_ITEM,
218+
TABLE,
219+
TABLE_ROW,
220+
TABLE_CELL,
221+
TABLE_ROW_GROUP,
222+
TABLE_HEADER_GROUP,
223+
TABLE_FOOTER_GROUP,
224+
TABLE_CAPTION,
218225
}
219226

220227
enum ListStyleType {

0 commit comments

Comments
 (0)