Skip to content

Commit

Permalink
support ability to ignore new title tags via config, event and `hx-…
Browse files Browse the repository at this point in the history
…swap`
  • Loading branch information
1cg committed Aug 31, 2023
1 parent ff20f88 commit c230931
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,10 @@ return (function () {
if (modifier.indexOf("transition:") === 0) {
swapSpec["transition"] = modifier.substr(11) === "true";
}
if (modifier.indexOf("ignoreTitle:") === 0) {
console.log("here");

This comment has been minimized.

Copy link
@Telroshan

Telroshan Aug 31, 2023

Collaborator

I don't want to be annoying but do we have to keep those 3 debug console logs ? (see here1 and here2 below)

This comment has been minimized.

Copy link
@1cg

1cg via email Aug 31, 2023

Author Contributor
swapSpec["ignoreTitle"] = modifier.substr(12) === "true";
}
if (modifier.indexOf("scroll:") === 0) {
var scrollSpec = modifier.substr(7);
var splitSpec = scrollSpec.split(":");
Expand Down Expand Up @@ -3284,6 +3288,7 @@ return (function () {
var xhr = responseInfo.xhr;
var target = responseInfo.target;
var etc = responseInfo.etc;
var requestConfig = responseInfo.requestConfig;

if (!triggerEvent(elt, 'htmx:beforeOnLoad', responseInfo)) return;

Expand Down Expand Up @@ -3332,12 +3337,14 @@ return (function () {
var shouldSwap = xhr.status >= 200 && xhr.status < 400 && xhr.status !== 204;
var serverResponse = xhr.response;
var isError = xhr.status >= 400;
var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse, isError:isError}, responseInfo);
var ignoreTitle = htmx.config.ignoreTitle
var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse, isError:isError, ignoreTitle:ignoreTitle }, responseInfo);
if (!triggerEvent(target, 'htmx:beforeSwap', beforeSwapDetails)) return;

target = beforeSwapDetails.target; // allow re-targeting
serverResponse = beforeSwapDetails.serverResponse; // allow updating content
isError = beforeSwapDetails.isError; // allow updating error
ignoreTitle = beforeSwapDetails.ignoreTitle; // allow updating ignoring title

responseInfo.target = target; // Make updated target available to response events
responseInfo.failed = isError; // Make failed property available to response events
Expand All @@ -3363,6 +3370,10 @@ return (function () {
}
var swapSpec = getSwapSpecification(elt, swapOverride);

if (swapSpec.hasOwnProperty('ignoreTitle')) {
ignoreTitle = swapSpec.ignoreTitle;
}

target.classList.add(htmx.config.swappingClass);

// optional transition API promise callbacks
Expand Down Expand Up @@ -3456,7 +3467,9 @@ return (function () {
}
}

if(settleInfo.title) {
console.log("here1", ignoreTitle)
if(settleInfo.title && !ignoreTitle) {
console.log("here2")
var titleElt = find("title");
if(titleElt) {
titleElt.innerHTML = settleInfo.title;
Expand Down
12 changes: 12 additions & 0 deletions test/attributes/hx-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,17 @@ describe("hx-swap attribute", function(){
}
});

it('hx-swap ignoreTitle works', function()
{
window.document.title = "Test Title";
this.server.respondWith("GET", "/test", function (xhr) {
xhr.respond(200, {}, "<title class=''>htmx rocks!</title>Clicked!");
});
var btn = make('<button hx-get="/test" hx-swap="innerHTML ignoreTitle:true">Click Me!</button>')
btn.click();
this.server.respond();
btn.innerText.should.equal("Clicked!");
window.document.title.should.equal("Test Title");
});

})
5 changes: 5 additions & 0 deletions www/content/attributes/hx-swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ modifier:

These attributes can be used to synchronize htmx with the timing of CSS transition effects.

#### Title: `ignoreTitle`

By default, htmx will update the title of the page if it finds a `<title>` tag in the response content. You can turn
off this behavior by setting the `ignoreTitle` option to true.

#### Scrolling: `scroll` & `show`

You can also change the scrolling behavior of the target element by using the `scroll` and `show` modifiers, both
Expand Down
29 changes: 27 additions & 2 deletions www/content/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,31 @@ View Transitions can be configured using CSS, as outlined in [the Chrome documen

You can see a view transition example on the [Animation Examples](/examples/animations#view-transitions) page.

#### Swap Options

The [hx-swap](@/attributes/hx-swap.md) attribute supports many options for tuning the swapping behavior of htmx. For
example, by default htmx will swap in the title of a title tag found anywhere in the new content. You can turn this
behavior off by setting the `ignoreTitle` modifier to true:

```html
<button hx-post="/like" hx-swap="outerHTML ignoreTitle:true">Like</button>
```

The modifiers available on `hx-swap` are:

| Option | Description |
|---------------|----------------------------------------------------------------------------------------------------------|
| `transition` | `true` or `false`, whether to use the view transition API for this swap |
| `swap` | The swap delay to use (e.g. `100ms`) between when old content is cleared and the new content is inserted |
| `settle` | The settle delay to use (e.g. `100ms`) between when new content is inserted and when it is settled |
| `ignoreTitle` | If set to `true`, any title found in the new content will be ignored and not update the docuemnt title |
| `scroll` | `top` or `bottom`, will scroll the target element to its top or bottom |
| `show` | `top` or `bottom`, will scroll the target elements top or bottom into view |

All swap modifiers appear after the swap style is specified, and are colon-separated.

See the [hx-swap](@/attributes/hx-swap.md) documentation for more details on these options.

### Synchronization {#synchronization}

Often you want to coordinate the requests between two elements. For example, you may want a request from one element
Expand Down Expand Up @@ -724,8 +749,7 @@ However, you could wrap the htmx-enhanced input in a form element:
hx-post="/search"
hx-trigger="keyup changed delay:500ms, search"
hx-target="#search-results"
hx-indicator=".htmx-indicator"
>
hx-indicator=".htmx-indicator">
</form>
```

Expand Down Expand Up @@ -1608,6 +1632,7 @@ listed below:
| `htmx.config.globalViewTransitions` | if set to `true`, htmx will use the [View Transition](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) API when swapping in new content. |
| `htmx.config.methodsThatUseUrlParams` | defaults to `["get"]`, htmx will format requests with this method by encoding their parameters in the URL, not the request body |
| `htmx.config.selfRequestsOnly` | defaults to `false`, if set to `true` will only allow AJAX requests to the same domain as the current document |
| `htmx.config.ignoreTitle` | defaults to `false`, if set to `true` htmx will not update the title of the document when a `title` tag is found in new content |

</div>

Expand Down
1 change: 1 addition & 0 deletions www/content/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ the documentation on [configuring swapping](@/docs.md#modifying_swapping_behavio
* `detail.xhr` - the `XMLHttpRequest`
* `detail.requestConfig` - the configuration of the AJAX request
* `detail.shouldSwap` - if the content will be swapped (defaults to `false` for non-200 response codes)
* `detail.ignoreTitle` - if `true` any title tag in the response will be ignored
* `detail.target` - the target of the swap

### Event - `htmx:beforeTransition` {#htmx:beforeTransition}
Expand Down

0 comments on commit c230931

Please sign in to comment.