MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / patchAttributeByTarget

Function patchAttributeByTarget

packages/studio/src/utils/sourcePatcher.ts:309–338  ·  view source on GitHub ↗
(
  html: string,
  target: PatchTarget,
  attr: string,
  value: string | null,
)

Source from the content-addressed store, hash-verified

307}
308
309function patchAttributeByTarget(
310 html: string,
311 target: PatchTarget,
312 attr: string,
313 value: string | null,
314): string {
315 const match = findTagByTarget(html, target);
316 if (!match) return html;
317
318 const fullAttr = attr.startsWith("data-") ? attr : `data-${attr}`;
319 const attrPattern = new RegExp(`\\b${escapeRegex(fullAttr)}=(["'])([^"']*)\\1`);
320 const tag = match.tag;
321
322 if (value === null) {
323 // Remove the attribute if present
324 if (!attrPattern.test(tag)) return html;
325 const removePattern = new RegExp(`\\s+${escapeRegex(fullAttr)}=(["'])[^"']*\\1`);
326 const newTag = tag.replace(removePattern, "");
327 return replaceTagAtMatch(html, match, newTag);
328 }
329
330 const escaped = escapeHtmlAttribute(value);
331 if (attrPattern.test(tag)) {
332 const newTag = tag.replace(attrPattern, `${fullAttr}="${escaped}"`);
333 return replaceTagAtMatch(html, match, newTag);
334 }
335
336 const newTag = tag + ` ${fullAttr}="${escaped}"`;
337 return replaceTagAtMatch(html, match, newTag);
338}
339
340/**
341 * Apply an attribute change to an element in the HTML source.

Callers 1

applyPatchByTargetFunction · 0.85

Calls 4

findTagByTargetFunction · 0.85
replaceTagAtMatchFunction · 0.85
escapeHtmlAttributeFunction · 0.85
escapeRegexFunction · 0.70

Tested by

no test coverage detected