From e2ae0ba74bf71ece7117ac7426a1940d18550e9f Mon Sep 17 00:00:00 2001 From: Rolaka Date: Sun, 5 Feb 2023 12:52:21 +0800 Subject: [PATCH 1/4] (fix) refine attributeRegex Fix attributeRegex that correctly match attributes that are not enclosed in quotes. fix #344 --- src/lib/extractAttributes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/extractAttributes.ts b/src/lib/extractAttributes.ts index bd5208a8..240ad343 100644 --- a/src/lib/extractAttributes.ts +++ b/src/lib/extractAttributes.ts @@ -2,7 +2,7 @@ import { AttributeNode, TextNode } from '../print/nodes'; export function extractAttributes(html: string): AttributeNode[] { const extractAttributesRegex = /<[a-z]+[\s\n]*([\s\S]*?)>/im; - const attributeRegex = /([^\s=]+)(?:=("|')([\s\S]*?)\2)?/gim; + const attributeRegex = /([^\s=]+)(?:=("|'|)([\s\S]*?)\2)?(?: |$)/gim; const [, attributesString] = html.match(extractAttributesRegex)!; From 0911e3ce87d4d639cf9186676a3e54823eed7ed9 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 22 Mar 2023 12:01:45 +0100 Subject: [PATCH 2/4] tighten up regex, tests --- src/lib/extractAttributes.ts | 2 +- .../samples/attributes-without-quotes/input.html | 14 ++++++++++++++ .../attributes-without-quotes/output.html | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/formatting/samples/attributes-without-quotes/input.html create mode 100644 test/formatting/samples/attributes-without-quotes/output.html diff --git a/src/lib/extractAttributes.ts b/src/lib/extractAttributes.ts index 240ad343..e6a359a6 100644 --- a/src/lib/extractAttributes.ts +++ b/src/lib/extractAttributes.ts @@ -2,7 +2,7 @@ import { AttributeNode, TextNode } from '../print/nodes'; export function extractAttributes(html: string): AttributeNode[] { const extractAttributesRegex = /<[a-z]+[\s\n]*([\s\S]*?)>/im; - const attributeRegex = /([^\s=]+)(?:=("|'|)([\s\S]*?)\2)?(?: |$)/gim; + const attributeRegex = /([^\s=]+)(?:=("|'|)([\s\S]*?)\2)?(?:\s|>|$)/gim; const [, attributesString] = html.match(extractAttributesRegex)!; diff --git a/test/formatting/samples/attributes-without-quotes/input.html b/test/formatting/samples/attributes-without-quotes/input.html new file mode 100644 index 00000000..13604029 --- /dev/null +++ b/test/formatting/samples/attributes-without-quotes/input.html @@ -0,0 +1,14 @@ + + + + +

asd

+
+ +
+ + diff --git a/test/formatting/samples/attributes-without-quotes/output.html b/test/formatting/samples/attributes-without-quotes/output.html new file mode 100644 index 00000000..0c130abf --- /dev/null +++ b/test/formatting/samples/attributes-without-quotes/output.html @@ -0,0 +1,16 @@ + + + + +

asd

+
+ +
+ + From cbedec8bae77f3b44eb301bce1e048c8efa654ec Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 22 Mar 2023 12:02:36 +0100 Subject: [PATCH 3/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 223f64d6..acca81ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - (feat) support `requirePragma` and `insertPragma` options ([#350](https://github.com/sveltejs/prettier-plugin-svelte/issues/350)) - (feat) support `` - (feat) trim whitespace in `class` attributes ([#339](https://github.com/sveltejs/prettier-plugin-svelte/issues/339)) +- (fix) handle script/style attributes without quotes ([#344](https://github.com/sveltejs/prettier-plugin-svelte/issues/344)) ## 2.9.0 From 36d5a31556b887d0ec6f46cdb6e425c981c1046d Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 22 Mar 2023 12:14:01 +0100 Subject: [PATCH 4/4] more correct regex, enhance test --- src/lib/extractAttributes.ts | 5 +++-- test/formatting/samples/attributes-without-quotes/input.html | 5 ++--- .../formatting/samples/attributes-without-quotes/output.html | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/extractAttributes.ts b/src/lib/extractAttributes.ts index e6a359a6..630878bb 100644 --- a/src/lib/extractAttributes.ts +++ b/src/lib/extractAttributes.ts @@ -2,7 +2,7 @@ import { AttributeNode, TextNode } from '../print/nodes'; export function extractAttributes(html: string): AttributeNode[] { const extractAttributesRegex = /<[a-z]+[\s\n]*([\s\S]*?)>/im; - const attributeRegex = /([^\s=]+)(?:=("|'|)([\s\S]*?)\2)?(?:\s|>|$)/gim; + const attributeRegex = /([^\s=]+)(?:=(?:(?:("|')([\s\S]*?)\2)|(?:(\S+?)(?:\s|>|$))))?/gim; const [, attributesString] = html.match(extractAttributesRegex)!; @@ -10,7 +10,8 @@ export function extractAttributes(html: string): AttributeNode[] { let match: RegExpMatchArray | null; while ((match = attributeRegex.exec(attributesString))) { - const [all, name, quotes, value] = match; + const [all, name, quotes, valueQuoted, valueUnquoted] = match; + const value = valueQuoted || valueUnquoted; const attrStart = match.index!; let valueNode: AttributeNode['value']; diff --git a/test/formatting/samples/attributes-without-quotes/input.html b/test/formatting/samples/attributes-without-quotes/input.html index 13604029..7fc0455b 100644 --- a/test/formatting/samples/attributes-without-quotes/input.html +++ b/test/formatting/samples/attributes-without-quotes/input.html @@ -1,8 +1,7 @@ - - +

asd

diff --git a/test/formatting/samples/attributes-without-quotes/output.html b/test/formatting/samples/attributes-without-quotes/output.html index 0c130abf..a803f6dd 100644 --- a/test/formatting/samples/attributes-without-quotes/output.html +++ b/test/formatting/samples/attributes-without-quotes/output.html @@ -1,6 +1,6 @@ - +

asd