Skip to content

[WIP] Fix/image link #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const htmlData = """
<h4>Header 4</h4>
<h5>Header 5</h5>
<h6>Header 6</h6>
<a href='https://google.com'><div>Test</div> Other test</a>
<h3>Ruby Support:</h3>
<p>
<ruby>
Expand Down Expand Up @@ -114,6 +115,7 @@ const htmlData = """
<a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a>
<img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' />
</p>
<a></a>
<!--
<h3>Video support:</h3>
<video controls>
Expand Down
39 changes: 35 additions & 4 deletions lib/src/interactable_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'package:flutter_html/src/html_elements.dart';
import 'package:flutter_html/style.dart';
import 'package:html/dom.dart' as dom;

import 'html_elements.dart';


/// An [InteractableElement] is a [StyledElement] that takes user gestures (e.g. tap).
class InteractableElement extends StyledElement {
String href;
Expand Down Expand Up @@ -32,10 +35,38 @@ InteractableElement parseInteractableElement(
switch (element.localName) {
case "a":
interactableElement.href = element.attributes['href'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many abstraction levels in the body of 1 method.
You should break it down to smaller method/func

Infact, the code should do like this ( i will assume that your code logic is correct)

if(hasNoChild(interactableElement)) {
underline(interactableElement);
} else {
underlineChildTextElements(interactableElement);
}

Then, the method underlineChildTextElements will take care the recurrsive search & apply the corresponding style to its children

interactableElement.style = Style(
color: Colors.blue,
textDecoration: TextDecoration.underline,
);
if (element.children?.isEmpty ?? true) {
interactableElement.style = Style(
textDecoration: TextDecoration.underline,
color: Colors.blue,
);
} else {
final allWidgets = List<TextContentElement>();
List<StyledElement> searchQueue =
List.from(interactableElement.children);

// recursively
while (searchQueue.isNotEmpty) {
final List<StyledElement> nextSearch = searchQueue
.expand((e) => e?.children ?? List<StyledElement>())
.toList();
allWidgets.addAll(nextSearch
.where((element) => element is TextContentElement)
.map((e) => e as TextContentElement));
searchQueue = nextSearch;
}

allWidgets
.where((element) => element is TextContentElement)
.forEach((element) {
final style = element.style ?? Style();
element.style = style.copyWith(
textDecoration: TextDecoration.underline,
color: Colors.blue,
);
});
}

break;
}

Expand Down