fix object styles not escaped (#4887)
* fix object styles not escaped * fix `shouldEscape` not passed down * add tests * fix package name * fix pnpm-lock * add changeset
This commit is contained in:
parent
4f73b98ae0
commit
37cb2fc02a
8 changed files with 71 additions and 2 deletions
5
.changeset/swift-moles-crash.md
Normal file
5
.changeset/swift-moles-crash.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
fix object styles not escaped
|
|
@ -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 '';
|
||||
}
|
||||
|
@ -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
|
||||
|
|
32
packages/astro/test/astro-object-style.test.js
Normal file
32
packages/astro/test/astro-object-style.test.js
Normal file
|
@ -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
packages/astro/test/fixtures/astro-object-style/package.json
vendored
Normal file
8
packages/astro/test/fixtures/astro-object-style/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/astro-object-style",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
1
packages/astro/test/fixtures/astro-object-style/src/components/Span.astro
vendored
Normal file
1
packages/astro/test/fixtures/astro-object-style/src/components/Span.astro
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
<span {...Astro.props} />
|
10
packages/astro/test/fixtures/astro-object-style/src/pages/component.astro
vendored
Normal file
10
packages/astro/test/fixtures/astro-object-style/src/pages/component.astro
vendored
Normal file
|
@ -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")'}} />
|
7
packages/astro/test/fixtures/astro-object-style/src/pages/index.astro
vendored
Normal file
7
packages/astro/test/fixtures/astro-object-style/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<span style="background-color:green" />
|
||||
|
||||
<span style={"background-color:red"} />
|
||||
|
||||
<span style={{backgroundColor:"blue"}} />
|
||||
|
||||
<span style={{backgroundImage:'url("a")'}} />
|
|
@ -1426,6 +1426,12 @@ importers:
|
|||
dependencies:
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/astro-object-style:
|
||||
specifiers:
|
||||
astro: workspace:*
|
||||
dependencies:
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/astro-page-directory-url:
|
||||
specifiers:
|
||||
astro: workspace:*
|
||||
|
|
Loading…
Reference in a new issue