diff options
author | Thomas Cowart <tncowart@gmail.com> | 2023-12-20 17:46:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-20 15:46:04 -0700 |
commit | 078d5da5b4425bb779aedaad079b0643a24754cd (patch) | |
tree | 3b8bc9d3c94ca6eec3b264b459fbde9aa81a75be /src/htmx.js | |
parent | 1f4903c213cc37f4d995c0585c2c01c850b0a5e9 (diff) | |
download | htmx-078d5da5b4425bb779aedaad079b0643a24754cd.tar.gz htmx-078d5da5b4425bb779aedaad079b0643a24754cd.zip |
Update parseInterval to handle "0" correctly (#1835)
* Update parseInterval to handle "0" correctly
When a parameter like "0ms" is passed in to parseInterval it gets parsed to 0.
Previously this would result in a return value of "undefined" because 0 is falsy
and thus the `return 0 || undefined` statements return undefined.
The purpose of the form `parseFloat(str) || undefined` was to return "undefined" if
parseFloat failed (parseFloat returns NaN, a falsy value, if it can't parse its
argument). Unfortunately, as mentioned, parseFloat can also succeed and return a
falsy value -- when the argument is "0" (or "0.0", etc.). So the new code, rather
than depending on the falsiness of the result of parseFloat, explicitly checks for
a NaN.
* Adds some semicolons
Adds some semicolons to parseInterval (and tests) for consistency.
* Add one more parseInterval test for "0"
Adds test test to make sure parseInterval works on "0".
* Adds functional tests for every, swap, settle, throttle, and delay
* Explcitly check that setTimeout values are > 0
These values come from user settings that are read from parseInterval,
so they could be a number or undefined.
If the value being checked is > 0 setTimeout will be called with some
associated function. If the value is 0 or 'undefined' the associated function
will be called immediately ('undefined' is not greater than 0).
* Change '!== undefined' to '> 0'
`pollInterval !== undefined` is a subtly different conditional than just `pollInterval` or `pollInterval > 0` (which are equivalent). Changes the conditional to `pollInterval > 0` so as to not change the behavior but also be more explicit in the test.
Diffstat (limited to 'src/htmx.js')
-rw-r--r-- | src/htmx.js | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/src/htmx.js b/src/htmx.js index 2bb58239..7861ae84 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -148,18 +148,20 @@ return (function () { function parseInterval(str) { if (str == undefined) { - return undefined + return undefined; } + + let interval = NaN; if (str.slice(-2) == "ms") { - return parseFloat(str.slice(0,-2)) || undefined - } - if (str.slice(-1) == "s") { - return (parseFloat(str.slice(0,-1)) * 1000) || undefined - } - if (str.slice(-1) == "m") { - return (parseFloat(str.slice(0,-1)) * 1000 * 60) || undefined + interval = parseFloat(str.slice(0, -2)); + } else if (str.slice(-1) == "s") { + interval = parseFloat(str.slice(0, -1)) * 1000; + } else if (str.slice(-1) == "m") { + interval = parseFloat(str.slice(0, -1)) * 1000 * 60; + } else { + interval = parseFloat(str); } - return parseFloat(str) || undefined + return isNaN(interval) ? undefined : interval; } /** @@ -1538,14 +1540,14 @@ return (function () { return; } - if (triggerSpec.throttle) { + if (triggerSpec.throttle > 0) { if (!elementData.throttle) { handler(elt, evt); elementData.throttle = setTimeout(function () { elementData.throttle = null; }, triggerSpec.throttle); } - } else if (triggerSpec.delay) { + } else if (triggerSpec.delay > 0) { elementData.delayed = setTimeout(function() { handler(elt, evt) }, triggerSpec.delay); } else { triggerEvent(elt, 'htmx:trigger') @@ -1822,7 +1824,7 @@ return (function () { handler(elt); } } - if (delay) { + if (delay > 0) { setTimeout(load, delay); } else { load(); @@ -1881,7 +1883,7 @@ return (function () { if (!maybeFilterEvent(triggerSpec, elt, makeEvent("load", {elt: elt}))) { loadImmediately(elt, handler, nodeData, triggerSpec.delay); } - } else if (triggerSpec.pollInterval) { + } else if (triggerSpec.pollInterval > 0) { nodeData.polling = true; processPolling(elt, handler, triggerSpec); } else { |