summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2022-03-10 14:36:07 -0600
committerGravatar GitHub <noreply@github.com> 2022-03-10 14:36:07 -0600
commit5f55af9cce1229713fd462f064185545ea6e4d3e (patch)
treeb93807c63f8e0ca122e00e0b9233394c2430cebd
parent7b18d4c2264eaf8c243308fcc59ce9c2cbf3334f (diff)
downloadastro-5f55af9cce1229713fd462f064185545ea6e4d3e.tar.gz
astro-5f55af9cce1229713fd462f064185545ea6e4d3e.tar.zst
astro-5f55af9cce1229713fd462f064185545ea6e4d3e.zip
fix(#2753): unescape `define:vars` (#2756)
* fix(#2753): unescape `define:vars` * test: add directives suite * chore: update lockfile
-rw-r--r--packages/astro/src/runtime/server/index.ts4
-rw-r--r--packages/astro/test/astro-directives.test.js20
-rw-r--r--packages/astro/test/fixtures/astro-directives/package.json8
-rw-r--r--packages/astro/test/fixtures/astro-directives/src/pages/define-vars.astro12
-rw-r--r--pnpm-lock.yaml6
5 files changed, 48 insertions, 2 deletions
diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts
index d2a022805..b81f88933 100644
--- a/packages/astro/src/runtime/server/index.ts
+++ b/packages/astro/src/runtime/server/index.ts
@@ -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.
diff --git a/packages/astro/test/astro-directives.test.js b/packages/astro/test/astro-directives.test.js
new file mode 100644
index 000000000..9fb2d5c6b
--- /dev/null
+++ b/packages/astro/test/astro-directives.test.js
@@ -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"');
+ });
+});
diff --git a/packages/astro/test/fixtures/astro-directives/package.json b/packages/astro/test/fixtures/astro-directives/package.json
new file mode 100644
index 000000000..adb003a59
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-directives/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "@astrojs/test-astro-directives",
+ "version": "0.0.0",
+ "private": true,
+ "dependencies": {
+ "astro": "workspace:*"
+ }
+}
diff --git a/packages/astro/test/fixtures/astro-directives/src/pages/define-vars.astro b/packages/astro/test/fixtures/astro-directives/src/pages/define-vars.astro
new file mode 100644
index 000000000..a6cc18ec6
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-directives/src/pages/define-vars.astro
@@ -0,0 +1,12 @@
+---
+let foo = 'bar'
+---
+
+<html>
+ <head></head>
+ <body>
+ <script id="inline" define:vars={{ foo }}>
+ console.log(foo);
+ </script>
+ </body>
+</html>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 96d7cf5fb..1b239e7af 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -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:*