Skip to content

Commit 5b550e5

Browse files
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
1 parent b98e4f2 commit 5b550e5

File tree

2 files changed

+3
-15
lines changed

2 files changed

+3
-15
lines changed

src/htmx.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,10 @@ var htmx = (function() {
337337
return '[hx-' + verb + '], [data-hx-' + verb + ']'
338338
}).join(', ')
339339

340-
const HEAD_TAG_REGEX = makeTagRegEx('head')
341-
342340
//= ===================================================================
343341
// Utilities
344342
//= ===================================================================
345343

346-
/**
347-
* @param {string} tag
348-
* @param {boolean} global
349-
* @returns {RegExp}
350-
*/
351-
function makeTagRegEx(tag, global = false) {
352-
return new RegExp(`<${tag}(\\s[^>]*>|>)([\\s\\S]*?)<\\/${tag}>`,
353-
global ? 'gim' : 'im')
354-
}
355-
356344
/**
357345
* Parses an interval string consistent with the way htmx does. Useful for plugins that have timing-related attributes.
358346
*
@@ -595,7 +583,7 @@ var htmx = (function() {
595583
*/
596584
function makeFragment(response) {
597585
// strip head tag to determine shape of response we are dealing with
598-
const responseWithNoHead = response.replace(HEAD_TAG_REGEX, '')
586+
const responseWithNoHead = response.replace(/<head(\s[^>]*)?>.*?<\/head>/is, '')
599587
const startTag = getStartTag(responseWithNoHead)
600588
/** @type DocumentFragmentWithTitle */
601589
let fragment

test/manual/hxboost_partial_template_parsing/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</style>
2020

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

0 commit comments

Comments
 (0)