Skip to content

Commit

Permalink
BREAKING: remove WidgetFactory.buildDivider (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson authored Oct 25, 2023
1 parent 027ae2b commit 4b878c4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
Binary file modified demo_app/test/goldens/HR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 23 additions & 19 deletions packages/core/lib/src/core_widget_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/material.dart'
show
// we want to limit Material usages to be as generic as possible
CircularProgressIndicator,
Divider,
Tooltip;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -40,7 +39,6 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
BuildOp? _styleVerticalAlign;
BuildOp? _tagA;
BuildOp? _tagDetails;
BuildOp? _tagHr;
BuildOp? _tagImg;
BuildOp? _tagLi;
BuildOp? _tagPre;
Expand Down Expand Up @@ -179,9 +177,6 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
);
}

/// Builds [Divider].
Widget? buildDivider(BuildTree tree) => const Divider();

/// Builds [GestureDetector].
///
/// Only [TapGestureRecognizer] is supported for now.
Expand Down Expand Up @@ -762,20 +757,6 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
tree.register(tagFont);
break;

case 'hr':
tree.register(
_tagHr ??= BuildOp(
debugLabel: localName,
defaultStyles: (_) => {
kCssDisplay: kCssDisplayBlock,
kCssMargin + kSuffixBottom: '1em',
},
onRenderBlock: (tree, _) => buildDivider(tree) ?? _,
priority: Priority.tagHr,
),
);
break;

case kTagH1:
tree.register(
const BuildOp.v2(
Expand Down Expand Up @@ -831,6 +812,17 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
);
break;

case kTagHr:
tree.register(
const BuildOp.v2(
debugLabel: kTagHr,
defaultStyles: _tagHrDefaultStyles,
onRenderBlock: _tagHrOnRenderBlock,
priority: Priority.tagHr,
),
);
break;

case kTagImg:
tree.register(_tagImg ??= TagImg(this).buildOp);
break;
Expand Down Expand Up @@ -1267,6 +1259,18 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
kCssMargin: '2.33em 0',
};

static StylesMap _tagHrDefaultStyles(dom.Element _) => {
kCssDisplay: kCssDisplayBlock,
kCssMargin: '0.5em 0',
kCssBorder + kSuffixTop: '1px solid',
};

static WidgetPlaceholder _tagHrOnRenderBlock(
BuildTree _,
WidgetPlaceholder placeholder,
) =>
placeholder.wrapWith((_, __) => Container());

static StylesMap _tagMark(dom.Element _) => {
kCssBackgroundColor: '#ff0',
kCssColor: '#000',
Expand Down
1 change: 1 addition & 0 deletions packages/core/lib/src/internal/core_ops.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const kTagH3 = 'h3';
const kTagH4 = 'h4';
const kTagH5 = 'h5';
const kTagH6 = 'h6';
const kTagHr = 'hr';
const kTagIns = 'ins';
const kTagKbd = 'kbd';
const kTagMark = 'mark';
Expand Down
42 changes: 36 additions & 6 deletions packages/core/test/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,6 @@ Future<void> main() async {
);
});

testWidgets('renders HR tag', (WidgetTester tester) async {
const html = '<hr/>';
final explained = await explainMargin(tester, html);
expect(explained, equals('[CssBlock:child=[Divider]],[SizedBox:0.0x10.0]'));
});

group('block elements', () {
const blockOutput = '[Column:children='
'[CssBlock:child=[RichText:(:First.)]],'
Expand Down Expand Up @@ -573,6 +567,42 @@ Future<void> main() async {
});
});

group('HR', () {
testWidgets('renders a horizontal line', (WidgetTester tester) async {
final explained = await explainMargin(tester, '<hr/>');
expect(
explained,
equals(
'[SizedBox:0.0x5.0],'
'[Container:border=(1.0@solid#FF001234,none,none,none),child=[CssBlock:child=[Container]]],'
'[SizedBox:0.0x5.0]',
),
);

// TODO: remove lint ignore when our minimum Flutter version >= 3.10
final block = tester.getSize(find.byType(CssBlock));
// ignore: deprecated_member_use
final deviceWidth = tester.binding.window.physicalSize.width /
// ignore: deprecated_member_use
tester.binding.window.devicePixelRatio;
expect(block.width, equals(deviceWidth));
});

testWidgets('renders custom color', (WidgetTester tester) async {
// https://github.com/daohoangson/flutter_widget_from_html/issues/1070
const html = '<hr style="border:none;border-bottom:1px solid #e34a3a">';
final explained = await explainMargin(tester, html);
expect(
explained,
equals(
'[SizedBox:0.0x5.0],'
'[Container:border=(none,none,1.0@solid#FFE34A3A,none),child=[CssBlock:child=[Container]]],'
'[SizedBox:0.0x5.0]',
),
);
});
});

group('background-color', () {
testWidgets('renders MARK tag', (WidgetTester tester) async {
const html = '<mark>Foo</mark>';
Expand Down

1 comment on commit 4b878c4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.