0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(#2753): unescape define:vars (#2756)

* fix(#2753): unescape `define:vars`

* test: add directives suite

* chore: update lockfile
This commit is contained in:
Nate Moore 2022-03-10 14:36:07 -06:00 committed by GitHub
parent 7b18d4c226
commit 5f55af9cce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 2 deletions

View file

@ -391,7 +391,7 @@ export function defineStyleVars(selector: string, vars: Record<any, any>) {
for (const [key, value] of Object.entries(vars)) {
output += ` --${key}: ${value};\n`;
}
return `${selector} {${output}}`;
return unescapeHTML(`${selector} {${output}}`);
}
// Adds variables to an inline script.
@ -400,7 +400,7 @@ export function defineScriptVars(vars: Record<any, any>) {
for (const [key, value] of Object.entries(vars)) {
output += `let ${key} = ${JSON.stringify(value)};\n`;
}
return output;
return unescapeHTML(output);
}
// Renders an endpoint request to completion, returning the body.

View file

@ -0,0 +1,20 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Directives', async () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-directives/' });
await fixture.build();
});
it('Passes define:vars to script elements', async () => {
const html = await fixture.readFile('/define-vars/index.html');
const $ = cheerio.load(html);
expect($('script#inline')).to.have.lengthOf(1);
expect($('script#inline').toString()).to.include('let foo = "bar"');
});
});

View file

@ -0,0 +1,8 @@
{
"name": "@astrojs/test-astro-directives",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,12 @@
---
let foo = 'bar'
---
<html>
<head></head>
<body>
<script id="inline" define:vars={{ foo }}>
console.log(foo);
</script>
</body>
</html>

View file

@ -582,6 +582,12 @@ importers:
dependencies:
astro: link:../../..
packages/astro/test/fixtures/astro-directives:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/astro-doctype:
specifiers:
astro: workspace:*