Optimize Head regex (#2781)

* 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
This commit is contained in:
MichaelWest22 2024-10-03 14:20:23 +13:00 committed by GitHub
parent b98e4f2b12
commit 5b550e5c49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 15 deletions

View File

@ -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.
*
@ -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

View File

@ -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-head-support@2.0.0/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">
@ -38,4 +38,4 @@
</ul>
</main>
</body>
</html>
</html>