Skip to content

Commit 994c705

Browse files
committed
Remove refactor
1 parent c3a1d7a commit 994c705

5 files changed

Lines changed: 87 additions & 57 deletions

File tree

src/htmx.js

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,19 +4039,25 @@ var htmx = (function() {
40394039
returnPromise: true
40404040
})
40414041
} else {
4042-
const { target, swap, source, event, ...restContext } = context
4043-
let resolvedTarget = resolveTarget(target)
4042+
let resolvedTarget = resolveTarget(context.target)
40444043
// If target is supplied but can't resolve OR source is supplied but both target and source can't be resolved
40454044
// then use DUMMY_ELT to abort the request with htmx:targetError to avoid it replacing body by mistake
4046-
if ((target && !resolvedTarget) || (source && !resolvedTarget && !resolveTarget(source))) {
4045+
if ((context.target && !resolvedTarget) || (context.source && !resolvedTarget && !resolveTarget(context.source))) {
40474046
resolvedTarget = DUMMY_ELT
40484047
}
4049-
return issueAjaxRequest(verb, path, resolveTarget(source), event, {
4050-
...restContext,
4051-
targetOverride: resolvedTarget,
4052-
swapOverride: swap,
4053-
returnPromise: true
4054-
})
4048+
return issueAjaxRequest(verb, path, resolveTarget(context.source), context.event,
4049+
{
4050+
handler: context.handler,
4051+
headers: context.headers,
4052+
values: context.values,
4053+
targetOverride: resolvedTarget,
4054+
swapOverride: context.swap,
4055+
select: context.select,
4056+
returnPromise: true,
4057+
push: context.push,
4058+
replace: context.replace,
4059+
selectOOB: context.selectOOB
4060+
})
40554061
}
40564062
} else {
40574063
return issueAjaxRequest(verb, path, null, null, {
@@ -4629,39 +4635,82 @@ var htmx = (function() {
46294635
* @return {HtmxHistoryUpdate}
46304636
*/
46314637
function determineHistoryUpdates(elt, responseInfo) {
4632-
const { xhr, pathInfo, etc } = responseInfo
4638+
const xhr = responseInfo.xhr
46334639

4634-
let push = xhr.getResponseHeader('HX-Push') || xhr.getResponseHeader('HX-Push-Url')
4635-
let replace = xhr.getResponseHeader('HX-Replace-Url')
4636-
const headerPath = push || replace
4640+
//= ==========================================
4641+
// First consult response headers
4642+
//= ==========================================
4643+
let pathFromHeaders = null
4644+
let typeFromHeaders = null
4645+
if (hasHeader(xhr, /HX-Push:/i)) {
4646+
pathFromHeaders = xhr.getResponseHeader('HX-Push')
4647+
typeFromHeaders = 'push'
4648+
} else if (hasHeader(xhr, /HX-Push-Url:/i)) {
4649+
pathFromHeaders = xhr.getResponseHeader('HX-Push-Url')
4650+
typeFromHeaders = 'push'
4651+
} else if (hasHeader(xhr, /HX-Replace-Url:/i)) {
4652+
pathFromHeaders = xhr.getResponseHeader('HX-Replace-Url')
4653+
typeFromHeaders = 'replace'
4654+
}
46374655

46384656
// if there was a response header, that has priority
4639-
if (!headerPath) {
4640-
// Next resolve via DOM values
4641-
push = getClosestAttributeValue(elt, 'hx-push-url') || etc.push
4642-
replace = getClosestAttributeValue(elt, 'hx-replace-url') || etc.replace
4643-
if (!push && !replace && getInternalData(elt).boosted) {
4644-
push = 'true'
4657+
if (pathFromHeaders) {
4658+
if (pathFromHeaders === 'false') {
4659+
return {}
4660+
} else {
4661+
return {
4662+
type: typeFromHeaders,
4663+
path: pathFromHeaders
4664+
}
46454665
}
46464666
}
4647-
let path = headerPath || push || replace
4648-
// unset or false indicates no push, return empty object
4649-
if (!path || path === 'false') {
4650-
return {}
4651-
}
46524667

4653-
// true indicates we want to follow wherever the server ended up sending us
4654-
if (path === 'true') {
4655-
path = pathInfo.responsePath || pathInfo.finalRequestPath // if there is no response path, go with the original request path
4656-
}
4657-
// restore any anchor associated with the request except for paths from respone headers to keep old behaviour
4658-
if ((!headerPath || headerPath === 'true') && pathInfo.anchor && path.indexOf('#') === -1) {
4659-
path = path + '#' + pathInfo.anchor
4668+
//= ==========================================
4669+
// Next resolve via DOM values
4670+
//= ==========================================
4671+
const requestPath = responseInfo.pathInfo.finalRequestPath
4672+
const responsePath = responseInfo.pathInfo.responsePath
4673+
4674+
const pushUrl = getClosestAttributeValue(elt, 'hx-push-url') || responseInfo.etc.push
4675+
const replaceUrl = getClosestAttributeValue(elt, 'hx-replace-url') || responseInfo.etc.replace
4676+
const elementIsBoosted = getInternalData(elt).boosted
4677+
4678+
let saveType = null
4679+
let path = null
4680+
4681+
if (pushUrl) {
4682+
saveType = 'push'
4683+
path = pushUrl
4684+
} else if (replaceUrl) {
4685+
saveType = 'replace'
4686+
path = replaceUrl
4687+
} else if (elementIsBoosted) {
4688+
saveType = 'push'
4689+
path = responsePath || requestPath // if there is no response path, go with the original request path
46604690
}
46614691

4662-
return {
4663-
type: push ? 'push' : 'replace',
4664-
path
4692+
if (path) {
4693+
// false indicates no push, return empty object
4694+
if (path === 'false') {
4695+
return {}
4696+
}
4697+
4698+
// true indicates we want to follow wherever the server ended up sending us
4699+
if (path === 'true') {
4700+
path = responsePath || requestPath // if there is no response path, go with the original request path
4701+
}
4702+
4703+
// restore any anchor associated with the request
4704+
if (responseInfo.pathInfo.anchor && path.indexOf('#') === -1) {
4705+
path = path + '#' + responseInfo.pathInfo.anchor
4706+
}
4707+
4708+
return {
4709+
type: saveType,
4710+
path
4711+
}
4712+
} else {
4713+
return {}
46654714
}
46664715
}
46674716

test/core/api.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -726,20 +726,4 @@ describe('Core htmx API test', function() {
726726
var path = sessionStorage.getItem('htmx-current-path-for-history')
727727
path.should.equal('/abc123')
728728
})
729-
730-
it('ajax api passes custom context properties to htmx events', function() {
731-
this.server.respondWith('GET', '/test', 'response')
732-
var div = make("<div id='d1'></div>")
733-
var customProp = null
734-
var handler = htmx.on('htmx:beforeRequest', function(event) {
735-
customProp = event.detail.etc.customProperty
736-
})
737-
htmx.ajax('GET', '/test', {
738-
target: '#d1',
739-
customProperty: 'testValue'
740-
})
741-
this.server.respond()
742-
customProp.should.equal('testValue')
743-
htmx.off('htmx:beforeRequest', handler)
744-
})
745729
})

www/content/api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ or
6868
* `selectOOB` - allows you to select content for out-of-band swaps from a response
6969
* `push` - can be `'true'` or a path to push a URL into browser location history
7070
* `replace` - can be `'true'` or a path to replace the URL in the browser location history
71-
* *custom properties* - any additional properties will be passed to some htmx events via `event.detail.etc`
7271

7372
##### Example
7473

www/content/headers/hx-push-url.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ description = """\
66

77
The `HX-Push-Url` header allows you to push a URL into the browser [location history](https://developer.mozilla.org/en-US/docs/Web/API/History_API).
88
This creates a new history entry, allowing navigation with the browser’s back and forward buttons.
9-
It works the same as [`hx-push-url` attribute](@/attributes/hx-push-url.md).
9+
This is similar to the [`hx-push-url` attribute](@/attributes/hx-push-url.md).
1010

1111
If present, this header overrides any behavior defined with attributes.
1212

1313
The possible values for this header are:
1414

1515
1. A URL to be pushed into the location bar.
1616
This may be relative or absolute, as per [`history.pushState()`](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState).
17-
2. `true`, which pushes the fetched URL into history and location bar.
18-
3. `false`, which prevents the browser’s history from being updated.
17+
2. `false`, which prevents the browser’s history from being updated.
1918

2019
## Notes
2120

www/content/headers/hx-replace-url.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ description = """\
77

88
The `HX-Replace-Url` header allows you to replace the current URL in the browser [location history](https://developer.mozilla.org/en-US/docs/Web/API/History_API).
99
This does not create a new history entry; in effect, it removes the previous current URL from the browser&rsquo;s history.
10-
It works the same as [`hx-replace-url` attribute](@/attributes/hx-replace-url.md).
10+
This is similar to the [`hx-replace-url` attribute](@/attributes/hx-replace-url.md).
1111

1212
If present, this header overrides any behavior defined with attributes.
1313

1414
The possible values for this header are:
1515

1616
1. A URL to replace the current URL in the location bar.
1717
This may be relative or absolute, as per [`history.replaceState()`](https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState), but must have the same origin as the current URL.
18-
2. `true`, which replaces the fetched URL into history and location bar.
19-
3. `false`, which prevents the browser’s current URL from being updated.
18+
2. `false`, which prevents the browser’s current URL from being updated.
2019

2120
## Notes
2221

0 commit comments

Comments
 (0)