From 5f55af9cce1229713fd462f064185545ea6e4d3e Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 10 Mar 2022 14:36:07 -0600 Subject: [PATCH] fix(#2753): unescape `define:vars` (#2756) * fix(#2753): unescape `define:vars` * test: add directives suite * chore: update lockfile --- packages/astro/src/runtime/server/index.ts | 4 ++-- packages/astro/test/astro-directives.test.js | 20 +++++++++++++++++++ .../fixtures/astro-directives/package.json | 8 ++++++++ .../src/pages/define-vars.astro | 12 +++++++++++ pnpm-lock.yaml | 6 ++++++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/astro/test/astro-directives.test.js create mode 100644 packages/astro/test/fixtures/astro-directives/package.json create mode 100644 packages/astro/test/fixtures/astro-directives/src/pages/define-vars.astro 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) { 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) { 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' +--- + + + + + + + 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:*