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

Commit

Permalink
Match all whitespace
Browse files Browse the repository at this point in the history
This might be too inclusive. And doesn't address the XPath matching.
  • Loading branch information
mtodd committed Dec 13, 2013
1 parent 3e38fe4 commit dd204f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/task_list/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ def self.filter(*args)
# :task_list_items - An array of TaskList::Item objects.
class Filter < HTML::Pipeline::Filter

Incomplete = "[ ]".freeze
IncompleteNBSP = "[\xC2\xA0]".freeze
Complete = "[x]".freeze
Incomplete = "[ ]".freeze
Complete = "[x]".freeze

IncompletePattern = /\[[[:space:]]\]/.freeze # matches all whitespace
CompletePattern = Regexp.escape(Complete).freeze

# Pattern used to identify all task list items.
# Useful when you need iterate over all items.
Expand All @@ -42,9 +44,8 @@ class Filter < HTML::Pipeline::Filter
(?:\s*[-+*]|(?:\d+\.))? # optional list prefix
\s* # optional whitespace prefix
( # checkbox
#{Regexp.escape(Complete)}|
#{Regexp.escape(Incomplete)}|
#{Regexp.escape(IncompleteNBSP)}
#{CompletePattern}|
#{IncompletePattern}
)
(?=\s) # followed by whitespace
/x
Expand All @@ -53,11 +54,9 @@ class Filter < HTML::Pipeline::Filter
# select UL/OL
".//li[starts-with(text(),'[ ]')]/..",
".//li[starts-with(text(),'[x]')]/..",
".//li[starts-with(text(),'[\xC2\xA0]')]/..",
# and those wrapped in Ps
".//li/p[1][starts-with(text(),'[ ]')]/../..",
".//li/p[1][starts-with(text(),'[x]')]/../..",
".//li/p[1][starts-with(text(),'[\xC2\xA0]')]/../.."
".//li/p[1][starts-with(text(),'[x]')]/../.."
].join(' | ').freeze

# Selects all LIs from a TaskList UL/OL
Expand Down
12 changes: 12 additions & 0 deletions test/task_list/filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,24 @@ def test_handles_encoding_correctly

# NOTE: This is an edge case experienced regularly by users using a Swiss
# German keyboard.
# See: https://github.com/github/github/pull/18362
def test_non_breaking_space_between_brackets
text = "- [\xC2\xA0] ok"
assert item = filter(text)[:output].css('.task-list-item').pop, "item expected"
assert_equal 'ok', item.text.strip
end

# See: https://github.com/github/github/pull/18362
def test_non_breaking_space_between_brackets_in_paras
text = <<-md
- [\xC2\xA0] one
- [\xC2\xA0] this one will be wrapped in a para
- [\xC2\xA0] this one too, wtf
md
assert_equal 3, filter(text)[:output].css(@item_selector).size
end

protected

def filter(input, context = @context, result = nil)
Expand Down

0 comments on commit dd204f9

Please sign in to comment.