3 [![Build][build-badge]][build]
4 [![Coverage][coverage-badge]][coverage]
5 [![Downloads][downloads-badge]][downloads]
6 [![Size][size-badge]][size]
8 Parse HTML character references: fast, spec-compliant, positional
16 npm install parse-entities
22 var decode = require('parse-entities')
24 decode('alpha & bravo')
27 decode('charlie ©cat; delta')
28 // => charlie ©cat; delta
30 decode('echo © foxtrot ≠ golf 𝌆 hotel')
31 // => echo © foxtrot ≠ golf 𝌆 hotel
36 ## `parseEntities(value[, options])`
40 ###### `options.additional`
42 Additional character to accept (`string?`, default: `''`).
43 This allows other characters, without error, when following an ampersand.
45 ###### `options.attribute`
47 Whether to parse `value` as an attribute value (`boolean?`, default:
50 ###### `options.nonTerminated`
52 Whether to allow non-terminated entities (`boolean`, default: `true`).
53 For example, `©cat` for `©cat`. This behaviour is spec-compliant but
54 can lead to unexpected results.
56 ###### `options.warning`
58 Error handler ([`Function?`][warning]).
62 Text handler ([`Function?`][text]).
64 ###### `options.reference`
66 Reference handler ([`Function?`][reference]).
68 ###### `options.warningContext`
70 Context used when invoking `warning` (`'*'`, optional).
72 ###### `options.textContext`
74 Context used when invoking `text` (`'*'`, optional).
76 ###### `options.referenceContext`
78 Context used when invoking `reference` (`'*'`, optional)
80 ###### `options.position`
82 Starting `position` of `value` (`Location` or `Position`, optional). Useful
83 when dealing with values nested in some sort of syntax tree. The default is:
87 start: {line: 1, column: 1, offset: 0},
94 `string` — Decoded `value`.
96 ### `function warning(reason, position, code)`
102 `this` refers to `warningContext` when given to `parseEntities`.
108 Human-readable reason for triggering a parse error (`string`).
112 Place at which the parse error occurred (`Position`).
116 Identifier of reason for triggering a parse error (`number`).
118 The following codes are used:
120 | Code | Example | Note |
121 | ---- | ------------------ | --------------------------------------------- |
122 | `1` | `foo & bar` | Missing semicolon (named) |
123 | `2` | `foo { bar` | Missing semicolon (numeric) |
124 | `3` | `Foo &bar baz` | Ampersand did not start a reference |
125 | `4` | `Foo &#` | Empty reference |
126 | `5` | `Foo &bar; baz` | Unknown entity |
127 | `6` | `Foo € baz` | [Disallowed reference][invalid] |
128 | `7` | `Foo � baz` | Prohibited: outside permissible unicode range |
130 ### `function text(value, location)`
136 `this` refers to `textContext` when given to `parseEntities`.
142 String of content (`string`).
146 Location at which `value` starts and ends (`Location`).
148 ### `function reference(value, location, source)`
150 Character reference handler.
154 `this` refers to `referenceContext` when given to `parseEntities`.
160 Encoded character reference (`string`).
164 Location at which `value` starts and ends (`Location`).
168 Source of character reference (`Location`).
172 * [`stringify-entities`](https://github.com/wooorm/stringify-entities)
173 — Encode HTML character references
174 * [`character-entities`](https://github.com/wooorm/character-entities)
175 — Info on character entities
176 * [`character-entities-html4`](https://github.com/wooorm/character-entities-html4)
177 — Info on HTML4 character entities
178 * [`character-entities-legacy`](https://github.com/wooorm/character-entities-legacy)
179 — Info on legacy character entities
180 * [`character-reference-invalid`](https://github.com/wooorm/character-reference-invalid)
181 — Info on invalid numeric character references
185 [MIT][license] © [Titus Wormer][author]
189 [build-badge]: https://img.shields.io/travis/wooorm/parse-entities.svg
191 [build]: https://travis-ci.org/wooorm/parse-entities
193 [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/parse-entities.svg
195 [coverage]: https://codecov.io/github/wooorm/parse-entities
197 [downloads-badge]: https://img.shields.io/npm/dm/parse-entities.svg
199 [downloads]: https://www.npmjs.com/package/parse-entities
201 [size-badge]: https://img.shields.io/bundlephobia/minzip/parse-entities.svg
203 [size]: https://bundlephobia.com/result?p=parse-entities
205 [npm]: https://docs.npmjs.com/cli/install
209 [author]: https://wooorm.com
211 [warning]: #function-warningreason-position-code
213 [text]: #function-textvalue-location
215 [reference]: #function-referencevalue-location-source
217 [invalid]: https://github.com/wooorm/character-reference-invalid