/
index.js
280 lines (232 loc) · 7.7 KB
/
index.js
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
var test = require('tape')
var localLinks = require('../local-links')
var jq = require('jquery')
var partial = require('lodash.partial')
var path = require('path')
function $ (id) {
return document.getElementById(id)
}
function e (id) {
return {target: $(id)}
}
function setup () {
var container = document.createElement('div')
container.id = 'container'
container.innerHTML = [
'<a id="local" href="/local/page/1">Local</a>',
'<a id="local2" href="/local/page/1">Local2</a>',
'<a id="local3" href="/local/page/1">Local3</a>',
'<a id="local-search" href="/local/page/1?param=2">Search</a>',
'<a id="relative" href="page-2">Relative</a>',
'<a id="global" href="http://google.com/page/number/1">Global</a>',
'<a href="/local/page/1"><span id="local-nested">Nested</span></a>',
'<a id="empty-in-page-hash" href="#">Empty Hash</a>',
'<a id="in-page-hash" href="#modal">Hash</a>',
'<a id="out-of-page-hash" href="/local/page/1#two">Out of Page hash</a>',
'<a id="global-hash" href="http://google.com/#hash">Global Hash</a>',
'<a id="active" href="' + window.location.pathname + '"">Active</a>',
'<span id="no-anchor">No anchor</span>',
'<a id="local-blank" href="/local/page/1" target="_blank">Local Blank</a>',
'<a id="local-blank-hash" href="#modal2" target="_blank">Local Blank Hash</a>'
].join('')
document.body.appendChild(container)
}
function triggerClick (el, modified, button) {
var ev
if (button === undefined) {
button = 0 /* left */
}
if (document.createEvent) {
ev = document.createEvent('MouseEvent')
ev.initMouseEvent(
'click',
true /* bubble */,
true /* cancelable */,
window, null,
0, 0, 0, 0, /* coordinates */
!!modified, false, false, false, /* modifier keys */
button,
null
)
el.dispatchEvent(ev)
} else if (document.createEventObject) {
ev = document.createEventObject()
ev.ctrlKey = !!modified
ev.button = button
el.dispatchEvent('onclick', ev)
}
}
function attachClick (el, fn) {
if (el.addEventListener) {
el.addEventListener('click', fn, false)
} else if (el.attachEvent) {
el.attachEvent('onclick', fn)
}
}
jq(function () {
setup()
var windowDir = path.dirname(window.location.pathname)
function _pathnameTest (method, t) {
var a = $('local')
var search = $('local-search')
var outHash = $('out-of-page-hash')
var span = $('local-nested')
var global = $('global')
var relative = $('relative')
var noAnchor = $('no-anchor')
var localBlank = $('local-blank')
t.plan(8)
t.equal(localLinks[method](a), '/local/page/1')
t.equal(localLinks[method](span), '/local/page/1')
t.equal(localLinks[method](search), '/local/page/1?param=2')
t.equal(localLinks[method](outHash), '/local/page/1#two')
t.equal(localLinks[method](global), null)
t.equal(localLinks[method](relative), (windowDir === '/' ? '' : windowDir) + '/page-2')
t.equal(localLinks[method](noAnchor), null)
t.equal(localLinks[method](localBlank), null)
t.end()
}
// Use this test for both pathname alias functions
test('HTML elements return pathname or null', partial(_pathnameTest, 'pathname'))
test('HTML elements return pathname or null', partial(_pathnameTest, 'getLocalPathname'))
test('Can be called with different context', function (t) {
var pathname = localLinks.pathname
var hash = localLinks.hash
var active = localLinks.active
t.plan(3)
t.equal(pathname($('local')), '/local/page/1')
t.equal(hash($('in-page-hash')), '#modal')
t.equal(active($('active')), true)
t.end()
})
test('Works if the argument is an event with a target', function (t) {
var ev = e('local')
var nestedEvent = e('local-nested')
var globalEvent = e('global')
t.plan(3)
t.equal(localLinks.pathname(ev), '/local/page/1')
t.equal(localLinks.pathname(nestedEvent), '/local/page/1')
t.equal(localLinks.pathname(globalEvent), null)
t.end()
})
test('Ignores modified events for a valid anchor', function (t) {
var ev = e('local')
ev.shiftKey = true
t.plan(1)
t.equal(localLinks.pathname(ev, $('local')), null)
t.end()
})
test('Will use anchor from second arg', function (t) {
var globalEvent = e(global)
var globalA = $('global')
var localA = $('local-nested')
t.plan(3)
t.equal(localLinks.pathname(globalEvent, localA), '/local/page/1')
t.equal(localLinks.pathname(globalA, localA), '/local/page/1')
t.equal(localLinks.pathname(localA, globalA), null)
t.end()
})
function _hashTest (method, t) {
var hash = $('in-page-hash')
var emptyHash = $('empty-in-page-hash')
var globalHash = $('global-hash')
var targetBlankHash = $('local-blank-hash')
t.plan(7)
t.equal(localLinks.pathname(hash), null)
t.equal(localLinks.pathname(emptyHash), null)
t.equal(localLinks.pathname(targetBlankHash), null)
t.equal(localLinks[method](hash), '#modal')
t.equal(localLinks[method](emptyHash), '#')
t.equal(localLinks[method](targetBlankHash), null)
t.equal(localLinks[method](globalHash), null)
t.end()
}
// Use this test for both hash alias functions
test('Test hash links', partial(_hashTest, 'hash'))
test('Test hash links', partial(_hashTest, 'getLocalHash'))
function _activeTest (method, t) {
t.plan(5)
t.equal(localLinks[method]($('active')), true)
t.equal(localLinks[method]($('global')), false)
t.equal(localLinks[method]($('local')), false)
t.equal(localLinks[method]($('local'), '/local/page/1'), true)
t.equal(localLinks[method]($('in-page-hash')), false)
t.end()
}
// Use this test for both active alias functions
test('Active returns boolean based on current page', partial(_activeTest, 'active'))
test('Active returns boolean based on current page', partial(_activeTest, 'isActive'))
test('Return null for garbage', function (t) {
t.plan(8)
t.equal(localLinks.pathname(null), null)
t.equal(localLinks.pathname($('whoops')), null)
t.equal(localLinks.pathname({}, {}, {}, true), null)
t.equal(localLinks.pathname('hey'), null)
t.equal(localLinks.pathname(false), null)
t.equal(localLinks.hash({}), null)
t.equal(localLinks.hash('hey'), null)
t.equal(localLinks.hash(false), null)
t.end()
})
test('Works on link clicks', function (t) {
var local = $('local')
var global = $('global')
var plan = 2
var count = 0
var end = function () {
count++
if (count === plan) {
t.end()
}
}
t.plan(plan)
attachClick(local, function (event) {
event.preventDefault()
t.equal(localLinks.pathname(event), '/local/page/1')
end()
})
attachClick(global, function (event) {
event.preventDefault()
t.equal(localLinks.pathname(event), null)
end()
})
triggerClick(local)
triggerClick(global)
})
test('Works on modified link clicks', function (t) {
var local = $('local2')
var plan = 1
var count = 0
var end = function () {
count++
if (count === plan) {
t.end()
}
}
t.plan(plan)
attachClick(local, function (event) {
event.preventDefault()
t.equal(localLinks.pathname(event), null)
end()
})
triggerClick(local, true)
})
test('Ignores middle clicks', function (t) {
var local = $('local3')
var plan = 1
var count = 0
var end = function () {
count++
if (count === plan) {
t.end()
}
}
t.plan(plan)
attachClick(local, function (event) {
event.preventDefault()
t.equal(localLinks.pathname(event), null, 'should ignore middle-button clicks')
end()
})
triggerClick(local, false, 1)
})
})