diff --git a/lib/html_parser.dart b/lib/html_parser.dart index c4e70ed785..9eb1a80cea 100644 --- a/lib/html_parser.dart +++ b/lib/html_parser.dart @@ -90,6 +90,7 @@ class HtmlParser extends StatelessWidget { customRender.keys.toList(), tagsList, navigationDelegateForIframe, + context, ); StyledElement? externalCssStyledTree; if (declarations.isNotEmpty) { @@ -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, ); @@ -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, ), ); } @@ -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, ), ); } @@ -155,12 +156,13 @@ class HtmlParser extends StatelessWidget { List customRenderTags, List tagsList, NavigationDelegate? navigationDelegateForIframe, + BuildContext context, ) { StyledElement tree = StyledElement( name: "[Tree Root]", children: [], node: html.documentElement, - style: Style(), + style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!), ); html.nodes.forEach((node) { diff --git a/test/html_parser_test.dart b/test/html_parser_test.dart index 953f956cb2..2410b4cdf0 100644 --- a/test/html_parser_test.dart +++ b/test/html_parser_test.dart @@ -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("Hello, World!"); - }); +void testNewParser(BuildContext context) { + HtmlParser.parseHTML("Hello, World!"); - test("lexDomTree works correctly", () { - StyledElement tree = HtmlParser.lexDomTree( - HtmlParser.parseHTML( - "Hello! Hello, World!Hello, New World!"), - [], - [], - null, - ); - print(tree.toString()); - }); + StyledElement tree = HtmlParser.lexDomTree( + HtmlParser.parseHTML( + "Hello! Hello, World!Hello, New World!"), + [], + [], + null, + context, + ); + print(tree.toString()); - test("InteractableElements work correctly", () { - StyledElement tree = HtmlParser.lexDomTree( - HtmlParser.parseHTML( - "Hello, World! This is a link"), - [], - [], - null); - print(tree.toString()); - }); + tree = HtmlParser.lexDomTree( + HtmlParser.parseHTML( + "Hello, World! This is a link"), + [], + [], + null, + context, + ); + print(tree.toString()); - test("ContentElements work correctly", () { - StyledElement tree = HtmlParser.lexDomTree( - HtmlParser.parseHTML(""), - [], - [], - null, - ); - print(tree.toString()); - }); + tree = HtmlParser.lexDomTree( + HtmlParser.parseHTML(""), + [], + [], + null, + context, + ); + print(tree.toString()); - test("Nesting of elements works correctly", () { - StyledElement tree = HtmlParser.lexDomTree( - HtmlParser.parseHTML( - "LinkHello, World! Bold and Italic"), - [], - [], - null, - ); - print(tree.toString()); - }); + tree = HtmlParser.lexDomTree( + HtmlParser.parseHTML( + "LinkHello, World! Bold and Italic"), + [], + [], + null, + context, + ); + print(tree.toString()); - test("Video Content Source Parser works correctly", () { - ReplacedElement videoContentElement = parseReplacedElement( - HtmlParser.parseHTML(""" + ReplacedElement videoContentElement = parseReplacedElement( + HtmlParser.parseHTML(""" Your browser does not support the video tag. """).getElementsByTagName("video")[0], - null, - ); + null, + ); - expect(videoContentElement, isA()); - 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()); + 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(""" Your browser does not support the audio tag. """).getElementsByTagName("audio")[0], - null, - ); - expect(audioContentElement, isA()); - 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()); + 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)); }