npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ashkiani/web-context-encoder

v1.0.0

Published

Context-specific encoding utilities for HTML and other web output contexts.

Readme

@ashkiani/web-context-encoder

Context-specific encoding utilities for HTML and other web output contexts.

Provides small, dependency-free helpers for encoding dynamic values before inserting them into generated output.

The current release supports HTML text and quoted ordinary HTML attribute contexts.


Installation

npm install @ashkiani/web-context-encoder

Why context-specific encoding?

Dynamic values inserted directly into generated HTML may be interpreted as markup or executable content instead of being displayed as text.

Output encoding converts characters that have special meaning in the destination context into a safe representation. Because HTML, JavaScript, CSS, and URLs are parsed differently, the correct encoding function depends on where the value will be inserted.

For additional guidance, see the OWASP Cross Site Scripting Prevention Cheat Sheet.


Usage

HTML text

Use encodeHtmlText() for a value placed between normal HTML tags:

const { encodeHtmlText } = require('@ashkiani/web-context-encoder');

const text = '<h1>some text</h1>';
const html = `<p>${encodeHtmlText(text)}</p>`;

The generated HTML contains:

<p>&lt;h1&gt;some text&lt;/h1&gt;</p>

The browser or email client displays the submitted value as text instead of interpreting it as HTML.

Quoted HTML attributes

Use encodeHtmlAttribute() for a value placed inside a quoted ordinary HTML attribute:

const { encodeHtmlAttribute } = require('@ashkiani/web-context-encoder');

const title = 'Internal "test"';
const html = `<div title="${encodeHtmlAttribute(title)}">Example</div>`;

Always quote the attribute value and use a hardcoded, ordinary attribute name.


API

encodeHtmlText(value)

Encodes a value for placement as plain text between normal HTML tags.

The following characters are encoded:

| Character | Encoded value | | --------- | ------------- | | & | &amp; | | < | &lt; | | > | &gt; | | " | &quot; | | ' | &#39; |

null and undefined are returned as an empty string. Other values are converted to strings before encoding.

encodeHtmlAttribute(value)

Encodes a value for placement inside a quoted ordinary HTML attribute. Letters and numbers are preserved; other characters are converted to hexadecimal HTML entities.

null and undefined are returned as an empty string. Other values are converted to strings before encoding.


Important security boundaries

Encoding is context-specific. These functions are intended only for the contexts documented above.

They do not make values safe for:

  • JavaScript or <script> blocks
  • Event-handler attributes such as onclick or onerror
  • CSS or style attributes
  • URLs without separate URL and protocol validation
  • Intentionally permitted HTML markup, which requires sanitization rather than encoding

This package performs output encoding. It does not replace input validation or HTML sanitization.

Encode values when generating output. Do not pre-encode values before storing them, and avoid encoding the same value more than once.


Testing

npm test

License

MIT © Siavash Ashkiani