Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit 4212d47

Browse files
gmaJared Deckard
authored andcommitted
Don't add task-list class to lists that have no tasks
Markdown documents can contain multiple lists, some of which might contain tasks, and some which might not. Prior to this commit the HTML::Pipeline::TaskList filter added the 'task-list' CSS class to all lists if at least one contained an item that looked like a task. This made it impossible to style task lists differently to other lists in the document.
1 parent c501778 commit 4212d47

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

lib/task_list/filter.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Filter < HTML::Pipeline::Filter
5454

5555
class XPathSelectorFunction
5656
def self.task_list_item(nodes)
57-
nodes if nodes.text =~ ItemPattern
57+
nodes.select { |node| node.text =~ ItemPattern }
5858
end
5959
end
6060

@@ -95,12 +95,13 @@ def render_task_list_item(item)
9595
item.source.sub(ItemPattern, render_item_checkbox(item)), 'utf-8'
9696
end
9797

98-
# Public: Select all task lists from the `doc`.
98+
# Public: Select all task list items within `container`.
9999
#
100100
# Returns an Array of Nokogiri::XML::Element objects for ordered and
101-
# unordered lists.
102-
def list_items
103-
doc.xpath(ListItemSelector, XPathSelectorFunction)
101+
# unordered lists. The container can either be the entire document (as
102+
# returned by `#doc`) or an Element object.
103+
def list_items(container)
104+
container.xpath(ListItemSelector, XPathSelectorFunction)
104105
end
105106

106107
# Filters the source for task list items.
@@ -112,7 +113,9 @@ def list_items
112113
#
113114
# Returns nothing.
114115
def filter!
115-
list_items.reverse.each do |li|
116+
list_items(doc).reverse.each do |li|
117+
next if list_items(li.parent).empty?
118+
116119
add_css_class(li.parent, 'task-list')
117120

118121
outer, inner =

test/task_list/filter_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def setup
1313
@item_selector = "input.task-list-item-checkbox[type=checkbox]"
1414
end
1515

16+
def test_has_no_effect_on_lists_with_no_tasks
17+
text = <<-md
18+
- plain
19+
- bullets
20+
md
21+
assert_equal 0, filter(text)[:output].css('ul.task-list').size
22+
end
23+
1624
def test_filters_items_in_a_list
1725
text = <<-md
1826
- [ ] incomplete

0 commit comments

Comments
 (0)