forked from GetmeUK/ContentTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.coffee
373 lines (249 loc) · 12.5 KB
/
tools.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# ToolShelf
describe 'ContentTools.ToolShelf.stow()', () ->
it 'should store a `ContentTools.Tool` instance against a name', () ->
tool = ContentTools.Tool
ContentTools.ToolShelf.stow(tool, 'tool')
expect(ContentTools.ToolShelf.fetch('tool')).toEqual tool
describe 'ContentTools.ToolShelf.fetch()', () ->
it 'should return a `ContentTools.Tool` instance by name', () ->
tool = ContentTools.Tools.Bold
expect(ContentTools.ToolShelf.fetch('bold')).toEqual tool
# Tools
# Bold
describe 'ContentTools.Tools.Bold.apply()', () ->
it 'should wrap the selected content in a bold tag if the bold tag is
not applied to all of the selection', () ->
region = new ContentEdit.Region(document.createElement('div'))
selection = new ContentSelect.Range(0, 4)
tool = ContentTools.Tools.Bold
# Bold applied to none of the selection
element = new ContentEdit.Text('p', {}, 'test')
region.attach(element)
tool.apply(element, selection, () =>)
expect(element.content.html()).toBe '<b>test</b>'
# Bold applied to only part of the selection
element = new ContentEdit.Text('p', {}, '<b>te</b>st')
region.attach(element)
tool.apply(element, selection, () =>)
expect(element.content.html()).toBe '<b>test</b>'
it 'should remove the bold tag from the selected content if the bold tag is
applied to all of the selection', () ->
element = new ContentEdit.Text('p', {}, '<b>test</b>')
region = new ContentEdit.Region(document.createElement('div'))
region.attach(element)
selection = new ContentSelect.Range(0, 4)
tool = ContentTools.Tools.Bold
# Check for selected content within bold tag
tool.apply(element, selection, () =>)
expect(element.content.html()).toBe 'test'
describe 'ContentTools.Tools.Bold.canApply()', () ->
it 'should return true if the element supports content and the selection is
not collapsed', () ->
element = new ContentEdit.Text('p', {}, 'test')
tool = ContentTools.Tools.Bold
# Check true for content element with expanded selection
selection = new ContentSelect.Range(0, 2)
expect(tool.canApply(element, selection)).toBe true
# Check false for content element with collapsed selection
selection = new ContentSelect.Range(0, 0)
expect(tool.canApply(element, selection)).toBe false
# Check false for non-content element
element = new ContentEdit.Image()
expect(tool.canApply(element, selection)).toBe false
describe 'ContentTools.Tools.Bold.isApplied()', () ->
it 'should return true if the selected content is wrapped in a bold
tag', () ->
element = new ContentEdit.Text('p', {}, '<b>te</b>st')
tool = ContentTools.Tools.Bold
# Check true if entire selection is bold
selection = new ContentSelect.Range(0, 2)
expect(tool.isApplied(element, selection)).toBe true
# Check false if only part of the selection is bold
selection = new ContentSelect.Range(0, 4)
expect(tool.isApplied(element, selection)).toBe false
# Check false if none of the selection is bold
selection = new ContentSelect.Range(2, 4)
expect(tool.isApplied(element, selection)).toBe false
# Italic
describe 'ContentTools.Tools.Italic.apply()', () ->
it 'should wrap the selected content in a italic tag if the italic tag is
not applied to all of the selection', () ->
region = new ContentEdit.Region(document.createElement('div'))
selection = new ContentSelect.Range(0, 4)
tool = ContentTools.Tools.Italic
# Italic applied to none of the selection
element = new ContentEdit.Text('p', {}, 'test')
region.attach(element)
tool.apply(element, selection, () =>)
expect(element.content.html()).toBe '<i>test</i>'
# Italic applied to only part of the selection
element = new ContentEdit.Text('p', {}, '<i>te</i>st')
region.attach(element)
tool.apply(element, selection, () =>)
expect(element.content.html()).toBe '<i>test</i>'
it 'should remove the italic tag from the selected content if the italic tag
is applied to all of the selection', () ->
element = new ContentEdit.Text('p', {}, '<i>test</i>')
region = new ContentEdit.Region(document.createElement('div'))
region.attach(element)
selection = new ContentSelect.Range(0, 4)
tool = ContentTools.Tools.Italic
# Check for selected content within italic tag
tool.apply(element, selection, () =>)
expect(element.content.html()).toBe 'test'
describe 'ContentTools.Tools.Italic.canApply()', () ->
it 'should return true if the element supports content and the selection is
not collapsed', () ->
element = new ContentEdit.Text('p', {}, 'test')
tool = ContentTools.Tools.Italic
# Check true for content element with expanded selection
selection = new ContentSelect.Range(0, 2)
expect(tool.canApply(element, selection)).toBe true
# Check false for content element with collapsed selection
selection = new ContentSelect.Range(0, 0)
expect(tool.canApply(element, selection)).toBe false
# Check false for non-content element
element = new ContentEdit.Image()
expect(tool.canApply(element, selection)).toBe false
describe 'ContentTools.Tools.Italic.isApplied()', () ->
it 'should return true if the selected content is wrapped in a italic
tag', () ->
element = new ContentEdit.Text('p', {}, '<i>te</i>st')
tool = ContentTools.Tools.Italic
# Check true if entire selection is italic
selection = new ContentSelect.Range(0, 2)
expect(tool.isApplied(element, selection)).toBe true
# Check false if only part of the selection is italic
selection = new ContentSelect.Range(0, 4)
expect(tool.isApplied(element, selection)).toBe false
# Check false if none of the selection is italic
selection = new ContentSelect.Range(2, 4)
expect(tool.isApplied(element, selection)).toBe false
# Link (TODO: add tests for apply)
describe 'ContentTools.Tools.Link.canApply()', () ->
it 'should return true if the element supports content and the selection is
not collapsed or if the element is an image', () ->
element = new ContentEdit.Text('p', {}, 'test')
tool = ContentTools.Tools.Link
# Check for content element with expanded selection
selection = new ContentSelect.Range(0, 2)
expect(tool.canApply(element, selection)).toBe true
# Check for content element with collapsed selection
selection = new ContentSelect.Range(0, 0)
expect(tool.canApply(element, selection)).toBe false
# Check for image element
element = new ContentEdit.Image()
expect(tool.canApply(element, selection)).toBe true
describe 'ContentTools.Tools.Link.getHref()', () ->
it 'should return the href for the first anchor tag found in a selection or
if the element is an image then for the anchor tag associated with
image', () ->
element = new ContentEdit.Text(
'p',
{},
'<a href="#test">te</a><a href="#test2">st</a>'
)
tool = ContentTools.Tools.Link
selection = new ContentSelect.Range(0, 2)
expect(tool.getHref(element, selection)).toBe '#test'
selection = new ContentSelect.Range(2, 4)
expect(tool.getHref(element, selection)).toBe '#test2'
# Check that if multiple links are spanned the href attribute of the
# first one is returned.
selection = new ContentSelect.Range(1, 4)
expect(tool.getHref(element, selection)).toBe '#test'
describe 'ContentTools.Tools.Link.isApplied()', () ->
it 'should return true if the selected content is wrapped in an anchor
tag or is an image with an associated anchor tag', () ->
element = new ContentEdit.Text('p', {}, '<a href="#test">te</a>st')
tool = ContentTools.Tools.Link
# Check true if entire selection is a link
selection = new ContentSelect.Range(0, 2)
expect(tool.isApplied(element, selection)).toBe true
# Check false if only part of the selection is a link
selection = new ContentSelect.Range(0, 4)
expect(tool.isApplied(element, selection)).toBe false
# Check false if none of the selection is a link
selection = new ContentSelect.Range(2, 4)
expect(tool.isApplied(element, selection)).toBe false
# Heading
describe 'ContentTools.Tools.Heading.apply()', () ->
it 'should change the tag name of a top level element supporting content to
h1', () ->
region = new ContentEdit.Region(document.createElement('div'))
selection = new ContentSelect.Range(0, 0)
tool = ContentTools.Tools.Heading
# Apply heading tool
element = new ContentEdit.Text('p', {}, 'test')
region.attach(element)
tool.apply(element, selection, () =>)
expect(element.tagName()).toBe 'h1'
describe 'ContentTools.Tools.Heading.canApply()', () ->
it 'should return true if the element is a top-level element that supports
content', () ->
region = new ContentEdit.Region(document.createElement('div'))
selection = new ContentSelect.Range(0, 0)
tool = ContentTools.Tools.Heading
element = new ContentEdit.Text('p', {}, 'test')
region.attach(element)
# Check true for content top-level content element
expect(tool.canApply(element, selection)).toBe true
# Check false for image element
image = new ContentEdit.Image()
region.attach(image)
expect(tool.canApply(image, selection)).toBe false
# Check for content element that is not top level
list = new ContentEdit.List('ul')
listItem = new ContentEdit.ListItem()
listItemText= new ContentEdit.ListItemText('test')
listItem.attach(listItemText)
list.attach(listItem)
region.attach(list)
expect(tool.canApply(list, selection)).toBe false
describe 'ContentTools.Tools.Heading.isApplied()', () ->
it 'should return false, tool does not support toggling', () ->
tool = ContentTools.Tools.Heading
element = new ContentEdit.Text('p', {}, 'test')
selection = new ContentSelect.Range(0, 0)
expect(tool.isApplied(element, selection)).toBe false
# Sub-heading
describe 'ContentTools.Tools.Subheading.apply()', () ->
it 'should change the tag name of a top level element supporting content to
h2', () ->
region = new ContentEdit.Region(document.createElement('div'))
selection = new ContentSelect.Range(0, 0)
tool = ContentTools.Tools.Subheading
# Apply heading tool
element = new ContentEdit.Text('p', {}, 'test')
region.attach(element)
tool.apply(element, selection, () =>)
expect(element.tagName()).toBe 'h2'
describe 'ContentTools.Tools.Subheading.canApply()', () ->
it 'should return true if the element is a top-level element that supports
content', () ->
region = new ContentEdit.Region(document.createElement('div'))
selection = new ContentSelect.Range(0, 0)
tool = ContentTools.Tools.Subheading
element = new ContentEdit.Text('p', {}, 'test')
region.attach(element)
# Check true for content top-level content element
expect(tool.canApply(element, selection)).toBe true
# Check false for image element
image = new ContentEdit.Image()
region.attach(image)
expect(tool.canApply(image, selection)).toBe false
# Check for content element that is not top level
list = new ContentEdit.List('ul')
listItem = new ContentEdit.ListItem()
listItemText= new ContentEdit.ListItemText('test')
listItem.attach(listItemText)
list.attach(listItem)
region.attach(list)
expect(tool.canApply(list, selection)).toBe false
describe 'ContentTools.Tools.Subheading.isApplied()', () ->
it 'should return false, tool does not support toggling', () ->
tool = ContentTools.Tools.Subheading
element = new ContentEdit.Text('p', {}, 'test')
selection = new ContentSelect.Range(0, 0)
expect(tool.isApplied(element, selection)).toBe false
# Paragraph