Skip to content

Commit 00da41b

Browse files
authored
fix(enginer-server): HTML escape (salesforce#1972)
1 parent 9002e6c commit 00da41b

File tree

11 files changed

+61
-3
lines changed

11 files changed

+61
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"props": {
3+
"attr": "\"></div>This 'should' be escaped<div attr=\""
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<x-attribute-dynamic-escape>
2+
<template shadowroot="open">
3+
<div
4+
data-attr='"&gt;&lt;/div&gt;This &#x27;should&#x27; be escaped&lt;div attr="'
5+
></div>
6+
</template>
7+
</x-attribute-dynamic-escape>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div data-attr={attr}></div>
3+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { LightningElement, api } from 'lwc';
2+
3+
export default class AttributeDynamicEscape extends LightningElement {
4+
@api attr;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"props": {
3+
"text": "I <b>should</b> escape 'this' & \"that\""
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<x-text-interpolation-escape>
2+
<template shadowroot="open">
3+
I &lt;b&gt;should&lt;/b&gt; escape &#x27;this&#x27; &amp; &quot;that&quot;
4+
</template>
5+
</x-text-interpolation-escape>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
{text}
3+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { LightningElement, api } from 'lwc';
2+
3+
export default class TextInterpolationEscape extends LightningElement {
4+
@api text;
5+
}

packages/@lwc/engine-server/src/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const renderer: Renderer<HostNode, HostElement> = {
6666
createText(content: string): HostNode {
6767
return {
6868
type: HostNodeType.Text,
69-
value: content,
69+
value: String(content),
7070
parent: null,
7171
};
7272
},

packages/@lwc/engine-server/src/serializer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
*/
77

88
import { isVoidElement } from '@lwc/shared';
9+
10+
import { htmlEscape } from './utils/html-escape';
911
import { HostElement, HostShadowRoot, HostAttribute, HostChildNode, HostNodeType } from './types';
1012

1113
function serializeAttributes(attributes: HostAttribute[]): string {
1214
return attributes
1315
.map((attr) =>
14-
attr.value.length ? `${attr.name}=${JSON.stringify(attr.value)}` : attr.name
16+
attr.value.length ? `${attr.name}=${JSON.stringify(htmlEscape(attr.value))}` : attr.name
1517
)
1618
.join(' ');
1719
}
@@ -21,7 +23,7 @@ function serializeChildNodes(children: HostChildNode[]): string {
2123
.map((child) => {
2224
switch (child.type) {
2325
case HostNodeType.Text:
24-
return child.value;
26+
return htmlEscape(child.value);
2527
case HostNodeType.Element:
2628
return serializeElement(child);
2729
}

0 commit comments

Comments
 (0)