From 29cec40e945b3f4056315bfe9637c979070d8050 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Wed, 23 Oct 2024 18:47:39 -0700 Subject: [PATCH 1/2] fix: improve fallback content printint --- .../auto.marko | 8 +------- .../concise.marko | 8 +------- .../html.marko | 8 +------- .../with-parens.marko | 8 +------- .../script-with-type.expected/auto.marko | 8 ++++++++ .../script-with-type.expected/concise.marko | 9 +++++++++ .../script-with-type.expected/html.marko | 8 ++++++++ .../with-parens.marko | 8 ++++++++ src/__tests__/fixtures/script-with-type.marko | 8 ++++++++ src/index.ts | 18 ++++++++++++++---- 10 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/auto.marko create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/concise.marko create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/html.marko create mode 100644 src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko create mode 100644 src/__tests__/fixtures/script-with-type.marko diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko index 16e124a..80d9acf 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/auto.marko @@ -1,18 +1,12 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko index 8806934..811abcd 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/concise.marko @@ -1,20 +1,14 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } style -- - body color: blue; - -- script -- - console->log( 'a' ); - -- diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko index 16e124a..80d9acf 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/html.marko @@ -1,18 +1,12 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } diff --git a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko index 16e124a..80d9acf 100644 --- a/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko +++ b/src/__tests__/__snapshots__/ignore-embedded-script-errors.expected/with-parens.marko @@ -1,18 +1,12 @@ -style { - invalid { color blue; } -} +style { invalid { color blue; } } diff --git a/src/__tests__/__snapshots__/script-with-type.expected/auto.marko b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko @@ -0,0 +1,8 @@ + diff --git a/src/__tests__/__snapshots__/script-with-type.expected/concise.marko b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko new file mode 100644 index 0000000..593d4e4 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko @@ -0,0 +1,9 @@ +script nonce=$global.cspNonce type="speculationrules" + -- + { + "prerender": [{ + "where": { "href_matches": "/*" }, + "eagerness": "moderate" + }] + } + -- diff --git a/src/__tests__/__snapshots__/script-with-type.expected/html.marko b/src/__tests__/__snapshots__/script-with-type.expected/html.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/html.marko @@ -0,0 +1,8 @@ + diff --git a/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko @@ -0,0 +1,8 @@ + diff --git a/src/__tests__/fixtures/script-with-type.marko b/src/__tests__/fixtures/script-with-type.marko new file mode 100644 index 0000000..92dcfc9 --- /dev/null +++ b/src/__tests__/fixtures/script-with-type.marko @@ -0,0 +1,8 @@ + diff --git a/src/index.ts b/src/index.ts index 37087b4..1a87426 100644 --- a/src/index.ts +++ b/src/index.ts @@ -734,7 +734,11 @@ export const printers: Record> = { : b.ifBreak("--", " --", { groupId }), opts.markoSyntax === "html" ? "" : b.line, replaceEmbeddedPlaceholders( - await toDoc(embeddedCode, { parser: scriptParser }), + await toDoc(embeddedCode, { + parser: scriptParser, + }).catch(() => + asLiteralTextContent(embeddedCode.trim()), + ), placeholders, ), opts.markoSyntax === "html" @@ -781,7 +785,12 @@ export const printers: Record> = { "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, "}", ]), @@ -888,14 +897,15 @@ export const printers: Record> = { : 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") { From 3c9020a45ccf2055978f515a490b77fbbcf4ad29 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Wed, 23 Oct 2024 19:04:32 -0700 Subject: [PATCH 2/2] fix: special case printing json scripts --- .../script-with-type.expected/auto.marko | 10 +++--- .../script-with-type.expected/concise.marko | 10 +++--- .../script-with-type.expected/html.marko | 10 +++--- .../with-parens.marko | 10 +++--- src/index.ts | 35 ++++++++++++++++--- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/__tests__/__snapshots__/script-with-type.expected/auto.marko b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko index 92dcfc9..dc82cd2 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/auto.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko @@ -1,8 +1,10 @@ diff --git a/src/__tests__/__snapshots__/script-with-type.expected/concise.marko b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko index 593d4e4..077b6d7 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/concise.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko @@ -1,9 +1,11 @@ script nonce=$global.cspNonce type="speculationrules" -- { - "prerender": [{ - "where": { "href_matches": "/*" }, - "eagerness": "moderate" - }] + "prerender": [ + { + "where": { "href_matches": "/*" }, + "eagerness": "moderate" + } + ] } -- diff --git a/src/__tests__/__snapshots__/script-with-type.expected/html.marko b/src/__tests__/__snapshots__/script-with-type.expected/html.marko index 92dcfc9..dc82cd2 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/html.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/html.marko @@ -1,8 +1,10 @@ diff --git a/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko index 92dcfc9..dc82cd2 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko @@ -1,8 +1,10 @@ diff --git a/src/index.ts b/src/index.ts index 1a87426..a008d01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -645,6 +645,7 @@ export const printers: Record> = { return async (toDoc, print) => { const placeholders = [] as Doc[]; const groupId = Symbol(); + const parser = getScriptParser(node); const doc: Doc[] = [ opts.markoSyntax === "html" ? "<" : "", "script", @@ -734,11 +735,13 @@ export const printers: Record> = { : b.ifBreak("--", " --", { groupId }), opts.markoSyntax === "html" ? "" : b.line, replaceEmbeddedPlaceholders( - await toDoc(embeddedCode, { - parser: scriptParser, - }).catch(() => - asLiteralTextContent(embeddedCode.trim()), - ), + parser === false + ? asLiteralTextContent(embeddedCode.trim()) + : await toDoc(embeddedCode, { + parser, + }).catch(() => + asLiteralTextContent(embeddedCode.trim()), + ), placeholders, ), opts.markoSyntax === "html" @@ -1258,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; +}