Skip to content

Commit ed48ee8

Browse files
authored
Merge pull request Sub6Resources#977 from Sub6Resources/bugfix/various-fixes
Various Fixes
2 parents 9adc596 + d7b564a commit ed48ee8

File tree

6 files changed

+73
-51
lines changed

6 files changed

+73
-51
lines changed

lib/html_parser.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:collection';
22
import 'dart:math';
33

4+
import 'package:collection/collection.dart';
45
import 'package:csslib/parser.dart' as cssparser;
56
import 'package:csslib/visitor.dart' as css;
67
import 'package:flutter/material.dart';
@@ -438,7 +439,7 @@ class HtmlParser extends StatelessWidget {
438439
&& tree.text!.startsWith(' ')
439440
&& tree.element?.localName != "br"
440441
&& (!keepLeadingSpace.data
441-
|| BLOCK_ELEMENTS.contains(tree.element?.localName ?? ""))
442+
|| tree.style.display == Display.BLOCK)
442443
&& (elementIndex < 1
443444
|| (elementIndex >= 1
444445
&& parentNodes?[elementIndex - 1] is dom.Text
@@ -747,11 +748,16 @@ class HtmlParser extends StatelessWidget {
747748
static StyledElement _removeEmptyElements(StyledElement tree) {
748749
List<StyledElement> toRemove = <StyledElement>[];
749750
bool lastChildBlock = true;
750-
tree.children.forEach((child) {
751+
tree.children.forEachIndexed((index, child) {
751752
if (child is EmptyContentElement || child is EmptyLayoutElement) {
752753
toRemove.add(child);
753754
} else if (child is TextContentElement
754-
&& (tree.name == "body" || tree.name == "ul")
755+
&& ((tree.name == "body"
756+
&& (index == 0
757+
|| index + 1 == tree.children.length
758+
|| tree.children[index - 1].style.display == Display.BLOCK
759+
|| tree.children[index + 1].style.display == Display.BLOCK))
760+
|| tree.name == "ul")
755761
&& child.text!.replaceAll(' ', '').isEmpty) {
756762
toRemove.add(child);
757763
} else if (child is TextContentElement

lib/src/css_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class ExpressionMapping {
621621
if (value is css.NumberTerm) {
622622
return FontSize(double.tryParse(value.text));
623623
} else if (value is css.PercentageTerm) {
624-
return FontSize.percent(int.tryParse(value.text)!);
624+
return FontSize.percent(double.tryParse(value.text)!);
625625
} else if (value is css.EmTerm) {
626626
return FontSize.em(double.tryParse(value.text));
627627
} else if (value is css.RemTerm) {

lib/src/replaced_element.dart

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:math';
22

3+
import 'package:collection/collection.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter/widgets.dart';
56
import 'package:flutter_html/html_parser.dart';
@@ -74,55 +75,70 @@ class RubyElement extends ReplacedElement {
7475

7576
@override
7677
Widget toWidget(RenderContext context) {
77-
String? textNode;
78+
StyledElement? node;
7879
List<Widget> widgets = <Widget>[];
79-
final rubySize = max(9.0, context.style.fontSize!.size! / 2);
80+
final rubySize = context.parser.style['rt']?.fontSize?.size ?? max(9.0, context.style.fontSize!.size! / 2);
8081
final rubyYPos = rubySize + rubySize / 2;
81-
context.tree.children.forEach((c) {
82-
if (c is TextContentElement) {
83-
textNode = c.text;
82+
List<StyledElement> children = [];
83+
context.tree.children.forEachIndexed((index, element) {
84+
if (!((element is TextContentElement)
85+
&& (element.text ?? "").trim().isEmpty
86+
&& index > 0
87+
&& index + 1 < context.tree.children.length
88+
&& !(context.tree.children[index - 1] is TextContentElement)
89+
&& !(context.tree.children[index + 1] is TextContentElement))) {
90+
children.add(element);
8491
}
85-
if (!(c is TextContentElement)) {
86-
if (c.name == "rt" && textNode != null) {
87-
final widget = Stack(
88-
alignment: Alignment.center,
89-
children: <Widget>[
90-
Container(
91-
alignment: Alignment.bottomCenter,
92-
child: Center(
93-
child: Transform(
94-
transform:
95-
Matrix4.translationValues(0, -(rubyYPos), 0),
96-
child: ContainerSpan(
97-
newContext: RenderContext(
98-
buildContext: context.buildContext,
99-
parser: context.parser,
100-
style: c.style,
101-
tree: c,
102-
),
92+
});
93+
children.forEach((c) {
94+
if (c.name == "rt" && node != null) {
95+
final widget = Stack(
96+
alignment: Alignment.center,
97+
children: <Widget>[
98+
Container(
99+
alignment: Alignment.bottomCenter,
100+
child: Center(
101+
child: Transform(
102+
transform:
103+
Matrix4.translationValues(0, -(rubyYPos), 0),
104+
child: ContainerSpan(
105+
newContext: RenderContext(
106+
buildContext: context.buildContext,
107+
parser: context.parser,
103108
style: c.style,
104-
child: Text(c.element!.innerHtml,
105-
style: c.style
106-
.generateTextStyle()
107-
.copyWith(fontSize: rubySize)),
108-
)))),
109-
ContainerSpan(
110-
newContext: context,
111-
style: context.style,
112-
child: Text(textNode!.trim(),
113-
style: context.style.generateTextStyle())),
114-
],
115-
);
116-
widgets.add(widget);
117-
}
109+
tree: c,
110+
),
111+
style: c.style,
112+
child: Text(c.element!.innerHtml,
113+
style: c.style
114+
.generateTextStyle()
115+
.copyWith(fontSize: rubySize)),
116+
)))),
117+
ContainerSpan(
118+
newContext: context,
119+
style: context.style,
120+
child: node is TextContentElement ? Text((node as TextContentElement).text?.trim() ?? "",
121+
style: context.style.generateTextStyle()) : null,
122+
children: node is TextContentElement ? null : [context.parser.parseTree(context, node!)]),
123+
],
124+
);
125+
widgets.add(widget);
126+
} else {
127+
node = c;
118128
}
119129
});
120-
return Row(
121-
key: AnchorKey.of(context.parser.key, this),
122-
crossAxisAlignment: CrossAxisAlignment.end,
123-
textBaseline: TextBaseline.alphabetic,
124-
mainAxisSize: MainAxisSize.min,
125-
children: widgets,
130+
return Padding(
131+
padding: EdgeInsets.only(top: rubySize),
132+
child: Wrap(
133+
key: AnchorKey.of(context.parser.key, this),
134+
runSpacing: rubySize,
135+
children: widgets.map((e) => Row(
136+
crossAxisAlignment: CrossAxisAlignment.end,
137+
textBaseline: TextBaseline.alphabetic,
138+
mainAxisSize: MainAxisSize.min,
139+
children: [e],
140+
)).toList(),
141+
),
126142
);
127143
}
128144
}

lib/style.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ class FontSize {
473473
const FontSize(this.size, {this.units = ""});
474474

475475
/// A percentage of the parent style's font size.
476-
factory FontSize.percent(int percent) {
477-
return FontSize(percent.toDouble() / -100.0, units: "%");
476+
factory FontSize.percent(double percent) {
477+
return FontSize(percent / -100.0, units: "%");
478478
}
479479

480480
factory FontSize.em(double? em) {

packages/flutter_html_audio/lib/flutter_html_audio.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class _AudioWidgetState extends State<AudioWidget> {
3636

3737
@override
3838
void initState() {
39-
final sources = <String?>[
39+
sources = <String?>[
4040
if (widget.context.tree.element?.attributes['src'] != null)
4141
widget.context.tree.element!.attributes['src'],
4242
...ReplacedElement.parseMediaSources(widget.context.tree.element!.children),

packages/flutter_html_video/lib/flutter_html_video.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class _VideoWidgetState extends State<VideoWidget> {
3939
@override
4040
void initState() {
4141
final attributes = widget.context.tree.element?.attributes ?? {};
42-
final sources = <String?>[
42+
sources = <String?>[
4343
if (attributes['src'] != null)
4444
attributes['src'],
4545
...ReplacedElement.parseMediaSources(widget.context.tree.element!.children),

0 commit comments

Comments
 (0)