From 14d582c0f50e90c4820b8a54a78e0655e386754e Mon Sep 17 00:00:00 2001 From: Ular Kimsanov Date: Wed, 25 Nov 2026 05:02:23 +0000 Subject: [PATCH] fix(studio): apply-to-scope writes only the grading attribute, exactly as given (#8767) * fix(studio): apply-to-scope writes only the grading attribute, exactly as given patchMediaTag passed the new attribute to String.prototype.replace as a replacement string, so `$&`, `$'` and similar in a grading value were expanded from the old tag and could split the attribute. Its match also needed a quoted value, so an unquoted or valueless attribute was duplicated instead of replaced. It now walks the tag's attributes the way the browser reads them and slices out the data-color-grading spans: the first gets the new value, any later duplicate is removed. When a grading is copied to other media, an unknown preset id, an unknown color space, or a LUT source with a scheme other than http, https or data is dropped instead of being written into other files. Variable references stay. * fix(studio): apply-to-scope keeps a known bare preset id The attribute's short form is a preset id alone, which core reads as a preset. It is now kept when it is a known id or a variable reference instead of being refused as unreadable. The quick-save concurrency test applied the filler preset warm, which is not a preset; it applies warm-daylight now. * fix(studio): grading attribute scan splits on HTML whitespace, appends after the last attribute The scan used JavaScript's \s, which also splits on a no-break space that HTML keeps inside an unquoted value, so clearing a grade could cut the source. It now separates on HTML whitespace only. A new attribute goes after the last attribute the scan read, so the slash that ends an unquoted value such as src=images/ stays part of it. --- packages/core/src/colorGrading.ts | 4 + .../editor/colorGradingScopePatch.test.ts | 179 +++++++++++++++++- .../editor/colorGradingScopePatch.ts | 90 ++++++++- .../studioColorGradingScope.test.ts | 51 +++++ .../src/components/studioColorGradingScope.ts | 18 +- .../src/hooks/timelineQuickSaves.test.ts | 4 +- 6 files changed, 330 insertions(+), 16 deletions(-) create mode 100644 packages/studio/src/components/studioColorGradingScope.test.ts diff --git a/packages/core/src/colorGrading.ts b/packages/core/src/colorGrading.ts index d3a08bc3bc..8a8dbd514f 100644 --- a/packages/core/src/colorGrading.ts +++ b/packages/core/src/colorGrading.ts @@ -646,6 +646,10 @@ const PRESETS_BY_ID = new Map( const VARIABLE_REF_RE = /^\$(?:\{([A-Za-z0-9_.:-]+)\}|([A-Za-z0-9_.:-]+))$/; +export function isHfColorGradingVariableRef(value: unknown): boolean { + return typeof value === "string" && VARIABLE_REF_RE.test(value.trim()); +} + const ADJUST_LIMITS: Record = { exposure: { min: -2, max: 2 }, contrast: { min: -1, max: 1 }, diff --git a/packages/studio/src/components/editor/colorGradingScopePatch.test.ts b/packages/studio/src/components/editor/colorGradingScopePatch.test.ts index 0684cef339..c9a5e852ec 100644 --- a/packages/studio/src/components/editor/colorGradingScopePatch.test.ts +++ b/packages/studio/src/components/editor/colorGradingScopePatch.test.ts @@ -1,5 +1,9 @@ +// @vitest-environment happy-dom import { describe, expect, it } from "vitest"; -import { patchMediaColorGradingInHtml } from "./colorGradingScopePatch"; +import { + keepKnownColorGradingValues, + patchMediaColorGradingInHtml, +} from "./colorGradingScopePatch"; describe("patchMediaColorGradingInHtml", () => { it.each(['"', "'"])("replaces and removes long grading values quoted with %s", (quote) => { @@ -92,3 +96,176 @@ describe("patchMediaColorGradingInHtml", () => { ); }); }); + +function readTag(html: string): { names: string[]; grading: string | null } { + const element = new DOMParser().parseFromString(html, "text/html").querySelector("video"); + if (!element) throw new Error(`no video in ${html}`); + return { + names: element.getAttributeNames(), + grading: element.getAttribute("data-color-grading"), + }; +} + +describe("patchMediaColorGradingInHtml with replacement patterns in the value", () => { + const patterns = [ + "$&", + "$'", + "$`", + "$1", + "$$", + "$", + "' data-extra='x' a='", + '" data-extra="x" a="', + ]; + it.each(['"', "'"])("writes the value as given into a tag quoted with %s", (quote) => { + const old = quote === '"' ? "{"preset":"old"}" : '{"preset":"old"}'; + const before = ``; + for (const pattern of patterns) { + const value = JSON.stringify({ preset: `a${pattern}b`, lut: { src: `${pattern}.cube` } }); + const { html, count } = patchMediaColorGradingInHtml(before, value); + expect(count).toBe(1); + expect(readTag(html)).toEqual({ + names: ["id", "data-color-grading", "muted"], + grading: value, + }); + } + }); +}); + +describe("patchMediaColorGradingInHtml with other ways to write the attribute", () => { + it.each([ + ["unquoted", "data-color-grading=clean-studio"], + ["with no value", "data-color-grading"], + ["with spaces around =", "data-color-grading = 'x'"], + ])("replaces an attribute written %s", (_, attribute) => { + const before = ``; + const value = '{"preset":"mono-clean"}'; + expect(readTag(patchMediaColorGradingInHtml(before, value).html)).toEqual({ + names: ["data-color-grading", "src"], + grading: value, + }); + expect(patchMediaColorGradingInHtml(before, null).html).toBe(``); + }); + + it.each([ + [ + "its own unquoted value holding =", + `