Skip to content

Commit d3bcd78

Browse files
proxy window.location for testing and extension overrides (#3283)
* proxy window.location for testing and extension overrides * when not whe in test name
1 parent 408850a commit d3bcd78

3 files changed

Lines changed: 46 additions & 9 deletions

File tree

src/htmx.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ var htmx = (function() {
275275
},
276276
/** @type {typeof parseInterval} */
277277
parseInterval: null,
278+
/**
279+
* proxy of window.location used for page reload functions
280+
* @type location
281+
*/
282+
location,
278283
/** @type {typeof internalEval} */
279284
_: null,
280285
version: '2.0.4'
@@ -2359,7 +2364,7 @@ var htmx = (function() {
23592364
if (path == null || path === '') {
23602365
// if there is no action attribute on the form set path to current href before the
23612366
// following logic to properly clear parameters on a GET (not on a POST!)
2362-
path = getDocument().location.href
2367+
path = location.href
23632368
}
23642369
if (verb === 'get' && path.includes('?')) {
23652370
path = path.replace(/\?[^#]+/, '')
@@ -3186,7 +3191,7 @@ var htmx = (function() {
31863191
saveToHistoryCache(path, elt)
31873192
}
31883193

3189-
if (htmx.config.historyEnabled) history.replaceState({ htmx: true }, getDocument().title, window.location.href)
3194+
if (htmx.config.historyEnabled) history.replaceState({ htmx: true }, getDocument().title, location.href)
31903195
}
31913196

31923197
/**
@@ -3233,7 +3238,7 @@ var htmx = (function() {
32333238
request.open('GET', path, true)
32343239
request.setRequestHeader('HX-Request', 'true')
32353240
request.setRequestHeader('HX-History-Restore-Request', 'true')
3236-
request.setRequestHeader('HX-Current-URL', getDocument().location.href)
3241+
request.setRequestHeader('HX-Current-URL', location.href)
32373242
request.onload = function() {
32383243
if (this.status >= 200 && this.status < 400) {
32393244
triggerEvent(getDocument().body, 'htmx:historyCacheMissLoad', details)
@@ -3282,7 +3287,7 @@ var htmx = (function() {
32823287
if (htmx.config.refreshOnHistoryMiss) {
32833288
// @ts-ignore: optional parameter in reload() function throws error
32843289
// noinspection JSUnresolvedReference
3285-
window.location.reload(true)
3290+
htmx.location.reload(true)
32863291
} else {
32873292
loadHistoryFromServer(path)
32883293
}
@@ -3614,7 +3619,7 @@ var htmx = (function() {
36143619
'HX-Trigger': getRawAttribute(elt, 'id'),
36153620
'HX-Trigger-Name': getRawAttribute(elt, 'name'),
36163621
'HX-Target': getAttributeValue(target, 'id'),
3617-
'HX-Current-URL': getDocument().location.href
3622+
'HX-Current-URL': location.href
36183623
}
36193624
getValuesForElement(elt, 'hx-headers', false, headers)
36203625
if (prompt !== undefined) {
@@ -4359,7 +4364,7 @@ var htmx = (function() {
43594364

43604365
// behavior of anchors w/ empty href is to use the current URL
43614366
if (path == null || path === '') {
4362-
path = getDocument().location.href
4367+
path = location.href
43634368
}
43644369

43654370
/**
@@ -4723,14 +4728,14 @@ var htmx = (function() {
47234728

47244729
if (hasHeader(xhr, /HX-Redirect:/i)) {
47254730
responseInfo.keepIndicators = true
4726-
location.href = xhr.getResponseHeader('HX-Redirect')
4727-
shouldRefresh && location.reload()
4731+
htmx.location.href = xhr.getResponseHeader('HX-Redirect')
4732+
shouldRefresh && htmx.location.reload()
47284733
return
47294734
}
47304735

47314736
if (shouldRefresh) {
47324737
responseInfo.keepIndicators = true
4733-
location.reload()
4738+
htmx.location.reload()
47344739
return
47354740
}
47364741

test/attributes/hx-push-url.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ describe('hx-push-url attribute', function() {
136136
getWorkArea().textContent.should.equal('test1')
137137
})
138138

139+
it('cache miss should refresh when refreshOnHistoryMiss true', function() {
140+
htmx.config.refreshOnHistoryMiss = true
141+
var refresh = false
142+
htmx.location = { reload: function() { refresh = true } }
143+
localStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
144+
htmx._('restoreHistory')('/test3')
145+
refresh.should.equal(true)
146+
htmx.location = window.location
147+
htmx.config.refreshOnHistoryMiss = false
148+
})
149+
139150
it('navigation should push an element into the cache w/ data-* prefix', function() {
140151
this.server.respondWith('GET', '/test', 'second')
141152
getWorkArea().innerHTML.should.be.equal('')

test/core/headers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,27 @@ describe('Core htmx AJAX headers', function() {
380380
}, 30)
381381
})
382382

383+
it('should refresh page on HX-Refresh', function() {
384+
var refresh = false
385+
htmx.location = { reload: function() { refresh = true } }
386+
this.server.respondWith('GET', '/test', [200, { 'HX-Refresh': 'true' }, ''])
387+
var div = make('<div id="testdiv" hx-trigger="click" hx-get="/test"></div>')
388+
div.click()
389+
this.server.respond()
390+
refresh.should.equal(true)
391+
htmx.location = window.location
392+
})
393+
394+
it('should update location.href on HX-Redirect', function() {
395+
htmx.location = { href: window.location.href }
396+
this.server.respondWith('GET', '/test', [200, { 'HX-Redirect': 'https://htmx.org/headers/hx-redirect/' }, ''])
397+
var div = make('<div id="testdiv" hx-trigger="click" hx-get="/test"></div>')
398+
div.click()
399+
this.server.respond()
400+
htmx.location.href.should.equal('https://htmx.org/headers/hx-redirect/')
401+
htmx.location = window.location
402+
})
403+
383404
it('request to restore history should include the HX-Request header', function() {
384405
this.server.respondWith('GET', '/test', function(xhr) {
385406
xhr.requestHeaders['HX-Request'].should.be.equal('true')

0 commit comments

Comments
 (0)