@@ -6,6 +6,7 @@ import 'dart:convert';
6
6
import 'package:flutter/gestures.dart' ;
7
7
import 'package:flutter/material.dart' ;
8
8
import 'package:flutter_html/flutter_html.dart' ;
9
+ import 'package:flutter_html/src/html_elements.dart' ;
9
10
import 'package:flutter_html/src/utils.dart' ;
10
11
11
12
typedef CustomRenderMatcher = bool Function (RenderContext context);
@@ -15,7 +16,8 @@ CustomRenderMatcher tagMatcher(String tag) => (context) {
15
16
};
16
17
17
18
CustomRenderMatcher blockElementMatcher () => (context) {
18
- return context.tree.style.display == Display .BLOCK &&
19
+ return (context.tree.style.display == Display .BLOCK ||
20
+ context.tree.style.display == Display .INLINE_BLOCK ) &&
19
21
(context.tree.children.isNotEmpty ||
20
22
context.tree.element? .localName == "hr" );
21
23
};
@@ -116,10 +118,6 @@ CustomRender blockElementRender({Style? style, List<InlineSpan>? children}) =>
116
118
children: (children as List <TextSpan >? ) ??
117
119
context.tree.children
118
120
.expandIndexed ((i, childTree) => [
119
- if (childTree.style.display == Display .BLOCK &&
120
- i > 0 &&
121
- context.tree.children[i - 1 ] is ReplacedElement )
122
- TextSpan (text: "\n " ),
123
121
context.parser.parseTree (context, childTree),
124
122
if (i != context.tree.children.length - 1 &&
125
123
childTree.style.display == Display .BLOCK &&
@@ -131,117 +129,109 @@ CustomRender blockElementRender({Style? style, List<InlineSpan>? children}) =>
131
129
);
132
130
}
133
131
return WidgetSpan (
134
- child : ContainerSpan (
135
- key : context.key ,
136
- newContext : context,
137
- style : style ?? context.tree.style ,
138
- shrinkWrap : context.parser.shrinkWrap ,
139
- children : children ??
140
- context.tree.children
141
- . expandIndexed ((i, childTree) => [
142
- if (context.parser.shrinkWrap &&
143
- childTree.style.display == Display . BLOCK &&
144
- i > 0 &&
145
- context.tree.children[i - 1 ] is ReplacedElement )
146
- TextSpan (text : " \n " ),
147
- context.parser. parseTree (context, childTree),
148
- if (i != context.tree.children.length - 1 &&
149
- childTree.style.display == Display . BLOCK &&
150
- childTree.element? .localName != "html" &&
151
- childTree.element ? .localName != "body" )
152
- TextSpan (text : " \n " ),
153
- ])
154
- . toList ( ),
155
- )) ;
132
+ alignment : PlaceholderAlignment .baseline,
133
+ baseline : TextBaseline .alphabetic ,
134
+ child : CssBoxWidget . withInlineSpanChildren (
135
+ key : context.key ,
136
+ style : style ?? context.tree.style ,
137
+ shrinkWrap : context.parser.shrinkWrap,
138
+ childIsReplaced :
139
+ REPLACED_EXTERNAL_ELEMENTS . contains (context.tree.name),
140
+ children : children ??
141
+ context.tree.children
142
+ . expandIndexed ((i, childTree) => [
143
+ context.parser. parseTree (context, childTree),
144
+ //TODO can this newline be added in a different step?
145
+ if (i != context.tree.children.length - 1 &&
146
+ childTree.style.display == Display . BLOCK &&
147
+ childTree.element ? .localName != "html" &&
148
+ childTree.element? .localName != "body" )
149
+ TextSpan (text : " \n " ),
150
+ ])
151
+ . toList (),
152
+ ),
153
+ );
156
154
});
157
155
158
156
CustomRender listElementRender (
159
157
{Style ? style, Widget ? child, List <InlineSpan >? children}) =>
160
158
CustomRender .inlineSpan (
161
- inlineSpan: (context, buildChildren) => WidgetSpan (
162
- child: ContainerSpan (
163
- key: context.key,
164
- newContext: context,
165
- style: style ?? context.tree.style,
166
- shrinkWrap: context.parser.shrinkWrap,
167
- child: Row (
168
- crossAxisAlignment: CrossAxisAlignment .start,
169
- mainAxisSize: MainAxisSize .min,
170
- textDirection:
171
- style? .direction ?? context.tree.style.direction,
172
- children: [
173
- (style? .listStylePosition ??
174
- context.tree.style.listStylePosition) ==
175
- ListStylePosition .OUTSIDE
176
- ? Padding (
177
- padding: style? .padding? .nonNegative ??
178
- context.tree.style.padding? .nonNegative ??
179
- EdgeInsets .only (
180
- left: (style? .direction ??
181
- context.tree.style.direction) !=
182
- TextDirection .rtl
183
- ? 10.0
184
- : 0.0 ,
185
- right: (style? .direction ??
186
- context.tree.style.direction) ==
187
- TextDirection .rtl
188
- ? 10.0
189
- : 0.0 ),
190
- child: style? .markerContent ??
191
- context.style.markerContent)
192
- : Container (height: 0 , width: 0 ),
193
- Text ("\u 0020" ,
194
- textAlign: TextAlign .right,
195
- style: TextStyle (fontWeight: FontWeight .w400)),
196
- Expanded (
197
- child: Padding (
198
- padding: (style? .listStylePosition ??
199
- context.tree.style.listStylePosition) ==
200
- ListStylePosition .INSIDE
201
- ? EdgeInsets .only (
202
- left: (style? .direction ??
203
- context.tree.style.direction) !=
204
- TextDirection .rtl
205
- ? 10.0
206
- : 0.0 ,
207
- right: (style? .direction ??
208
- context.tree.style.direction) ==
209
- TextDirection .rtl
210
- ? 10.0
211
- : 0.0 )
212
- : EdgeInsets .zero,
213
- child: StyledText (
214
- textSpan: TextSpan (
215
- children: _getListElementChildren (
216
- style? .listStylePosition ??
217
- context.tree.style.listStylePosition,
218
- buildChildren)
219
- ..insertAll (
220
- 0 ,
221
- context.tree.style.listStylePosition ==
222
- ListStylePosition .INSIDE
223
- ? [
224
- WidgetSpan (
225
- alignment:
226
- PlaceholderAlignment
227
- .middle,
228
- child: style? .markerContent ??
229
- context.style
230
- .markerContent ??
231
- Container (
232
- height: 0 , width: 0 ))
233
- ]
234
- : []),
235
- style: style? .generateTextStyle () ??
236
- context.style.generateTextStyle (),
237
- ),
238
- style: style ?? context.style,
239
- renderContext: context,
240
- )))
241
- ],
159
+ inlineSpan: (context, buildChildren) => WidgetSpan (
160
+ child: CssBoxWidget (
161
+ key: context.key,
162
+ style: style ?? context.tree.style,
163
+ shrinkWrap: context.parser.shrinkWrap,
164
+ child: Row (
165
+ crossAxisAlignment: CrossAxisAlignment .start,
166
+ mainAxisSize: MainAxisSize .min,
167
+ textDirection: style? .direction ?? context.tree.style.direction,
168
+ children: [
169
+ (style? .listStylePosition ??
170
+ context.tree.style.listStylePosition) ==
171
+ ListStylePosition .OUTSIDE
172
+ ? Padding (
173
+ padding: style? .padding? .nonNegative ??
174
+ context.tree.style.padding? .nonNegative ??
175
+ EdgeInsets .only (
176
+ left: (style? .direction ??
177
+ context.tree.style.direction) !=
178
+ TextDirection .rtl
179
+ ? 10.0
180
+ : 0.0 ,
181
+ right: (style? .direction ??
182
+ context.tree.style.direction) ==
183
+ TextDirection .rtl
184
+ ? 10.0
185
+ : 0.0 ),
186
+ child:
187
+ style? .markerContent ?? context.style.markerContent)
188
+ : Container (height: 0 , width: 0 ),
189
+ Text ("\u 0020" ,
190
+ textAlign: TextAlign .right,
191
+ style: TextStyle (fontWeight: FontWeight .w400)),
192
+ Expanded (
193
+ child: Padding (
194
+ padding: (style? .listStylePosition ??
195
+ context.tree.style.listStylePosition) ==
196
+ ListStylePosition .INSIDE
197
+ ? EdgeInsets .only (
198
+ left: (style? .direction ??
199
+ context.tree.style.direction) !=
200
+ TextDirection .rtl
201
+ ? 10.0
202
+ : 0.0 ,
203
+ right: (style? .direction ??
204
+ context.tree.style.direction) ==
205
+ TextDirection .rtl
206
+ ? 10.0
207
+ : 0.0 )
208
+ : EdgeInsets .zero,
209
+ child: CssBoxWidget .withInlineSpanChildren (
210
+ children: _getListElementChildren (
211
+ style? .listStylePosition ??
212
+ context.tree.style.listStylePosition,
213
+ buildChildren)
214
+ ..insertAll (
215
+ 0 ,
216
+ context.tree.style.listStylePosition ==
217
+ ListStylePosition .INSIDE
218
+ ? [
219
+ WidgetSpan (
220
+ alignment: PlaceholderAlignment .middle,
221
+ child: style? .markerContent ??
222
+ context.style.markerContent ??
223
+ Container (height: 0 , width: 0 ))
224
+ ]
225
+ : []),
226
+ style: style ?? context.style,
227
+ ),
242
228
),
243
229
),
244
- ));
230
+ ],
231
+ ),
232
+ ),
233
+ ),
234
+ );
245
235
246
236
CustomRender replacedElementRender (
247
237
{PlaceholderAlignment ? alignment,
@@ -482,14 +472,9 @@ CustomRender verticalAlignRender(
482
472
key: context.key,
483
473
offset: Offset (
484
474
0 , verticalOffset ?? _getVerticalOffset (context.tree)),
485
- child: StyledText (
486
- textSpan: TextSpan (
487
- style: style? .generateTextStyle () ??
488
- context.style.generateTextStyle (),
489
- children: children ?? buildChildren.call (),
490
- ),
475
+ child: CssBoxWidget .withInlineSpanChildren (
476
+ children: children ?? buildChildren.call (),
491
477
style: context.style,
492
- renderContext: context,
493
478
),
494
479
),
495
480
));
@@ -512,19 +497,21 @@ CustomRender fallbackRender({Style? style, List<InlineSpan>? children}) =>
512
497
.toList (),
513
498
));
514
499
515
- final Map <CustomRenderMatcher , CustomRender > defaultRenders = {
516
- blockElementMatcher (): blockElementRender (),
517
- listElementMatcher (): listElementRender (),
518
- textContentElementMatcher (): textContentElementRender (),
519
- dataUriMatcher (): base64ImageRender (),
520
- assetUriMatcher (): assetImageRender (),
521
- networkSourceMatcher (): networkImageRender (),
522
- replacedElementMatcher (): replacedElementRender (),
523
- interactableElementMatcher (): interactableElementRender (),
524
- layoutElementMatcher (): layoutElementRender (),
525
- verticalAlignMatcher (): verticalAlignRender (),
526
- fallbackMatcher (): fallbackRender (),
527
- };
500
+ Map <CustomRenderMatcher , CustomRender > generateDefaultRenders () {
501
+ return {
502
+ blockElementMatcher (): blockElementRender (),
503
+ listElementMatcher (): listElementRender (),
504
+ textContentElementMatcher (): textContentElementRender (),
505
+ dataUriMatcher (): base64ImageRender (),
506
+ assetUriMatcher (): assetImageRender (),
507
+ networkSourceMatcher (): networkImageRender (),
508
+ replacedElementMatcher (): replacedElementRender (),
509
+ interactableElementMatcher (): interactableElementRender (),
510
+ layoutElementMatcher (): layoutElementRender (),
511
+ verticalAlignMatcher (): verticalAlignRender (),
512
+ fallbackMatcher (): fallbackRender (),
513
+ };
514
+ }
528
515
529
516
List <InlineSpan > _getListElementChildren (
530
517
ListStylePosition ? position, Function () buildChildren) {
@@ -585,9 +572,9 @@ final _dataUriFormat = RegExp(
585
572
double _getVerticalOffset (StyledElement tree) {
586
573
switch (tree.style.verticalAlign) {
587
574
case VerticalAlign .SUB :
588
- return tree.style.fontSize! .size ! / 2.5 ;
575
+ return tree.style.fontSize! .value / 2.5 ;
589
576
case VerticalAlign .SUPER :
590
- return tree.style.fontSize! .size ! / - 2.5 ;
577
+ return tree.style.fontSize! .value / - 2.5 ;
591
578
default :
592
579
return 0 ;
593
580
}
0 commit comments