Skip to content

Commit b643264

Browse files
committed
Merge branch 'master' of https://github.com/Sub6Resources/flutter_html into misc/dart-ui-shim
2 parents ac0b573 + c15d499 commit b643264

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

example/lib/main.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const htmlData = r"""
4242
</ruby>
4343
&nbsp;is Japanese Kanji.
4444
</p>
45+
<h3>Support for maxLines:</h3>
46+
<h5>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vestibulum sapien feugiat lorem tempor, id porta orci elementum. Fusce sed justo id arcu egestas congue. Fusce tincidunt lacus ipsum, in imperdiet felis ultricies eu. In ullamcorper risus felis, ac maximus dui bibendum vel. Integer ligula tortor, facilisis eu mauris ut, ultrices hendrerit ex. Donec scelerisque massa consequat, eleifend mauris eu, mollis dui. Donec placerat augue tortor, et tincidunt quam tempus non. Quisque sagittis enim nisi, eu condimentum lacus egestas ac. Nam facilisis luctus ipsum, at aliquam urna fermentum a. Quisque tortor dui, faucibus in ante eget, pellentesque mattis nibh. In augue dolor, euismod vitae eleifend nec, tempus vel urna. Donec vitae augue accumsan ligula fringilla ultrices et vel ex.</h5>
4547
<h3>Support for <code>sub</code>/<code>sup</code></h3>
4648
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>
4749
<p>One of the most <span>common</span> equations in all of physics is <br /><var>E</var>=<var>m</var><var>c</var><sup>2</sup>.</p>
@@ -261,30 +263,36 @@ class _MyHomePageState extends State<MyHomePage> {
261263
padding: EdgeInsets.all(6),
262264
alignment: Alignment.topLeft,
263265
),
266+
'h5': Style(maxLines: 2, textOverflow: TextOverflow.ellipsis),
264267
},
265268
customRender: {
266269
"table": (context, child) {
267270
return SingleChildScrollView(
268271
scrollDirection: Axis.horizontal,
269-
child: (context.tree as TableLayoutElement).toWidget(context),
272+
child:
273+
(context.tree as TableLayoutElement).toWidget(context),
270274
);
271275
}
272276
},
273277
customImageRenders: {
274-
networkSourceMatcher(domains: ["flutter.dev"]): (context, attributes, element) {
278+
networkSourceMatcher(domains: ["flutter.dev"]):
279+
(context, attributes, element) {
275280
return FlutterLogo(size: 36);
276281
},
277-
networkSourceMatcher(domains: ["mydomain.com"]): networkImageRender(
282+
networkSourceMatcher(domains: ["mydomain.com"]):
283+
networkImageRender(
278284
headers: {"Custom-Header": "some-value"},
279285
altWidget: (alt) => Text(alt ?? ""),
280286
loadingWidget: () => Text("Loading..."),
281287
),
282288
// On relative paths starting with /wiki, prefix with a base url
283-
(attr, _) => attr["src"] != null && attr["src"]!.startsWith("/wiki"):
289+
(attr, _) =>
290+
attr["src"] != null && attr["src"]!.startsWith("/wiki"):
284291
networkImageRender(
285292
mapUrl: (url) => "https://upload.wikimedia.org" + url!),
286293
// Custom placeholder image for broken links
287-
networkSourceMatcher(): networkImageRender(altWidget: (_) => FlutterLogo()),
294+
networkSourceMatcher():
295+
networkImageRender(altWidget: (_) => FlutterLogo()),
288296
},
289297
onLinkTap: (url, _, __, ___) {
290298
print("Opening $url...");

lib/html_parser.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,15 @@ class HtmlParser extends StatelessWidget {
577577
static StyledElement _processBeforesAndAfters(StyledElement tree) {
578578
if (tree.style.before != null) {
579579
tree.children.insert(
580-
0, TextContentElement(text: tree.style.before, style: tree.style));
580+
0, TextContentElement(text: tree.style.before, style: tree.style.copyWith(beforeAfterNull: true, display: Display.INLINE)));
581581
}
582582
if (tree.style.after != null) {
583583
tree.children
584-
.add(TextContentElement(text: tree.style.after, style: tree.style));
585-
} else {
586-
tree.children.forEach(_processBeforesAndAfters);
584+
.add(TextContentElement(text: tree.style.after, style: tree.style.copyWith(beforeAfterNull: true, display: Display.INLINE)));
587585
}
586+
587+
tree.children.forEach(_processBeforesAndAfters);
588+
588589
return tree;
589590
}
590591

@@ -833,6 +834,8 @@ class StyledText extends StatelessWidget {
833834
textAlign: style.textAlign,
834835
textDirection: style.direction,
835836
textScaleFactor: textScaleFactor,
837+
maxLines: style.maxLines,
838+
overflow: style.textOverflow,
836839
),
837840
);
838841
}

lib/src/widgets/iframe_web.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ class IframeContentElement extends ReplacedElement {
3737
//not actually an error
3838
ui.platformViewRegistry.registerViewFactory(createdViewId, (int viewId) => iframe);
3939
return Container(
40-
width: width ?? (height ?? 150) * 2,
41-
height: height ?? (width ?? 300) / 2,
42-
child: Directionality(
43-
textDirection: TextDirection.ltr,
44-
child: HtmlElementView(
45-
viewType: createdViewId,
46-
)
47-
)
40+
width: width ?? (height ?? 150) * 2,
41+
height: height ?? (width ?? 300) / 2,
42+
child: Directionality(
43+
textDirection: TextDirection.ltr,
44+
child: HtmlElementView(
45+
viewType: createdViewId,
46+
)
47+
)
4848
);
4949
}
5050
}

lib/style.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ class Style {
175175
Alignment? alignment;
176176
String? markerContent;
177177

178+
/// MaxLine
179+
///
180+
///
181+
///
182+
///
183+
int? maxLines;
184+
185+
/// TextOverflow
186+
///
187+
///
188+
///
189+
///
190+
TextOverflow? textOverflow;
191+
178192
Style({
179193
this.backgroundColor = Colors.transparent,
180194
this.color,
@@ -207,13 +221,25 @@ class Style {
207221
this.border,
208222
this.alignment,
209223
this.markerContent,
224+
this.maxLines,
225+
this.textOverflow,
210226
}) {
211227
if (this.alignment == null &&
212228
(display == Display.BLOCK || display == Display.LIST_ITEM)) {
213229
this.alignment = Alignment.centerLeft;
214230
}
215231
}
216232

233+
static Map<String, Style> fromThemeData(ThemeData theme) => {
234+
'h1': Style.fromTextStyle(theme.textTheme.headline1!),
235+
'h2': Style.fromTextStyle(theme.textTheme.headline2!),
236+
'h3': Style.fromTextStyle(theme.textTheme.headline3!),
237+
'h4': Style.fromTextStyle(theme.textTheme.headline4!),
238+
'h5': Style.fromTextStyle(theme.textTheme.headline5!),
239+
'h6': Style.fromTextStyle(theme.textTheme.headline6!),
240+
'body': Style.fromTextStyle(theme.textTheme.bodyText2!),
241+
};
242+
217243
TextStyle generateTextStyle() {
218244
return TextStyle(
219245
backgroundColor: backgroundColor,
@@ -278,6 +304,9 @@ class Style {
278304
//TODO merge border
279305
alignment: other.alignment,
280306
markerContent: other.markerContent,
307+
308+
maxLines: other.maxLines,
309+
textOverflow: other.textOverflow,
281310
);
282311
}
283312

@@ -313,6 +342,8 @@ class Style {
313342
textShadow: child.textShadow ?? textShadow,
314343
whiteSpace: child.whiteSpace ?? whiteSpace,
315344
wordSpacing: child.wordSpacing ?? wordSpacing,
345+
maxLines: child.maxLines ?? maxLines,
346+
textOverflow: child.textOverflow ?? textOverflow,
316347
);
317348
}
318349

@@ -348,6 +379,9 @@ class Style {
348379
Border? border,
349380
Alignment? alignment,
350381
String? markerContent,
382+
int? maxLines,
383+
TextOverflow? textOverflow,
384+
bool? beforeAfterNull,
351385
}) {
352386
return Style(
353387
backgroundColor: backgroundColor ?? this.backgroundColor,
@@ -377,11 +411,13 @@ class Style {
377411
whiteSpace: whiteSpace ?? this.whiteSpace,
378412
width: width ?? this.width,
379413
wordSpacing: wordSpacing ?? this.wordSpacing,
380-
before: before ?? this.before,
381-
after: after ?? this.after,
414+
before: beforeAfterNull == true ? null : before ?? this.before,
415+
after: beforeAfterNull == true ? null : after ?? this.after,
382416
border: border ?? this.border,
383417
alignment: alignment ?? this.alignment,
384418
markerContent: markerContent ?? this.markerContent,
419+
maxLines: maxLines ?? this.maxLines,
420+
textOverflow: textOverflow ?? this.textOverflow,
385421
);
386422
}
387423

0 commit comments

Comments
 (0)