Skip to content

Commit bd8fc87

Browse files
committed
[WIP] recursively apply styles
1 parent 6a8e58b commit bd8fc87

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const htmlData = """
3535
<h4>Header 4</h4>
3636
<h5>Header 5</h5>
3737
<h6>Header 6</h6>
38+
<a href='https://google.com'><div>Test</div> Other test</a>
3839
<h3>Ruby Support:</h3>
3940
<p>
4041
<ruby>
@@ -114,6 +115,7 @@ const htmlData = """
114115
<a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
115116
<img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' />
116117
</p>
118+
<a></a>
117119
<!--
118120
<h3>Video support:</h3>
119121
<video controls>

lib/src/interactable_element.dart

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import 'package:flutter_html/src/html_elements.dart';
33
import 'package:flutter_html/style.dart';
44
import 'package:html/dom.dart' as dom;
55

6+
import 'html_elements.dart';
7+
import 'html_elements.dart';
8+
import 'html_elements.dart';
9+
import 'html_elements.dart';
10+
import 'html_elements.dart';
11+
import 'html_elements.dart';
12+
import 'html_elements.dart';
13+
614
/// An [InteractableElement] is a [StyledElement] that takes user gestures (e.g. tap).
715
class InteractableElement extends StyledElement {
816
String href;
@@ -32,10 +40,38 @@ InteractableElement parseInteractableElement(
3240
switch (element.localName) {
3341
case "a":
3442
interactableElement.href = element.attributes['href'];
35-
interactableElement.style = Style(
36-
color: Colors.blue,
37-
textDecoration: TextDecoration.underline,
38-
);
43+
if (element.children?.isEmpty ?? true) {
44+
interactableElement.style = Style(
45+
textDecoration: TextDecoration.underline,
46+
color: Colors.blue,
47+
);
48+
} else {
49+
final allWidgets = List<TextContentElement>();
50+
List<StyledElement> searchQueue =
51+
List.from(interactableElement.children);
52+
53+
// recursively
54+
while (searchQueue.isNotEmpty) {
55+
final List<StyledElement> nextSearch = searchQueue
56+
.expand((e) => e?.children ?? List<StyledElement>())
57+
.toList();
58+
allWidgets.addAll(nextSearch
59+
.where((element) => element is TextContentElement)
60+
.map((e) => e as TextContentElement));
61+
searchQueue = nextSearch;
62+
}
63+
64+
allWidgets
65+
.where((element) => element is TextContentElement)
66+
.forEach((element) {
67+
final style = element.style ?? Style();
68+
element.style = style.copyWith(
69+
textDecoration: TextDecoration.underline,
70+
color: Colors.blue,
71+
);
72+
});
73+
}
74+
3975
break;
4076
}
4177

0 commit comments

Comments
 (0)