Skip to content

Commit def53ac

Browse files
authored
Merge pull request Sub6Resources#507 from tneotia/feature/details-tag
Add support for <details> and <summary>
2 parents e8f50ed + 234adb4 commit def53ac

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

lib/html_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ class HtmlParser extends StatelessWidget {
663663
List<StyledElement> toRemove = new List<StyledElement>();
664664
bool lastChildBlock = true;
665665
tree.children?.forEach((child) {
666-
if (child is EmptyContentElement) {
666+
if (child is EmptyContentElement || child is EmptyLayoutElement) {
667667
toRemove.add(child);
668668
} else if (child is TextContentElement && (child.text.isEmpty)) {
669669
toRemove.add(child);

lib/src/html_elements.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const STYLED_ELEMENTS = [
6666
"p",
6767
"pre",
6868
"section",
69+
"summary",
6970
"ul",
7071
];
7172

@@ -88,6 +89,7 @@ const REPLACED_ELEMENTS = [
8889
];
8990

9091
const LAYOUT_ELEMENTS = [
92+
"details",
9193
"table",
9294
"tr",
9395
"tbody",

lib/src/interactable_element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ InteractableElement parseInteractableElement(
4040
}
4141

4242
return interactableElement;
43-
}
43+
}

lib/src/layout_element.dart

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,84 @@ TableStyleElement parseTableDefinitionElement(
251251
}
252252
}
253253

254+
class DetailsContentElement extends LayoutElement {
255+
List<dom.Element> elementList;
256+
257+
DetailsContentElement({
258+
String name,
259+
List<StyledElement> children,
260+
dom.Element node,
261+
this.elementList,
262+
}) : super(name: name, node: node, children: children);
263+
264+
@override
265+
Widget toWidget(RenderContext context) {
266+
List<InlineSpan> childrenList = children?.map((tree) => context.parser.parseTree(context, tree))?.toList();
267+
List<InlineSpan> toRemove = [];
268+
if (childrenList != null) {
269+
for (InlineSpan child in childrenList) {
270+
if (child is TextSpan && child.text != null && child.text.trim().isEmpty) {
271+
toRemove.add(child);
272+
}
273+
}
274+
for (InlineSpan child in toRemove) {
275+
childrenList.remove(child);
276+
}
277+
}
278+
InlineSpan firstChild = childrenList?.isNotEmpty == true ? childrenList.first : null;
279+
return ExpansionTile(
280+
expandedAlignment: Alignment.centerLeft,
281+
title: elementList?.isNotEmpty == true && elementList?.first?.localName == "summary" ? StyledText(
282+
textSpan: TextSpan(
283+
style: style.generateTextStyle(),
284+
children: [firstChild] ?? [],
285+
),
286+
style: style,
287+
) : Text("Details"),
288+
children: [
289+
StyledText(
290+
textSpan: TextSpan(
291+
style: style.generateTextStyle(),
292+
children: getChildren(childrenList, context, elementList?.isNotEmpty == true && elementList?.first?.localName == "summary" ? firstChild : null)
293+
),
294+
style: style,
295+
),
296+
]
297+
);
298+
}
299+
300+
List<InlineSpan> getChildren(List<InlineSpan> children, RenderContext context, InlineSpan firstChild) {
301+
if (children == null) {
302+
return [];
303+
} else {
304+
if (firstChild != null) children.removeAt(0);
305+
return children;
306+
}
307+
}
308+
}
309+
310+
class EmptyLayoutElement extends LayoutElement {
311+
EmptyLayoutElement({String name = "empty"}) : super(name: name);
312+
313+
@override
314+
Widget toWidget(_) => null;
315+
}
316+
254317
LayoutElement parseLayoutElement(
255-
dom.Element element,
256-
List<StyledElement> children,
318+
dom.Element element,
319+
List<StyledElement> children,
257320
) {
258321
switch (element.localName) {
322+
case "details":
323+
if (children?.isEmpty ?? false) {
324+
return EmptyLayoutElement();
325+
}
326+
return DetailsContentElement(
327+
node: element,
328+
name: element.localName,
329+
children: children,
330+
elementList: element.children
331+
);
259332
case "table":
260333
return TableLayoutElement(
261334
name: element.localName,

0 commit comments

Comments
 (0)