Skip to content

Commit 85ceede

Browse files
fix(cli,parsers): consolidate scaffold resolution regexes into one definition
applyResolutionPreset kept its own prefix-capturing regex literals for the html/body CSS size and meta viewport content, duplicating the same three patterns already defined in @hyperframes/parsers for lint's read-only root_dimensions_mismatch comparison. Merge to one definition per pattern that captures both the prefix (for in-place replace) and the digits (for comparison), so the two callers can no longer drift on where a scaffold's resolution lives. Existing applyResolutionPreset tests stay green. Co-Authored-By: Miguel Ángel <[email protected]>
1 parent 9a7b6e1 commit 85ceede

4 files changed

Lines changed: 78 additions & 29 deletions

File tree

‎packages/cli/src/commands/init.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ import {
5353
normalizeResolutionFlag,
5454
type CanvasResolution,
5555
} from "@hyperframes/core";
56+
import {
57+
HTML_BODY_CSS_HEIGHT_FIRST_RE,
58+
HTML_BODY_CSS_WIDTH_FIRST_RE,
59+
VIEWPORT_META_SIZE_RE,
60+
} from "@hyperframes/parsers";
5661

5762
function resolveScaffoldTemplateId(exampleFlag: string | undefined, hasMediaFile: boolean): string {
5863
const example = exampleFlag === "agent" ? "blank" : exampleFlag;
@@ -527,20 +532,18 @@ export function applyResolutionPreset(destDir: string, resolution: CanvasResolut
527532

528533
// Inline `html, body { ... }` CSS: handle width-before-height and
529534
// height-before-width orderings. Hand-authored templates can use either.
530-
const bodyCssRe = /(html\s*,\s*body\s*\{[^}]*?width:\s*)\d+px([^}]*?height:\s*)\d+px/i;
531-
if (bodyCssRe.test(html)) {
532-
html = html.replace(bodyCssRe, `$1${width}px$2${height}px`);
535+
// Groups 1 and 3 are the text before each dimension, 2 and 4 the digits.
536+
if (HTML_BODY_CSS_WIDTH_FIRST_RE.test(html)) {
537+
html = html.replace(HTML_BODY_CSS_WIDTH_FIRST_RE, `$1${width}px$3${height}px`);
533538
changed = true;
534539
}
535-
const bodyCssReverseRe = /(html\s*,\s*body\s*\{[^}]*?height:\s*)\d+px([^}]*?width:\s*)\d+px/i;
536-
if (bodyCssReverseRe.test(html)) {
537-
html = html.replace(bodyCssReverseRe, `$1${height}px$2${width}px`);
540+
if (HTML_BODY_CSS_HEIGHT_FIRST_RE.test(html)) {
541+
html = html.replace(HTML_BODY_CSS_HEIGHT_FIRST_RE, `$1${height}px$3${width}px`);
538542
changed = true;
539543
}
540544

541-
const viewportRe = /(<meta[^>]*name=["']viewport["'][^>]*content=["'])width=\d+,\s*height=\d+/i;
542-
if (viewportRe.test(html)) {
543-
html = html.replace(viewportRe, `$1width=${width}, height=${height}`);
545+
if (VIEWPORT_META_SIZE_RE.test(html)) {
546+
html = html.replace(VIEWPORT_META_SIZE_RE, `$1${width}$3${height}`);
544547
changed = true;
545548
}
546549

‎packages/lint/src/rules/core.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,17 @@ function findVisibleMarkupCommentLeak(source: string): string | null {
239239

240240
type ScaffoldSize = { width: string; height: string };
241241

242+
// Groups 1 and 3 are prefix text for applyResolutionPreset's in-place
243+
// replace; lint only reads the digit groups (2 and 4).
242244
function readHtmlBodyCssSize(source: string): ScaffoldSize | null {
243245
const widthFirst = source.match(HTML_BODY_CSS_WIDTH_FIRST_RE);
244246
if (widthFirst) {
245-
const [, width = "", height = ""] = widthFirst;
247+
const [, , width = "", , height = ""] = widthFirst;
246248
return { width, height };
247249
}
248250
const heightFirst = source.match(HTML_BODY_CSS_HEIGHT_FIRST_RE);
249251
if (heightFirst) {
250-
const [, height = "", width = ""] = heightFirst;
252+
const [, , height = "", , width = ""] = heightFirst;
251253
return { width, height };
252254
}
253255
return null;
@@ -256,7 +258,7 @@ function readHtmlBodyCssSize(source: string): ScaffoldSize | null {
256258
function readViewportMetaSize(source: string): ScaffoldSize | null {
257259
const match = source.match(VIEWPORT_META_SIZE_RE);
258260
if (!match) return null;
259-
const [, width = "", height = ""] = match;
261+
const [, , width = "", , height = ""] = match;
260262
return { width, height };
261263
}
262264

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
HTML_BODY_CSS_HEIGHT_FIRST_RE,
4+
HTML_BODY_CSS_WIDTH_FIRST_RE,
5+
VIEWPORT_META_SIZE_RE,
6+
} from "./canvasScaffoldPatterns.js";
7+
8+
// Both callers key off group numbers: @hyperframes/lint's
9+
// root_dimensions_mismatch reads the digits, @hyperframes/cli's
10+
// applyResolutionPreset substitutes `$1<new>$3<new>` to keep the text around
11+
// them. Pin the layout here so neither can renumber against the other.
12+
const CASES = [
13+
{
14+
name: "HTML_BODY_CSS_WIDTH_FIRST_RE",
15+
re: HTML_BODY_CSS_WIDTH_FIRST_RE,
16+
source: "html, body { margin: 0; width: 1920px; height: 1080px; overflow: hidden; }",
17+
digits: ["1920", "1080"],
18+
replacement: "$13840px$32160px",
19+
replaced: "html, body { margin: 0; width: 3840px; height: 2160px; overflow: hidden; }",
20+
},
21+
{
22+
name: "HTML_BODY_CSS_HEIGHT_FIRST_RE",
23+
re: HTML_BODY_CSS_HEIGHT_FIRST_RE,
24+
source: "html, body { margin: 0; height: 1080px; width: 1920px; overflow: hidden; }",
25+
digits: ["1080", "1920"],
26+
replacement: "$12160px$33840px",
27+
replaced: "html, body { margin: 0; height: 2160px; width: 3840px; overflow: hidden; }",
28+
},
29+
{
30+
name: "VIEWPORT_META_SIZE_RE",
31+
re: VIEWPORT_META_SIZE_RE,
32+
source: '<meta name="viewport" content="width=1920, height=1080" />',
33+
digits: ["1920", "1080"],
34+
replacement: "$13840$32160",
35+
replaced: '<meta name="viewport" content="width=3840, height=2160" />',
36+
},
37+
];
38+
39+
describe("canvas scaffold patterns", () => {
40+
it.each(CASES)("$name exposes the two dimensions as groups 2 and 4", ({ re, source, digits }) => {
41+
const match = source.match(re);
42+
expect(match).not.toBeNull();
43+
expect(match?.slice(1)).toHaveLength(4);
44+
expect([match?.[2], match?.[4]]).toEqual(digits);
45+
});
46+
47+
it.each(CASES)(
48+
"$name replaces both dimensions in place, leaving the rest untouched",
49+
({ re, source, replacement, replaced }) => {
50+
expect(source.replace(re, replacement)).toBe(replaced);
51+
},
52+
);
53+
});
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
/**
2-
* Where a scaffolded project's canvas resolution lives besides the composition
3-
* root's own `data-width`/`data-height`: the inline `html, body { width;
4-
* height }` CSS block and the `<meta name="viewport">` `content` attribute.
5-
* `@hyperframes/cli`'s `applyResolutionPreset` rewrites those same two places
6-
* when a project scaffolds with `--resolution`, but keeps its own
7-
* prefix-capturing regexes for that replace — these are a separate read-only
8-
* definition of the same locations, not the literal patterns it uses.
9-
*
10-
* Lives in `@hyperframes/parsers` rather than `@hyperframes/cli` because
11-
* `@hyperframes/lint` cannot depend on `cli` (`cli` depends on `lint`, not the
12-
* reverse), and `parsers` is a dependency both already share.
2+
* Where a scaffolded project's canvas resolution lives besides the root's own
3+
* data-width/data-height: the html/body CSS size and the meta viewport content.
134
*/
145

15-
/** Matches `html, body { ...width: <n>px... height: <n>px... }`. */
6+
/** html/body CSS, width first. Groups 1/3 are the surrounding text, 2/4 the digits. */
167
export const HTML_BODY_CSS_WIDTH_FIRST_RE =
17-
/html\s*,\s*body\s*\{[^}]*?width:\s*(\d+)px[^}]*?height:\s*(\d+)px/i;
8+
/(html\s*,\s*body\s*\{[^}]*?width:\s*)(\d+)px([^}]*?height:\s*)(\d+)px/i;
189

19-
/** Matches the same block with height authored before width. */
10+
/** Same block, height authored first. */
2011
export const HTML_BODY_CSS_HEIGHT_FIRST_RE =
21-
/html\s*,\s*body\s*\{[^}]*?height:\s*(\d+)px[^}]*?width:\s*(\d+)px/i;
12+
/(html\s*,\s*body\s*\{[^}]*?height:\s*)(\d+)px([^}]*?width:\s*)(\d+)px/i;
2213

23-
/** Matches `<meta ... name="viewport" ... content="width=<n>, height=<n>">`. */
14+
/** Viewport meta content. Shared by lint (reads groups 2/4) and cli (replaces via 1/3). */
2415
export const VIEWPORT_META_SIZE_RE =
25-
/<meta[^>]*name=["']viewport["'][^>]*content=["']width=(\d+),\s*height=(\d+)/i;
16+
/(<meta[^>]*name=["']viewport["'][^>]*content=["']width=)(\d+)(,\s*height=)(\d+)/i;

0 commit comments

Comments
 (0)