feat(static-pages): extend data model with route/enabled/status/media/SEO
Milestone 1 of the Static Pages Module sprint. - StaticPageConfig / ContentPage / ContentPageBootstrapInput gain: explicit editable route (defaults from slug, independently overridable), enabled (master on/off), status: 'draft'|'published' (per-page publish lifecycle, independent of the whole-bootstrap draft/publish cycle), customTemplate, heroImage/thumbnail/gallery, and seo.robots. - ContentPageService: normalizePage/normalizePages default missing enabled/status to enabled+published so existing bootstrap data never gets silently un-published; only the editor's createPage() opts a brand-new page into 'draft'. Legacy array-format pages get the same treatment. - resolvePage now returns null (storefront 404) for a disabled or draft page, regardless of whether the surrounding bootstrap itself is published - affects the storefront static-page route AND the auto-generated footer nav group (both go through this same resolver), which is the correct behavior. - validatePages extended: duplicateRoutes (route can now diverge from slug), invalidHtml, invalidSeo (canonical/ogImage URL shape, known robots tokens). - New schema/validators/primitives.validateHtml: stack-based tag-balance check (void/self-closing elements skipped, comments stripped). Caught and fixed a real bug during its own spec run: the initial implementation popped the stack back to the nearest matching ancestor on a mismatched closing tag, which silently swallowed a genuinely unclosed inner tag instead of flagging it - now a closing tag must match the top of the stack exactly. - toBootstrapRecord serializes the new fields; visible mirrors enabled so any reader of the older field name stays truthful. - Specs: content-page.service.spec.ts (new), primitives.spec.ts (validateHtml). Gate: tsc --noEmit, npm test (57/57), arch:check, build all green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
isValidHttpUrl,
|
||||
normalizeRoute,
|
||||
validateCss,
|
||||
validateHtml,
|
||||
validateJson,
|
||||
} from './primitives';
|
||||
|
||||
@@ -77,4 +78,33 @@ describe('validation primitives', () => {
|
||||
expect(normalizeRoute('/')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateHtml', () => {
|
||||
it('accepts well-formed markup with nested tags', () => {
|
||||
expect(validateHtml('<p>Hello <strong>world</strong></p><ul><li>one</li></ul>').ok).toBeTrue();
|
||||
});
|
||||
it('accepts void elements without a closing tag', () => {
|
||||
expect(validateHtml('<p>Line<br>break</p><hr><img src="x.png">').ok).toBeTrue();
|
||||
});
|
||||
it('accepts self-closing tags', () => {
|
||||
expect(validateHtml('<div><span/></div>').ok).toBeTrue();
|
||||
});
|
||||
it('ignores comments', () => {
|
||||
expect(validateHtml('<p>hi</p><!-- <div> unclosed inside a comment -->').ok).toBeTrue();
|
||||
});
|
||||
it('rejects an unclosed tag', () => {
|
||||
const result = validateHtml('<p>Hello <strong>world</p>');
|
||||
expect(result.ok).toBeFalse();
|
||||
expect(result.error).toContain('strong');
|
||||
expect(result.error).toContain('p');
|
||||
});
|
||||
it('rejects a stray closing tag', () => {
|
||||
const result = validateHtml('<p>Hello</p></div>');
|
||||
expect(result.ok).toBeFalse();
|
||||
expect(result.error).toContain('div');
|
||||
});
|
||||
it('accepts empty input', () => {
|
||||
expect(validateHtml('').ok).toBeTrue();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -79,3 +79,55 @@ export function extractStyleBlocks(html: string): string[] {
|
||||
export function normalizeRoute(route: string): string {
|
||||
return (route ?? '').trim().replace(/^\/+|\/+$/g, '').toLowerCase();
|
||||
}
|
||||
|
||||
/** HTML void elements per the WHATWG spec: never require (or accept) a closing tag. */
|
||||
const VOID_ELEMENTS = new Set([
|
||||
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
||||
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
||||
]);
|
||||
|
||||
/**
|
||||
* Best-effort HTML well-formedness check: strips comments, then walks tags
|
||||
* with a stack, ignoring void/self-closing elements, and fails on a mismatched
|
||||
* or unclosed tag. Intentionally lightweight (no DOM parser dependency,
|
||||
* consistent with `validateCss` above) - it catches the common authoring
|
||||
* breakages (a tag left open, a stray closing tag) without validating full
|
||||
* HTML5 parsing semantics.
|
||||
*/
|
||||
export function validateHtml(raw: string): StringCheckResult {
|
||||
const source = (raw ?? '').replace(/<!--[\s\S]*?-->/g, '');
|
||||
const stack: string[] = [];
|
||||
const tagRe = /<\/?([a-zA-Z][a-zA-Z0-9-]*)[^>]*?(\/)?>/g;
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
while ((match = tagRe.exec(source)) !== null) {
|
||||
const [full, rawName, selfClosingSlash] = match;
|
||||
const name = rawName.toLowerCase();
|
||||
const isClosing = full.startsWith('</');
|
||||
|
||||
if (VOID_ELEMENTS.has(name) || selfClosingSlash) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isClosing) {
|
||||
// Strict top-of-stack match: a closing tag must close the innermost open
|
||||
// tag. Popping back to the nearest ancestor with the same name (instead
|
||||
// of requiring an exact match) would silently swallow a genuinely
|
||||
// unclosed inner tag - exactly the breakage this check exists to catch.
|
||||
if (stack.length === 0) {
|
||||
return { ok: false, error: `Unexpected closing tag </${name}>` };
|
||||
}
|
||||
if (stack[stack.length - 1] !== name) {
|
||||
return { ok: false, error: `Expected </${stack[stack.length - 1]}> but found </${name}>` };
|
||||
}
|
||||
stack.pop();
|
||||
} else {
|
||||
stack.push(name);
|
||||
}
|
||||
}
|
||||
|
||||
if (stack.length > 0) {
|
||||
return { ok: false, error: `Unclosed tag <${stack[stack.length - 1]}>` };
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user