Skip to content

Commit 972861b

Browse files
committed
Remove summary child when displaying contents of dropdown, handle empty details tag without errors
1 parent deac075 commit 972861b

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lib/html_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ class HtmlParser extends StatelessWidget {
651651
List<StyledElement> toRemove = new List<StyledElement>();
652652
bool lastChildBlock = true;
653653
tree.children?.forEach((child) {
654-
if (child is EmptyContentElement) {
654+
if (child is EmptyContentElement || child is EmptyLayoutElement) {
655655
toRemove.add(child);
656656
} else if (child is TextContentElement && (child.text.isEmpty)) {
657657
toRemove.add(child);

lib/src/layout_element.dart

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,19 @@ TableStyleElement parseTableDefinitionElement(dom.Element element,
245245
}
246246

247247
class DetailsContentElement extends LayoutElement {
248-
List<dom.Element> title;
248+
List<dom.Element> elementList;
249249

250250
DetailsContentElement({
251251
String name,
252252
List<StyledElement> children,
253253
dom.Element node,
254-
this.title,
254+
this.elementList,
255255
}) : super(name: name, node: node, children: children);
256256

257257
@override
258258
Widget toWidget(RenderContext context) {
259259
return ExpansionTile(
260-
title: title.first.localName == "summary" && children != null ? StyledText(
260+
title: children != null && elementList.first.localName == "summary" ? StyledText(
261261
textSpan: TextSpan(
262262
style: style.generateTextStyle(),
263263
children: [children
@@ -271,16 +271,30 @@ class DetailsContentElement extends LayoutElement {
271271
StyledText(
272272
textSpan: TextSpan(
273273
style: style.generateTextStyle(),
274-
children: children
275-
.map((tree) => context.parser.parseTree(context, tree))
276-
.toList() ??
277-
[],
274+
children: getChildren(children, context)
278275
),
279276
style: style,
280277
),
281278
]
282279
);
283280
}
281+
282+
List<InlineSpan> getChildren(List<StyledElement> children, RenderContext context) {
283+
if (children.map((tree) => context.parser.parseTree(context, tree)).toList() == null) {
284+
return [];
285+
} else {
286+
List<InlineSpan> reducedChildren = children.map((tree) => context.parser.parseTree(context, tree)).toList();
287+
reducedChildren.removeAt(0);
288+
return reducedChildren;
289+
}
290+
}
291+
}
292+
293+
class EmptyLayoutElement extends LayoutElement {
294+
EmptyLayoutElement({String name = "empty"}) : super(name: name);
295+
296+
@override
297+
Widget toWidget(_) => null;
284298
}
285299

286300
LayoutElement parseLayoutElement(
@@ -289,13 +303,14 @@ LayoutElement parseLayoutElement(
289303
) {
290304
switch (element.localName) {
291305
case "details":
306+
if (children == null || children.isEmpty) {
307+
return EmptyLayoutElement();
308+
}
292309
return DetailsContentElement(
293310
node: element,
294311
name: element.localName,
295-
//todo
296-
//need to remove the summary tag itself before passing the children to the widget build function somehow
297-
children: element.children.first.localName == "summary" ? children : children,
298-
title: element.children
312+
children: children,
313+
elementList: element.children
299314
);
300315
case "table":
301316
return TableLayoutElement(

0 commit comments

Comments
 (0)