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: 10 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.13.1-alpha.1

- Remove `BuildTree.apply` (#1027)
- Restore optional second param for `HtmlStyleBuilder.enqueue` (#1027)

## 0.13.0-alpha.5

- Expose core legacy in enhanced package (#1027)
- Fix border 0 is still being rendered (#1045)

## 0.13.0-alpha.2

- Fix inline `white-space: nowrap`. (#944)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ tree.apply(callback, TextAlign.justify);
tree.register(BuildOp.v1(
onParsed: (tree) {
// can be used to change text, inline contents, etc.
tree.append(...);
return tree..append(...);
},
onRenderBlock: (tree, child) {
// use this to render special widget, wrap it into something else, etc.
Expand Down Expand Up @@ -314,7 +314,7 @@ class _SmiliesWidgetFactory extends WidgetFactory {
final smilieOp = BuildOp.v1(
onParsed: (tree) {
final alt = tree.element.attributes['alt'];
tree.addText(kSmilies[alt] ?? alt);
return tree..addText(kSmilies[alt] ?? alt ?? '');
},
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/core_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef DefaultStyles = StylesMap Function(BuildTree tree);
/// ```dart
/// BuildOp.v1(
/// onChild: (tree, subTree) {
/// if (!subTree.element.parent != tree.element) return;
/// if (subTree.element.parent != tree.element) return;
/// subTree.doSomething();
/// },
/// );
Expand Down
17 changes: 16 additions & 1 deletion packages/core/lib/src/core_legacy.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';

import 'core_data.dart';
Expand All @@ -26,6 +25,22 @@ extension LegacyWidgetFactory on WidgetFactory {
@Deprecated('Use HtmlStyle instead.')
typedef TextStyleHtml = HtmlStyle;

extension LegacyTextStyleHtml on TextStyleHtml {
/// The input [TextStyle].
@Deprecated('Use .textStyle instead.')
TextStyle get style => textStyle;

/// Gets dependency by type [T].
@Deprecated('Use .value instead.')
T getDependency<T>() {
final dep = value<T>();
if (dep != null) {
return dep;
}
throw StateError('The $T dependency could not be found');
}
}

/// A legacy HTML styling builder.
@Deprecated('Use HtmlStyleBuilder instead.')
typedef TextStyleBuilder<T> = HtmlStyleBuilder;
Expand Down
30 changes: 16 additions & 14 deletions packages/core/lib/src/core_widget_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
case kTagA:
if (attrs.containsKey(kAttributeAHref)) {
tree
..apply<BuildContext?>(TagA.defaultColor, null)
..styleBuilder.enqueue<BuildContext?>(TagA.defaultColor)
..register(_tagA ??= TagA(this).buildOp);
}

Expand Down Expand Up @@ -672,14 +672,16 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {

case 'b':
case 'strong':
tree.apply(TextStyleOps.fontWeight, FontWeight.bold);
tree.styleBuilder.enqueue(TextStyleOps.fontWeight, FontWeight.bold);
break;

case 'big':
tree.apply(TextStyleOps.fontSizeTerm, kCssFontSizeLarger);
tree.styleBuilder
.enqueue(TextStyleOps.fontSizeTerm, kCssFontSizeLarger);
break;
case 'small':
tree.apply(TextStyleOps.fontSizeTerm, kCssFontSizeSmaller);
tree.styleBuilder
.enqueue(TextStyleOps.fontSizeTerm, kCssFontSizeSmaller);
break;

case kTagBr:
Expand All @@ -701,14 +703,14 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
case 'em':
case 'i':
case 'var':
tree.apply(TextStyleOps.fontStyle, FontStyle.italic);
tree.styleBuilder.enqueue(TextStyleOps.fontStyle, FontStyle.italic);
break;

case kTagCode:
case kTagKbd:
case kTagSamp:
case kTagTt:
tree.apply(
tree.styleBuilder.enqueue(
TextStyleOps.fontFamily,
const [kTagCodeFont1, kTagCodeFont2],
);
Expand Down Expand Up @@ -985,26 +987,26 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
case kCssColor:
final color = tryParseColor(style.value);
if (color != null) {
tree.apply(TextStyleOps.color, color);
tree.styleBuilder.enqueue(TextStyleOps.color, color);
}
break;

case kCssDirection:
final term = style.term;
if (term != null) {
tree.apply(TextStyleOps.textDirection, term);
tree.styleBuilder.enqueue(TextStyleOps.textDirection, term);
}
break;

case kCssFontFamily:
final list = TextStyleOps.fontFamilyTryParse(style.values);
tree.apply(TextStyleOps.fontFamily, list);
tree.styleBuilder.enqueue(TextStyleOps.fontFamily, list);
break;

case kCssFontSize:
final value = style.value;
if (value != null) {
tree.apply(TextStyleOps.fontSize, value);
tree.styleBuilder.enqueue(TextStyleOps.fontSize, value);
}
break;

Expand All @@ -1013,7 +1015,7 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
final fontStyle =
term != null ? TextStyleOps.fontStyleTryParse(term) : null;
if (fontStyle != null) {
tree.apply(TextStyleOps.fontStyle, fontStyle);
tree.styleBuilder.enqueue(TextStyleOps.fontStyle, fontStyle);
}
break;

Expand All @@ -1022,7 +1024,7 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
final fontWeight =
value != null ? TextStyleOps.fontWeightTryParse(value) : null;
if (fontWeight != null) {
tree.apply(TextStyleOps.fontWeight, fontWeight);
tree.styleBuilder.enqueue(TextStyleOps.fontWeight, fontWeight);
}
break;

Expand All @@ -1038,7 +1040,7 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
case kCssLineHeight:
final value = style.value;
if (value != null) {
tree.apply(
tree.styleBuilder.enqueue(
_styleBuilderLineHeight ??= TextStyleOps.lineHeight(this),
value,
);
Expand Down Expand Up @@ -1082,7 +1084,7 @@ class WidgetFactory extends WidgetFactoryResetter with AnchorWidgetFactory {
final whitespace =
term != null ? TextStyleOps.whitespaceTryParse(term) : null;
if (whitespace != null) {
tree.apply(TextStyleOps.whitespace, whitespace);
tree.styleBuilder.enqueue(TextStyleOps.whitespace, whitespace);
}
break;
}
Expand Down
7 changes: 0 additions & 7 deletions packages/core/lib/src/data/build_bits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@ abstract class BuildTree extends BuildBit {
@Deprecated('Use .getStyle instead.')
css.Declaration? operator [](String key) => getStyle(key);

/// {@macro flutter_widget_from_html.enqueue}
void apply<T>(
HtmlStyle Function(HtmlStyle style, T input) callback,
T input,
) =>
styleBuilder.enqueue(callback, input);

/// Appends [bit].
///
/// See also: [prepend].
Expand Down
18 changes: 4 additions & 14 deletions packages/core/lib/src/data/html_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ class HtmlStyle {
);
}

/// The input [TextStyle].
@Deprecated('Use .textStyle instead.')
TextStyle get style => textStyle;

/// The text direction.
TextDirection get textDirection => value()!;

Expand All @@ -69,10 +65,6 @@ class HtmlStyle {
);
}

/// Gets dependency by type [T].
@Deprecated('Use .value instead.')
T? getDependency<T>() => value<T>();

/// Gets value of type [T].
///
/// The initial set of values are populated by [WidgetFactory.getDependencies].
Expand All @@ -97,17 +89,15 @@ class HtmlStyleBuilder {

HtmlStyleBuilder([this.parent, this._queue]);

/// {@template flutter_widget_from_html.enqueue}
/// Enqueues an HTML styling callback.
///
/// The callback will receive the current [HtmlStyle] being built.
/// As a special case, declare `T=BuildContext?` to receive the [BuildContext].
/// {@endtemplate}
void enqueue<T>(
HtmlStyle Function(HtmlStyle style, T input) callback,
T input,
) {
final item = _HtmlStyleCallback(callback, input);
HtmlStyle Function(HtmlStyle style, T input) callback, [
T? input,
]) {
final item = _HtmlStyleCallback(callback, input as T);
final queue = _queue ??= [];
queue.add(item);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/internal/ops/style_background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StyleBackground {
return;
}

tree.apply(_color, color);
tree.styleBuilder.enqueue(_color, color);
},
priority: BoxModel.background,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/internal/ops/style_text_align.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension StyleTextAlign on WidgetFactory {
static BuildTree _onParsed(BuildTree tree) {
final textAlign = tree.textAlignData.textAlign;
if (textAlign != null) {
tree.apply(_textAlign, textAlign);
tree.styleBuilder.enqueue(_textAlign, textAlign);
}
return tree;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/core/lib/src/internal/ops/style_text_decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void textDecorationApply(BuildTree tree, css.Declaration style) {
style.property == kCssTextDecorationLine) {
final line = TextDecorationLine.tryParse(value);
if (line != null) {
tree.apply(textDecorationLine, line);
tree.styleBuilder.enqueue(textDecorationLine, line);
continue;
}
}
Expand All @@ -26,7 +26,7 @@ void textDecorationApply(BuildTree tree, css.Declaration style) {
style.property == kCssTextDecorationStyle) {
final tds = tryParseTextDecorationStyle(value);
if (tds != null) {
tree.apply(textDecorationStyle, tds);
tree.styleBuilder.enqueue(textDecorationStyle, tds);
continue;
}
}
Expand All @@ -35,7 +35,7 @@ void textDecorationApply(BuildTree tree, css.Declaration style) {
style.property == kCssTextDecorationColor) {
final color = tryParseColor(value);
if (color != null) {
tree.apply(textDecorationColor, color);
tree.styleBuilder.enqueue(textDecorationColor, color);
continue;
}
}
Expand All @@ -45,7 +45,8 @@ void textDecorationApply(BuildTree tree, css.Declaration style) {
style.property == kCssTextDecorationWidth) {
final length = tryParseCssLength(value);
if (length != null && length.unit == CssLengthUnit.percentage) {
tree.apply(textDecorationThickness, length.number / 100.0);
tree.styleBuilder
.enqueue(textDecorationThickness, length.number / 100.0);
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/internal/ops/tag_a.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TagA {
return tree;
}

return tree..apply(_builder, recognizer);
return tree..styleBuilder.enqueue(_builder, recognizer);
},
priority: Priority.tagA,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/internal/ops/tag_li.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TagLi {
) {
final tree = itemTree.sub()
..maxLines = 1
..apply(TextStyleOps.whitespace, CssWhitespace.nowrap);
..styleBuilder.enqueue(TextStyleOps.whitespace, CssWhitespace.nowrap);
final listData = listTree.listData;
final listStyleType = itemTree.itemStyleType ?? listTree.listStyleType;
final index = listData.markerReversed
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/internal/ops/tag_ruby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension TagRuby on WidgetFactory {
);
break;
case kTagRt:
subTree.apply(TextStyleOps.fontSizeEm, .5);
subTree.styleBuilder.enqueue(TextStyleOps.fontSizeEm, .5);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: flutter_widget_from_html_core
version: 0.13.0-alpha.2
version: 0.13.1-alpha.1
description: Flutter package to render html as widgets that focuses on correctness and extensibility.
homepage: https://github.com/daohoangson/flutter_widget_from_html/tree/master/packages/core

Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ Future<void> main() async {
expect(e, equals('[CssBlock:child=[RichText:(:1 [RichText:(:2)])]]'));
});

testWidgets('#646: renders onRenderBlock inline', (tester) async {
testWidgets('renders onRenderBlock inline', (tester) async {
// https://github.com/daohoangson/flutter_widget_from_html/issues/646
const html = '<span style="display:inline-block;">Foo</span>';
final explained = await explain(
Expand All @@ -971,7 +971,7 @@ Future<void> main() async {
expect(explained, equals('[Text:Bar]'));
});

testWidgets('#799: inline block with bg, v-align', (tester) async {
testWidgets('inline block with bg, v-align', (tester) async {
// https://github.com/daohoangson/flutter_widget_from_html/issues/799
const html = '<span style="background-color: #FF6600; '
'display: inline-block; vertical-align: middle">Foo</span>';
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/src/core_legacy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,33 @@ void main() {
});
});

group('TextStyleHtml', () {
group('getDependency', () {
final dep1 = _LegacyTextStyleHtmlDep1();
final style = HtmlStyle.root(
[
const TextScaleFactor(1.0),
TextDirection.ltr,
const TextStyle(inherit: false),
dep1,
],
null,
);

test('returns value', () {
final dep = style.getDependency<_LegacyTextStyleHtmlDep1>();
expect(dep, equals(dep1));
});

test('throws value', () {
expect(
() => style.getDependency<_LegacyTextStyleHtmlDep2>(),
throwsStateError,
);
});
});
});

group('WidgetFactory', () {
group('gestureTapCallback', () {
test('calls onTapUrl', () {
Expand Down Expand Up @@ -589,6 +616,10 @@ class _BuildOpWidgetFactory extends WidgetFactory {
}
}

class _LegacyTextStyleHtmlDep1 {}

class _LegacyTextStyleHtmlDep2 {}

class _LoggerApp extends StatefulWidget {
final Widget child;
final List<LogRecord> records;
Expand Down
Loading