Skip to content

Commit 04c9cec

Browse files
committed
content: Add start attribute support for ordered list
Fixes: #59
1 parent a055486 commit 04c9cec

File tree

4 files changed

+84
-22
lines changed

4 files changed

+84
-22
lines changed

lib/model/content.dart

+28-6
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,14 @@ class HeadingNode extends BlockInlineContainerNode {
253253

254254
enum ListStyle { ordered, unordered }
255255

256-
class ListNode extends BlockContentNode {
257-
const ListNode(this.style, this.items, {super.debugHtmlNode});
256+
sealed class ListNode extends BlockContentNode {
257+
const ListNode({required this.items, super.debugHtmlNode});
258258

259-
final ListStyle style;
260259
final List<List<BlockContentNode>> items;
261260

262261
@override
263262
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
264263
super.debugFillProperties(properties);
265-
properties.add(FlagProperty('ordered', value: style == ListStyle.ordered,
266-
ifTrue: 'ordered', ifFalse: 'unordered'));
267264
}
268265

269266
@override
@@ -275,6 +272,22 @@ class ListNode extends BlockContentNode {
275272
}
276273
}
277274

275+
class OrderedListNode extends ListNode {
276+
const OrderedListNode({this.start = 1, required super.items, super.debugHtmlNode});
277+
278+
final int start;
279+
280+
@override
281+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
282+
super.debugFillProperties(properties);
283+
properties.add(IntProperty('start', start));
284+
}
285+
}
286+
287+
class UnorderedListNode extends ListNode {
288+
const UnorderedListNode({required super.items, super.debugHtmlNode});
289+
}
290+
278291
class QuotationNode extends BlockContentNode {
279292
const QuotationNode(this.nodes, {super.debugHtmlNode});
280293

@@ -1081,7 +1094,16 @@ class _ZulipContentParser {
10811094
items.add(parseImplicitParagraphBlockContentList(item.nodes));
10821095
}
10831096

1084-
return ListNode(listStyle!, items, debugHtmlNode: debugHtmlNode);
1097+
if (listStyle == ListStyle.ordered) {
1098+
final startAttr = element.attributes['start'];
1099+
final start = startAttr != null
1100+
? int.tryParse(startAttr, radix: 10)
1101+
: 1;
1102+
if (start == null) return UnimplementedBlockContentNode(htmlNode: element);
1103+
return OrderedListNode(start: start, items: items, debugHtmlNode: debugHtmlNode);
1104+
} else {
1105+
return UnorderedListNode(items: items, debugHtmlNode: debugHtmlNode);
1106+
}
10851107
}
10861108

10871109
BlockContentNode parseSpoilerNode(dom.Element divElement) {

lib/widgets/content.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,16 @@ class ListNodeWidget extends StatelessWidget {
482482
final items = List.generate(node.items.length, (index) {
483483
final item = node.items[index];
484484
String marker;
485-
switch (node.style) {
485+
switch (node) {
486486
// TODO(#161): different unordered marker styles at different levels of nesting
487487
// see:
488488
// https://html.spec.whatwg.org/multipage/rendering.html#lists
489489
// https://www.w3.org/TR/css-counter-styles-3/#simple-symbolic
490490
// TODO proper alignment of unordered marker; should be "• ", one space,
491491
// but that comes out too close to item; not sure what's fixing that
492492
// in a browser
493-
case ListStyle.unordered: marker = "• "; break;
494-
// TODO(#59) ordered lists starting not at 1
495-
case ListStyle.ordered: marker = "${index+1}. "; break;
493+
case UnorderedListNode(): marker = "• "; break;
494+
case OrderedListNode(:final start): marker = "${start + index}. "; break;
496495
}
497496
return ListItemWidget(marker: marker, nodes: item);
498497
});

test/model/content_test.dart

+42-12
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ class ContentExample {
306306
'<p><em>italic</em> <a href="https://zulip.com/">zulip</a></p>\n'
307307
'</div></div>',
308308
[SpoilerNode(
309-
header: [ListNode(ListStyle.ordered, [
310-
[ListNode(ListStyle.unordered, [
309+
header: [OrderedListNode(items: [
310+
[UnorderedListNode(items: [
311311
[HeadingNode(level: HeadingLevel.h2, links: null, nodes: [
312312
TextNode('hello'),
313313
])]
@@ -763,7 +763,7 @@ class ContentExample {
763763
'<div class="message_inline_image">'
764764
'<a href="https://chat.zulip.org/user_avatars/2/realm/icon.png">'
765765
'<img src="https://chat.zulip.org/user_avatars/2/realm/icon.png"></a></div></li>\n</ul>', [
766-
ListNode(ListStyle.unordered, [[
766+
UnorderedListNode(items: [[
767767
ImageNodeList([
768768
ImageNode(srcUrl: 'https://chat.zulip.org/user_avatars/2/realm/icon.png',
769769
thumbnailUrl: null, loading: false,
@@ -785,7 +785,7 @@ class ContentExample {
785785
'<div class="message_inline_image">'
786786
'<a href="https://chat.zulip.org/user_avatars/2/realm/icon.png?version=2" title="icon.png">'
787787
'<img src="https://chat.zulip.org/user_avatars/2/realm/icon.png?version=2"></a></div></li>\n</ul>', [
788-
ListNode(ListStyle.unordered, [[
788+
UnorderedListNode(items: [[
789789
ParagraphNode(wasImplicit: true, links: null, nodes: [
790790
LinkNode(url: 'https://chat.zulip.org/user_avatars/2/realm/icon.png', nodes: [TextNode('icon.png')]),
791791
TextNode(' '),
@@ -814,7 +814,7 @@ class ContentExample {
814814
'<a href="https://chat.zulip.org/user_avatars/2/realm/icon.png" title="icon.png">'
815815
'<img src="https://chat.zulip.org/user_avatars/2/realm/icon.png"></a></div>'
816816
'more text</li>\n</ul>', [
817-
ListNode(ListStyle.unordered, [[
817+
UnorderedListNode(items: [[
818818
const ParagraphNode(wasImplicit: true, links: null, nodes: [
819819
LinkNode(url: 'https://chat.zulip.org/user_avatars/2/realm/icon.png', nodes: [TextNode('icon.png')]),
820820
TextNode(' '),
@@ -1382,7 +1382,7 @@ void main() {
13821382
testParse('<ol>',
13831383
// "1. first\n2. then"
13841384
'<ol>\n<li>first</li>\n<li>then</li>\n</ol>', const [
1385-
ListNode(ListStyle.ordered, [
1385+
OrderedListNode(items: [
13861386
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('first')])],
13871387
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('then')])],
13881388
]),
@@ -1391,7 +1391,7 @@ void main() {
13911391
testParse('<ul>',
13921392
// "* something\n* another"
13931393
'<ul>\n<li>something</li>\n<li>another</li>\n</ul>', const [
1394-
ListNode(ListStyle.unordered, [
1394+
UnorderedListNode(items: [
13951395
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('something')])],
13961396
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('another')])],
13971397
]),
@@ -1400,7 +1400,7 @@ void main() {
14001400
testParse('implicit paragraph with internal <br>',
14011401
// "* a\n b"
14021402
'<ul>\n<li>a<br>\n b</li>\n</ul>', const [
1403-
ListNode(ListStyle.unordered, [
1403+
UnorderedListNode(items: [
14041404
[ParagraphNode(wasImplicit: true, links: null, nodes: [
14051405
TextNode('a'),
14061406
LineBreakInlineNode(),
@@ -1412,13 +1412,43 @@ void main() {
14121412
testParse('explicit paragraphs',
14131413
// "* a\n\n b"
14141414
'<ul>\n<li>\n<p>a</p>\n<p>b</p>\n</li>\n</ul>', const [
1415-
ListNode(ListStyle.unordered, [
1415+
UnorderedListNode(items: [
14161416
[
14171417
ParagraphNode(links: null, nodes: [TextNode('a')]),
14181418
ParagraphNode(links: null, nodes: [TextNode('b')]),
14191419
],
14201420
]),
14211421
]);
1422+
1423+
testParse('ordered list - large start number (9999)',
1424+
// "9999. first\n10000. second"
1425+
'<ol start="9999">\n<li>first</li>\n<li>second</li>\n</ol>', const [
1426+
OrderedListNode(
1427+
start: 9999,
1428+
items: [
1429+
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('first')])],
1430+
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('second')])],
1431+
]),
1432+
]);
1433+
1434+
testParse('ordered list - default start (1)',
1435+
'<ol>\n<li>first</li>\n<li>second</li>\n</ol>', const [
1436+
OrderedListNode(
1437+
items: [
1438+
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('first')])],
1439+
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('second')])],
1440+
]),
1441+
]);
1442+
1443+
testParse('ordered list - custom start (5)',
1444+
'<ol start="5">\n<li>fifth</li>\n<li>sixth</li>\n</ol>', const [
1445+
OrderedListNode(
1446+
start: 5,
1447+
items: [
1448+
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('fifth')])],
1449+
[ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('sixth')])],
1450+
]),
1451+
]);
14221452
});
14231453

14241454
testParseExample(ContentExample.spoilerDefaultHeader);
@@ -1451,7 +1481,7 @@ void main() {
14511481
testParse('link in list item',
14521482
// "* [t](/u)"
14531483
'<ul>\n<li><a href="/u">t</a></li>\n</ul>', const [
1454-
ListNode(ListStyle.unordered, [
1484+
UnorderedListNode(items: [
14551485
[ParagraphNode(links: null, wasImplicit: true, nodes: [
14561486
LinkNode(url: '/u', nodes: [TextNode('t')]),
14571487
])],
@@ -1509,10 +1539,10 @@ void main() {
15091539
'<ol>\n<li>\n<blockquote>\n<h6>two</h6>\n<ul>\n<li>three</li>\n'
15101540
'</ul>\n</blockquote>\n<div class="codehilite"><pre><span></span>'
15111541
'<code>four\n</code></pre></div>\n\n</li>\n</ol>', const [
1512-
ListNode(ListStyle.ordered, [[
1542+
OrderedListNode(items: [[
15131543
QuotationNode([
15141544
HeadingNode(level: HeadingLevel.h6, links: null, nodes: [TextNode('two')]),
1515-
ListNode(ListStyle.unordered, [[
1545+
UnorderedListNode(items: [[
15161546
ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('three')]),
15171547
]]),
15181548
]),

test/widgets/content_test.dart

+11
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ void main() {
230230
});
231231
});
232232

233+
group('ListNodeWidget', () {
234+
testWidgets('ordered list with custom start', (tester) async {
235+
await prepareContent(tester, plainContent('<ol start="3">\n<li>third</li>\n<li>fourth</li>\n</ol>'));
236+
237+
expect(find.text('3. '), findsOneWidget);
238+
expect(find.text('4. '), findsOneWidget);
239+
expect(find.text('third'), findsOneWidget);
240+
expect(find.text('fourth'), findsOneWidget);
241+
});
242+
});
243+
233244
group('Spoiler', () {
234245
testContentSmoke(ContentExample.spoilerDefaultHeader);
235246
testContentSmoke(ContentExample.spoilerPlainCustomHeader);

0 commit comments

Comments
 (0)