Skip to content

feat(html/unstable): add escapeJs and escapeCss functions #6782

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lionel-rowe
Copy link
Contributor

Resolves #6748

@lionel-rowe lionel-rowe requested a review from kt3k as a code owner August 2, 2025 11:41
@github-actions github-actions bot added the html label Aug 2, 2025
@lionel-rowe lionel-rowe force-pushed the html-escaping-modes branch from ebfc40b to c998523 Compare August 2, 2025 11:47
Copy link

codecov bot commented Aug 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.14%. Comparing base (c3331d5) to head (c998523).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6782   +/-   ##
=======================================
  Coverage   94.14%   94.14%           
=======================================
  Files         589      591    +2     
  Lines       42540    42564   +24     
  Branches     6717     6721    +4     
=======================================
+ Hits        40050    40073   +23     
- Misses       2440     2442    +2     
+ Partials       50       49    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kt3k
Copy link
Member

kt3k commented Aug 18, 2025

escapeCss looks good to me.

* );
* ```
*/
export function escapeJs(data: unknown, options: EscapeJsOptions = {}): string {
Copy link
Member

Choose a reason for hiding this comment

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

I don't see why data accepts JSON-serializable object. Shouldn't this be escapeJs(str: string): string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes it a lot more ergonomic to pass arbitrary(-ish) data:

// might be nested arbitrarily deeply
declare const data: string | number | Record<string, string> | Record<string, Record<string, string>>
const scriptContentToInject = `<script>window.handleData(${escapeJs(data)})</script>`

Or (because the output is also JSON-compatible):

const importMap = { imports: { zod: 'https://esm.sh/v131/[email protected]' } }
const importMapScriptTag = `<script type="importmap">${escapeJs(importMap)}</script>`

Copy link
Member

Choose a reason for hiding this comment

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

Ok. Makes sense. Can you add those as usage examples? Otherwise, I guess users might not get how to use it.

@kt3k kt3k changed the title feat(html): add escapeJs and escapeCss functions feat(html/unstable): add escapeJs and escapeCss functions Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a different escaping mode for script + style tags in escapeHtml
2 participants