Skip to content

Commit 44cd9a6

Browse files
committed
add support for list style position
1 parent 6a8e58b commit 44cd9a6

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

lib/html_parser.dart

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,35 +253,57 @@ class HtmlParser extends StatelessWidget {
253253
),
254254
);
255255
} else if (tree.style?.display == Display.LIST_ITEM) {
256+
List<Widget> children;
257+
if (tree.style?.listStylePosition == ListStylePosition.OUTSIDE) {
258+
children = [
259+
PositionedDirectional(
260+
width: 30, //TODO derive this from list padding.
261+
start: 0,
262+
child: Text('${newContext.style.markerContent}\t',
263+
textAlign: TextAlign.right,
264+
style: newContext.style.generateTextStyle()),
265+
),
266+
Padding(
267+
padding:
268+
EdgeInsets.only(left: 30), //TODO derive this from list padding.
269+
child: StyledText(
270+
textSpan: TextSpan(
271+
children: tree.children
272+
?.map((tree) => parseTree(newContext, tree))
273+
?.toList() ??
274+
[],
275+
style: newContext.style.generateTextStyle(),
276+
),
277+
style: newContext.style,
278+
),
279+
)
280+
];
281+
} else if (tree.style?.listStylePosition == ListStylePosition.INSIDE) {
282+
children = [
283+
Padding(
284+
padding:
285+
EdgeInsets.only(left: 30), //TODO derive this from list padding.
286+
child: StyledText(
287+
textSpan: TextSpan(
288+
text: '${newContext.style.markerContent}\t',
289+
children: tree.children
290+
?.map((tree) => parseTree(newContext, tree))
291+
?.toList() ??
292+
[],
293+
style: newContext.style.generateTextStyle(),
294+
),
295+
style: newContext.style,
296+
),
297+
)
298+
];
299+
}
256300
return WidgetSpan(
257301
child: ContainerSpan(
258302
newContext: newContext,
259303
style: tree.style,
260304
shrinkWrap: context.parser.shrinkWrap,
261305
child: Stack(
262-
children: <Widget>[
263-
PositionedDirectional(
264-
width: 30, //TODO derive this from list padding.
265-
start: 0,
266-
child: Text('${newContext.style.markerContent}\t',
267-
textAlign: TextAlign.right,
268-
style: newContext.style.generateTextStyle()),
269-
),
270-
Padding(
271-
padding: EdgeInsets.only(
272-
left: 30), //TODO derive this from list padding.
273-
child: StyledText(
274-
textSpan: TextSpan(
275-
children: tree.children
276-
?.map((tree) => parseTree(newContext, tree))
277-
?.toList() ??
278-
[],
279-
style: newContext.style.generateTextStyle(),
280-
),
281-
style: newContext.style,
282-
),
283-
)
284-
],
306+
children: children,
285307
),
286308
),
287309
);

lib/style.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class Style {
7777
/// Default: ListStyleType.DISC
7878
ListStyleType listStyleType;
7979

80+
/// CSS attribute "`list-style-position`"
81+
///
82+
/// Inherited: yes,
83+
/// Default: ListStylePosition.OUTSIDE
84+
ListStylePosition listStylePosition;
85+
8086
/// CSS attribute "`padding`"
8187
///
8288
/// Inherited: no,
@@ -172,6 +178,7 @@ class Style {
172178
this.height,
173179
this.letterSpacing,
174180
this.listStyleType,
181+
this.listStylePosition,
175182
this.padding,
176183
this.margin,
177184
this.textAlign,
@@ -194,6 +201,9 @@ class Style {
194201
(display == Display.BLOCK || display == Display.LIST_ITEM)) {
195202
this.alignment = Alignment.centerLeft;
196203
}
204+
if (this.listStylePosition == null && display == Display.LIST_ITEM) {
205+
this.listStylePosition = ListStylePosition.OUTSIDE;
206+
}
197207
}
198208

199209
TextStyle generateTextStyle() {
@@ -239,6 +249,7 @@ class Style {
239249
height: other.height,
240250
letterSpacing: other.letterSpacing,
241251
listStyleType: other.listStyleType,
252+
listStylePosition: other.listStylePosition,
242253
padding: other.padding,
243254
//TODO merge EdgeInsets
244255
margin: other.margin,
@@ -276,6 +287,7 @@ class Style {
276287
fontWeight: child.fontWeight ?? fontWeight,
277288
letterSpacing: child.letterSpacing ?? letterSpacing,
278289
listStyleType: child.listStyleType ?? listStyleType,
290+
listStylePosition: child.listStylePosition ?? listStylePosition,
279291
textAlign: child.textAlign ?? textAlign,
280292
textShadow: child.textShadow ?? textShadow,
281293
whiteSpace: child.whiteSpace ?? whiteSpace,
@@ -296,6 +308,7 @@ class Style {
296308
double height,
297309
double letterSpacing,
298310
ListStyleType listStyleType,
311+
ListStylePosition listStylePosition,
299312
EdgeInsets padding,
300313
EdgeInsets margin,
301314
TextAlign textAlign,
@@ -327,6 +340,7 @@ class Style {
327340
height: height ?? this.height,
328341
letterSpacing: letterSpacing ?? this.letterSpacing,
329342
listStyleType: listStyleType ?? this.listStyleType,
343+
listStylePosition: listStylePosition ?? this.listStylePosition,
330344
padding: padding ?? this.padding,
331345
margin: margin ?? this.margin,
332346
textAlign: textAlign ?? this.textAlign,
@@ -406,6 +420,11 @@ enum ListStyleType {
406420
DECIMAL,
407421
}
408422

423+
enum ListStylePosition {
424+
OUTSIDE,
425+
INSIDE,
426+
}
427+
409428
enum VerticalAlign {
410429
BASELINE,
411430
SUB,

0 commit comments

Comments
 (0)