Skip to content
This repository was archived by the owner on Nov 1, 2017. It is now read-only.

Commit 3e38fe4

Browse files
committed
Match whitespace, including non-breaking spaces, test properly
1 parent 1b78991 commit 3e38fe4

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

app/assets/javascripts/task_lists.coffee

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,21 @@
9999
# To automatically enable TaskLists, add the `js-task-list-enable` class to the
100100
# `js-task-list-container`.
101101

102-
incomplete = "[ ]"
103-
incompleteNBSP = "[\xC2\xA0]"
104-
complete = "[x]"
102+
incomplete = "[ ]"
103+
complete = "[x]"
105104

106-
escape = (str) ->
107-
str.replace(/([\[\]])/g, "\\$1")
105+
# Escapes the String for regular expression matching.
106+
escapePattern = (str) ->
107+
str.
108+
replace(/([\[\]])/g, "\\$1"). # escape square brackets
109+
replace(/\s/, "\\s") # match all white space
110+
111+
incompletePattern = ///
112+
#{escapePattern(incomplete)}
113+
///
114+
completePattern = ///
115+
#{escapePattern(complete)}
116+
///
108117

109118
# Pattern used to identify all task list items.
110119
# Useful when you need iterate over all items.
@@ -113,9 +122,8 @@ itemPattern = ///
113122
(?:\s*[-+*]|(?:\d+\.))? # optional list prefix
114123
\s* # optional whitespace prefix
115124
( # checkbox
116-
#{escape(complete)}|
117-
#{escape(incomplete)}|
118-
#{escape(incompleteNBSP)}
125+
#{escapePattern(complete)}|
126+
#{escapePattern(incomplete)}
119127
)
120128
(?=\s) # followed by whitespace
121129
///
@@ -137,9 +145,8 @@ codeFencesPattern = ///
137145
itemsInParasPattern = ///
138146
^
139147
(
140-
#{escape(complete)}|
141-
#{escape(incomplete)}|
142-
#{escape(incompleteNBSP)}
148+
#{escapePattern(complete)}|
149+
#{escapePattern(incomplete)}
143150
)
144151
.+
145152
$
@@ -159,9 +166,9 @@ updateTaskListItem = (source, itemIndex, checked) ->
159166
if index == itemIndex
160167
line =
161168
if checked
162-
line.replace(incomplete, complete).replace(incompleteNBSP, complete)
169+
line.replace(incompletePattern, complete)
163170
else
164-
line.replace(complete, incomplete)
171+
line.replace(completePattern, incomplete)
165172
line
166173
result.join("\n")
167174

test/unit/test_updates.coffee

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module "TaskList updates",
2020
disabled: true
2121
checked: false
2222

23+
# non-breaking space. See: https://github.com/github/task-lists/pull/14
24+
@nbsp = String.fromCharCode(160)
2325
@incompleteNBSPItem = $ '<li>', class: 'task-list-item'
2426
@incompleteNBSPCheckbox = $ '<input>',
2527
type: 'checkbox'
@@ -30,19 +32,19 @@ module "TaskList updates",
3032
@field = $ '<textarea>', class: 'js-task-list-field', text: """
3133
- [x] complete
3234
- [ ] incomplete
33-
- [\xC2\xA0] incompleteNBSP
35+
- [#{@nbsp}] incompleteNBSP
3436
"""
3537

3638
@changes =
3739
toComplete: """
3840
- [ ] complete
3941
- [ ] incomplete
40-
- [\xC2\xA0] incompleteNBSP
42+
- [#{@nbsp}] incompleteNBSP
4143
"""
4244
toIncomplete: """
4345
- [x] complete
4446
- [x] incomplete
45-
- [\xC2\xA0] incompleteNBSP
47+
- [#{@nbsp}] incompleteNBSP
4648
"""
4749
toIncompleteNBSP: """
4850
- [x] complete
@@ -99,6 +101,7 @@ asyncTest "updates the source, marking the complete item as incomplete", ->
99101

100102
@completeCheckbox.click()
101103

104+
# See: https://github.com/github/task-lists/pull/14
102105
asyncTest "updates the source for items with non-breaking spaces", ->
103106
expect 3
104107

0 commit comments

Comments
 (0)