-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serializer): replace LWC scope tokens (BREAKING CHANGE) (#185)
- Loading branch information
1 parent
a73e0f4
commit 434a43c
Showing
17 changed files
with
399 additions
and
113 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
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
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
18 changes: 18 additions & 0 deletions
18
packages/@lwc/jest-serializer/src/clean-element-classes.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,18 @@ | ||
/* | ||
* Copyright (c) 2023, 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 | ||
*/ | ||
|
||
const { isKnownScopeToken } = require('@lwc/jest-shared'); | ||
|
||
function cleanElementClasses(elm) { | ||
for (const name of [...elm.classList]) { | ||
if (isKnownScopeToken(name)) { | ||
elm.classList.replace(name, '__lwc_scope_token__'); | ||
} | ||
} | ||
} | ||
|
||
module.exports = cleanElementClasses; |
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,16 @@ | ||
/* | ||
* Copyright (c) 2023, 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 | ||
*/ | ||
|
||
const { getKnownScopeTokens } = require('@lwc/jest-shared'); | ||
|
||
function cleanStyleElement(elm) { | ||
// attributes in the HTML namespace are case-insensitive, so the regex must be case-insensitive | ||
const regex = new RegExp(getKnownScopeTokens().join('|'), 'gi'); | ||
elm.textContent = elm.textContent.replace(regex, '__lwc_scope_token__'); | ||
} | ||
|
||
module.exports = cleanStyleElement; |
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
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
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
57 changes: 57 additions & 0 deletions
57
test/src/modules/serializer/styledMultiple/__tests__/styledMultiple.spec.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,57 @@ | ||
/* | ||
* Copyright (c) 2023, 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 { createElement } from 'lwc'; | ||
import StyledMultiple from '../styledMultiple'; | ||
|
||
it('serializes component with HTML - styled in shadow DOM - multiple attrs/classes/styles', () => { | ||
const elm = createElement('serializer-component', { is: StyledMultiple }); | ||
document.body.appendChild(elm); | ||
|
||
if (global['lwc-jest'].nativeShadow) { | ||
expect(elm).toMatchInlineSnapshot(` | ||
<serializer-component | ||
class="__lwc_scope_token__" | ||
> | ||
#shadow-root(open) | ||
<style | ||
type="text/css" | ||
> | ||
:host {opacity: 0.5;}h1 {color: red;}.foo {background: azure;} | ||
</style> | ||
<style | ||
type="text/css" | ||
> | ||
:host {color: goldenrod;}h1.__lwc_scope_token__ {background: blue;}.foo.__lwc_scope_token__ {opacity: 0.7;} | ||
</style> | ||
<h1 | ||
class="foo bar __lwc_scope_token__" | ||
data-bar="bar" | ||
data-foo="foo" | ||
> | ||
I am an LWC component with multiple classes, attributes, and styles | ||
</h1> | ||
</serializer-component> | ||
`); | ||
} else { | ||
expect(elm).toMatchInlineSnapshot(` | ||
<serializer-component | ||
__lwc_scope_token__="" | ||
class="__lwc_scope_token__" | ||
> | ||
#shadow-root(open) | ||
<h1 | ||
__lwc_scope_token__="" | ||
class="foo bar __lwc_scope_token__" | ||
data-bar="bar" | ||
data-foo="foo" | ||
> | ||
I am an LWC component with multiple classes, attributes, and styles | ||
</h1> | ||
</serializer-component> | ||
`); | ||
} | ||
}); |
11 changes: 11 additions & 0 deletions
11
test/src/modules/serializer/styledMultiple/styledMultiple.css
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,11 @@ | ||
:host { | ||
opacity: 0.5; | ||
} | ||
|
||
h1 { | ||
color: red; | ||
} | ||
|
||
.foo { | ||
background: azure; | ||
} |
7 changes: 7 additions & 0 deletions
7
test/src/modules/serializer/styledMultiple/styledMultiple.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,7 @@ | ||
<template> | ||
<h1 | ||
class="foo bar" | ||
data-foo="foo" | ||
data-bar="bar" | ||
>I am an LWC component with multiple classes, attributes, and styles</h1> | ||
</template> |
11 changes: 11 additions & 0 deletions
11
test/src/modules/serializer/styledMultiple/styledMultiple.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,11 @@ | ||
/* | ||
* Copyright (c) 2018, 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 { LightningElement } from 'lwc'; | ||
|
||
export default class extends LightningElement { | ||
} |
11 changes: 11 additions & 0 deletions
11
test/src/modules/serializer/styledMultiple/styledMultiple.scoped.css
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,11 @@ | ||
:host { | ||
color: goldenrod; | ||
} | ||
|
||
h1 { | ||
background: blue; | ||
} | ||
|
||
.foo { | ||
opacity: 0.7; | ||
} |
Oops, something went wrong.