Skip to content
Merged
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
10 changes: 6 additions & 4 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class HtmlParser extends StatelessWidget {
customRender.keys.toList(),
tagsList,
navigationDelegateForIframe,
context,
);
StyledElement? externalCssStyledTree;
if (declarations.isNotEmpty) {
Expand All @@ -104,7 +105,7 @@ class HtmlParser extends StatelessWidget {
buildContext: context,
parser: this,
tree: cleanedTree,
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
style: cleanedTree.style,
),
cleanedTree,
);
Expand All @@ -122,7 +123,7 @@ class HtmlParser extends StatelessWidget {
buildContext: context,
parser: this,
tree: cleanedTree,
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
style: cleanedTree.style,
),
);
}
Expand All @@ -134,7 +135,7 @@ class HtmlParser extends StatelessWidget {
buildContext: context,
parser: this,
tree: cleanedTree,
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
style: cleanedTree.style,
),
);
}
Expand All @@ -155,12 +156,13 @@ class HtmlParser extends StatelessWidget {
List<String> customRenderTags,
List<String> tagsList,
NavigationDelegate? navigationDelegateForIframe,
BuildContext context,
) {
StyledElement tree = StyledElement(
name: "[Tree Root]",
children: <StyledElement>[],
node: html.documentElement,
style: Style(),
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
);

html.nodes.forEach((node) {
Expand Down
171 changes: 85 additions & 86 deletions test/html_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,116 +18,115 @@ void main() {
),
);
});
testWidgets('Test new parser (hacky workaround to get BuildContext)', (WidgetTester tester) async {
await tester.pumpWidget(
Builder(
builder: (BuildContext context) {
testNewParser(context);

testNewParser();
// The builder function must return a widget.
return Placeholder();
},
),
);
});
}

void testNewParser() {
test("Html Parser works correctly", () {
HtmlParser.parseHTML("<b>Hello, World!</b>");
});
void testNewParser(BuildContext context) {
HtmlParser.parseHTML("<b>Hello, World!</b>");

test("lexDomTree works correctly", () {
StyledElement tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML(
"Hello! <b>Hello, World!</b><i>Hello, New World!</i>"),
[],
[],
null,
);
print(tree.toString());
});
StyledElement tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML(
"Hello! <b>Hello, World!</b><i>Hello, New World!</i>"),
[],
[],
null,
context,
);
print(tree.toString());

test("InteractableElements work correctly", () {
StyledElement tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML(
"Hello, World! <a href='https://example.com'>This is a link</a>"),
[],
[],
null);
print(tree.toString());
});
tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML(
"Hello, World! <a href='https://example.com'>This is a link</a>"),
[],
[],
null,
context,
);
print(tree.toString());

test("ContentElements work correctly", () {
StyledElement tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML("<img src='https://image.example.com' />"),
[],
[],
null,
);
print(tree.toString());
});
tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML("<img src='https://image.example.com' />"),
[],
[],
null,
context,
);
print(tree.toString());

test("Nesting of elements works correctly", () {
StyledElement tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML(
"<div><div><div><div><a href='link'>Link</a><div>Hello, World! <b>Bold and <i>Italic</i></b></div></div></div></div></div>"),
[],
[],
null,
);
print(tree.toString());
});
tree = HtmlParser.lexDomTree(
HtmlParser.parseHTML(
"<div><div><div><div><a href='link'>Link</a><div>Hello, World! <b>Bold and <i>Italic</i></b></div></div></div></div></div>"),
[],
[],
null,
context,
);
print(tree.toString());

test("Video Content Source Parser works correctly", () {
ReplacedElement videoContentElement = parseReplacedElement(
HtmlParser.parseHTML("""
ReplacedElement videoContentElement = parseReplacedElement(
HtmlParser.parseHTML("""
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
""").getElementsByTagName("video")[0],
null,
);
null,
);

expect(videoContentElement, isA<VideoContentElement>());
if (videoContentElement is VideoContentElement) {
expect(videoContentElement.showControls, equals(true),
reason: "Controls isn't working");
expect(videoContentElement.src, hasLength(2),
reason: "Not enough sources...");
}
});
expect(videoContentElement, isA<VideoContentElement>());
if (videoContentElement is VideoContentElement) {
expect(videoContentElement.showControls, equals(true),
reason: "Controls isn't working");
expect(videoContentElement.src, hasLength(2),
reason: "Not enough sources...");
}

test("Audio Content Source Parser works correctly", () {
ReplacedElement audioContentElement = parseReplacedElement(
HtmlParser.parseHTML("""
ReplacedElement audioContentElement = parseReplacedElement(
HtmlParser.parseHTML("""
<audio controls>
<source src='audio.mp3' type='audio/mpeg'>
<source src='audio.wav' type='audio/wav'>
Your browser does not support the audio tag.
</audio>
""").getElementsByTagName("audio")[0],
null,
);
expect(audioContentElement, isA<AudioContentElement>());
if (audioContentElement is AudioContentElement) {
expect(audioContentElement.showControls, equals(true),
reason: "Controls isn't working");
expect(audioContentElement.src, hasLength(2),
reason: "Not enough sources...");
}
});
null,
);
expect(audioContentElement, isA<AudioContentElement>());
if (audioContentElement is AudioContentElement) {
expect(audioContentElement.showControls, equals(true),
reason: "Controls isn't working");
expect(audioContentElement.src, hasLength(2),
reason: "Not enough sources...");
}

test("Test style merging", () {
Style style1 = Style(
display: Display.BLOCK,
fontWeight: FontWeight.bold,
);
Style style1 = Style(
display: Display.BLOCK,
fontWeight: FontWeight.bold,
);

Style style2 = Style(
before: "* ",
direction: TextDirection.rtl,
fontStyle: FontStyle.italic,
);
Style style2 = Style(
before: "* ",
direction: TextDirection.rtl,
fontStyle: FontStyle.italic,
);

Style finalStyle = style1.merge(style2);
Style finalStyle = style1.merge(style2);

expect(finalStyle.display, equals(Display.BLOCK));
expect(finalStyle.before, equals("* "));
expect(finalStyle.direction, equals(TextDirection.rtl));
expect(finalStyle.fontStyle, equals(FontStyle.italic));
expect(finalStyle.fontWeight, equals(FontWeight.bold));
});
expect(finalStyle.display, equals(Display.BLOCK));
expect(finalStyle.before, equals("* "));
expect(finalStyle.direction, equals(TextDirection.rtl));
expect(finalStyle.fontStyle, equals(FontStyle.italic));
expect(finalStyle.fontWeight, equals(FontWeight.bold));
}