Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(template-compiler): normalize class and style attribute values #4553

Merged
merged 8 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</div>
<div style="border-width: 1px; border-style: solid; border-color: red;">
</div>
<div style="border-width: 1px !important; border-style: solid; border-color: red !important;">
<div style="border-width: 1px !important; border-style: solid; border-color: red !important;">
</div>
<div style="color: salmon; background-color: chocolate;">
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<svg height="150" width="400">
<defs>
<linearGradient id="grad1" x1="0%" x2="100%" y1="0%" y2="0%">
<stop class="static" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"/>
<stop class="static" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"/>
<stop class="static" offset="0%" style="stop-color: rgb(255,255,0); stop-opacity: 1;"/>
<stop class="static" offset="100%" style="stop-color: rgb(255,0,0); stop-opacity: 1;"/>
</linearGradient>
</defs>
<ellipse class="static" cx="200" cy="70" fill="url(#grad1)" rx="85" ry="55"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createElement } from 'lwc';
import Component from 'x/component';

describe('style and class whitespace normalization', () => {
it('should normalize style whitespace', async () => {
const elm = createElement('x-component', { is: Component });
document.body.appendChild(elm);
await Promise.resolve();

const actual = [...elm.shadowRoot.querySelectorAll('[style]')].map((elm) =>
elm.getAttribute('style')
);
expect(actual).toEqual([
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red !important;',
'color: red;',
'color: red; background-color: aqua;',
'color: red; background-color: aqua;',
'--its-a-tab: red;',
'--its-a-tab-and-a-space: red;',
]);
});
it('should normalize class whitespace', async () => {
const elm = createElement('x-component', { is: Component });
document.body.appendChild(elm);
await Promise.resolve();

const actual = [...elm.shadowRoot.querySelectorAll('[class]')].map((elm) =>
elm.getAttribute('class')
);
expect(actual).toEqual([
'boo',
'boo',
'foo bar',
'foo bar',
'foo bar',
'foo bar',
'foo bar',
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div style="color: red !important "></div>
<div style="color: red !important ;"></div>
<div style="color: red !important "></div>
<div style="color: red !important ;"></div>
<div style="color: red ! important;"></div>
<div style="color: red ! important"></div>
<div style="color: red !IMPORTANT "></div>
<div style="color: red !IMPORTANT ;"></div>
<div style="color: red !IMPORTANT "></div>
<div style="color: red !IMPORTANT ;"></div>
<div style="color: red ! IMPORTANT;"></div>
<div style="color: red ! IMPORTANT"></div>
<div style=" color : red ! IMPORTANT ; "></div>
<div style=" color : red ! IMPORTAnt ; "></div>
<div style=" color : red ; "></div>
<div style="color: red; background-color: aqua "></div>
<div style="color: red ; background-color: aqua; "></div>
<div style=" --its-a-tab: red ; "></div>
<div style=" --its-a-tab-and-a-space: red ; "></div>
<div class=" boo "></div>
<div class=" boo"></div>
<div class=" foo bar "></div>
<div class="foo bar"></div>
<div class=" foo bar "></div>
<div class=" foo bar "></div>
<div class=" foo bar "></div>
</template>
ekashida marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import _implicitScopedStylesheets from "./class.scoped.css?scoped=true";
import { freezeTemplate, parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<div class="foo${0}"${2}></div>`;
const $fragment2 = parseFragment`<div class="foo bar${0}"${2}></div>`;
const $fragment3 = parseFragment`<div class=" foo bar ${0}"${2}></div>`;
const $fragment4 = parseFragment`<div class="foo bar${0}"${2}></div>`;
const $fragment3 = parseFragment`<div class="foo bar${0}"${2}></div>`;
const $fragment4 = parseFragment`<div class="foo bar${0}"${2}></div>`;
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment } = $api;
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _implicitStylesheets from "./class.css";
import _implicitScopedStylesheets from "./class.scoped.css?scoped=true";
import { freezeTemplate, parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<section class="foo bar baz-fiz${0}"${2}></section>`;
const $fragment1 = parseFragment`<section class="foo bar baz-fiz${0}"${2}></section>`;
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment } = $api;
return [api_static_fragment($fragment1, 1)];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<template>
<div style="background: blue !important; color: red; opacity: 0.5 !important"></div>
<div style="background: blue !IMPORTANT; color: red; opacity: 0.5 !IMPORTANT"></div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"location": {
"startLine": 1,
"startColumn": 1,
"endLine": 3,
"endLine": 4,
"endColumn": 12,
"start": 0,
"end": 111,
"end": 200,
"startTag": {
"startLine": 1,
"startColumn": 1,
Expand All @@ -17,12 +17,12 @@
"end": 10
},
"endTag": {
"startLine": 3,
"startLine": 4,
"startColumn": 1,
"endLine": 3,
"endLine": 4,
"endColumn": 12,
"start": 100,
"end": 111
"start": 189,
"end": 200
}
},
"directives": [],
Expand Down Expand Up @@ -77,6 +77,57 @@
"directives": [],
"listeners": [],
"children": []
},
{
"type": "Element",
"name": "div",
"namespace": "http://www.w3.org/1999/xhtml",
"location": {
"startLine": 3,
"startColumn": 5,
"endLine": 3,
"endColumn": 89,
"start": 104,
"end": 188,
"startTag": {
"startLine": 3,
"startColumn": 5,
"endLine": 3,
"endColumn": 83,
"start": 104,
"end": 182
},
"endTag": {
"startLine": 3,
"startColumn": 83,
"endLine": 3,
"endColumn": 89,
"start": 182,
"end": 188
}
},
"attributes": [
{
"type": "Attribute",
"name": "style",
"value": {
"type": "Literal",
"value": "background: blue !IMPORTANT; color: red; opacity: 0.5 !IMPORTANT"
},
"location": {
"startLine": 3,
"startColumn": 10,
"endLine": 3,
"endColumn": 82,
"start": 109,
"end": 181
}
}
],
"properties": [],
"directives": [],
"listeners": [],
"children": []
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import _implicitStylesheets from "./style-important.css";
import _implicitScopedStylesheets from "./style-important.scoped.css?scoped=true";
import { freezeTemplate, parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<div style="background: blue !important; color: red; opacity: 0.5 !important"${3}></div>`;
const $fragment1 = parseFragment`<div style="background: blue !important; color: red; opacity: 0.5 !important;"${3}></div>`;
const $fragment2 = parseFragment`<div style="background: blue !important; color: red; opacity: 0.5 !important;"${3}></div>`;
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment } = $api;
return [api_static_fragment($fragment1, 1)];
return [
api_static_fragment($fragment1, 1),
api_static_fragment($fragment2, 3),
];
/*LWC compiler vX.X.X*/
}
export default registerTemplate(tmpl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<section style="font-size: 12px; color: red; margin: 10px 5px 10px"></section>
<section style="--my-color: blue; color: var(--my-color)"></section>
<section style="font-size: 12px;color:red;margin:10px 5px 10px"></section>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"location": {
"startLine": 1,
"startColumn": 1,
"endLine": 4,
"endLine": 5,
"endColumn": 12,
"start": 0,
"end": 178,
"end": 257,
"startTag": {
"startLine": 1,
"startColumn": 1,
Expand All @@ -17,12 +17,12 @@
"end": 10
},
"endTag": {
"startLine": 4,
"startLine": 5,
"startColumn": 1,
"endLine": 4,
"endLine": 5,
"endColumn": 12,
"start": 167,
"end": 178
"start": 246,
"end": 257
}
},
"directives": [],
Expand Down Expand Up @@ -128,6 +128,57 @@
"directives": [],
"listeners": [],
"children": []
},
{
"type": "Element",
"name": "section",
"namespace": "http://www.w3.org/1999/xhtml",
"location": {
"startLine": 4,
"startColumn": 5,
"endLine": 4,
"endColumn": 79,
"start": 171,
"end": 245,
"startTag": {
"startLine": 4,
"startColumn": 5,
"endLine": 4,
"endColumn": 69,
"start": 171,
"end": 235
},
"endTag": {
"startLine": 4,
"startColumn": 69,
"endLine": 4,
"endColumn": 79,
"start": 235,
"end": 245
}
},
"attributes": [
{
"type": "Attribute",
"name": "style",
"value": {
"type": "Literal",
"value": "font-size:\t12px;color:red;margin:10px 5px 10px"
},
"location": {
"startLine": 4,
"startColumn": 14,
"endLine": 4,
"endColumn": 68,
"start": 180,
"end": 234
}
}
],
"properties": [],
"directives": [],
"listeners": [],
"children": []
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import _implicitStylesheets from "./style-static.css";
import _implicitScopedStylesheets from "./style-static.scoped.css?scoped=true";
import { freezeTemplate, parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<section style="font-size: 12px; color: red; margin: 10px 5px 10px"${3}></section>`;
const $fragment2 = parseFragment`<section style="--my-color: blue; color: var(--my-color)"${3}></section>`;
const $fragment1 = parseFragment`<section style="font-size: 12px; color: red; margin: 10px 5px 10px;"${3}></section>`;
const $fragment2 = parseFragment`<section style="--my-color: blue; color: var(--my-color);"${3}></section>`;
const $fragment3 = parseFragment`<section style="font-size: 12px; color: red; margin: 10px 5px 10px;"${3}></section>`;
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment } = $api;
return [
api_static_fragment($fragment1, 1),
api_static_fragment($fragment2, 3),
api_static_fragment($fragment3, 5),
];
/*LWC compiler vX.X.X*/
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _implicitStylesheets from "./static-content-optimization.css";
import _implicitScopedStylesheets from "./static-content-optimization.scoped.css?scoped=true";
import { freezeTemplate, parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<div${3}><span${"a1:data-dynamic"}${3}></span><span data-static="bar"${3}></span><span${"s3"}${3}></span><span style="quux"${3}></span><span${3}>${"t6"}</span><span${"a7:data-dynamic"}${"s7"}${"c7"}${2}>${"t8"}</span></div>`;
const $fragment1 = parseFragment`<div${3}><span${"a1:data-dynamic"}${3}></span><span data-static="bar"${3}></span><span${"s3"}${3}></span><span style=""${3}></span><span${3}>${"t6"}</span><span${"a7:data-dynamic"}${"s7"}${"c7"}${2}>${"t8"}</span></div>`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove the style attribute in the case where its value is an empty string.

const stc0 = {
key: 0,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _implicitStylesheets from "./linear-gradient.css";
import _implicitScopedStylesheets from "./linear-gradient.scoped.css?scoped=true";
import { freezeTemplate, parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<svg height="150" width="400"${3}><defs${3}><linearGradient${"a2:id"} x1="0%" y1="0%" x2="100%" y2="0%"${3}><stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"${3}/><stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"${3}/></linearGradient></defs><ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)"${3}/></svg>`;
const $fragment1 = parseFragment`<svg height="150" width="400"${3}><defs${3}><linearGradient${"a2:id"} x1="0%" y1="0%" x2="100%" y2="0%"${3}><stop offset="0%" style="stop-color: rgb(255,255,0); stop-opacity: 1;"${3}/><stop offset="100%" style="stop-color: rgb(255,0,0); stop-opacity: 1;"${3}/></linearGradient></defs><ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)"${3}/></svg>`;
function tmpl($api, $cmp, $slotset, $ctx) {
const {
gid: api_scoped_id,
Expand Down
11 changes: 11 additions & 0 deletions packages/@lwc/template-compiler/src/codegen/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ export function parseStyleText(cssText: string): { [name: string]: string } {
return styleMap;
}

export function normalizeStyleAttribute(style: string): string {
const styleMap = parseStyleText(style);

const styles = Object.entries(styleMap).map(([key, value]) => {
value = value.replace(IMPORTANT_FLAG, ' !important').trim();
return `${key}: ${value};`;
});

return styles.join(' ');
}

const IMPORTANT_FLAG = /\s*!\s*important\s*$/i;

// Given a map of CSS property keys to values, return an array AST like:
Expand Down
Loading
Loading