GithubHelp home page GithubHelp logo

sindresorhus / escape-goat Goto Github PK

View Code? Open in Web Editor NEW
502.0 7.0 32.0 1.71 MB

&๐Ÿ; Escape a string for use in HTML or the inverse

License: MIT License

JavaScript 87.04% TypeScript 12.96%
goats goat caprine escape html javascript nodejs

escape-goat's Introduction

escape-goat

Escape a string for use in HTML or the inverse

Install

$ npm install escape-goat

Usage

import {htmlEscape, htmlUnescape} from 'escape-goat';

htmlEscape('๐Ÿฆ„ & ๐Ÿ');
//=> '๐Ÿฆ„ & ๐Ÿ'

htmlUnescape('๐Ÿฆ„ & ๐Ÿ');
//=> '๐Ÿฆ„ & ๐Ÿ'

htmlEscape('Hello <em>World</em>');
//=> 'Hello &lt;em&gt;World&lt;/em&gt;'

const url = 'https://sindresorhus.com?x="๐Ÿฆ„"';

htmlEscape`<a href="${url}">Unicorn</a>`;
//=> '<a href="https://sindresorhus.com?x=&quot;๐Ÿฆ„&quot;">Unicorn</a>'

const escapedUrl = 'https://sindresorhus.com?x=&quot;๐Ÿฆ„&quot;';

htmlUnescape`URL from HTML: ${escapedUrl}`;
//=> 'URL from HTML: https://sindresorhus.com?x="๐Ÿฆ„"'

API

htmlEscape(string)

Escapes the following characters in the given string argument: & < > " '

The function also works as a tagged template literal that escapes interpolated values.

Note: This method of escaping is only safe when inserting data into normal tags like body, div, p, b, td, etc. Inserting htmlEscape'd data into tags like script and style opens your app to XSS vulnerabilities.

htmlUnescape(htmlString)

Unescapes the following HTML entities in the given htmlString argument: &amp; &lt; &gt; &quot; &#39;

The function also works as a tagged template literal that unescapes interpolated values.

Tip

Ensure you always quote your HTML attributes to prevent possible XSS.

FAQ

Why yet another HTML escaping package?

I couldn't find one I liked that was tiny, well-tested, and had both escape and unescape methods.

escape-goat's People

Contributors

bendingbender avatar bfred-it avatar fregante avatar joelbandi avatar mahovich avatar mifi avatar nicoleetlui avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

escape-goat's Issues

Handling non-string values

Maybe you should handle non-string values passed to escape and unescape methods like:

// booleans
escapeGoat.escape(true);
escapeGoat.unescape(true);

// objects
escapeGoat.escape({hello: 'world'});
escapeGoat.unescape({hello: 'world'});

// other
escapeGoat.escape(null);
escapeGoat.escape(undefined);

// Any other non-string value like arrays, sets, maps, symbols, regexes

The origin of this issue is from linkify-urls package (dependent of escape-goat) where the following code would throw an error:

const linkifyUrls = require('linkify-urls');

linkifyUrls('See https://sindresorhus.com', {
  attributes: {
    class: 'unicorn',
    hidden: true // <- this thing
  }
});

// throws
// TypeError: input.replace is not a function

Some type checking would be helpful with either

  • converting all primitive values (boolean, null, undefined) to strings
  • or returning empty string for null and undefined and only converting boolean values to strings

Merge tag functions

What do you think about overloading the regular htmlEscape/Unescape functions to automatically become tag functions? The detection would be simple: Array.isArray

Add template literal tags

Before:

const g = require('escape-goat');
const escaped = `<html a=${g.escape(a)}><b>${g.escape(b)}</b> ${g.escape(c)}</html>`;

After:

const g = require('escape-goat');
const escaped = g.escapeTag`<html a=${a}><b>${b}</b> ${c}</html>`;

The code required is minimal and this can be improved further:

module.exports = function eskape(strs, ...vals) {
    var out = strs[0];

    for (let i = 0; i < vals.length; i++) {
        out = out + escape(vals[i]) + strs[i + 1];
    }

    return out;
}

Code from https://github.com/wbinnssmith/eskape/blob/master/src.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.