Skip to content

Commit

Permalink
Optimize Head regex (bigskysoftware#2781)
Browse files Browse the repository at this point in the history
* remove shared tag regex utility function that is no longer really needed

* fix head-support manual test to point to externally hosted extension

* minimize regex
  • Loading branch information
MichaelWest22 authored Oct 3, 2024
1 parent b98e4f2 commit 5b550e5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
14 changes: 1 addition & 13 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,10 @@ var htmx = (function() {
return '[hx-' + verb + '], [data-hx-' + verb + ']'
}).join(', ')

const HEAD_TAG_REGEX = makeTagRegEx('head')

//= ===================================================================
// Utilities
//= ===================================================================

/**
* @param {string} tag
* @param {boolean} global
* @returns {RegExp}
*/
function makeTagRegEx(tag, global = false) {
return new RegExp(`<${tag}(\\s[^>]*>|>)([\\s\\S]*?)<\\/${tag}>`,
global ? 'gim' : 'im')
}

/**
* Parses an interval string consistent with the way htmx does. Useful for plugins that have timing-related attributes.
*
Expand Down Expand Up @@ -595,7 +583,7 @@ var htmx = (function() {
*/
function makeFragment(response) {
// strip head tag to determine shape of response we are dealing with
const responseWithNoHead = response.replace(HEAD_TAG_REGEX, '')
const responseWithNoHead = response.replace(/<head(\s[^>]*)?>.*?<\/head>/is, '')
const startTag = getStartTag(responseWithNoHead)
/** @type DocumentFragmentWithTitle */
let fragment
Expand Down
4 changes: 2 additions & 2 deletions test/manual/hxboost_partial_template_parsing/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</style>

<script src="../../../src/htmx.js" hx-preserve="true"></script>
<script src="../../../src/ext/head-support.js" hx-preserve="true"></script>
<script src="https://unpkg.com/htmx-ext[email protected]/head-support.js" hx-preserve="true"></script>
</head>
<body hx-ext="head-support" hx-boost="true">
<header hx-push-url="false" hx-target="main" hx-swap="innerHTML">
Expand All @@ -38,4 +38,4 @@ <h1># Index</h1>
</ul>
</main>
</body>
</html>
</html>

0 comments on commit 5b550e5

Please sign in to comment.