-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: escape
<
in attribute strings (#12989)
Svelte 4 version of #11411
- Loading branch information
1 parent
5ec4409
commit 83e96e0
Showing
13 changed files
with
59 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: escape `<` in attribute strings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const ATTR_REGEX = /[&"<]/g; | ||
const CONTENT_REGEX = /[&<]/g; | ||
|
||
/** | ||
* Note: this method is performance sensitive and has been optimized | ||
* https://github.com/sveltejs/svelte/pull/5701 | ||
* @param {unknown} value | ||
* @returns {string} | ||
*/ | ||
export function escape(value, is_attr = false) { | ||
const str = String(value); | ||
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX; | ||
pattern.lastIndex = 0; | ||
let escaped = ''; | ||
let last = 0; | ||
while (pattern.test(str)) { | ||
const i = pattern.lastIndex - 1; | ||
const ch = str[i]; | ||
escaped += str.substring(last, i) + (ch === '&' ? '&' : ch === '"' ? '"' : '<'); | ||
last = i + 1; | ||
} | ||
return escaped + str.substring(last); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/svelte/test/server-side-rendering/samples/escaped-attr-2/_expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<noscript | ||
><a href="</noscript><script>console.log('should not run')</script>">test</a></noscript | ||
> |
8 changes: 8 additions & 0 deletions
8
packages/svelte/test/server-side-rendering/samples/escaped-attr-2/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script> | ||
const x = `</noscript><script>console.log('should not run')<` + `/script>` | ||
</script> | ||
|
||
<noscript> | ||
<a href={x}>test</a> | ||
</noscript> | ||
|
1 change: 1 addition & 0 deletions
1
packages/svelte/test/server-side-rendering/samples/escaped-attr-3/_expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div title="&<">blah</div> |
1 change: 1 addition & 0 deletions
1
packages/svelte/test/server-side-rendering/samples/escaped-attr-3/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div title="&<">blah</div> |
1 change: 1 addition & 0 deletions
1
packages/svelte/test/server-side-rendering/samples/escaped-attr/_expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<noscript><a href="</noscript><script>throw new Error('fooo')</script>">test</a></noscript> |
3 changes: 3 additions & 0 deletions
3
packages/svelte/test/server-side-rendering/samples/escaped-attr/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<noscript> | ||
<a href="</noscript><script>throw new Error('fooo')</script>">test</a> | ||
</noscript> |