This repository has been archived on 2020-11-02. You can view files and clone it, but cannot push or open issues or pull requests.
TripSit_Suite/node_modules/html-escaper
2020-11-01 22:46:04 +00:00
..
cjs Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
esm Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
test Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
index.js Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
LICENSE.txt Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
min.js Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
package.json Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00
README.md Initial comission of TheLounge base files 2020-11-01 22:46:04 +00:00

html-escaper Build Status Coverage Status

A simple module to escape/unescape common problematic entities.

How

This package is available in npm so npm install html-escaper is all you need to do, using eventually the global flag too.

Once the module is present

var html = require('html-escaper');

// two basic methods
html.escape('string');
html.unescape('escaped string');

Why

there is basically one rule only: do not ever replace one char after another if you are transforming a string into another.

// WARNING: THIS IS WRONG
// if you are that kind of dev that does this
function escape(s) {
  return s.replace(/&/g, "&")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/'/g, "&#39;")
          .replace(/"/g, "&quot;");
}

// you might be the same dev that does this too
function unescape(s) {
  return s.replace(/&amp;/g, "&")
          .replace(/&lt;/g, "<")
          .replace(/&gt;/g, ">")
          .replace(/&#39;/g, "'")
          .replace(/&quot;/g, '"');
}

// guess what we have here ?
unescape('&amp;lt;');

// now guess this XSS too ...
unescape('&amp;lt;script&amp;gt;alert("yo")&amp;lt;/script&amp;gt;');

The last example will produce <script>alert("yo")</script> instead of the expected &lt;script&gt;alert("yo")&lt;/script&gt;.

Nothing like this could possibly happen if we grab all chars at once and either ways. Its just a fortunate case that after swapping & with &amp; no other replace will be affected, but its not portable and universally a bad practice.

Grab all chars at once, no excuses!

more details As somebody might think its an unescape issue only, its not. Being an anti-pattern with side effects works both ways.

As example, changing the order of the replacement in escaping would produce the unexpected:

function escape(s) {
  return s.replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/'/g, "&#39;")
          .replace(/"/g, "&quot;")
          .replace(/&/g, "&amp;");
}

escape('<'); // &amp;lt; instead of &lt;

If we do not want to code with the fear that the order wasnt perfect or that our order in either escaping or unescaping is different from the order another method or function used, if we understand the issue and we agree its potentially a disaster prone approach, if we add the fact in this case creating 4 RegExp objects each time and invoking 4 times .replace trough the String.prototype is also potentially slower than creating one function only holding one object, or holding the function too, we should agree there is not absolutely any valid reason to keep proposing a char-by-char implementation.

We have proofs this approach can fail already so … why should we risk? Just avoid and grab all chars at once or simply use this tiny utility.

Backtick

Internt explorer < 9 has some backtick issue

For compatibility sake with common server-side HTML entities encoders and decoders, and in order to have the most reliable I/O, this little utility will NOT fix this IE < 9 problem.

It is also important to note that if we create valid HTML and we set attributes at runtime through this utility, backticks in strings cannot possibly affect attribute behaviors.

var img = new Image();
img.src = html.escape(
  'x` `<script>alert(1)</script>"` `'
);
// it won't cause problems even in IE < 9

However, if you use innerHTML and you target IE < 9 then this might be a problem.

Accordingly, if you need more chars and/or backticks to be escaped and unescaped, feel free to use alternatives like lodash or he

Here a bit more of my POV and why I havent implemented same thing alternatives did. Good news: those are alternatives ;-)