Fix nested client load directive (#1030)
* escape </script> in string literals * add changeset
This commit is contained in:
parent
bef7247812
commit
618ea3a8ea
2 changed files with 21 additions and 2 deletions
5
.changeset/fair-dogs-tie.md
Normal file
5
.changeset/fair-dogs-tie.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Properly escapes script tags with nested client:load directives when passing Astro components into framework components via props. Browsers interpret script end tags in strings as script end tags, resulting in syntax errors.
|
|
@ -1,12 +1,26 @@
|
|||
import type { Renderer, AstroComponentMetadata } from '../@types/astro';
|
||||
import hash from 'shorthash';
|
||||
import { valueToEstree, Value } from 'estree-util-value-to-estree';
|
||||
import { generate } from 'astring';
|
||||
import { generate, GENERATOR, Generator } from 'astring';
|
||||
import * as astroHtml from './renderer-html';
|
||||
|
||||
// A more robust version alternative to `JSON.stringify` that can handle most values
|
||||
// see https://github.com/remcohaszing/estree-util-value-to-estree#readme
|
||||
const serialize = (value: Value) => generate(valueToEstree(value));
|
||||
const customGenerator: Generator = {
|
||||
...GENERATOR,
|
||||
Literal(node, state) {
|
||||
if (node.raw != null) {
|
||||
// escape closing script tags in strings so browsers wouldn't interpret them as
|
||||
// closing the actual end tag in HTML
|
||||
state.write(node.raw.replace('</script>', '<\\/script>'));
|
||||
} else {
|
||||
GENERATOR.Literal(node, state);
|
||||
}
|
||||
},
|
||||
};
|
||||
const serialize = (value: Value) => generate(valueToEstree(value), {
|
||||
generator: customGenerator,
|
||||
});
|
||||
|
||||
export interface RendererInstance {
|
||||
source: string | null;
|
||||
|
|
Loading…
Reference in a new issue