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(ssr): use htmlEscape consistently #5116

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all 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
15 changes: 10 additions & 5 deletions packages/@lwc/ssr-runtime/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { getOwnPropertyNames, isNull, isString, isUndefined, DEFAULT_SSR_MODE } from '@lwc/shared';
import {
getOwnPropertyNames,
isNull,
isString,
isUndefined,
DEFAULT_SSR_MODE,
htmlEscape,
} from '@lwc/shared';
import { mutationTracker } from './mutation-tracker';
import { SYMBOL__GENERATE_MARKUP } from './lightning-element';
import type { LightningElement, LightningElementConstructor } from './lightning-element';
import type { Attributes, Properties } from './types';

const escapeAttrVal = (attrValue: string) =>
attrValue.replaceAll('&', '&').replaceAll('"', '"');

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For comparison:

export function htmlEscape(str: string, attrMode: boolean = false): string {
const searchValue = attrMode ? /["&]/g : /["'<>&]/g;
return str.replace(searchValue, (char) => ESCAPED_CHARS[char]);
}

function renderAttrsPrivate(
instance: LightningElement,
attrs: Attributes,
Expand Down Expand Up @@ -58,7 +62,8 @@ function renderAttrsPrivate(
}
}

result += attrValue === '' ? ` ${attrName}` : ` ${attrName}="${escapeAttrVal(attrValue)}"`;
result +=
attrValue === '' ? ` ${attrName}` : ` ${attrName}="${htmlEscape(attrValue, true)}"`;
}

// If we didn't render any `class` attribute, render one for the scope token(s)
Expand Down