@hyperframes/core 0.9.82 → 0.9.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -81,17 +81,28 @@ function replaceOpeningTags(html, names, replace) {
81
81
  parts.push(html.slice(cursor));
82
82
  return parts.join("");
83
83
  }
84
- function replaceIdTags(html, id, replace) {
84
+ /** Applies each tag's durations, in the order given, in one scan of the page however many there are. */
85
+ function applyDurationsById(html, resolutions, apply) {
86
+ const durationsById = new Map();
87
+ for (const { id, duration } of resolutions) {
88
+ const durations = durationsById.get(id);
89
+ if (durations)
90
+ durations.push(duration);
91
+ else
92
+ durationsById.set(id, [duration]);
93
+ }
85
94
  const parts = [];
86
95
  let cursor = 0;
87
96
  for (const span of scanHtmlOpeningTags(html)) {
88
97
  if (!span.closed)
89
98
  continue;
90
- const tag = html.slice(span.start, span.end);
91
99
  const attr = span.attributes.find((attr) => attr.name === "id");
92
- if (attr?.kind !== "value" || decodeAuthoredAttribute(attr.value) !== id)
100
+ if (attr?.kind !== "value")
101
+ continue;
102
+ const durations = durationsById.get(decodeAuthoredAttribute(attr.value));
103
+ if (!durations)
93
104
  continue;
94
- parts.push(html.slice(cursor, span.start), replace(tag));
105
+ parts.push(html.slice(cursor, span.start), durations.reduce(apply, html.slice(span.start, span.end)));
95
106
  cursor = span.end;
96
107
  }
97
108
  parts.push(html.slice(cursor));
@@ -209,25 +220,21 @@ export function compileTimingAttrs(html) {
209
220
  * GSAP timeline queries.
210
221
  */
211
222
  export function injectDurations(html, resolutions) {
212
- for (const { id, duration } of resolutions) {
213
- // Match the element's opening tag by id
214
- html = replaceIdTags(html, id, (tag) => {
215
- let result = tag;
216
- // Add data-duration if missing
217
- if (parseStrictFiniteTimingNumber(getAttr(result, "data-duration")) == null) {
218
- result = setAttr(result, "data-duration", String(duration));
219
- }
220
- // Add data-end if missing. Skip relative id-refs.
221
- if (!hasAttr(result, "data-end")) {
222
- const start = parseNumeric(getAttr(result, "data-start"));
223
- if (start != null) {
224
- result = injectAttr(result, "data-end", String(start + duration));
225
- }
223
+ return applyDurationsById(html, resolutions, (tag, duration) => {
224
+ let result = tag;
225
+ // Add data-duration if missing
226
+ if (parseStrictFiniteTimingNumber(getAttr(result, "data-duration")) == null) {
227
+ result = setAttr(result, "data-duration", String(duration));
228
+ }
229
+ // Add data-end if missing. Skip relative id-refs.
230
+ if (!hasAttr(result, "data-end")) {
231
+ const start = parseNumeric(getAttr(result, "data-start"));
232
+ if (start != null) {
233
+ result = injectAttr(result, "data-end", String(start + duration));
226
234
  }
227
- return result;
228
- });
229
- }
230
- return html;
235
+ }
236
+ return result;
237
+ });
231
238
  }
232
239
  /**
233
240
  * Extract video/audio elements that already have data-duration set.
@@ -265,17 +272,14 @@ export function extractResolvedMedia(html) {
265
272
  * and recomputes data-end accordingly.
266
273
  */
267
274
  export function clampDurations(html, clamps) {
268
- for (const { id, duration } of clamps) {
269
- html = replaceIdTags(html, id, (tag) => {
270
- // Replace data-duration value
271
- if (hasAttr(tag, "data-duration"))
272
- tag = setAttr(tag, "data-duration", String(duration));
273
- const start = parseNumeric(getAttr(tag, "data-start"));
274
- if (start != null && hasAttr(tag, "data-end")) {
275
- tag = setAttr(tag, "data-end", String(start + duration));
276
- }
277
- return tag;
278
- });
279
- }
280
- return html;
275
+ return applyDurationsById(html, clamps, (tag, duration) => {
276
+ // Replace data-duration value
277
+ if (hasAttr(tag, "data-duration"))
278
+ tag = setAttr(tag, "data-duration", String(duration));
279
+ const start = parseNumeric(getAttr(tag, "data-start"));
280
+ if (start != null && hasAttr(tag, "data-end")) {
281
+ tag = setAttr(tag, "data-end", String(start + duration));
282
+ }
283
+ return tag;
284
+ });
281
285
  }