Skip to content

Commit 7d7003a

Browse files
authored
fix(studio): match style attributes with explicit quote boundaries (#3712)
* fix(studio): match style attributes with explicit quote boundaries * fix(studio): apply quote boundaries to active source writers
1 parent e9250fc commit 7d7003a

6 files changed

Lines changed: 86 additions & 8 deletions

File tree

‎packages/studio/src/components/editor/colorGradingScopePatch.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ import { describe, expect, it } from "vitest";
22
import { patchMediaColorGradingInHtml } from "./colorGradingScopePatch";
33

44
describe("patchMediaColorGradingInHtml", () => {
5+
it.each(['"', "'"])("replaces and removes long grading values quoted with %s", (quote) => {
6+
const otherQuote = quote === '"' ? "'" : '"';
7+
for (const value of [
8+
"",
9+
"line1\nline2",
10+
"a".repeat(100_000),
11+
`data-color-grading=${otherQuote}a`.repeat(10_000),
12+
]) {
13+
const html = `<img alt="kept"\tDATA-COLOR-GRADING=${quote}${value}${quote} />`;
14+
expect(patchMediaColorGradingInHtml(html, "new")).toEqual({
15+
html: '<img alt="kept" data-color-grading="new" />',
16+
count: 1,
17+
});
18+
expect(patchMediaColorGradingInHtml(html, null)).toEqual({
19+
html: '<img alt="kept" />',
20+
count: 1,
21+
});
22+
}
23+
});
24+
525
it("adds color grading to video and image tags only", () => {
626
const { html, count } = patchMediaColorGradingInHtml(
727
`<div><video id="v"></video><img id="i" /><audio id="a"></audio></div>`,

‎packages/studio/src/components/editor/colorGradingScopePatch.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const MEDIA_TAG_RE = /<\s*(video|img)\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi;
2-
const COLOR_GRADING_ATTR_RE = /\sdata-color-grading=(["'])([\s\S]*?)\1/i;
2+
const COLOR_GRADING_ATTR_RE = /\sdata-color-grading=(?:"[^"]*"|'[^']*')/i;
33
const IGNORED_HTML_RANGE_RE = /<!--[\s\S]*?-->|<(script|style)\b[\s\S]*?<\/\1\s*>/gi;
44

55
interface TextRange {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from "vitest";
2+
import { mergeStyleIntoTag } from "./htmlEditor";
3+
4+
describe("mergeStyleIntoTag", () => {
5+
it.each([
6+
['<div style="color: red; opacity: 1">', '<div style="color: red; opacity: 0.5">'],
7+
["<div style='color: red; opacity: 1'>", "<div style='color: red; opacity: 0.5'>"],
8+
['<div style="">', '<div style="opacity: 0.5">'],
9+
["<div style=''>", "<div style='opacity: 0.5'>"],
10+
['<div style="color:\nred">', '<div style="color: red; opacity: 0.5">'],
11+
["<div style='font-family: \"a\"'>", "<div style='font-family: \"a\"; opacity: 0.5'>"],
12+
["<div style=\"font-family: 'a'\">", "<div style=\"font-family: 'a'; opacity: 0.5\">"],
13+
[
14+
"<div style=\"color:red\" style='color:blue'>",
15+
"<div style=\"color: red; opacity: 0.5\" style='color:blue'>",
16+
],
17+
['<div STYLE="color:red">', '<div STYLE="color:red" style="opacity: 0.5">'],
18+
['<div style = "color:red">', '<div style = "color:red" style="opacity: 0.5">'],
19+
['<div data-style="color:red">', '<div data-style="color: red; opacity: 0.5">'],
20+
["<img/>", '<img style="opacity: 0.5"/>'],
21+
['<div style="color:red>', '<div style="color:red style="opacity: 0.5">'],
22+
])("preserves quote and source behavior (case %#)", (tag, expected) => {
23+
expect(mergeStyleIntoTag(tag, "opacity: 0.5")).toBe(expected);
24+
expect(mergeStyleIntoTag(tag, " \n")).toBe(tag);
25+
});
26+
27+
it.each(['"', "'"])("handles long values delimited by %s", (quote) => {
28+
const value = "a".repeat(100_000);
29+
const tag = `<div style=${quote}--label:${value}${quote}>`;
30+
expect(mergeStyleIntoTag(tag, "opacity: 0.5")).toBe(
31+
`<div style=${quote}--label: ${value}; opacity: 0.5${quote}>`,
32+
);
33+
const opener = `style=${quote}a`;
34+
expect(mergeStyleIntoTag(`<div style=${quote}${opener.repeat(10_000)}>`, "opacity: 0.5")).toBe(
35+
`<div style=${quote}opacity: 0.5${quote}a${opener.repeat(9_999)}>`,
36+
);
37+
const unterminated = `<div style=${quote}${value}>`;
38+
expect(mergeStyleIntoTag(unterminated, "opacity: 0.5")).toBe(
39+
`<div style=${quote}${value} style="opacity: 0.5">`,
40+
);
41+
});
42+
});

‎packages/studio/src/utils/htmlEditor.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export function mergeStyleIntoTag(tag: string, newStyles: string): string {
2929

3030
const incoming = parseStyleString(newStyles);
3131

32-
// Match style="..." or style='...' — handle multi-line attrs via dotall-like trick
33-
const styleAttrRe = /style=(["'])([\s\S]*?)\1/;
32+
// Match each quote-delimited form explicitly, including multi-line values.
33+
const styleAttrRe = /style=(")([^"]*)"|style=(')([^']*)'/;
3434
const match = tag.match(styleAttrRe);
3535

3636
if (match) {
37-
const quote = match[1];
38-
const existing = parseStyleString(match[2]);
37+
const quote = match[1] ?? match[3];
38+
const existing = parseStyleString(match[2] ?? match[4]);
3939
const merged = { ...existing, ...incoming };
4040
const serialized = Object.entries(merged)
4141
.map(([k, v]) => `${k}: ${v}`)

‎packages/studio/src/utils/sourcePatcher.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ import {
88
} from "./sourcePatcher";
99

1010
describe("applyPatchByTarget", () => {
11+
it.each(['"', "'"])("patches long and multiline styles quoted with %s", (quote) => {
12+
const otherQuote = quote === '"' ? "'" : '"';
13+
const op: PatchOperation = { type: "inline-style", property: "opacity", value: "0.5" };
14+
for (const value of [
15+
"",
16+
"line1\nline2",
17+
"a".repeat(100_000),
18+
`style=${otherQuote}a`.repeat(10_000),
19+
]) {
20+
const html = `<img id="hero" class="hero" style=${quote}--label:${value}${quote} />`;
21+
const expected = `<img id="hero" class="hero" style=${quote}--label: ${value}; opacity: 0.5${quote} />`;
22+
expect(applyPatch(html, "hero", op)).toBe(expected);
23+
expect(applyPatchByTarget(html, { selector: ".hero" }, op)).toBe(expected);
24+
}
25+
});
26+
1127
it("updates a composition host by data-composition-id selector", () => {
1228
const html = `<div data-composition-id="intro" data-start="0" data-track-index="1"></div>`;
1329
const op: PatchOperation = { type: "attribute", property: "start", value: "2.5" };

‎packages/studio/src/utils/sourcePatcher.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ function patchInlineStyleInTag(
184184
if (!tag) return html;
185185

186186
// Check if there's an existing style attribute
187-
const styleMatch = /\bstyle=(["'])([\s\S]*?)\1/.exec(tag);
187+
const styleMatch = /\bstyle=(")([^"]*)"|\bstyle=(')([^']*)'/.exec(tag);
188188
if (styleMatch) {
189-
const existingStyle = styleMatch[2];
190-
const quote = styleMatch[1];
189+
const existingStyle = styleMatch[2] ?? styleMatch[4];
190+
const quote = styleMatch[1] ?? styleMatch[3];
191191
// Parse existing properties
192192
const props = new Map<string, string>();
193193
for (const part of splitInlineStyleDeclarations(existingStyle)) {

0 commit comments

Comments
 (0)