fix(core): keep the styles of a composition whose id contains $ patterns (#8141)

* test(core): a composition id with $ patterns scopes as text, not as a replace pattern

* fix(core): scoping writes a composition id's $ as text, at compile time and in the runtime script
This commit is contained in:
Miguel Ángel
2026-11-18 06:53:30 +00:00
committed by GitHub
parent c6e3f30bea
commit 12a2f83c49
4 changed files with 52 additions and 19 deletions
@@ -418,11 +418,14 @@ window.__timelines.main = gsap.timeline({ paused: true });</script><script type=
var __hfTimingSelectorPattern = `\\s*\\[\\s*data-(?:start|duration)\\s*=\\s*(?:"[^"]*"|'[^']*')\\s*\\]`;
var __hfAuthoredRootSelector = __hfAuthoredRootId ? "[" + __hfAuthoredRootAttr + '="' + __hfEscapeAttr(__hfAuthoredRootId) + '"]' : "";
var __hfAuthoredRootEntries = __hfAuthoredRootSelector ? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }] : [];
var __hfRootSelectorText = function() {
return __hfRootSelector;
};
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelector).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelector).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelector);
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelectorText).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelectorText).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};
@@ -966,11 +969,14 @@ window.__timelines.main = gsap.timeline({ paused: true });</script><script type=
var __hfTimingSelectorPattern = `\\s*\\[\\s*data-(?:start|duration)\\s*=\\s*(?:"[^"]*"|'[^']*')\\s*\\]`;
var __hfAuthoredRootSelector = __hfAuthoredRootId ? "[" + __hfAuthoredRootAttr + '="' + __hfEscapeAttr(__hfAuthoredRootId) + '"]' : "";
var __hfAuthoredRootEntries = __hfAuthoredRootSelector ? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }] : [];
var __hfRootSelectorText = function() {
return __hfRootSelector;
};
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelector).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelector).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelector);
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelectorText).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelectorText).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};
@@ -1514,11 +1520,14 @@ window.__timelines.main = gsap.timeline({ paused: true });</script><script type=
var __hfTimingSelectorPattern = `\\s*\\[\\s*data-(?:start|duration)\\s*=\\s*(?:"[^"]*"|'[^']*')\\s*\\]`;
var __hfAuthoredRootSelector = __hfAuthoredRootId ? "[" + __hfAuthoredRootAttr + '="' + __hfEscapeAttr(__hfAuthoredRootId) + '"]' : "";
var __hfAuthoredRootEntries = __hfAuthoredRootSelector ? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }] : [];
var __hfRootSelectorText = function() {
return __hfRootSelector;
};
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelector).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelector).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelector);
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelectorText).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelectorText).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};
@@ -445,11 +445,14 @@ window.__timelines.main = gsap.timeline({ paused: true });
var __hfTimingSelectorPattern = `\\s*\\[\\s*data-(?:start|duration)\\s*=\\s*(?:"[^"]*"|'[^']*')\\s*\\]`;
var __hfAuthoredRootSelector = __hfAuthoredRootId ? "[" + __hfAuthoredRootAttr + '="' + __hfEscapeAttr(__hfAuthoredRootId) + '"]' : "";
var __hfAuthoredRootEntries = __hfAuthoredRootSelector ? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }] : [];
var __hfRootSelectorText = function() {
return __hfRootSelector;
};
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelector).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelector).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelector);
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelectorText).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelectorText).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};
@@ -994,11 +997,14 @@ window.__timelines.main = gsap.timeline({ paused: true });
var __hfTimingSelectorPattern = `\\s*\\[\\s*data-(?:start|duration)\\s*=\\s*(?:"[^"]*"|'[^']*')\\s*\\]`;
var __hfAuthoredRootSelector = __hfAuthoredRootId ? "[" + __hfAuthoredRootAttr + '="' + __hfEscapeAttr(__hfAuthoredRootId) + '"]' : "";
var __hfAuthoredRootEntries = __hfAuthoredRootSelector ? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }] : [];
var __hfRootSelectorText = function() {
return __hfRootSelector;
};
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelector).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelector).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelector);
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelectorText).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelectorText).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};
@@ -1543,11 +1549,14 @@ window.__timelines.main = gsap.timeline({ paused: true });
var __hfTimingSelectorPattern = `\\s*\\[\\s*data-(?:start|duration)\\s*=\\s*(?:"[^"]*"|'[^']*')\\s*\\]`;
var __hfAuthoredRootSelector = __hfAuthoredRootId ? "[" + __hfAuthoredRootAttr + '="' + __hfEscapeAttr(__hfAuthoredRootId) + '"]' : "";
var __hfAuthoredRootEntries = __hfAuthoredRootSelector ? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }] : [];
var __hfRootSelectorText = function() {
return __hfRootSelector;
};
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelector).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelector).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelector);
var normalized = selector.replace(new RegExp(__hfRootSelectorPattern + "(?:" + __hfTimingSelectorPattern + ")+", "g"), __hfRootSelectorText).replace(new RegExp("(?:" + __hfTimingSelectorPattern + ")+" + __hfRootSelectorPattern, "g"), __hfRootSelectorText).replace(new RegExp(__hfRootSelectorPattern, "g"), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, "g"), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};
@@ -1604,3 +1604,17 @@ describe("wrapScopedCompositionScript — authored source delimiters", () => {
expect(wrapped.replace(authored, "")).toContain("__timelines");
});
});
// fails without the fix: the scope went to String.replace as the replacement, so $' and $` in an id expanded.
it.each(["a$'b", "a$`b", "a$&b"])(
"scopes the id %s exactly as a plain id, its $ kept as text",
(id) => {
const css = (compId: string) =>
`[data-composition-id="${compId}"] .title { opacity: 0; }\n` +
`[data-composition-id="${compId}"][data-start="0"] { opacity: 1; }\n` +
`[data-duration="2"][data-composition-id="${compId}"] .card { color: red; }`;
const plain = scopeCssToComposition(css("aZZb"), "aZZb");
expect(scopeCssToComposition(css(id), id)).toBe(plain.split("aZZb").join(id));
},
);
@@ -134,7 +134,7 @@ function scopeSelectorOnce(
// sits nested inside the host and would inherit the effect twice.
return compositionBoxSelector(scope);
}
return selectorWithoutRootTiming.replace(compositionIdPattern, scope);
return selectorWithoutRootTiming.replace(compositionIdPattern, () => scope);
}
const leading = selectorWithoutRootTiming.match(/^\s*/)?.[0] ?? "";
const trailing = selectorWithoutRootTiming.match(/\s*$/)?.[0] ?? "";
@@ -159,8 +159,8 @@ function normalizeCompositionRootSelector(
const compAttr = String.raw`\[\s*data-composition-id\s*=\s*(?:"${quotedCompId}"|'${quotedCompId}')\s*\]`;
const timingAttr = String.raw`\s*\[\s*data-(?:start|duration)\s*=\s*(?:"[^"]*"|'[^']*')\s*\]`;
return selector
.replace(new RegExp(`${compAttr}(?:${timingAttr})+`, "g"), scope)
.replace(new RegExp(`(?:${timingAttr})+${compAttr}`, "g"), scope);
.replace(new RegExp(`${compAttr}(?:${timingAttr})+`, "g"), () => scope)
.replace(new RegExp(`(?:${timingAttr})+${compAttr}`, "g"), () => scope);
}
const GLOBAL_AT_RULES = new Set(["keyframes", "-webkit-keyframes", "font-face"]);
@@ -523,14 +523,15 @@ export function wrapScopedCompositionScript(
var __hfAuthoredRootEntries = __hfAuthoredRootSelector
? [{ id: __hfAuthoredRootId, replacement: __hfAuthoredRootSelector }]
: [];
var __hfRootSelectorText = function() { return __hfRootSelector; };
var __hfNormalizeSelector = function(selector) {
if (!__hfCompId || typeof selector !== "string") return selector;
var normalized = selector
.replace(new RegExp(__hfRootSelectorPattern + '(?:' + __hfTimingSelectorPattern + ')+', 'g'), __hfRootSelector)
.replace(new RegExp('(?:' + __hfTimingSelectorPattern + ')+' + __hfRootSelectorPattern, 'g'), __hfRootSelector)
.replace(new RegExp(__hfRootSelectorPattern, 'g'), __hfRootSelector);
.replace(new RegExp(__hfRootSelectorPattern + '(?:' + __hfTimingSelectorPattern + ')+', 'g'), __hfRootSelectorText)
.replace(new RegExp('(?:' + __hfTimingSelectorPattern + ')+' + __hfRootSelectorPattern, 'g'), __hfRootSelectorText)
.replace(new RegExp(__hfRootSelectorPattern, 'g'), __hfRootSelectorText);
if (__hfTimelineCompId !== __hfCompId) {
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, 'g'), __hfRootSelector);
normalized = normalized.replace(new RegExp(__hfTimelineRootSelectorPattern, 'g'), __hfRootSelectorText);
}
return window.__hfSvgSelectorAliases.rewrite(normalized, __hfAuthoredRootEntries);
};