(html: string, resolutions: ResolvedDuration[])
| 193 | * GSAP timeline queries. |
| 194 | */ |
| 195 | export function injectDurations(html: string, resolutions: ResolvedDuration[]): string { |
| 196 | for (const { id, duration } of resolutions) { |
| 197 | // Match the element's opening tag by id |
| 198 | const idPattern = new RegExp(`(<[^>]*id=["']${escapeRegex(id)}["'][^>]*>)`, "gi"); |
| 199 | |
| 200 | html = html.replace(idPattern, (tag) => { |
| 201 | let result = tag; |
| 202 | |
| 203 | // Add data-duration if missing |
| 204 | if (!hasAttr(result, "data-duration")) { |
| 205 | result = injectAttr(result, "data-duration", String(duration)); |
| 206 | } |
| 207 | |
| 208 | // Add data-end if missing |
| 209 | if (!hasAttr(result, "data-end")) { |
| 210 | const startStr = getAttr(result, "data-start"); |
| 211 | const start = startStr ? parseFloat(startStr) : 0; |
| 212 | result = injectAttr(result, "data-end", String(start + duration)); |
| 213 | } |
| 214 | |
| 215 | return result; |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | return html; |
| 220 | } |
| 221 | |
| 222 | function escapeRegex(str: string): string { |
| 223 | return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
no test coverage detected