| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
(() => { |
| 20 |
"use strict"; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
const MARK = "data-instant"; |
| 26 |
|
| 27 |
|
| 28 |
* The absolute instant a local wall-clock string names, as RFC 3339. |
| 29 |
* |
| 30 |
* `new Date(v)` reads a bare `YYYY-MM-DDTHH:mm` in the browser's own zone, |
| 31 |
* which is exactly the reading wanted. A value the browser cannot parse is |
| 32 |
* left alone rather than replaced with `Invalid Date`: an unparseable value |
| 33 |
* is a validation failure the route should see and answer, and rewriting it |
| 34 |
* here would turn a legible error into an unreadable one. |
| 35 |
|
| 36 |
const instant = (local) => { |
| 37 |
const at = new Date(local); |
| 38 |
return Number.isNaN(at.getTime()) ? null : at.toISOString(); |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
document.body.addEventListener("htmx:config:request", (event) => { |
| 50 |
const source = event.detail?.ctx?.sourceElement; |
| 51 |
const body = event.detail?.ctx?.request?.body; |
| 52 |
if (!source?.querySelectorAll || !body) return; |
| 53 |
|
| 54 |
const marked = |
| 55 |
source.matches?.(`[${MARK}]`) === true |
| 56 |
? [source] |
| 57 |
: source.querySelectorAll(`[${MARK}]`); |
| 58 |
for (const input of marked) { |
| 59 |
const name = input.getAttribute("name"); |
| 60 |
if (!name) continue; |
| 61 |
const local = body.get(name); |
| 62 |
if (typeof local !== "string" || !local) continue; |
| 63 |
const moment = instant(local); |
| 64 |
if (moment !== null) body.set(name, moment); |
| 65 |
} |
| 66 |
}); |
| 67 |
})(); |
| 68 |
|