-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(engine): avoid non-string scope tokens (#4519)
- Loading branch information
1 parent
777e84c
commit 5355a9c
Showing
6 changed files
with
143 additions
and
1 deletion.
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
76 changes: 76 additions & 0 deletions
76
packages/@lwc/integration-karma/test/rendering/sanitize-stylesheet-token/index.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,76 @@ | ||
import { createElement, setFeatureFlagForTest } from 'lwc'; | ||
import { catchUnhandledRejectionsAndErrors } from 'test-utils'; | ||
import Component from 'x/component'; | ||
|
||
let caughtError; | ||
let logger; | ||
|
||
catchUnhandledRejectionsAndErrors((error) => { | ||
caughtError = error; | ||
}); | ||
|
||
beforeEach(() => { | ||
logger = spyOn(console, 'warn'); | ||
}); | ||
|
||
afterEach(() => { | ||
caughtError = undefined; | ||
}); | ||
|
||
const props = ['stylesheetToken', 'stylesheetTokens', 'legacyStylesheetToken']; | ||
|
||
props.forEach((prop) => { | ||
describe(prop, () => { | ||
beforeEach(() => { | ||
setFeatureFlagForTest('ENABLE_LEGACY_SCOPE_TOKENS', prop === 'legacyStylesheetToken'); | ||
}); | ||
|
||
afterEach(() => { | ||
setFeatureFlagForTest('ENABLE_LEGACY_SCOPE_TOKENS', false); | ||
// We keep a cache of parsed static fragments; these need to be reset | ||
// since they can vary based on whether we use the legacy scope token or not. | ||
window.__lwcResetFragmentCache(); | ||
// Reset template object for clean state between tests | ||
Component.resetTemplate(); | ||
}); | ||
|
||
it('W-16614556 should not render arbitrary content via stylesheet token', async () => { | ||
const elm = createElement('x-component', { is: Component }); | ||
elm.propToUse = prop; | ||
try { | ||
document.body.appendChild(elm); | ||
} catch (err) { | ||
// In synthetic custom element lifecycle, the error is thrown synchronously on `appendChild` | ||
caughtError = err; | ||
} | ||
|
||
await Promise.resolve(); | ||
|
||
if (process.env.NATIVE_SHADOW && process.env.DISABLE_STATIC_CONTENT_OPTIMIZATION) { | ||
// If we're rendering in native shadow and the static content optimization is disabled, | ||
// then there's no problem with non-string stylesheet tokens because they are only rendered | ||
// as class attribute values using either `classList` or `setAttribute` (and this only applies | ||
// when `*.scoped.css` is being used). | ||
expect(elm.shadowRoot.children.length).toBe(1); | ||
} else { | ||
expect(elm.shadowRoot.children.length).toBe(0); // does not render | ||
|
||
expect(caughtError).not.toBeUndefined(); | ||
expect(caughtError.message).toMatch( | ||
/stylesheet token must be a string|Failed to execute 'setAttribute'|Invalid qualified name|String contains an invalid character|The string contains invalid characters/ | ||
); | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
// no warnings in prod mode | ||
expect(logger).not.toHaveBeenCalled(); | ||
} else { | ||
// dev mode | ||
expect(logger).toHaveBeenCalledTimes(1); | ||
expect(logger.calls.allArgs()[0]).toMatch( | ||
new RegExp(`Mutating the "${prop}" property on a template is deprecated`) | ||
); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
...@lwc/integration-karma/test/rendering/sanitize-stylesheet-token/x/component/component.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,3 @@ | ||
div { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
...lwc/integration-karma/test/rendering/sanitize-stylesheet-token/x/component/component.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> | ||
<div></div> | ||
</template> |
46 changes: 46 additions & 0 deletions
46
.../@lwc/integration-karma/test/rendering/sanitize-stylesheet-token/x/component/component.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,46 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
import template from './component.html'; | ||
|
||
export default class Component extends LightningElement { | ||
@api propToUse; | ||
|
||
count = 0; | ||
|
||
render() { | ||
const token = { | ||
[Symbol.toPrimitive]: () => { | ||
if (this.count === 0) { | ||
return `yolo-${++this.count}`; | ||
} else { | ||
return `yolo=yolo-${++this.count}`; | ||
} | ||
}, | ||
}; | ||
|
||
const { propToUse } = this; | ||
if (propToUse === 'stylesheetTokens') { | ||
// this legacy format uses an object | ||
template[propToUse] = { | ||
hostAttribute: token, | ||
shadowAttribute: token, | ||
}; | ||
} else { | ||
// stylesheetToken or legacyStylesheetToken | ||
// this format uses a string | ||
template[propToUse] = token; | ||
} | ||
|
||
return template; | ||
} | ||
} | ||
|
||
// Reset template object for clean state between tests | ||
const { stylesheetToken, stylesheetTokens, legacyStylesheetToken } = template; | ||
|
||
Component.resetTemplate = () => { | ||
Object.assign(template, { | ||
stylesheetToken, | ||
stylesheetTokens, | ||
legacyStylesheetToken, | ||
}); | ||
}; |