Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve fallback content printing #58

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
style {
invalid { color blue; }
}
style { invalid { color blue; } }

<style>

body
color: blue;

</style>

<script>

console->log(
'a'
);

</script>
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
style {
invalid { color blue; }
}
style { invalid { color blue; } }

style
--
body
color: blue;
--
script
--
console->log(
'a'
);
--
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
style {
invalid { color blue; }
}
style { invalid { color blue; } }

<style>

body
color: blue;

</style>

<script>

console->log(
'a'
);

</script>
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
style {
invalid { color blue; }
}
style { invalid { color blue; } }

<style>

body
color: blue;

</style>

<script>

console->log(
'a'
);

</script>
10 changes: 10 additions & 0 deletions src/__tests__/__snapshots__/script-with-type.expected/auto.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
script nonce=$global.cspNonce type="speculationrules"
--
{
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
--
10 changes: 10 additions & 0 deletions src/__tests__/__snapshots__/script-with-type.expected/html.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
</script>
8 changes: 8 additions & 0 deletions src/__tests__/fixtures/script-with-type.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}]
}
</script>
43 changes: 39 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ export const printers: Record<string, Printer<types.Node>> = {
return async (toDoc, print) => {
const placeholders = [] as Doc[];
const groupId = Symbol();
const parser = getScriptParser(node);
const doc: Doc[] = [
opts.markoSyntax === "html" ? "<" : "",
"script",
Expand Down Expand Up @@ -734,7 +735,13 @@ export const printers: Record<string, Printer<types.Node>> = {
: b.ifBreak("--", " --", { groupId }),
opts.markoSyntax === "html" ? "" : b.line,
replaceEmbeddedPlaceholders(
await toDoc(embeddedCode, { parser: scriptParser }),
parser === false
? asLiteralTextContent(embeddedCode.trim())
: await toDoc(embeddedCode, {
parser,
}).catch(() =>
asLiteralTextContent(embeddedCode.trim()),
),
placeholders,
),
opts.markoSyntax === "html"
Expand Down Expand Up @@ -781,7 +788,12 @@ export const printers: Record<string, Printer<types.Node>> = {
"style",
!lang || lang === ".css" ? "" : lang,
" {",
b.indent([b.line, await toDoc(code, { parser })]),
b.indent([
b.line,
await toDoc(code, { parser }).catch(() =>
asLiteralTextContent(code.trim()),
),
]),
b.line,
"}",
]),
Expand Down Expand Up @@ -888,14 +900,15 @@ export const printers: Record<string, Printer<types.Node>> = {
: b.ifBreak("--", " --", { groupId }),
opts.markoSyntax === "html" ? "" : b.line,
replaceEmbeddedPlaceholders(
await toDoc(embeddedCode, { parser }),
await toDoc(embeddedCode, { parser }).catch(() =>
asLiteralTextContent(embeddedCode.trim()),
),
placeholders,
),
opts.markoSyntax === "html"
? ""
: b.ifBreak([b.softline, "--"]),
]);

doc.push(b.indent([wrapSep, bodyDoc]));

if (opts.markoSyntax === "html") {
Expand Down Expand Up @@ -1248,3 +1261,25 @@ function setConfig(config: Config) {
},
};
}

function getScriptParser(tag: types.MarkoTag) {
for (const attr of tag.attributes) {
if (attr.type === "MarkoAttribute" && attr.name === "type") {
switch (
attr.value.type === "StringLiteral" ? attr.value.value : undefined
) {
case "module":
case "text/javascript":
case "application/javascript":
return scriptParser;
case "importmap":
case "speculationrules":
return "json";
default:
return false;
}
}
}

return scriptParser;
}
Loading