Skip to content

Improve handling of tight lists #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
35 changes: 23 additions & 12 deletions lib/handlers/list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,37 @@ function listItem(h, node, parent) {
var children = node.children
var head = children[0]
var props = {}
var single = false
var result
var result = all(h, node)
var container
var loose = false
var index
var child

if (
(!parent || !parent.loose) &&
children.length === 1 &&
head.type === 'paragraph'
) {
single = true
/* A list item is loose if its parent list is loose, or if it directly
* contains two block-level elements */
if (parent) {
loose = Boolean(parent.loose)
} else if (children.length > 1) {
loose = true
}

result = all(h, single ? head : node)
/* Tight lists should not render 'paragraph' nodes as 'p' tags */
if (!loose) {
for (index = 0; index < result.length; index++) {
child = result[index]
if (child.tagName === 'p') {
result.splice.apply(result, [index, 1].concat(child.children))
index += child.children.length
}
}
}

if (typeof node.checked === 'boolean') {
if (!single && (!head || head.type !== 'paragraph')) {
if (loose && (!head || head.type !== 'paragraph')) {
result.unshift(h(null, 'p', []))
}

container = single ? result : result[0].children
container = loose ? result[0].children : result

if (container.length !== 0) {
container.unshift(u('text', ' '))
Expand All @@ -48,7 +59,7 @@ function listItem(h, node, parent) {
props.className = ['task-list-item']
}

if (!single && result.length !== 0) {
if (loose && result.length !== 0) {
result = wrap(result, true)
}

Expand Down
23 changes: 5 additions & 18 deletions test/footnote.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ test('Footnote', function(t) {
properties: {id: 'fn-1'}
},
[
u('text', '\n'),
u('element', {tagName: 'p', properties: {}}, [
u('text', 'bravo')
]),
u('text', '\n'),
u('text', 'bravo'),
u(
'element',
{
Expand All @@ -65,8 +61,7 @@ test('Footnote', function(t) {
}
},
[u('text', '↩')]
),
u('text', '\n')
)
]
),
u('text', '\n')
Expand Down Expand Up @@ -152,9 +147,7 @@ test('Footnote', function(t) {
properties: {id: 'fn-1'}
},
[
u('text', '\n'),
u('text', 'bravo'),
u('text', '\n'),
u(
'element',
{
Expand All @@ -165,8 +158,7 @@ test('Footnote', function(t) {
}
},
[u('text', '↩')]
),
u('text', '\n')
)
]
),
u('text', '\n'),
Expand All @@ -177,11 +169,7 @@ test('Footnote', function(t) {
properties: {id: 'fn-2'}
},
[
u('text', '\n'),
u('element', {tagName: 'p', properties: {}}, [
u('text', 'charlie')
]),
u('text', '\n'),
u('text', 'charlie'),
u(
'element',
{
Expand All @@ -192,8 +180,7 @@ test('Footnote', function(t) {
}
},
[u('text', '↩')]
),
u('text', '\n')
)
]
),
u('text', '\n')
Expand Down
60 changes: 25 additions & 35 deletions test/list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,18 @@ test('ListItem', function(t) {
t.deepEqual(
to(u('listItem', {checked: true}, [u('html', '<!--tango-->')])),
u('element', {tagName: 'li', properties: {className: ['task-list-item']}}, [
u('text', '\n'),
u('element', {tagName: 'p', properties: {}}, [
u(
'element',
{
tagName: 'input',
properties: {
type: 'checkbox',
checked: true,
disabled: true
}
},
[]
)
]),
u('text', '\n')
u(
'element',
{
tagName: 'input',
properties: {
type: 'checkbox',
checked: true,
disabled: true
}
},
[]
)
]),
'should support checkboxes in `listItem`s without paragraph'
)
Expand All @@ -114,22 +110,18 @@ test('ListItem', function(t) {
t.deepEqual(
to(u('listItem', {checked: true}, [])),
u('element', {tagName: 'li', properties: {className: ['task-list-item']}}, [
u('text', '\n'),
u('element', {tagName: 'p', properties: {}}, [
u(
'element',
{
tagName: 'input',
properties: {
type: 'checkbox',
checked: true,
disabled: true
}
},
[]
)
]),
u('text', '\n')
u(
'element',
{
tagName: 'input',
properties: {
type: 'checkbox',
checked: true,
disabled: true
}
},
[]
)
]),
'should support checkboxes in `listItem`s without children'
)
Expand All @@ -143,13 +135,11 @@ test('ListItem', function(t) {
])
),
u('element', {tagName: 'li', properties: {}}, [
u('text', '\n'),
u('element', {tagName: 'ul', properties: {}}, [
u('text', '\n'),
u('element', {tagName: 'li', properties: {}}, [u('text', 'Alpha')]),
u('text', '\n')
]),
u('text', '\n')
])
]),
'should support lists in `listItem`s'
)
Expand Down