-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ssr): template class binding @ W-17219264 (#4931)
- Loading branch information
Showing
15 changed files
with
121 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
6 changes: 6 additions & 0 deletions
6
...es/@lwc/engine-server/src/__tests__/fixtures/attribute-class/unstyled/array/expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<x-parent> | ||
<template shadowrootmode="open"> | ||
<button class="button__icon"> | ||
</button> | ||
</template> | ||
</x-parent> |
3 changes: 3 additions & 0 deletions
3
packages/@lwc/engine-server/src/__tests__/fixtures/attribute-class/unstyled/array/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const tagName = 'x-parent'; | ||
export { default } from 'x/parent'; | ||
export * from 'x/parent'; |
3 changes: 3 additions & 0 deletions
3
...server/src/__tests__/fixtures/attribute-class/unstyled/array/modules/x/parent/parent.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<button class={computedClassNames}></button> | ||
</template> |
9 changes: 9 additions & 0 deletions
9
...e-server/src/__tests__/fixtures/attribute-class/unstyled/array/modules/x/parent/parent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class CustomButton extends LightningElement { | ||
get computedClassNames() { | ||
return [{ | ||
button__icon: true | ||
}] | ||
} | ||
} |
Empty file.
6 changes: 6 additions & 0 deletions
6
...s/@lwc/engine-server/src/__tests__/fixtures/attribute-class/unstyled/object/expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<x-parent> | ||
<template shadowrootmode="open"> | ||
<button class="button__icon"> | ||
</button> | ||
</template> | ||
</x-parent> |
3 changes: 3 additions & 0 deletions
3
packages/@lwc/engine-server/src/__tests__/fixtures/attribute-class/unstyled/object/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const tagName = 'x-parent'; | ||
export { default } from 'x/parent'; | ||
export * from 'x/parent'; |
3 changes: 3 additions & 0 deletions
3
...erver/src/__tests__/fixtures/attribute-class/unstyled/object/modules/x/parent/parent.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<button class={computedClassNames}></button> | ||
</template> |
9 changes: 9 additions & 0 deletions
9
...-server/src/__tests__/fixtures/attribute-class/unstyled/object/modules/x/parent/parent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class CustomButton extends LightningElement { | ||
get computedClassNames() { | ||
return { | ||
button__icon: true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
import { isArray, isObject, isString, isUndefined, keys, isNull, StringTrim } from './language'; | ||
|
||
/** | ||
* [ncls] - Normalize class name attribute. | ||
* | ||
* Transforms the provided class property value from an object/string into a string the diffing algo | ||
* can operate on. | ||
* | ||
* This implementation is borrowed from Vue: | ||
* https://github.com/vuejs/core/blob/e790e1bdd7df7be39e14780529db86e4da47a3db/packages/shared/src/normalizeProp.ts#L63-L82 | ||
*/ | ||
export function normalizeClass(value: unknown): string | undefined { | ||
if (isUndefined(value) || isNull(value)) { | ||
// Returning undefined here improves initial render cost, because the old vnode's class will be considered | ||
// undefined in the `patchClassAttribute` routine, so `oldClass === newClass` will be true so we return early | ||
return undefined; | ||
} | ||
|
||
let res = ''; | ||
|
||
if (isString(value)) { | ||
res = value; | ||
} else if (isArray(value)) { | ||
for (let i = 0; i < value.length; i++) { | ||
const normalized = normalizeClass(value[i]); | ||
if (normalized) { | ||
res += normalized + ' '; | ||
} | ||
} | ||
} else if (isObject(value) && !isNull(value)) { | ||
// Iterate own enumerable keys of the object | ||
const _keys = keys(value); | ||
for (let i = 0; i < _keys.length; i += 1) { | ||
const key = _keys[i]; | ||
if ((value as Record<string, unknown>)[key]) { | ||
res += key + ' '; | ||
} | ||
} | ||
} | ||
|
||
return StringTrim.call(res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters