import fc from 'fast-check';
// Generators for HTML built from known pieces, each with a model of what it
// is. The script step (config/inlineScripts.ts) is tested against the model,
// never against its own scanner.
export type HtmlPieceKind =
| 'executable'
| 'external'
| 'empty'
| 'data'
| 'comment'
| 'rawText'
| 'attributeDecoy'
| 'markup';
export interface HtmlPiece {
kind: HtmlPieceKind;
html: string;
// Executable pieces only: the script body and the attribute text between
// the tag name and the closing '>'
body?: string;
attributes?: string;
}
export interface HtmlModel {
html: string;
pieces: HtmlPiece[];
// What sits between two pieces
separator: string;
}
// Bodies never contain a script end tag or a comment opener (the step
// refuses those on purpose). Several snippets contain '<' and '>' to show a
// body is not parsed as markup. A small pool makes equal bodies likely, so
// the "same body, same file" property gets exercised.
const snippet = fc.constantFrom(
'window.a=1;',
'var x = "";',
'if (ad) { f(); }',
'/* comment */ g();',
'document.currentScript.remove();/*$tsr-stream-boundary*/',
'$RB=[];$RV=function(a){return a};',
"const s = '';",
'const é = "ünï";',
'requestAnimationFrame(function(){$RT=performance.now()});',
);
const padding = fc.constantFrom('', ' ', '\n', '\r\n ', '\t');
// Non-empty after trimming, with the padding a minifier would leave out
export const scriptBody: fc.Arbitrary = fc
.tuple(padding, fc.array(snippet, { minLength: 1, maxLength: 3 }), padding)
.map(([before, core, after]) => before + core.join('\n') + after);
// Attribute text of an executable script, verbatim as it sits in the tag.
// Quoted values may contain '>' and the tag may end in a slash.
const executableAttributes = fc.constantFrom(
'',
' type="module"',
' type="text/javascript"',
' type=""',
' type = "text/ecmascript"',
" TYPE='Module'",
' type=application/javascript',
' data-tsr-stream-part=""',
' id="_R_"',
' class="a" nonce="x" data-n=1',
' data-x="a>b"',
" data-q='1 > 0'",
' language="javascript"',
' /',
);
const tagCase = fc.constantFrom(
{ open: 'script', close: 'script' },
{ open: 'SCRIPT', close: 'SCRIPT ' },
{ open: 'Script', close: 'sCrIpT\n' },
);
const canonicalCase = fc.constant({ open: 'script', close: 'script' });
function executableOf(cases: typeof tagCase): fc.Arbitrary {
return fc
.tuple(cases, executableAttributes, scriptBody)
.map(([names, attributes, body]) => ({
kind: 'executable' as const,
html: `<${names.open}${attributes}>${body}${names.close}>`,
body,
attributes,
}));
}
// Mixed tag-name case and end-tag spacing; the step turns these into
// lowercase references
export const executablePiece = executableOf(tagCase);
// Only ``, so a moved script can be put back exactly
const canonicalExecutablePiece = executableOf(canonicalCase);
const externalPiece: fc.Arbitrary = fc
.constantFrom(
'',
'',
'',
'',
'',
'',
)
.map((html) => ({ kind: 'external' as const, html }));
const emptyPiece: fc.Arbitrary = fc
.constantFrom(
'',
'',
'',
'',
)
.map((html) => ({ kind: 'empty' as const, html }));
const dataPiece: fc.Arbitrary = fc
.tuple(
fc.constantFrom(
'application/ld+json',
'application/json',
'importmap',
'speculationrules',
'text/plain',
'text/x-template',
' APPLICATION/LD+JSON ',
),
fc.constantFrom(
'{"a":1}',
'{"imports":{"x":"/x.js"}}',
'{"name":"a \\u003c/script\\u003e"}',
'if (a ({
kind: 'data' as const,
html: ``,
}));
// Text that looks like a script but sits where the scanner must skip it
const commentPiece: fc.Arbitrary = fc
.constantFrom(
'',
'',
'',
'',
'',
)
.map((html) => ({ kind: 'comment' as const, html }));
const rawTextPiece: fc.Arbitrary = fc
.constantFrom(
'',
'',
'',
'a ',
'',
'',
'',
)
.map((html) => ({ kind: 'rawText' as const, html }));
const attributeDecoyPiece: fc.Arbitrary = fc
.constantFrom(
'
',
)
.map((html) => ({ kind: 'attributeDecoy' as const, html }));
// Complete markup with no script in it. Text pieces never end in '<', so a
// piece cannot glue onto the next one.
const markupPiece: fc.Arbitrary = fc
.constantFrom(
'',
'',
'
text
',
'
1 < 2 and 3 > 2
',
' ',
'',
'',
'plain text',
'
',
)
.map((html) => ({ kind: 'markup' as const, html }));
// Everything the step must leave alone, whatever surrounds it
export const decoyPiece: fc.Arbitrary = fc.oneof(
externalPiece,
emptyPiece,
dataPiece,
commentPiece,
rawTextPiece,
attributeDecoyPiece,
markupPiece,
);
export const htmlPiece: fc.Arbitrary = fc.oneof(
{ arbitrary: executablePiece, weight: 3 },
{ arbitrary: decoyPiece, weight: 4 },
);
const canonicalHtmlPiece: fc.Arbitrary = fc.oneof(
{ arbitrary: canonicalExecutablePiece, weight: 3 },
{ arbitrary: decoyPiece, weight: 4 },
);
const separator = fc.constantFrom('', '\n', ' ');
function modelOf(pieces: fc.Arbitrary): fc.Arbitrary {
return fc
.tuple(fc.array(pieces, { maxLength: 12 }), separator)
.map(([list, join]) => ({
html: list.map((piece) => piece.html).join(join),
pieces: list,
separator: join,
}));
}
// A document and what each part of it is
export const htmlWithModel: fc.Arbitrary = modelOf(htmlPiece);
// The same, with executable scripts written as ``
// only, so that putting the moved bodies back restores the input exactly
export const canonicalHtmlWithModel: fc.Arbitrary =
modelOf(canonicalHtmlPiece);
// Independent complete fragments, for split-and-join properties: each one
// parses the same alone as inside a larger document
export const htmlFragments: fc.Arbitrary = fc.array(htmlPiece, {
maxLength: 10,
});
// A build output for the runner: which expected pages exist and what is wrong
// with them, plus files that do not belong. The test turns this into a file
// map and computes the expected violations from the same description.
export type BadScript = 'none' | 'unsupported' | 'unterminated';
export interface PageScenario {
present: boolean;
content: HtmlModel;
// Already processed: references a moved script without executable ones
reference: boolean;
bad: BadScript;
// The assembled address sits in the page text
leakAddress: boolean;
}
export interface ExtraFile {
name: string;
address: boolean;
rootPath: boolean;
}
export interface OutputScenario {
// index.html, over-mij.html, soorten-fotoshoots/newborn.html, 404.html
pages: PageScenario[];
unexpected: string[];
extras: ExtraFile[];
sourceMap: boolean;
sitemap: 'ok' | 'missing' | 'differs';
}
const pageScenario: fc.Arbitrary = fc.record({
present: fc.boolean(),
content: htmlWithModel,
reference: fc.boolean(),
bad: fc.constantFrom(
'none',
'none',
'none',
'unsupported',
'unterminated',
),
leakAddress: fc.boolean(),
});
const extraFile: fc.Arbitrary = fc.record({
name: fc.constantFrom(
'assets/app.js',
'assets/app.css',
'robots.txt',
'data.json',
'manifest.webmanifest',
'feed.xml',
),
address: fc.boolean(),
rootPath: fc.boolean(),
});
export const outputScenario: fc.Arbitrary = fc.record({
pages: fc.tuple(pageScenario, pageScenario, pageScenario, pageScenario),
unexpected: fc.uniqueArray(
fc.constantFrom(
'extra.html',
'soorten-fotoshoots/index.html',
'a/b.html',
),
{ maxLength: 2 },
),
extras: fc.uniqueArray(extraFile, {
selector: (file) => file.name,
maxLength: 3,
}),
sourceMap: fc.boolean(),
sitemap: fc.constantFrom('ok', 'ok', 'missing', 'differs'),
});