-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate unescaped HTML inside of expressions (#2489)
* feat: implement automatic escaping * feat: deprecate automatic escaping * fix: cast unescapeHTML as string * fix: slot fallback behavior * fix: unescaped content * Update escape.ts * Update escape.ts * feat: update internal components to use `set:html` * chore: update compiler * chore: update changeset
- Loading branch information
1 parent
b9da87a
commit 618a16f
Showing
9 changed files
with
74 additions
and
31 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,8 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Add support for the `set:html` and `set:text` directives. | ||
|
||
With the introduction of these directives, unescaped HTML content in expressions is now deprecated. Please migrate to `set:html` in order to continue injecting unescaped HTML in future versions of Astro—you can use `<Fragment set:html={content}>` to avoid a wrapper element. `set:text` allows you to opt-in to escaping now, but it will soon become the default. | ||
|
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 |
---|---|---|
@@ -1,3 +1,34 @@ | ||
const entities = { '"': 'quot', '&': 'amp', "'": 'apos', '<': 'lt', '>': 'gt' } as const; | ||
|
||
export const escapeHTML = (string: any) => string.replace(/["'&<>]/g, (char: keyof typeof entities) => '&' + entities[char] + ';'); | ||
const warned = new Set<string>(); | ||
export const escapeHTML = (string: any, { deprecated = false }: { deprecated?: boolean } = {}) => { | ||
const escaped = string.replace(/["'&<>]/g, (char: keyof typeof entities) => '&' + entities[char] + ';'); | ||
if (!deprecated) return escaped; | ||
if (warned.has(string) || !string.match(/[&<>]/g)) return string; | ||
// eslint-disable-next-line no-console | ||
console.warn(`Unescaped HTML content found inside expression! | ||
The next minor version of Astro will automatically escape all | ||
expression content. Please use the \`set:html\` directive. | ||
Expression content: | ||
${string}`); | ||
warned.add(string); | ||
|
||
// Return unescaped content for now. To be removed. | ||
return string; | ||
} | ||
|
||
/** | ||
* RawString is a "blessed" version of String | ||
* that is not subject to escaping. | ||
*/ | ||
export class UnescapedString extends String {} | ||
|
||
/** | ||
* unescapeHTML marks a string as raw, unescaped HTML. | ||
* This should only be generated internally, not a public API. | ||
* | ||
* Need to cast the return value `as unknown as string` so TS doesn't yell at us. | ||
*/ | ||
export const unescapeHTML = (str: any) => new UnescapedString(str) as unknown as string; |
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