Skip to content

Commit 4ce3dd3

Browse files
Rolakadummdidumm
andauthored
fix: refine attributeRegex (#345)
Fix attributeRegex so that it correctly matches attributes that are not enclosed in quotes. fix #344 --------- Co-authored-by: Simon Holthausen <[email protected]>
1 parent a94563c commit 4ce3dd3

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- (feat) support `requirePragma` and `insertPragma` options ([#350](https://github.com/sveltejs/prettier-plugin-svelte/issues/350))
66
- (feat) support `<svelte:document>`
77
- (feat) trim whitespace in `class` attributes ([#339](https://github.com/sveltejs/prettier-plugin-svelte/issues/339))
8+
- (fix) handle script/style attributes without quotes ([#344](https://github.com/sveltejs/prettier-plugin-svelte/issues/344))
89

910
## 2.9.0
1011

src/lib/extractAttributes.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import { AttributeNode, TextNode } from '../print/nodes';
22

33
export function extractAttributes(html: string): AttributeNode[] {
44
const extractAttributesRegex = /<[a-z]+[\s\n]*([\s\S]*?)>/im;
5-
const attributeRegex = /([^\s=]+)(?:=("|')([\s\S]*?)\2)?/gim;
5+
const attributeRegex = /([^\s=]+)(?:=(?:(?:("|')([\s\S]*?)\2)|(?:(\S+?)(?:\s|>|$))))?/gim;
66

77
const [, attributesString] = html.match(extractAttributesRegex)!;
88

99
const attrs: AttributeNode[] = [];
1010

1111
let match: RegExpMatchArray | null;
1212
while ((match = attributeRegex.exec(attributesString))) {
13-
const [all, name, quotes, value] = match;
13+
const [all, name, quotes, valueQuoted, valueUnquoted] = match;
14+
const value = valueQuoted || valueUnquoted;
1415
const attrStart = match.index!;
1516

1617
let valueNode: AttributeNode['value'];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts"context=module
2+
></script>
3+
4+
<script lang=ts boolean></script>
5+
6+
<p class=foo>asd</p>
7+
<div>
8+
<script lang=ts>d</script>
9+
</div>
10+
11+
<style lang="less
12+
13+
"></style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts" context="module"></script>
2+
3+
<script lang="ts" boolean></script>
4+
5+
<p class="foo">asd</p>
6+
<div>
7+
<script lang="ts">
8+
d;
9+
</script>
10+
</div>
11+
12+
<style
13+
lang="less
14+
15+
"
16+
></style>

0 commit comments

Comments
 (0)