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
2 changes: 1 addition & 1 deletion lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class HtmlParser extends StatelessWidget {
} else if (INTERACTABLE_ELEMENTS.contains(node.localName)) {
return parseInteractableElement(node, children);
} else if (REPLACED_ELEMENTS.contains(node.localName)) {
return parseReplacedElement(node, navigationDelegateForIframe);
return parseReplacedElement(node, children, navigationDelegateForIframe);
} else if (LAYOUT_ELEMENTS.contains(node.localName)) {
return parseLayoutElement(node, children);
} else if (TABLE_CELL_ELEMENTS.contains(node.localName)) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/html_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const STYLED_ELEMENTS = [
"kbd",
"mark",
"q",
"rt",
"s",
"samp",
"small",
Expand Down
48 changes: 32 additions & 16 deletions lib/src/replaced_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ abstract class ReplacedElement extends StyledElement {
required String name,
required Style style,
required String elementId,
List<StyledElement>? children,
dom.Element? node,
this.alignment = PlaceholderAlignment.aboveBaseline,
}) : super(name: name, children: [], style: style, node: node, elementId: elementId);
}) : super(name: name, children: children ?? [], style: style, node: node, elementId: elementId);

static List<String?> parseMediaSources(List<dom.Element> elements) {
return elements
Expand Down Expand Up @@ -230,22 +231,24 @@ class EmptyContentElement extends ReplacedElement {
class RubyElement extends ReplacedElement {
dom.Element element;

RubyElement({required this.element, String name = "ruby"})
: super(name: name, alignment: PlaceholderAlignment.middle, style: Style(), elementId: element.id);
RubyElement({
required this.element,
required List<StyledElement> children,
String name = "ruby"
}) : super(name: name, alignment: PlaceholderAlignment.middle, style: Style(), elementId: element.id, children: children);

@override
Widget toWidget(RenderContext context) {
dom.Node? textNode;
String? textNode;
List<Widget> widgets = <Widget>[];
//TODO calculate based off of parent font size.
Copy link
Contributor

Choose a reason for hiding this comment

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

😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we really calculate based off parent font size though? I feel that calculating based on whatever styling is set on the ruby tag should be sufficient, and now we even allow for specific styling on rt itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree.

final rubySize = max(9.0, context.style.fontSize!.size! / 2);
final rubyYPos = rubySize + rubySize / 2;
element.nodes.forEach((c) {
if (c.nodeType == dom.Node.TEXT_NODE) {
textNode = c;
context.tree.children.forEach((c) {
if (c is TextContentElement) {
textNode = c.text;
}
if (c is dom.Element) {
if (c.localName == "rt" && textNode != null) {
if (!(c is TextContentElement)) {
if (c.name == "rt" && textNode != null) {
final widget = Stack(
alignment: Alignment.center,
children: <Widget>[
Expand All @@ -255,12 +258,23 @@ class RubyElement extends ReplacedElement {
child: Transform(
transform:
Matrix4.translationValues(0, -(rubyYPos), 0),
child: Text(c.innerHtml,
style: context.style
.generateTextStyle()
.copyWith(fontSize: rubySize))))),
Container(
child: Text(textNode!.text!.trim(),
child: ContainerSpan(
newContext: RenderContext(
buildContext: context.buildContext,
parser: context.parser,
style: c.style,
tree: c,
),
style: c.style,
child: Text(c.element!.innerHtml,
style: c.style
.generateTextStyle()
.copyWith(fontSize: rubySize)),
)))),
ContainerSpan(
newContext: context,
style: context.style,
child: Text(textNode!.trim(),
style: context.style.generateTextStyle())),
],
);
Expand Down Expand Up @@ -361,6 +375,7 @@ class MathElement extends ReplacedElement {

ReplacedElement parseReplacedElement(
dom.Element element,
List<StyledElement> children,
NavigationDelegate? navigationDelegateForIframe,
) {
switch (element.localName) {
Expand Down Expand Up @@ -435,6 +450,7 @@ ReplacedElement parseReplacedElement(
case "ruby":
return RubyElement(
element: element,
children: children,
);
case "math":
return MathElement(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/styled_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StyledElement {
}) : this._node = node;

bool matchesSelector(String selector) =>
_node != null && matches(_node as dom.Element, selector);
(_node != null && matches(_node as dom.Element, selector)) || name == selector;

Map<String, String> get attributes =>
_node?.attributes.map((key, value) {
Expand Down
2 changes: 2 additions & 0 deletions test/html_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void testNewParser(BuildContext context) {
Your browser does not support the video tag.
</video>
""").getElementsByTagName("video")[0],
[],
null,
);

Expand All @@ -101,6 +102,7 @@ void testNewParser(BuildContext context) {
Your browser does not support the audio tag.
</audio>
""").getElementsByTagName("audio")[0],
[],
null,
);
expect(audioContentElement, isA<AudioContentElement>());
Expand Down