(html: string, clamps: ResolvedDuration[])
| 265 | * and recomputes data-end accordingly. |
| 266 | */ |
| 267 | export function clampDurations(html: string, clamps: ResolvedDuration[]): string { |
| 268 | for (const { id, duration } of clamps) { |
| 269 | const idPattern = new RegExp(`(<[^>]*id=["']${escapeRegex(id)}["'][^>]*>)`, "gi"); |
| 270 | |
| 271 | html = html.replace(idPattern, (tag) => { |
| 272 | // Replace data-duration value |
| 273 | tag = tag.replace(/data-duration=["'][^"']*["']/, `data-duration="${duration}"`); |
| 274 | |
| 275 | // Recompute data-end from data-start + clamped duration |
| 276 | const startStr = getAttr(tag, "data-start"); |
| 277 | const start = startStr ? parseFloat(startStr) : 0; |
| 278 | tag = tag.replace(/data-end=["'][^"']*["']/, `data-end="${start + duration}"`); |
| 279 | |
| 280 | return tag; |
| 281 | }); |
| 282 | } |
| 283 | |
| 284 | return html; |
| 285 | } |
no test coverage detected