Skip to content

Commit

Permalink
Merge pull request #1 from Sub6Resources/new-parser
Browse files Browse the repository at this point in the history
update from base repo
  • Loading branch information
ngaurav committed Feb 21, 2020
2 parents 3badf23 + b5046d9 commit b55c0b1
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 28 deletions.
18 changes: 18 additions & 0 deletions example/ios/Flutter/Flutter.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# NOTE: This podspec is NOT to be published. It is only used as a local source!
#

Pod::Spec.new do |s|
s.name = 'Flutter'
s.version = '1.0.0'
s.summary = 'High-performance, high-fidelity mobile apps.'
s.description = <<-DESC
Flutter provides an easy and productive way to build and deploy high-performance mobile apps for Android and iOS.
DESC
s.homepage = 'https://flutter.io'
s.license = { :type => 'MIT' }
s.author = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' }
s.source = { :git => 'https://github.com/flutter/engine', :tag => s.version.to_s }
s.ios.deployment_target = '8.0'
s.vendored_frameworks = 'Flutter.framework'
end
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const htmlData = """
</p>
<h3>Support for <code>sub</code>/<code>sup</code></h3>
Solve for <var>x<sub>n</sub></var>: log<sub>2</sub>(<var>x</var><sup>2</sup>+<var>n</var>) = 9<sup>3</sup>
<p>One of the most common equations in all of physics is <var>E</var>=<var>m</var><var>c</var><sup>2</sup>.</p>
<p>One of the most <span>common</span> equations in all of physics is <var>E</var>=<var>m</var><var>c</var><sup>2</sup>.</p>
<h3>Table support:</h3>
<table>
<colgroup>
Expand Down
53 changes: 40 additions & 13 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'dart:collection';
import 'dart:math';

import 'package:csslib/parser.dart' as cssparser;
import 'package:csslib/visitor.dart' as css;
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/src/html_elements.dart';
import 'package:flutter_html/src/layout_element.dart';
import 'package:flutter_html/src/utils.dart';
import 'package:flutter_html/style.dart';
import 'package:flutter/material.dart';
import 'package:csslib/visitor.dart' as css;
import 'package:html/dom.dart' as dom;
import 'package:flutter_html/src/html_elements.dart';
import 'package:html/parser.dart' as htmlparser;
import 'package:csslib/parser.dart' as cssparser;

typedef OnTap = void Function(String url);
typedef CustomRender = Widget Function(
Expand Down Expand Up @@ -67,7 +67,7 @@ class HtmlParser extends StatelessWidget {
cleanedTree,
);

return RichText(text: parsedTree);
return StyledText(textSpan: parsedTree, style: cleanedTree.style);
}

/// [parseHTML] converts a string of HTML to a DOM document using the dart `html` library.
Expand Down Expand Up @@ -281,14 +281,15 @@ class HtmlParser extends StatelessWidget {
Padding(
padding: EdgeInsets.only(
left: 30), //TODO derive this from list padding.
child: RichText(
text: TextSpan(
child: StyledText(
textSpan: TextSpan(
children: tree.children
?.map((tree) => parseTree(newContext, tree))
?.toList() ??
[],
style: newContext.style.generateTextStyle(),
),
style: newContext.style,
),
)
],
Expand Down Expand Up @@ -317,14 +318,15 @@ class HtmlParser extends StatelessWidget {
},
),
},
child: RichText(
text: TextSpan(
child: StyledText(
textSpan: TextSpan(
style: newContext.style.generateTextStyle(),
children: tree.children
.map((tree) => parseTree(newContext, tree))
.toList() ??
[],
),
style: newContext.style,
),
),
);
Expand All @@ -349,14 +351,15 @@ class HtmlParser extends StatelessWidget {
return WidgetSpan(
child: Transform.translate(
offset: Offset(0, verticalOffset),
child: RichText(
text: TextSpan(
child: StyledText(
textSpan: TextSpan(
style: newContext.style.generateTextStyle(),
children: tree.children
.map((tree) => parseTree(newContext, tree))
.toList() ??
[],
),
style: newContext.style,
),
),
);
Expand Down Expand Up @@ -694,12 +697,36 @@ class ContainerSpan extends StatelessWidget {
margin: style?.margin,
alignment: shrinkWrap ? null : style?.alignment,
child: child ??
RichText(
text: TextSpan(
StyledText(
textSpan: TextSpan(
style: newContext.style.generateTextStyle(),
children: children,
),
style: newContext.style,
),
);
}
}

class StyledText extends StatelessWidget {
final InlineSpan textSpan;
final Style style;

const StyledText({
this.textSpan,
this.style,
});

@override
Widget build(BuildContext context) {
return SizedBox(
width: style.display == Display.BLOCK || style.display == Display.LIST_ITEM? double.infinity: null,
child: Text.rich(
textSpan,
style: style.generateTextStyle(),
textAlign: style.textAlign,
textDirection: style.direction,
),
);
}
}
5 changes: 3 additions & 2 deletions lib/src/layout_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ class TableRowLayoutElement extends LayoutElement {
color: c.style.backgroundColor,
border: c.style.border,
),
child: RichText(
text: context.parser.parseTree(context, c),
child: StyledText(
textSpan: context.parser.parseTree(context, c),
style: c.style,
)));
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/styled_element.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_html/style.dart';
import 'package:html/dom.dart' as dom;
//TODO(Sub6Resources) don't use the internal code of the html package as it may change unexpectedly.
//TODO(Sub6Resources): don't use the internal code of the html package as it may change unexpectedly.
import 'package:html/src/query_selector.dart';

/// A [StyledElement] applies a style to all of its children.
Expand Down Expand Up @@ -85,7 +85,7 @@ StyledElement parseStyledElement(
? TextDirection.rtl
: TextDirection.ltr;
styledElement.style = Style(
textDirection: textDirection,
direction: textDirection,
);
break;
case "big":
Expand Down
Loading

0 comments on commit b55c0b1

Please sign in to comment.