diff --git a/src/index.ts b/src/index.ts index d1b11f60..af3c060e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { SupportLanguage, Parser, Printer } from 'prettier'; import { print } from './print'; import { ASTNode } from './print/nodes'; import { embed } from './embed'; -import { snipTagContent } from './lib/snipTagContent'; +import { snipScriptAndStyleTagContent } from './lib/snipTagContent'; function locStart(node: any) { return node.start; @@ -40,8 +40,7 @@ export const parsers: Record = { } }, preprocess: (text, options) => { - text = snipTagContent('style', text); - text = snipTagContent('script', text, '{}'); + text = snipScriptAndStyleTagContent(text); text = text.trim(); // Prettier sets the preprocessed text as the originalText in case // the Svelte formatter is called directly. In case it's called diff --git a/src/lib/snipTagContent.ts b/src/lib/snipTagContent.ts index b61757af..e3557f56 100644 --- a/src/lib/snipTagContent.ts +++ b/src/lib/snipTagContent.ts @@ -1,14 +1,54 @@ export const snippedTagContentAttribute = '✂prettier:content✂'; -export function snipTagContent(tagName: string, source: string, placeholder = ''): string { - const regex = new RegExp(`|<${tagName}([^]*?)>([^]*?)<\/${tagName}>`, 'g'); - return source.replace(regex, (match, attributes, content) => { - if (match.startsWith('|<${tagName}([^]*?)>([^]*?)<\/${tagName}>`, 'g'); + } } export function hasSnippedContent(text: string) { diff --git a/test/printer/samples/style-inside-script.html b/test/printer/samples/style-inside-script.html new file mode 100644 index 00000000..d12761ea --- /dev/null +++ b/test/printer/samples/style-inside-script.html @@ -0,0 +1,3 @@ + diff --git a/test/printer/samples/style-unclosed-inside-script.html b/test/printer/samples/style-unclosed-inside-script.html new file mode 100644 index 00000000..3f1fbec0 --- /dev/null +++ b/test/printer/samples/style-unclosed-inside-script.html @@ -0,0 +1,5 @@ + + +