Adding delegated implementation
This commit is contained in:
1 parent
3cfbd0ea63
commit
7238b09112
326 files changed
+109874
-20
No files matched your search
+20
@@ -0,0 +1,20 @@
|
||||
Copyright Mathias Bynens <https://mathiasbynens.be/>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+379
@@ -0,0 +1,379 @@
|
||||
# he [](https://travis-ci.org/mathiasbynens/he) [](https://codecov.io/github/mathiasbynens/he?branch=master) [](https://gemnasium.com/mathiasbynens/he)
|
||||
|
||||
_he_ (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports [all standardized named character references as per HTML](https://html.spec.whatwg.org/multipage/syntax.html#named-character-references), handles [ambiguous ampersands](https://mathiasbynens.be/notes/ambiguous-ampersands) and other edge cases [just like a browser would](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references), has an extensive test suite, and — contrary to many other JavaScript solutions — _he_ handles astral Unicode symbols just fine. [An online demo is available.](https://mothereff.in/html-entities)
|
||||
|
||||
## Installation
|
||||
|
||||
Via [npm](https://www.npmjs.com/):
|
||||
|
||||
```bash
|
||||
npm install he
|
||||
```
|
||||
|
||||
Via [Bower](http://bower.io/):
|
||||
|
||||
```bash
|
||||
bower install he
|
||||
```
|
||||
|
||||
Via [Component](https://github.com/component/component):
|
||||
|
||||
```bash
|
||||
component install mathiasbynens/he
|
||||
```
|
||||
|
||||
In a browser:
|
||||
|
||||
```html
|
||||
<script src="he.js"></script>
|
||||
```
|
||||
|
||||
In [Node.js](https://nodejs.org/), [io.js](https://iojs.org/), [Narwhal](http://narwhaljs.org/), and [RingoJS](http://ringojs.org/):
|
||||
|
||||
```js
|
||||
var he = require('he');
|
||||
```
|
||||
|
||||
In [Rhino](http://www.mozilla.org/rhino/):
|
||||
|
||||
```js
|
||||
load('he.js');
|
||||
```
|
||||
|
||||
Using an AMD loader like [RequireJS](http://requirejs.org/):
|
||||
|
||||
```js
|
||||
require(
|
||||
{
|
||||
'paths': {
|
||||
'he': 'path/to/he'
|
||||
}
|
||||
},
|
||||
['he'],
|
||||
function(he) {
|
||||
console.log(he);
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `he.version`
|
||||
|
||||
A string representing the semantic version number.
|
||||
|
||||
### `he.encode(text, options)`
|
||||
|
||||
This function takes a string of text and encodes (by default) any symbols that aren’t printable ASCII symbols and `&`, `<`, `>`, `"`, `'`, and `` ` ``, replacing them with character references.
|
||||
|
||||
```js
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux');
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
```
|
||||
|
||||
As long as the input string contains [allowed code points](https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream) only, the return value of this function is always valid HTML. Any [(invalid) code points that cannot be represented using a character reference](https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides) in the input are not encoded:
|
||||
|
||||
```js
|
||||
he.encode('foo \0 bar');
|
||||
// → 'foo \0 bar'
|
||||
```
|
||||
|
||||
However, enabling [the `strict` option](https://github.com/mathiasbynens/he#strict) causes invalid code points to throw an exception. With `strict` enabled, `he.encode` either throws (if the input contains invalid code points) or returns a string of valid HTML.
|
||||
|
||||
The `options` object is optional. It recognizes the following properties:
|
||||
|
||||
#### `useNamedReferences`
|
||||
|
||||
The default value for the `useNamedReferences` option is `false`. This means that `encode()` will not use any named character references (e.g. `©`) in the output — hexadecimal escapes (e.g. `©`) will be used instead. Set it to `true` to enable the use of named references.
|
||||
|
||||
**Note that if compatibility with older browsers is a concern, this option should remain disabled.**
|
||||
|
||||
```js
|
||||
// Using the global default setting (defaults to `false`):
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux');
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly disallow named references:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'useNamedReferences': false
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly allow named references:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'useNamedReferences': true
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
```
|
||||
|
||||
#### `decimal`
|
||||
|
||||
The default value for the `decimal` option is `false`. If the option is enabled, `encode` will generally use decimal escapes (e.g. `©`) rather than hexadecimal escapes (e.g. `©`). Beside of this replacement, the basic behavior remains the same when combined with other options. For example: if both options `useNamedReferences` and `decimal` are enabled, named references (e.g. `©`) are used over decimal escapes. HTML entities without a named reference are encoded using decimal escapes.
|
||||
|
||||
```js
|
||||
// Using the global default setting (defaults to `false`):
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux');
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly disable decimal escapes:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'decimal': false
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly enable decimal escapes:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'decimal': true
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly allow named references and decimal escapes:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'useNamedReferences': true,
|
||||
'decimal': true
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
```
|
||||
|
||||
#### `encodeEverything`
|
||||
|
||||
The default value for the `encodeEverything` option is `false`. This means that `encode()` will not use any character references for printable ASCII symbols that don’t need escaping. Set it to `true` to encode every symbol in the input string. When set to `true`, this option takes precedence over `allowUnsafeSymbols` (i.e. setting the latter to `true` in such a case has no effect).
|
||||
|
||||
```js
|
||||
// Using the global default setting (defaults to `false`):
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux');
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly encode all symbols:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'encodeEverything': true
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
|
||||
// This setting can be combined with the `useNamedReferences` option:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux', {
|
||||
'encodeEverything': true,
|
||||
'useNamedReferences': true
|
||||
});
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
```
|
||||
|
||||
#### `strict`
|
||||
|
||||
The default value for the `strict` option is `false`. This means that `encode()` will encode any HTML text content you feed it, even if it contains any symbols that cause [parse errors](https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream). To throw an error when such invalid HTML is encountered, set the `strict` option to `true`. This option makes it possible to use _he_ as part of HTML parsers and HTML validators.
|
||||
|
||||
```js
|
||||
// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):
|
||||
he.encode('\x01');
|
||||
// → ''
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly enable error-tolerant mode:
|
||||
he.encode('\x01', {
|
||||
'strict': false
|
||||
});
|
||||
// → ''
|
||||
|
||||
// Passing an `options` object to `encode`, to explicitly enable strict mode:
|
||||
he.encode('\x01', {
|
||||
'strict': true
|
||||
});
|
||||
// → Parse error
|
||||
```
|
||||
|
||||
#### `allowUnsafeSymbols`
|
||||
|
||||
The default value for the `allowUnsafeSymbols` option is `false`. This means that characters that are unsafe for use in HTML content (`&`, `<`, `>`, `"`, `'`, and `` ` ``) will be encoded. When set to `true`, only non-ASCII characters will be encoded. If the `encodeEverything` option is set to `true`, this option will be ignored.
|
||||
|
||||
```js
|
||||
he.encode('foo © and & ampersand', {
|
||||
'allowUnsafeSymbols': true
|
||||
});
|
||||
// → 'foo © and & ampersand'
|
||||
```
|
||||
|
||||
#### Overriding default `encode` options globally
|
||||
|
||||
The global default setting can be overridden by modifying the `he.encode.options` object. This saves you from passing in an `options` object for every call to `encode` if you want to use the non-default setting.
|
||||
|
||||
```js
|
||||
// Read the global default setting:
|
||||
he.encode.options.useNamedReferences;
|
||||
// → `false` by default
|
||||
|
||||
// Override the global default setting:
|
||||
he.encode.options.useNamedReferences = true;
|
||||
|
||||
// Using the global default setting, which is now `true`:
|
||||
he.encode('foo © bar ≠ baz 𝌆 qux');
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
```
|
||||
|
||||
### `he.decode(html, options)`
|
||||
|
||||
This function takes a string of HTML and decodes any named and numerical character references in it using [the algorithm described in section 12.2.4.69 of the HTML spec](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references).
|
||||
|
||||
```js
|
||||
he.decode('foo © bar ≠ baz 𝌆 qux');
|
||||
// → 'foo © bar ≠ baz 𝌆 qux'
|
||||
```
|
||||
|
||||
The `options` object is optional. It recognizes the following properties:
|
||||
|
||||
#### `isAttributeValue`
|
||||
|
||||
The default value for the `isAttributeValue` option is `false`. This means that `decode()` will decode the string as if it were used in [a text context in an HTML document](https://html.spec.whatwg.org/multipage/syntax.html#data-state). HTML has different rules for [parsing character references in attribute values](https://html.spec.whatwg.org/multipage/syntax.html#character-reference-in-attribute-value-state) — set this option to `true` to treat the input string as if it were used as an attribute value.
|
||||
|
||||
```js
|
||||
// Using the global default setting (defaults to `false`, i.e. HTML text context):
|
||||
he.decode('foo&bar');
|
||||
// → 'foo&bar'
|
||||
|
||||
// Passing an `options` object to `decode`, to explicitly assume an HTML text context:
|
||||
he.decode('foo&bar', {
|
||||
'isAttributeValue': false
|
||||
});
|
||||
// → 'foo&bar'
|
||||
|
||||
// Passing an `options` object to `decode`, to explicitly assume an HTML attribute value context:
|
||||
he.decode('foo&bar', {
|
||||
'isAttributeValue': true
|
||||
});
|
||||
// → 'foo&bar'
|
||||
```
|
||||
|
||||
#### `strict`
|
||||
|
||||
The default value for the `strict` option is `false`. This means that `decode()` will decode any HTML text content you feed it, even if it contains any entities that cause [parse errors](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references). To throw an error when such invalid HTML is encountered, set the `strict` option to `true`. This option makes it possible to use _he_ as part of HTML parsers and HTML validators.
|
||||
|
||||
```js
|
||||
// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):
|
||||
he.decode('foo&bar');
|
||||
// → 'foo&bar'
|
||||
|
||||
// Passing an `options` object to `decode`, to explicitly enable error-tolerant mode:
|
||||
he.decode('foo&bar', {
|
||||
'strict': false
|
||||
});
|
||||
// → 'foo&bar'
|
||||
|
||||
// Passing an `options` object to `decode`, to explicitly enable strict mode:
|
||||
he.decode('foo&bar', {
|
||||
'strict': true
|
||||
});
|
||||
// → Parse error
|
||||
```
|
||||
|
||||
#### Overriding default `decode` options globally
|
||||
|
||||
The global default settings for the `decode` function can be overridden by modifying the `he.decode.options` object. This saves you from passing in an `options` object for every call to `decode` if you want to use a non-default setting.
|
||||
|
||||
```js
|
||||
// Read the global default setting:
|
||||
he.decode.options.isAttributeValue;
|
||||
// → `false` by default
|
||||
|
||||
// Override the global default setting:
|
||||
he.decode.options.isAttributeValue = true;
|
||||
|
||||
// Using the global default setting, which is now `true`:
|
||||
he.decode('foo&bar');
|
||||
// → 'foo&bar'
|
||||
```
|
||||
|
||||
### `he.escape(text)`
|
||||
|
||||
This function takes a string of text and escapes it for use in text contexts in XML or HTML documents. Only the following characters are escaped: `&`, `<`, `>`, `"`, `'`, and `` ` ``.
|
||||
|
||||
```js
|
||||
he.escape('<img src=\'x\' onerror="prompt(1)">');
|
||||
// → '<img src='x' onerror="prompt(1)">'
|
||||
```
|
||||
|
||||
### `he.unescape(html, options)`
|
||||
|
||||
`he.unescape` is an alias for `he.decode`. It takes a string of HTML and decodes any named and numerical character references in it.
|
||||
|
||||
### Using the `he` binary
|
||||
|
||||
To use the `he` binary in your shell, simply install _he_ globally using npm:
|
||||
|
||||
```bash
|
||||
npm install -g he
|
||||
```
|
||||
|
||||
After that you will be able to encode/decode HTML entities from the command line:
|
||||
|
||||
```bash
|
||||
$ he --encode 'föo ♥ bår 𝌆 baz'
|
||||
föo ♥ bår 𝌆 baz
|
||||
|
||||
$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz'
|
||||
föo ♥ bår 𝌆 baz
|
||||
|
||||
$ he --decode 'föo ♥ bår 𝌆 baz'
|
||||
föo ♥ bår 𝌆 baz
|
||||
```
|
||||
|
||||
Read a local text file, encode it for use in an HTML text context, and save the result to a new file:
|
||||
|
||||
```bash
|
||||
$ he --encode < foo.txt > foo-escaped.html
|
||||
```
|
||||
|
||||
Or do the same with an online text file:
|
||||
|
||||
```bash
|
||||
$ curl -sL "http://git.io/HnfEaw" | he --encode > escaped.html
|
||||
```
|
||||
|
||||
Or, the opposite — read a local file containing a snippet of HTML in a text context, decode it back to plain text, and save the result to a new file:
|
||||
|
||||
```bash
|
||||
$ he --decode < foo-escaped.html > foo.txt
|
||||
```
|
||||
|
||||
Or do the same with an online HTML snippet:
|
||||
|
||||
```bash
|
||||
$ curl -sL "http://git.io/HnfEaw" | he --decode > decoded.txt
|
||||
```
|
||||
|
||||
See `he --help` for the full list of options.
|
||||
|
||||
## Support
|
||||
|
||||
_he_ has been tested in at least:
|
||||
|
||||
* Chrome 27-50
|
||||
* Firefox 3-45
|
||||
* Safari 4-9
|
||||
* Opera 10-12, 15–37
|
||||
* IE 6–11
|
||||
* Edge
|
||||
* Narwhal 0.3.2
|
||||
* Node.js v0.10, v0.12, v4, v5
|
||||
* PhantomJS 1.9.0
|
||||
* Rhino 1.7RC4
|
||||
* RingoJS 0.8-0.11
|
||||
|
||||
## Unit tests & code coverage
|
||||
|
||||
After cloning this repository, run `npm install` to install the dependencies needed for he development and testing. You may want to install Istanbul _globally_ using `npm install istanbul -g`.
|
||||
|
||||
Once that’s done, you can run the unit tests in Node using `npm test` or `node tests/tests.js`. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use `grunt test`.
|
||||
|
||||
To generate the code coverage report, use `grunt cover`.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
Thanks to [Simon Pieters](https://simon.html5.org/) ([@zcorpan](https://twitter.com/zcorpan)) for the many suggestions.
|
||||
|
||||
## Author
|
||||
|
||||
| [](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
||||
|---|
|
||||
| [Mathias Bynens](https://mathiasbynens.be/) |
|
||||
|
||||
## License
|
||||
|
||||
_he_ is available under the [MIT](https://mths.be/mit) license.
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env node
|
||||
(function() {
|
||||
|
||||
var fs = require('fs');
|
||||
var he = require('../he.js');
|
||||
var strings = process.argv.splice(2);
|
||||
var stdin = process.stdin;
|
||||
var data;
|
||||
var timeout;
|
||||
var action;
|
||||
var options = {};
|
||||
var log = console.log;
|
||||
|
||||
var main = function() {
|
||||
var option = strings[0];
|
||||
var count = 0;
|
||||
|
||||
if (/^(?:-h|--help|undefined)$/.test(option)) {
|
||||
log(
|
||||
'he v%s - https://mths.be/he',
|
||||
he.version
|
||||
);
|
||||
log([
|
||||
'\nUsage:\n',
|
||||
'\the [--escape] string',
|
||||
'\the [--encode] [--use-named-refs] [--everything] [--allow-unsafe] [--decimal] string',
|
||||
'\the [--decode] [--attribute] [--strict] string',
|
||||
'\the [-v | --version]',
|
||||
'\the [-h | --help]',
|
||||
'\nExamples:\n',
|
||||
'\the --escape \\<img\\ src\\=\\\'x\\\'\\ onerror\\=\\"prompt\\(1\\)\\"\\>',
|
||||
'\techo \'© 𝌆\' | he --decode'
|
||||
].join('\n'));
|
||||
return process.exit(option ? 0 : 1);
|
||||
}
|
||||
|
||||
if (/^(?:-v|--version)$/.test(option)) {
|
||||
log('v%s', he.version);
|
||||
return process.exit(0);
|
||||
}
|
||||
|
||||
strings.forEach(function(string) {
|
||||
// Process options
|
||||
if (string == '--escape') {
|
||||
action = 'escape';
|
||||
return;
|
||||
}
|
||||
if (string == '--encode') {
|
||||
action = 'encode';
|
||||
return;
|
||||
}
|
||||
if (string == '--use-named-refs') {
|
||||
action = 'encode';
|
||||
options.useNamedReferences = true;
|
||||
return;
|
||||
}
|
||||
if (string == '--everything') {
|
||||
action = 'encode';
|
||||
options.encodeEverything = true;
|
||||
return;
|
||||
}
|
||||
if (string == '--allow-unsafe') {
|
||||
action = 'encode';
|
||||
options.allowUnsafeSymbols = true;
|
||||
return;
|
||||
}
|
||||
if (string == '--decimal') {
|
||||
action = 'encode';
|
||||
options.decimal = true;
|
||||
return;
|
||||
}
|
||||
if (string == '--decode') {
|
||||
action = 'decode';
|
||||
return;
|
||||
}
|
||||
if (string == '--attribute') {
|
||||
action = 'decode';
|
||||
options.isAttributeValue = true;
|
||||
return;
|
||||
}
|
||||
if (string == '--strict') {
|
||||
action = 'decode';
|
||||
options.strict = true;
|
||||
return;
|
||||
}
|
||||
// Process string(s)
|
||||
var result;
|
||||
if (!action) {
|
||||
log('Error: he requires at least one option and a string argument.');
|
||||
log('Try `he --help` for more information.');
|
||||
return process.exit(1);
|
||||
}
|
||||
try {
|
||||
result = he[action](string, options);
|
||||
log(result);
|
||||
count++;
|
||||
} catch(error) {
|
||||
log(error.message + '\n');
|
||||
log('Error: failed to %s.', action);
|
||||
log('If you think this is a bug in he, please report it:');
|
||||
log('https://github.com/mathiasbynens/he/issues/new');
|
||||
log(
|
||||
'\nStack trace using he@%s:\n',
|
||||
he.version
|
||||
);
|
||||
log(error.stack);
|
||||
return process.exit(1);
|
||||
}
|
||||
});
|
||||
if (!count) {
|
||||
log('Error: he requires a string argument.');
|
||||
log('Try `he --help` for more information.');
|
||||
return process.exit(1);
|
||||
}
|
||||
// Return with exit status 0 outside of the `forEach` loop, in case
|
||||
// multiple strings were passed in.
|
||||
return process.exit(0);
|
||||
};
|
||||
|
||||
if (stdin.isTTY) {
|
||||
// handle shell arguments
|
||||
main();
|
||||
} else {
|
||||
// Either the script is called from within a non-TTY context, or `stdin`
|
||||
// content is being piped in.
|
||||
if (!process.stdout.isTTY) {
|
||||
// The script was called from a non-TTY context. This is a rather uncommon
|
||||
// use case we don’t actively support. However, we don’t want the script
|
||||
// to wait forever in such cases, so…
|
||||
timeout = setTimeout(function() {
|
||||
// …if no piped data arrived after a whole minute, handle shell
|
||||
// arguments instead.
|
||||
main();
|
||||
}, 60000);
|
||||
}
|
||||
data = '';
|
||||
stdin.on('data', function(chunk) {
|
||||
clearTimeout(timeout);
|
||||
data += chunk;
|
||||
});
|
||||
stdin.on('end', function() {
|
||||
strings.push(data.trim());
|
||||
main();
|
||||
});
|
||||
stdin.resume();
|
||||
}
|
||||
|
||||
}());
|
||||
+342
@@ -0,0 +1,342 @@
|
||||
/*! https://mths.be/he v1.1.1 by @mathias | MIT license */
|
||||
;(function(root) {
|
||||
|
||||
// Detect free variables `exports`.
|
||||
var freeExports = typeof exports == 'object' && exports;
|
||||
|
||||
// Detect free variable `module`.
|
||||
var freeModule = typeof module == 'object' && module &&
|
||||
module.exports == freeExports && module;
|
||||
|
||||
// Detect free variable `global`, from Node.js or Browserified code,
|
||||
// and use it as `root`.
|
||||
var freeGlobal = typeof global == 'object' && global;
|
||||
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
|
||||
root = freeGlobal;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
// All astral symbols.
|
||||
var regexAstralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
||||
// All ASCII symbols (not just printable ASCII) except those listed in the
|
||||
// first column of the overrides table.
|
||||
// https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides
|
||||
var regexAsciiWhitelist = /[\x01-\x7F]/g;
|
||||
// All BMP symbols that are not ASCII newlines, printable ASCII symbols, or
|
||||
// code points listed in the first column of the overrides table on
|
||||
// https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides.
|
||||
var regexBmpWhitelist = /[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g;
|
||||
|
||||
var regexEncodeNonAscii = /<\u20D2|=\u20E5|>\u20D2|\u205F\u200A|\u219D\u0338|\u2202\u0338|\u2220\u20D2|\u2229\uFE00|\u222A\uFE00|\u223C\u20D2|\u223D\u0331|\u223E\u0333|\u2242\u0338|\u224B\u0338|\u224D\u20D2|\u224E\u0338|\u224F\u0338|\u2250\u0338|\u2261\u20E5|\u2264\u20D2|\u2265\u20D2|\u2266\u0338|\u2267\u0338|\u2268\uFE00|\u2269\uFE00|\u226A\u0338|\u226A\u20D2|\u226B\u0338|\u226B\u20D2|\u227F\u0338|\u2282\u20D2|\u2283\u20D2|\u228A\uFE00|\u228B\uFE00|\u228F\u0338|\u2290\u0338|\u2293\uFE00|\u2294\uFE00|\u22B4\u20D2|\u22B5\u20D2|\u22D8\u0338|\u22D9\u0338|\u22DA\uFE00|\u22DB\uFE00|\u22F5\u0338|\u22F9\u0338|\u2933\u0338|\u29CF\u0338|\u29D0\u0338|\u2A6D\u0338|\u2A70\u0338|\u2A7D\u0338|\u2A7E\u0338|\u2AA1\u0338|\u2AA2\u0338|\u2AAC\uFE00|\u2AAD\uFE00|\u2AAF\u0338|\u2AB0\u0338|\u2AC5\u0338|\u2AC6\u0338|\u2ACB\uFE00|\u2ACC\uFE00|\u2AFD\u20E5|[\xA0-\u0113\u0116-\u0122\u0124-\u012B\u012E-\u014D\u0150-\u017E\u0192\u01B5\u01F5\u0237\u02C6\u02C7\u02D8-\u02DD\u0311\u0391-\u03A1\u03A3-\u03A9\u03B1-\u03C9\u03D1\u03D2\u03D5\u03D6\u03DC\u03DD\u03F0\u03F1\u03F5\u03F6\u0401-\u040C\u040E-\u044F\u0451-\u045C\u045E\u045F\u2002-\u2005\u2007-\u2010\u2013-\u2016\u2018-\u201A\u201C-\u201E\u2020-\u2022\u2025\u2026\u2030-\u2035\u2039\u203A\u203E\u2041\u2043\u2044\u204F\u2057\u205F-\u2063\u20AC\u20DB\u20DC\u2102\u2105\u210A-\u2113\u2115-\u211E\u2122\u2124\u2127-\u2129\u212C\u212D\u212F-\u2131\u2133-\u2138\u2145-\u2148\u2153-\u215E\u2190-\u219B\u219D-\u21A7\u21A9-\u21AE\u21B0-\u21B3\u21B5-\u21B7\u21BA-\u21DB\u21DD\u21E4\u21E5\u21F5\u21FD-\u2205\u2207-\u2209\u220B\u220C\u220F-\u2214\u2216-\u2218\u221A\u221D-\u2238\u223A-\u2257\u2259\u225A\u225C\u225F-\u2262\u2264-\u228B\u228D-\u229B\u229D-\u22A5\u22A7-\u22B0\u22B2-\u22BB\u22BD-\u22DB\u22DE-\u22E3\u22E6-\u22F7\u22F9-\u22FE\u2305\u2306\u2308-\u2310\u2312\u2313\u2315\u2316\u231C-\u231F\u2322\u2323\u232D\u232E\u2336\u233D\u233F\u237C\u23B0\u23B1\u23B4-\u23B6\u23DC-\u23DF\u23E2\u23E7\u2423\u24C8\u2500\u2502\u250C\u2510\u2514\u2518\u251C\u2524\u252C\u2534\u253C\u2550-\u256C\u2580\u2584\u2588\u2591-\u2593\u25A1\u25AA\u25AB\u25AD\u25AE\u25B1\u25B3-\u25B5\u25B8\u25B9\u25BD-\u25BF\u25C2\u25C3\u25CA\u25CB\u25EC\u25EF\u25F8-\u25FC\u2605\u2606\u260E\u2640\u2642\u2660\u2663\u2665\u2666\u266A\u266D-\u266F\u2713\u2717\u2720\u2736\u2758\u2772\u2773\u27C8\u27C9\u27E6-\u27ED\u27F5-\u27FA\u27FC\u27FF\u2902-\u2905\u290C-\u2913\u2916\u2919-\u2920\u2923-\u292A\u2933\u2935-\u2939\u293C\u293D\u2945\u2948-\u294B\u294E-\u2976\u2978\u2979\u297B-\u297F\u2985\u2986\u298B-\u2996\u299A\u299C\u299D\u29A4-\u29B7\u29B9\u29BB\u29BC\u29BE-\u29C5\u29C9\u29CD-\u29D0\u29DC-\u29DE\u29E3-\u29E5\u29EB\u29F4\u29F6\u2A00-\u2A02\u2A04\u2A06\u2A0C\u2A0D\u2A10-\u2A17\u2A22-\u2A27\u2A29\u2A2A\u2A2D-\u2A31\u2A33-\u2A3C\u2A3F\u2A40\u2A42-\u2A4D\u2A50\u2A53-\u2A58\u2A5A-\u2A5D\u2A5F\u2A66\u2A6A\u2A6D-\u2A75\u2A77-\u2A9A\u2A9D-\u2AA2\u2AA4-\u2AB0\u2AB3-\u2AC8\u2ACB\u2ACC\u2ACF-\u2ADB\u2AE4\u2AE6-\u2AE9\u2AEB-\u2AF3\u2AFD\uFB00-\uFB04]|\uD835[\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDCCF\uDD04\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDD6B]/g;
|
||||
var encodeMap = {'\xAD':'shy','\u200C':'zwnj','\u200D':'zwj','\u200E':'lrm','\u2063':'ic','\u2062':'it','\u2061':'af','\u200F':'rlm','\u200B':'ZeroWidthSpace','\u2060':'NoBreak','\u0311':'DownBreve','\u20DB':'tdot','\u20DC':'DotDot','\t':'Tab','\n':'NewLine','\u2008':'puncsp','\u205F':'MediumSpace','\u2009':'thinsp','\u200A':'hairsp','\u2004':'emsp13','\u2002':'ensp','\u2005':'emsp14','\u2003':'emsp','\u2007':'numsp','\xA0':'nbsp','\u205F\u200A':'ThickSpace','\u203E':'oline','_':'lowbar','\u2010':'dash','\u2013':'ndash','\u2014':'mdash','\u2015':'horbar',',':'comma',';':'semi','\u204F':'bsemi',':':'colon','\u2A74':'Colone','!':'excl','\xA1':'iexcl','?':'quest','\xBF':'iquest','.':'period','\u2025':'nldr','\u2026':'mldr','\xB7':'middot','\'':'apos','\u2018':'lsquo','\u2019':'rsquo','\u201A':'sbquo','\u2039':'lsaquo','\u203A':'rsaquo','"':'quot','\u201C':'ldquo','\u201D':'rdquo','\u201E':'bdquo','\xAB':'laquo','\xBB':'raquo','(':'lpar',')':'rpar','[':'lsqb',']':'rsqb','{':'lcub','}':'rcub','\u2308':'lceil','\u2309':'rceil','\u230A':'lfloor','\u230B':'rfloor','\u2985':'lopar','\u2986':'ropar','\u298B':'lbrke','\u298C':'rbrke','\u298D':'lbrkslu','\u298E':'rbrksld','\u298F':'lbrksld','\u2990':'rbrkslu','\u2991':'langd','\u2992':'rangd','\u2993':'lparlt','\u2994':'rpargt','\u2995':'gtlPar','\u2996':'ltrPar','\u27E6':'lobrk','\u27E7':'robrk','\u27E8':'lang','\u27E9':'rang','\u27EA':'Lang','\u27EB':'Rang','\u27EC':'loang','\u27ED':'roang','\u2772':'lbbrk','\u2773':'rbbrk','\u2016':'Vert','\xA7':'sect','\xB6':'para','@':'commat','*':'ast','/':'sol','undefined':null,'&':'amp','#':'num','%':'percnt','\u2030':'permil','\u2031':'pertenk','\u2020':'dagger','\u2021':'Dagger','\u2022':'bull','\u2043':'hybull','\u2032':'prime','\u2033':'Prime','\u2034':'tprime','\u2057':'qprime','\u2035':'bprime','\u2041':'caret','`':'grave','\xB4':'acute','\u02DC':'tilde','^':'Hat','\xAF':'macr','\u02D8':'breve','\u02D9':'dot','\xA8':'die','\u02DA':'ring','\u02DD':'dblac','\xB8':'cedil','\u02DB':'ogon','\u02C6':'circ','\u02C7':'caron','\xB0':'deg','\xA9':'copy','\xAE':'reg','\u2117':'copysr','\u2118':'wp','\u211E':'rx','\u2127':'mho','\u2129':'iiota','\u2190':'larr','\u219A':'nlarr','\u2192':'rarr','\u219B':'nrarr','\u2191':'uarr','\u2193':'darr','\u2194':'harr','\u21AE':'nharr','\u2195':'varr','\u2196':'nwarr','\u2197':'nearr','\u2198':'searr','\u2199':'swarr','\u219D':'rarrw','\u219D\u0338':'nrarrw','\u219E':'Larr','\u219F':'Uarr','\u21A0':'Rarr','\u21A1':'Darr','\u21A2':'larrtl','\u21A3':'rarrtl','\u21A4':'mapstoleft','\u21A5':'mapstoup','\u21A6':'map','\u21A7':'mapstodown','\u21A9':'larrhk','\u21AA':'rarrhk','\u21AB':'larrlp','\u21AC':'rarrlp','\u21AD':'harrw','\u21B0':'lsh','\u21B1':'rsh','\u21B2':'ldsh','\u21B3':'rdsh','\u21B5':'crarr','\u21B6':'cularr','\u21B7':'curarr','\u21BA':'olarr','\u21BB':'orarr','\u21BC':'lharu','\u21BD':'lhard','\u21BE':'uharr','\u21BF':'uharl','\u21C0':'rharu','\u21C1':'rhard','\u21C2':'dharr','\u21C3':'dharl','\u21C4':'rlarr','\u21C5':'udarr','\u21C6':'lrarr','\u21C7':'llarr','\u21C8':'uuarr','\u21C9':'rrarr','\u21CA':'ddarr','\u21CB':'lrhar','\u21CC':'rlhar','\u21D0':'lArr','\u21CD':'nlArr','\u21D1':'uArr','\u21D2':'rArr','\u21CF':'nrArr','\u21D3':'dArr','\u21D4':'iff','\u21CE':'nhArr','\u21D5':'vArr','\u21D6':'nwArr','\u21D7':'neArr','\u21D8':'seArr','\u21D9':'swArr','\u21DA':'lAarr','\u21DB':'rAarr','\u21DD':'zigrarr','\u21E4':'larrb','\u21E5':'rarrb','\u21F5':'duarr','\u21FD':'loarr','\u21FE':'roarr','\u21FF':'hoarr','\u2200':'forall','\u2201':'comp','\u2202':'part','\u2202\u0338':'npart','\u2203':'exist','\u2204':'nexist','\u2205':'empty','\u2207':'Del','\u2208':'in','\u2209':'notin','\u220B':'ni','\u220C':'notni','\u03F6':'bepsi','\u220F':'prod','\u2210':'coprod','\u2211':'sum','+':'plus','\xB1':'pm','\xF7':'div','\xD7':'times','<':'lt','\u226E':'nlt','<\u20D2':'nvlt','=':'equals','\u2260':'ne','=\u20E5':'bne','\u2A75':'Equal','>':'gt','\u226F':'ngt','>\u20D2':'nvgt','\xAC':'not','|':'vert','\xA6':'brvbar','\u2212':'minus','\u2213':'mp','\u2214':'plusdo','\u2044':'frasl','\u2216':'setmn','\u2217':'lowast','\u2218':'compfn','\u221A':'Sqrt','\u221D':'prop','\u221E':'infin','\u221F':'angrt','\u2220':'ang','\u2220\u20D2':'nang','\u2221':'angmsd','\u2222':'angsph','\u2223':'mid','\u2224':'nmid','\u2225':'par','\u2226':'npar','\u2227':'and','\u2228':'or','\u2229':'cap','\u2229\uFE00':'caps','\u222A':'cup','\u222A\uFE00':'cups','\u222B':'int','\u222C':'Int','\u222D':'tint','\u2A0C':'qint','\u222E':'oint','\u222F':'Conint','\u2230':'Cconint','\u2231':'cwint','\u2232':'cwconint','\u2233':'awconint','\u2234':'there4','\u2235':'becaus','\u2236':'ratio','\u2237':'Colon','\u2238':'minusd','\u223A':'mDDot','\u223B':'homtht','\u223C':'sim','\u2241':'nsim','\u223C\u20D2':'nvsim','\u223D':'bsim','\u223D\u0331':'race','\u223E':'ac','\u223E\u0333':'acE','\u223F':'acd','\u2240':'wr','\u2242':'esim','\u2242\u0338':'nesim','\u2243':'sime','\u2244':'nsime','\u2245':'cong','\u2247':'ncong','\u2246':'simne','\u2248':'ap',Line truncated
|
||||
|
||||
var regexEscape = /["&'<>`]/g;
|
||||
var escapeMap = {
|
||||
'"': '"',
|
||||
'&': '&',
|
||||
'\'': ''',
|
||||
'<': '<',
|
||||
// See https://mathiasbynens.be/notes/ambiguous-ampersands: in HTML, the
|
||||
// following is not strictly necessary unless it’s part of a tag or an
|
||||
// unquoted attribute value. We’re only escaping it to support those
|
||||
// situations, and for XML support.
|
||||
'>': '>',
|
||||
// In Internet Explorer ≤ 8, the backtick character can be used
|
||||
// to break out of (un)quoted attribute values or HTML comments.
|
||||
// See http://html5sec.org/#102, http://html5sec.org/#108, and
|
||||
// http://html5sec.org/#133.
|
||||
'`': '`'
|
||||
};
|
||||
|
||||
var regexInvalidEntity = /&#(?:[xX][^a-fA-F0-9]|[^0-9xX])/;
|
||||
var regexInvalidRawCodePoint = /[\0-\x08\x0B\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]|[\uD83F\uD87F\uD8BF\uD8FF\uD93F\uD97F\uD9BF\uD9FF\uDA3F\uDA7F\uDABF\uDAFF\uDB3F\uDB7F\uDBBF\uDBFF][\uDFFE\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
|
||||
var regexDecode = /&#([0-9]+)(;?)|&#[xX]([a-fA-F0-9]+)(;?)|&([0-9a-zA-Z]+);|&(Aacute|Agrave|Atilde|Ccedil|Eacute|Egrave|Iacute|Igrave|Ntilde|Oacute|Ograve|Oslash|Otilde|Uacute|Ugrave|Yacute|aacute|agrave|atilde|brvbar|ccedil|curren|divide|eacute|egrave|frac12|frac14|frac34|iacute|igrave|iquest|middot|ntilde|oacute|ograve|oslash|otilde|plusmn|uacute|ugrave|yacute|AElig|Acirc|Aring|Ecirc|Icirc|Ocirc|THORN|Ucirc|acirc|acute|aelig|aring|cedil|ecirc|icirc|iexcl|laquo|micro|ocirc|pound|raquo|szlig|thorn|times|ucirc|Auml|COPY|Euml|Iuml|Ouml|QUOT|Uuml|auml|cent|copy|euml|iuml|macr|nbsp|ordf|ordm|ouml|para|quot|sect|sup1|sup2|sup3|uuml|yuml|AMP|ETH|REG|amp|deg|eth|not|reg|shy|uml|yen|GT|LT|gt|lt)([=a-zA-Z0-9])?/g;
|
||||
var decodeMap = {'aacute':'\xE1','Aacute':'\xC1','abreve':'\u0103','Abreve':'\u0102','ac':'\u223E','acd':'\u223F','acE':'\u223E\u0333','acirc':'\xE2','Acirc':'\xC2','acute':'\xB4','acy':'\u0430','Acy':'\u0410','aelig':'\xE6','AElig':'\xC6','af':'\u2061','afr':'\uD835\uDD1E','Afr':'\uD835\uDD04','agrave':'\xE0','Agrave':'\xC0','alefsym':'\u2135','aleph':'\u2135','alpha':'\u03B1','Alpha':'\u0391','amacr':'\u0101','Amacr':'\u0100','amalg':'\u2A3F','amp':'&','AMP':'&','and':'\u2227','And':'\u2A53','andand':'\u2A55','andd':'\u2A5C','andslope':'\u2A58','andv':'\u2A5A','ang':'\u2220','ange':'\u29A4','angle':'\u2220','angmsd':'\u2221','angmsdaa':'\u29A8','angmsdab':'\u29A9','angmsdac':'\u29AA','angmsdad':'\u29AB','angmsdae':'\u29AC','angmsdaf':'\u29AD','angmsdag':'\u29AE','angmsdah':'\u29AF','angrt':'\u221F','angrtvb':'\u22BE','angrtvbd':'\u299D','angsph':'\u2222','angst':'\xC5','angzarr':'\u237C','aogon':'\u0105','Aogon':'\u0104','aopf':'\uD835\uDD52','Aopf':'\uD835\uDD38','ap':'\u2248','apacir':'\u2A6F','ape':'\u224A','apE':'\u2A70','apid':'\u224B','apos':'\'','ApplyFunction':'\u2061','approx':'\u2248','approxeq':'\u224A','aring':'\xE5','Aring':'\xC5','ascr':'\uD835\uDCB6','Ascr':'\uD835\uDC9C','Assign':'\u2254','ast':'*','asymp':'\u2248','asympeq':'\u224D','atilde':'\xE3','Atilde':'\xC3','auml':'\xE4','Auml':'\xC4','awconint':'\u2233','awint':'\u2A11','backcong':'\u224C','backepsilon':'\u03F6','backprime':'\u2035','backsim':'\u223D','backsimeq':'\u22CD','Backslash':'\u2216','Barv':'\u2AE7','barvee':'\u22BD','barwed':'\u2305','Barwed':'\u2306','barwedge':'\u2305','bbrk':'\u23B5','bbrktbrk':'\u23B6','bcong':'\u224C','bcy':'\u0431','Bcy':'\u0411','bdquo':'\u201E','becaus':'\u2235','because':'\u2235','Because':'\u2235','bemptyv':'\u29B0','bepsi':'\u03F6','bernou':'\u212C','Bernoullis':'\u212C','beta':'\u03B2','Beta':'\u0392','beth':'\u2136','between':'\u226C','bfr':'\uD835\uDD1F','Bfr':'\uD835\uDD05','bigcap':'\u22C2','bigcirc':'\u25EF','bigcup':'\u22C3','bigodot':'\u2A00','bigoplus':'\u2A01','bigotimes':'\u2A02','bigsqcup':'\u2A06','bigstar':'\u2605','bigtriangledown':'\u25BD','bigtriangleup':'\u25B3','biguplus':'\u2A04','bigvee':'\u22C1','bigwedge':'\u22C0','bkarow':'\u290D','blacklozenge':'\u29EB','blacksquare':'\u25AA','blacktriangle':'\u25B4','blacktriangledown':'\u25BE','blacktriangleleft':'\u25C2','blacktriangleright':'\u25B8','blank':'\u2423','blk12':'\u2592','blk14':'\u2591','blk34':'\u2593','block':'\u2588','bne':'=\u20E5','bnequiv':'\u2261\u20E5','bnot':'\u2310','bNot':'\u2AED','bopf':'\uD835\uDD53','Bopf':'\uD835\uDD39','bot':'\u22A5','bottom':'\u22A5','bowtie':'\u22C8','boxbox':'\u29C9','boxdl':'\u2510','boxdL':'\u2555','boxDl':'\u2556','boxDL':'\u2557','boxdr':'\u250C','boxdR':'\u2552','boxDr':'\u2553','boxDR':'\u2554','boxh':'\u2500','boxH':'\u2550','boxhd':'\u252C','boxhD':'\u2565','boxHd':'\u2564','boxHD':'\u2566','boxhu':'\u2534','boxhU':'\u2568','boxHu':'\u2567','boxHU':'\u2569','boxminus':'\u229F','boxplus':'\u229E','boxtimes':'\u22A0','boxul':'\u2518','boxuL':'\u255B','boxUl':'\u255C','boxUL':'\u255D','boxur':'\u2514','boxuR':'\u2558','boxUr':'\u2559','boxUR':'\u255A','boxv':'\u2502','boxV':'\u2551','boxvh':'\u253C','boxvH':'\u256A','boxVh':'\u256B','boxVH':'\u256C','boxvl':'\u2524','boxvL':'\u2561','boxVl':'\u2562','boxVL':'\u2563','boxvr':'\u251C','boxvR':'\u255E','boxVr':'\u255F','boxVR':'\u2560','bprime':'\u2035','breve':'\u02D8','Breve':'\u02D8','brvbar':'\xA6','bscr':'\uD835\uDCB7','Bscr':'\u212C','bsemi':'\u204F','bsim':'\u223D','bsime':'\u22CD','bsol':'\\','bsolb':'\u29C5','bsolhsub':'\u27C8','bull':'\u2022','bullet':'\u2022','bump':'\u224E','bumpe':'\u224F','bumpE':'\u2AAE','bumpeq':'\u224F','Bumpeq':'\u224E','cacute':'\u0107','Cacute':'\u0106','cap':'\u2229','Cap':'\u22D2','capand':'\u2A44','capbrcup':'\u2A49','capcap':'\u2A4B','capcup':'\u2A47','capdot':'\u2A40','CapitalDifferentialD':'\u2145','caps':'\u2229\uFE00','caret':'\u2041','caron':'\u02C7','Cayleys':'\u212D','ccaps':'\u2A4D','ccaron':'\u010D','Ccaron':'\u010C','ccedil':'\xE7','Ccedil':'\xC7','ccirc':'\u0109','Ccirc':'\u0108','Cconint':'\u2230','ccups':'\u2A4C','ccupssm':'\u2A50','cdot':'\u010B','Cdot':'\u010A','cedil':'\xB8','Cedilla':'\xB8','cemptyv':'\u29B2','cent':'\xA2','centerdot':'\xB7','CenterDot':'\xB7','cfr':'\uD835\uDD20','Cfr':'\u212D','chcy':'\u0447','CHcy':'\u0427','check':'\u2713','checkmark':'\u2713','chi':'\u03C7','Chi':'\u03A7','cir':'\u25CB','circ':'\u02C6','circeq':'\u2257','circlearrowleft':'\u21BA','circlearrowright':'\u21BB','circledast':'\u229B','circledcirc':'\u229A','circleddash':'\u229D','CircleDot':'\u2299','circledR':'\xAE','circledS':'\u24C8','CircleMinus':'\u2296','CirclePlus':'\u2295','CircleTimes':'\u2297','cire':'\u2257','cirE':'\u29C3','cirfnint':'\u2A10','cirmid':'\u2AEF','cirscir':'\u29C2','ClockwiseContourIntegral':'\u2232','CloseCurlyDoubleQuote':'\u201D','CloseCurlyQuote':'\u2019','clubs':'\u2663','clubsuit':'\u2663','colon':':','Colon':'\u2237','colone':'\u2254','Colone':'\u2A74','colLine truncated
|
||||
var decodeMapLegacy = {'aacute':'\xE1','Aacute':'\xC1','acirc':'\xE2','Acirc':'\xC2','acute':'\xB4','aelig':'\xE6','AElig':'\xC6','agrave':'\xE0','Agrave':'\xC0','amp':'&','AMP':'&','aring':'\xE5','Aring':'\xC5','atilde':'\xE3','Atilde':'\xC3','auml':'\xE4','Auml':'\xC4','brvbar':'\xA6','ccedil':'\xE7','Ccedil':'\xC7','cedil':'\xB8','cent':'\xA2','copy':'\xA9','COPY':'\xA9','curren':'\xA4','deg':'\xB0','divide':'\xF7','eacute':'\xE9','Eacute':'\xC9','ecirc':'\xEA','Ecirc':'\xCA','egrave':'\xE8','Egrave':'\xC8','eth':'\xF0','ETH':'\xD0','euml':'\xEB','Euml':'\xCB','frac12':'\xBD','frac14':'\xBC','frac34':'\xBE','gt':'>','GT':'>','iacute':'\xED','Iacute':'\xCD','icirc':'\xEE','Icirc':'\xCE','iexcl':'\xA1','igrave':'\xEC','Igrave':'\xCC','iquest':'\xBF','iuml':'\xEF','Iuml':'\xCF','laquo':'\xAB','lt':'<','LT':'<','macr':'\xAF','micro':'\xB5','middot':'\xB7','nbsp':'\xA0','not':'\xAC','ntilde':'\xF1','Ntilde':'\xD1','oacute':'\xF3','Oacute':'\xD3','ocirc':'\xF4','Ocirc':'\xD4','ograve':'\xF2','Ograve':'\xD2','ordf':'\xAA','ordm':'\xBA','oslash':'\xF8','Oslash':'\xD8','otilde':'\xF5','Otilde':'\xD5','ouml':'\xF6','Ouml':'\xD6','para':'\xB6','plusmn':'\xB1','pound':'\xA3','quot':'"','QUOT':'"','raquo':'\xBB','reg':'\xAE','REG':'\xAE','sect':'\xA7','shy':'\xAD','sup1':'\xB9','sup2':'\xB2','sup3':'\xB3','szlig':'\xDF','thorn':'\xFE','THORN':'\xDE','times':'\xD7','uacute':'\xFA','Uacute':'\xDA','ucirc':'\xFB','Ucirc':'\xDB','ugrave':'\xF9','Ugrave':'\xD9','uml':'\xA8','uuml':'\xFC','Uuml':'\xDC','yacute':'\xFD','Yacute':'\xDD','yen':'\xA5','yuml':'\xFF'};
|
||||
var decodeMapNumeric = {'0':'\uFFFD','128':'\u20AC','130':'\u201A','131':'\u0192','132':'\u201E','133':'\u2026','134':'\u2020','135':'\u2021','136':'\u02C6','137':'\u2030','138':'\u0160','139':'\u2039','140':'\u0152','142':'\u017D','145':'\u2018','146':'\u2019','147':'\u201C','148':'\u201D','149':'\u2022','150':'\u2013','151':'\u2014','152':'\u02DC','153':'\u2122','154':'\u0161','155':'\u203A','156':'\u0153','158':'\u017E','159':'\u0178'};
|
||||
var invalidReferenceCodePoints = [1,2,3,4,5,6,7,8,11,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,64976,64977,64978,64979,64980,64981,64982,64983,64984,64985,64986,64987,64988,64989,64990,64991,64992,64993,64994,64995,64996,64997,64998,64999,65000,65001,65002,65003,65004,65005,65006,65007,65534,65535,131070,131071,196606,196607,262142,262143,327678,327679,393214,393215,458750,458751,524286,524287,589822,589823,655358,655359,720894,720895,786430,786431,851966,851967,917502,917503,983038,983039,1048574,1048575,1114110,1114111];
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
var stringFromCharCode = String.fromCharCode;
|
||||
|
||||
var object = {};
|
||||
var hasOwnProperty = object.hasOwnProperty;
|
||||
var has = function(object, propertyName) {
|
||||
return hasOwnProperty.call(object, propertyName);
|
||||
};
|
||||
|
||||
var contains = function(array, value) {
|
||||
var index = -1;
|
||||
var length = array.length;
|
||||
while (++index < length) {
|
||||
if (array[index] == value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
var merge = function(options, defaults) {
|
||||
if (!options) {
|
||||
return defaults;
|
||||
}
|
||||
var result = {};
|
||||
var key;
|
||||
for (key in defaults) {
|
||||
// A `hasOwnProperty` check is not needed here, since only recognized
|
||||
// option names are used anyway. Any others are ignored.
|
||||
result[key] = has(options, key) ? options[key] : defaults[key];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// Modified version of `ucs2encode`; see https://mths.be/punycode.
|
||||
var codePointToSymbol = function(codePoint, strict) {
|
||||
var output = '';
|
||||
if ((codePoint >= 0xD800 && codePoint <= 0xDFFF) || codePoint > 0x10FFFF) {
|
||||
// See issue #4:
|
||||
// “Otherwise, if the number is in the range 0xD800 to 0xDFFF or is
|
||||
// greater than 0x10FFFF, then this is a parse error. Return a U+FFFD
|
||||
// REPLACEMENT CHARACTER.”
|
||||
if (strict) {
|
||||
parseError('character reference outside the permissible Unicode range');
|
||||
}
|
||||
return '\uFFFD';
|
||||
}
|
||||
if (has(decodeMapNumeric, codePoint)) {
|
||||
if (strict) {
|
||||
parseError('disallowed character reference');
|
||||
}
|
||||
return decodeMapNumeric[codePoint];
|
||||
}
|
||||
if (strict && contains(invalidReferenceCodePoints, codePoint)) {
|
||||
parseError('disallowed character reference');
|
||||
}
|
||||
if (codePoint > 0xFFFF) {
|
||||
codePoint -= 0x10000;
|
||||
output += stringFromCharCode(codePoint >>> 10 & 0x3FF | 0xD800);
|
||||
codePoint = 0xDC00 | codePoint & 0x3FF;
|
||||
}
|
||||
output += stringFromCharCode(codePoint);
|
||||
return output;
|
||||
};
|
||||
|
||||
var hexEscape = function(codePoint) {
|
||||
return '&#x' + codePoint.toString(16).toUpperCase() + ';';
|
||||
};
|
||||
|
||||
var decEscape = function(codePoint) {
|
||||
return '&#' + codePoint + ';';
|
||||
};
|
||||
|
||||
var parseError = function(message) {
|
||||
throw Error('Parse error: ' + message);
|
||||
};
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
var encode = function(string, options) {
|
||||
options = merge(options, encode.options);
|
||||
var strict = options.strict;
|
||||
if (strict && regexInvalidRawCodePoint.test(string)) {
|
||||
parseError('forbidden code point');
|
||||
}
|
||||
var encodeEverything = options.encodeEverything;
|
||||
var useNamedReferences = options.useNamedReferences;
|
||||
var allowUnsafeSymbols = options.allowUnsafeSymbols;
|
||||
var escapeCodePoint = options.decimal ? decEscape : hexEscape;
|
||||
|
||||
var escapeBmpSymbol = function(symbol) {
|
||||
return escapeCodePoint(symbol.charCodeAt(0));
|
||||
};
|
||||
|
||||
if (encodeEverything) {
|
||||
// Encode ASCII symbols.
|
||||
string = string.replace(regexAsciiWhitelist, function(symbol) {
|
||||
// Use named references if requested & possible.
|
||||
if (useNamedReferences && has(encodeMap, symbol)) {
|
||||
return '&' + encodeMap[symbol] + ';';
|
||||
}
|
||||
return escapeBmpSymbol(symbol);
|
||||
});
|
||||
// Shorten a few escapes that represent two symbols, of which at least one
|
||||
// is within the ASCII range.
|
||||
if (useNamedReferences) {
|
||||
string = string
|
||||
.replace(/>\u20D2/g, '>⃒')
|
||||
.replace(/<\u20D2/g, '<⃒')
|
||||
.replace(/fj/g, 'fj');
|
||||
}
|
||||
// Encode non-ASCII symbols.
|
||||
if (useNamedReferences) {
|
||||
// Encode non-ASCII symbols that can be replaced with a named reference.
|
||||
string = string.replace(regexEncodeNonAscii, function(string) {
|
||||
// Note: there is no need to check `has(encodeMap, string)` here.
|
||||
return '&' + encodeMap[string] + ';';
|
||||
});
|
||||
}
|
||||
// Note: any remaining non-ASCII symbols are handled outside of the `if`.
|
||||
} else if (useNamedReferences) {
|
||||
// Apply named character references.
|
||||
// Encode `<>"'&` using named character references.
|
||||
if (!allowUnsafeSymbols) {
|
||||
string = string.replace(regexEscape, function(string) {
|
||||
return '&' + encodeMap[string] + ';'; // no need to check `has()` here
|
||||
});
|
||||
}
|
||||
// Shorten escapes that represent two symbols, of which at least one is
|
||||
// `<>"'&`.
|
||||
string = string
|
||||
.replace(/>\u20D2/g, '>⃒')
|
||||
.replace(/<\u20D2/g, '<⃒');
|
||||
// Encode non-ASCII symbols that can be replaced with a named reference.
|
||||
string = string.replace(regexEncodeNonAscii, function(string) {
|
||||
// Note: there is no need to check `has(encodeMap, string)` here.
|
||||
return '&' + encodeMap[string] + ';';
|
||||
});
|
||||
} else if (!allowUnsafeSymbols) {
|
||||
// Encode `<>"'&` using hexadecimal escapes, now that they’re not handled
|
||||
// using named character references.
|
||||
string = string.replace(regexEscape, escapeBmpSymbol);
|
||||
}
|
||||
return string
|
||||
// Encode astral symbols.
|
||||
.replace(regexAstralSymbols, function($0) {
|
||||
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
|
||||
var high = $0.charCodeAt(0);
|
||||
var low = $0.charCodeAt(1);
|
||||
var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
|
||||
return escapeCodePoint(codePoint);
|
||||
})
|
||||
// Encode any remaining BMP symbols that are not printable ASCII symbols
|
||||
// using a hexadecimal escape.
|
||||
.replace(regexBmpWhitelist, escapeBmpSymbol);
|
||||
};
|
||||
// Expose default options (so they can be overridden globally).
|
||||
encode.options = {
|
||||
'allowUnsafeSymbols': false,
|
||||
'encodeEverything': false,
|
||||
'strict': false,
|
||||
'useNamedReferences': false,
|
||||
'decimal' : false
|
||||
};
|
||||
|
||||
var decode = function(html, options) {
|
||||
options = merge(options, decode.options);
|
||||
var strict = options.strict;
|
||||
if (strict && regexInvalidEntity.test(html)) {
|
||||
parseError('malformed character reference');
|
||||
}
|
||||
return html.replace(regexDecode, function($0, $1, $2, $3, $4, $5, $6, $7) {
|
||||
var codePoint;
|
||||
var semicolon;
|
||||
var decDigits;
|
||||
var hexDigits;
|
||||
var reference;
|
||||
var next;
|
||||
if ($1) {
|
||||
// Decode decimal escapes, e.g. `𝌆`.
|
||||
decDigits = $1;
|
||||
semicolon = $2;
|
||||
if (strict && !semicolon) {
|
||||
parseError('character reference was not terminated by a semicolon');
|
||||
}
|
||||
codePoint = parseInt(decDigits, 10);
|
||||
return codePointToSymbol(codePoint, strict);
|
||||
}
|
||||
if ($3) {
|
||||
// Decode hexadecimal escapes, e.g. `𝌆`.
|
||||
hexDigits = $3;
|
||||
semicolon = $4;
|
||||
if (strict && !semicolon) {
|
||||
parseError('character reference was not terminated by a semicolon');
|
||||
}
|
||||
codePoint = parseInt(hexDigits, 16);
|
||||
return codePointToSymbol(codePoint, strict);
|
||||
}
|
||||
if ($5) {
|
||||
// Decode named character references with trailing `;`, e.g. `©`.
|
||||
reference = $5;
|
||||
if (has(decodeMap, reference)) {
|
||||
return decodeMap[reference];
|
||||
} else {
|
||||
// Ambiguous ampersand. https://mths.be/notes/ambiguous-ampersands
|
||||
if (strict) {
|
||||
parseError(
|
||||
'named character reference was not terminated by a semicolon'
|
||||
);
|
||||
}
|
||||
return $0;
|
||||
}
|
||||
}
|
||||
// If we’re still here, it’s a legacy reference for sure. No need for an
|
||||
// extra `if` check.
|
||||
// Decode named character references without trailing `;`, e.g. `&`
|
||||
// This is only a parse error if it gets converted to `&`, or if it is
|
||||
// followed by `=` in an attribute context.
|
||||
reference = $6;
|
||||
next = $7;
|
||||
if (next && options.isAttributeValue) {
|
||||
if (strict && next == '=') {
|
||||
parseError('`&` did not start a character reference');
|
||||
}
|
||||
return $0;
|
||||
} else {
|
||||
if (strict) {
|
||||
parseError(
|
||||
'named character reference was not terminated by a semicolon'
|
||||
);
|
||||
}
|
||||
// Note: there is no need to check `has(decodeMapLegacy, reference)`.
|
||||
return decodeMapLegacy[reference] + (next || '');
|
||||
}
|
||||
});
|
||||
};
|
||||
// Expose default options (so they can be overridden globally).
|
||||
decode.options = {
|
||||
'isAttributeValue': false,
|
||||
'strict': false
|
||||
};
|
||||
|
||||
var escape = function(string) {
|
||||
return string.replace(regexEscape, function($0) {
|
||||
// Note: there is no need to check `has(escapeMap, $0)` here.
|
||||
return escapeMap[$0];
|
||||
});
|
||||
};
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
var he = {
|
||||
'version': '1.1.1',
|
||||
'encode': encode,
|
||||
'decode': decode,
|
||||
'escape': escape,
|
||||
'unescape': decode
|
||||
};
|
||||
|
||||
// Some AMD build optimizers, like r.js, check for specific condition patterns
|
||||
// like the following:
|
||||
if (
|
||||
typeof define == 'function' &&
|
||||
typeof define.amd == 'object' &&
|
||||
define.amd
|
||||
) {
|
||||
define(function() {
|
||||
return he;
|
||||
});
|
||||
} else if (freeExports && !freeExports.nodeType) {
|
||||
if (freeModule) { // in Node.js, io.js, or RingoJS v0.8.0+
|
||||
freeModule.exports = he;
|
||||
} else { // in Narwhal or RingoJS v0.7.0-
|
||||
for (var key in he) {
|
||||
has(he, key) && (freeExports[key] = he[key]);
|
||||
}
|
||||
}
|
||||
} else { // in Rhino or a web browser
|
||||
root.he = he;
|
||||
}
|
||||
|
||||
}(this));
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
.Dd April 5, 2016
|
||||
.Dt he 1
|
||||
.Sh NAME
|
||||
.Nm he
|
||||
.Nd encode/decode HTML entities just like a browser would
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Op Fl -escape Ar string
|
||||
.br
|
||||
.Op Fl -encode Ar string
|
||||
.br
|
||||
.Op Fl -encode Fl -use-named-refs Fl -everything Fl -allow-unsafe Ar string
|
||||
.br
|
||||
.Op Fl -decode Ar string
|
||||
.br
|
||||
.Op Fl -decode Fl -attribute Ar string
|
||||
.br
|
||||
.Op Fl -decode Fl -strict Ar string
|
||||
.br
|
||||
.Op Fl v | -version
|
||||
.br
|
||||
.Op Fl h | -help
|
||||
.Sh DESCRIPTION
|
||||
.Nm
|
||||
encodes/decodes HTML entities in strings just like a browser would.
|
||||
.Sh OPTIONS
|
||||
.Bl -ohang -offset
|
||||
.It Sy "--escape"
|
||||
Take a string of text and escape it for use in text contexts in XML or HTML documents. Only the following characters are escaped: `&`, `<`, `>`, `"`, and `'`.
|
||||
.It Sy "--encode"
|
||||
Take a string of text and encode any symbols that aren't printable ASCII symbols and that can be replaced with character references. For example, it would turn `©` into `©`, but it wouldn't turn `+` into `+` since there is no point in doing so. Additionally, it replaces any remaining non-ASCII symbols with a hexadecimal escape sequence (e.g. `𝌆`). The return value of this function is always valid HTML.
|
||||
.It Sy "--encode --use-named-refs"
|
||||
Enable the use of named character references (like `©`) in the output. If compatibility with older browsers is a concern, don't use this option.
|
||||
.It Sy "--encode --everything"
|
||||
Encode every symbol in the input string, even safe printable ASCII symbols.
|
||||
.It Sy "--encode --allow-unsafe"
|
||||
Encode non-ASCII characters only. This leaves unsafe HTML/XML symbols like `&`, `<`, `>`, `"`, and `'` intact.
|
||||
.It Sy "--encode --decimal"
|
||||
Use decimal digits rather than hexadecimal digits for encoded character references, e.g. output `©` instead of `©`.
|
||||
.It Sy "--decode"
|
||||
Takes a string of HTML and decode any named and numerical character references in it using the algorithm described in the HTML spec.
|
||||
.It Sy "--decode --attribute"
|
||||
Parse the input as if it was an HTML attribute value rather than a string in an HTML text content.
|
||||
.It Sy "--decode --strict"
|
||||
Throw an error if an invalid character reference is encountered.
|
||||
.It Sy "-v, --version"
|
||||
Print he's version.
|
||||
.It Sy "-h, --help"
|
||||
Show the help screen.
|
||||
.El
|
||||
.Sh EXIT STATUS
|
||||
The
|
||||
.Nm he
|
||||
utility exits with one of the following values:
|
||||
.Pp
|
||||
.Bl -tag -width flag -compact
|
||||
.It Li 0
|
||||
.Nm
|
||||
did what it was instructed to do successfully; either it encoded/decoded the input and printed the result, or it printed the version or usage message.
|
||||
.It Li 1
|
||||
.Nm
|
||||
encountered an error.
|
||||
.El
|
||||
.Sh EXAMPLES
|
||||
.Bl -ohang -offset
|
||||
.It Sy "he --escape '<script>alert(1)</script>'"
|
||||
Print an escaped version of the given string that is safe for use in HTML text contexts, escaping only `&`, `<`, `>`, `"`, and `'`.
|
||||
.It Sy "he --decode '©𝌆'"
|
||||
Print the decoded version of the given HTML string.
|
||||
.It Sy "echo\ '©𝌆'\ |\ he --decode"
|
||||
Print the decoded version of the HTML string that gets piped in.
|
||||
.El
|
||||
.Sh BUGS
|
||||
he's bug tracker is located at <https://github.com/mathiasbynens/he/issues>.
|
||||
.Sh AUTHOR
|
||||
Mathias Bynens <https://mathiasbynens.be/>
|
||||
.Sh WWW
|
||||
<https://mths.be/he>
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
{
|
||||
"_from": "he@1.1.1",
|
||||
"_id": "he@1.1.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=",
|
||||
"_location": "/he",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "he@1.1.1",
|
||||
"name": "he",
|
||||
"escapedName": "he",
|
||||
"rawSpec": "1.1.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.1.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/mocha"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
|
||||
"_shasum": "93410fd21b009735151f8868c2f271f3427e23fd",
|
||||
"_spec": "he@1.1.1",
|
||||
"_where": "/home/ionspin/Projects/Future/kotlin-multiplatform-crypto/crypto/src/jsMain/npm/node_modules/mocha",
|
||||
"author": {
|
||||
"name": "Mathias Bynens",
|
||||
"url": "https://mathiasbynens.be/"
|
||||
},
|
||||
"bin": {
|
||||
"he": "bin/he"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mathiasbynens/he/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "A robust HTML entities encoder/decoder with full Unicode support.",
|
||||
"devDependencies": {
|
||||
"codecov.io": "^0.1.6",
|
||||
"grunt": "^0.4.5",
|
||||
"grunt-shell": "^1.1.1",
|
||||
"grunt-template": "^0.2.3",
|
||||
"istanbul": "^0.4.2",
|
||||
"jsesc": "^1.0.0",
|
||||
"lodash": "^4.8.2",
|
||||
"qunit-extras": "^1.4.5",
|
||||
"qunitjs": "~1.11.0",
|
||||
"regenerate": "^1.2.1",
|
||||
"requirejs": "^2.1.22",
|
||||
"sort-object": "^3.0.2"
|
||||
},
|
||||
"directories": {
|
||||
"bin": "bin",
|
||||
"man": "man",
|
||||
"test": "tests"
|
||||
},
|
||||
"files": [
|
||||
"LICENSE-MIT.txt",
|
||||
"he.js",
|
||||
"bin/",
|
||||
"man/"
|
||||
],
|
||||
"homepage": "https://mths.be/he",
|
||||
"keywords": [
|
||||
"string",
|
||||
"entities",
|
||||
"entity",
|
||||
"html",
|
||||
"encode",
|
||||
"decode",
|
||||
"unicode"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "he.js",
|
||||
"man": [
|
||||
"/home/ionspin/Projects/Future/kotlin-multiplatform-crypto/crypto/src/jsMain/npm/node_modules/he/man/he.1"
|
||||
],
|
||||
"name": "he",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mathiasbynens/he.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "grunt build",
|
||||
"test": "node tests/tests.js"
|
||||
},
|
||||
"version": "1.1.1"
|
||||
}
|
||||
Reference in new issue
Block a user