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

Commit 1b78991

Browse files
committed
Support updating task lists with non-breaking spaces
1 parent 6be4073 commit 1b78991

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

app/assets/javascripts/task_lists.coffee

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

102-
incomplete = "[ ]"
103-
complete = "[x]"
102+
incomplete = "[ ]"
103+
incompleteNBSP = "[\xC2\xA0]"
104+
complete = "[x]"
105+
106+
escape = (str) ->
107+
str.replace(/([\[\]])/g, "\\$1")
104108

105109
# Pattern used to identify all task list items.
106110
# Useful when you need iterate over all items.
@@ -109,8 +113,9 @@ itemPattern = ///
109113
(?:\s*[-+*]|(?:\d+\.))? # optional list prefix
110114
\s* # optional whitespace prefix
111115
( # checkbox
112-
#{complete. replace(/([\[\]])/g, "\\$1")}|
113-
#{incomplete.replace(/([\[\]])/g, "\\$1")}
116+
#{escape(complete)}|
117+
#{escape(incomplete)}|
118+
#{escape(incompleteNBSP)}
114119
)
115120
(?=\s) # followed by whitespace
116121
///
@@ -132,8 +137,9 @@ codeFencesPattern = ///
132137
itemsInParasPattern = ///
133138
^
134139
(
135-
#{complete. replace(/[\[\]]/g, "\\$1")}|
136-
#{incomplete.replace(/[\[\]]/g, "\\$1")}
140+
#{escape(complete)}|
141+
#{escape(incomplete)}|
142+
#{escape(incompleteNBSP)}
137143
)
138144
.+
139145
$
@@ -153,7 +159,7 @@ updateTaskListItem = (source, itemIndex, checked) ->
153159
if index == itemIndex
154160
line =
155161
if checked
156-
line.replace(incomplete, complete)
162+
line.replace(incomplete, complete).replace(incompleteNBSP, complete)
157163
else
158164
line.replace(complete, incomplete)
159165
line

test/unit/test_updates.coffee

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,34 @@ module "TaskList updates",
2020
disabled: true
2121
checked: false
2222

23+
@incompleteNBSPItem = $ '<li>', class: 'task-list-item'
24+
@incompleteNBSPCheckbox = $ '<input>',
25+
type: 'checkbox'
26+
class: 'task-list-item-checkbox'
27+
disabled: true
28+
checked: false
29+
2330
@field = $ '<textarea>', class: 'js-task-list-field', text: """
2431
- [x] complete
2532
- [ ] incomplete
33+
- [\xC2\xA0] incompleteNBSP
2634
"""
2735

2836
@changes =
2937
toComplete: """
3038
- [ ] complete
3139
- [ ] incomplete
40+
- [\xC2\xA0] incompleteNBSP
3241
"""
3342
toIncomplete: """
3443
- [x] complete
3544
- [x] incomplete
45+
- [\xC2\xA0] incompleteNBSP
46+
"""
47+
toIncompleteNBSP: """
48+
- [x] complete
49+
- [ ] incomplete
50+
- [x] incompleteNBSP
3651
"""
3752

3853
@completeItem.append @completeCheckbox
@@ -43,6 +58,10 @@ module "TaskList updates",
4358
@list.append @incompleteItem
4459
@incompleteItem.expectedIndex = 2
4560

61+
@incompleteNBSPItem.append @incompleteNBSPCheckbox
62+
@list.append @incompleteNBSPItem
63+
@incompleteNBSPItem.expectedIndex = 3
64+
4665
@container.append @list
4766
@container.append @field
4867

@@ -79,3 +98,17 @@ asyncTest "updates the source, marking the complete item as incomplete", ->
7998
, 20
8099

81100
@completeCheckbox.click()
101+
102+
asyncTest "updates the source for items with non-breaking spaces", ->
103+
expect 3
104+
105+
@field.on 'tasklist:changed', (event, index, checked) =>
106+
ok checked
107+
equal index, @incompleteNBSPItem.expectedIndex
108+
equal @field.val(), @changes.toIncompleteNBSP
109+
110+
setTimeout ->
111+
start()
112+
, 20
113+
114+
@incompleteNBSPCheckbox.click()

0 commit comments

Comments
 (0)