Skip to content

Commit 9661568

Browse files
committed
Merge branch 'master' of https://github.com/Sub6Resources/flutter_html into feature/style-tag-support
� Conflicts: � lib/style.dart
2 parents 380ca5a + c15d499 commit 9661568

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-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
@@ -616,14 +616,15 @@ class HtmlParser extends StatelessWidget {
616616
static StyledElement _processBeforesAndAfters(StyledElement tree) {
617617
if (tree.style.before != null) {
618618
tree.children.insert(
619-
0, TextContentElement(text: tree.style.before, style: tree.style));
619+
0, TextContentElement(text: tree.style.before, style: tree.style.copyWith(beforeAfterNull: true, display: Display.INLINE)));
620620
}
621621
if (tree.style.after != null) {
622622
tree.children
623-
.add(TextContentElement(text: tree.style.after, style: tree.style));
624-
} else {
625-
tree.children.forEach(_processBeforesAndAfters);
623+
.add(TextContentElement(text: tree.style.after, style: tree.style.copyWith(beforeAfterNull: true, display: Display.INLINE)));
626624
}
625+
626+
tree.children.forEach(_processBeforesAndAfters);
627+
627628
return tree;
628629
}
629630

@@ -872,6 +873,8 @@ class StyledText extends StatelessWidget {
872873
textAlign: style.textAlign,
873874
textDirection: style.direction,
874875
textScaleFactor: textScaleFactor,
876+
maxLines: style.maxLines,
877+
overflow: style.textOverflow,
875878
),
876879
);
877880
}

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: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ class Style {
177177
Alignment? alignment;
178178
String? markerContent;
179179

180+
/// MaxLine
181+
///
182+
///
183+
///
184+
///
185+
int? maxLines;
186+
187+
/// TextOverflow
188+
///
189+
///
190+
///
191+
///
192+
TextOverflow? textOverflow;
193+
180194
Style({
181195
this.backgroundColor = Colors.transparent,
182196
this.color,
@@ -209,13 +223,25 @@ class Style {
209223
this.border,
210224
this.alignment,
211225
this.markerContent,
226+
this.maxLines,
227+
this.textOverflow,
212228
}) {
213229
if (this.alignment == null &&
214230
(display == Display.BLOCK || display == Display.LIST_ITEM)) {
215231
this.alignment = Alignment.centerLeft;
216232
}
217233
}
218234

235+
static Map<String, Style> fromThemeData(ThemeData theme) => {
236+
'h1': Style.fromTextStyle(theme.textTheme.headline1!),
237+
'h2': Style.fromTextStyle(theme.textTheme.headline2!),
238+
'h3': Style.fromTextStyle(theme.textTheme.headline3!),
239+
'h4': Style.fromTextStyle(theme.textTheme.headline4!),
240+
'h5': Style.fromTextStyle(theme.textTheme.headline5!),
241+
'h6': Style.fromTextStyle(theme.textTheme.headline6!),
242+
'body': Style.fromTextStyle(theme.textTheme.bodyText2!),
243+
};
244+
219245
static Map<String, Style> fromCSS(String css, OnCSSParseError? onCSSParseError) {
220246
final declarations = parseExternalCSS(css, onCSSParseError);
221247
Map<String, Style> styleMap = {};
@@ -289,6 +315,8 @@ class Style {
289315
//TODO merge border
290316
alignment: other.alignment,
291317
markerContent: other.markerContent,
318+
maxLines: other.maxLines,
319+
textOverflow: other.textOverflow,
292320
);
293321
}
294322

@@ -324,6 +352,8 @@ class Style {
324352
textShadow: child.textShadow ?? textShadow,
325353
whiteSpace: child.whiteSpace ?? whiteSpace,
326354
wordSpacing: child.wordSpacing ?? wordSpacing,
355+
maxLines: child.maxLines ?? maxLines,
356+
textOverflow: child.textOverflow ?? textOverflow,
327357
);
328358
}
329359

@@ -359,6 +389,9 @@ class Style {
359389
Border? border,
360390
Alignment? alignment,
361391
String? markerContent,
392+
int? maxLines,
393+
TextOverflow? textOverflow,
394+
bool? beforeAfterNull,
362395
}) {
363396
return Style(
364397
backgroundColor: backgroundColor ?? this.backgroundColor,
@@ -388,11 +421,13 @@ class Style {
388421
whiteSpace: whiteSpace ?? this.whiteSpace,
389422
width: width ?? this.width,
390423
wordSpacing: wordSpacing ?? this.wordSpacing,
391-
before: before ?? this.before,
392-
after: after ?? this.after,
424+
before: beforeAfterNull == true ? null : before ?? this.before,
425+
after: beforeAfterNull == true ? null : after ?? this.after,
393426
border: border ?? this.border,
394427
alignment: alignment ?? this.alignment,
395428
markerContent: markerContent ?? this.markerContent,
429+
maxLines: maxLines ?? this.maxLines,
430+
textOverflow: textOverflow ?? this.textOverflow,
396431
);
397432
}
398433

0 commit comments

Comments
 (0)