Skip to content

Commit

Permalink
fix object styles not escaped (#4887)
Browse files Browse the repository at this point in the history
* fix object styles not escaped

* fix `shouldEscape` not passed down

* add tests

* fix package name

* fix pnpm-lock

* add changeset
  • Loading branch information
Calvin-LL committed Oct 4, 2022
1 parent 4f73b98 commit 37cb2fc
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-moles-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

fix object styles not escaped
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the

// support "class" from an expression passed into an element (#782)
if (key === 'class:list') {
const listValue = toAttributeString(serializeListValue(value));
const listValue = toAttributeString(serializeListValue(value), shouldEscape);
if (listValue === '') {
return '';
}
Expand All @@ -81,7 +81,7 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the

// support object styles for better JSX compat
if (key === 'style' && !(value instanceof HTMLString) && typeof value === 'object') {
return markHTMLString(` ${key}="${toStyleString(value)}"`);
return markHTMLString(` ${key}="${toAttributeString(toStyleString(value), shouldEscape)}"`);
}

// support `className` for better JSX compat
Expand Down
32 changes: 32 additions & 0 deletions packages/astro/test/astro-object-style.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Object style', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: './fixtures/astro-object-style/' });
await fixture.build();
});

it('Passes style attributes as expected to elements', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('[style="background-color:green"]')).to.have.lengthOf(1);
expect($('[style="background-color:red"]')).to.have.lengthOf(1);
expect($('[style="background-color:blue"]')).to.have.lengthOf(1);
expect($(`[style='background-image:url("a")']`)).to.have.lengthOf(1);
});

it('Passes style attributes as expected to components', async () => {
const html = await fixture.readFile('/component/index.html');
const $ = cheerio.load(html);

expect($('[style="background-color:green"]')).to.have.lengthOf(1);
expect($('[style="background-color:red"]')).to.have.lengthOf(1);
expect($('[style="background-color:blue"]')).to.have.lengthOf(1);
expect($(`[style='background-image:url("a")']`)).to.have.lengthOf(1);
});
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/astro-object-style/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-object-style",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span {...Astro.props} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import Component from '../components/Span.astro'
---
<Component style="background-color:green" />

<Component style={"background-color:red"} />

<Component style={{backgroundColor:"blue"}} />

<Component style={{backgroundImage:'url("a")'}} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<span style="background-color:green" />

<span style={"background-color:red"} />

<span style={{backgroundColor:"blue"}} />

<span style={{backgroundImage:'url("a")'}} />
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37cb2fc

Please sign in to comment.