11/**
22 * @typedef {import('hast').Root } Root
33 * @typedef {import('vfile').VFileCompatible } VFileCompatible
4- * @typedef {Pick<import('hast-util-from-parse5').Options, 'space'|'verbose'> } FromParse5Options
4+ */
5+
6+ /**
7+ * @typedef {Pick<import('hast-util-from-parse5').Options, 'space' | 'verbose'> } FromParse5Options
58 *
69 * @typedef {keyof errors } ErrorCode
7- * @typedef {0|1|2|boolean|null|undefined } ErrorSeverity
8- * @typedef {Partial<Record<ErrorCode, ErrorSeverity>> } ErrorFields
10+ * Known names of parse errors.
11+ * @typedef {0 | 1 | 2 | boolean } ErrorSeverity
12+ * Error severity:
13+ *
14+ * * `0` or `false`
15+ * — turn the parse error off
16+ * * `1` or `true`
17+ * — turn the parse error into a warning
18+ * * `2`
19+ * — turn the parse error into an actual error: processing stops.
20+ * @typedef {Partial<Record<ErrorCode, ErrorSeverity | null | undefined>> } ErrorFields
21+ * Error configuration.
922 *
1023 * @typedef Error
24+ * Error from `parse5`.
1125 * @property {string } code
1226 * @property {number } startLine
1327 * @property {number } startCol
1731 * @property {number } endOffset
1832 *
1933 * @callback OnError
34+ * Handle errors.
2035 * @param {VFileMessage } error
36+ * Message.
2137 * @returns {void }
38+ * Nothing.
2239 *
2340 * @typedef ParseFields
24- * @property {boolean| undefined } [fragment=false]
41+ * @property {boolean | null | undefined } [fragment=false]
2542 * Specify whether to parse a fragment, instead of a complete document.
43+ *
2644 * In document mode, unopened `html`, `head`, and `body` elements are opened
2745 * in just the right places.
28- * @property {OnError|undefined } [onerror=false]
46+ * @property {OnError | null | undefined } [onerror]
47+ * Call `onerror` with parse errors while parsing.
48+ *
2949 * > 👉 **Note**: parse errors are currently being added to HTML.
3050 * > Not all errors emitted by parse5 (or us) are specced yet.
3151 * > Some documentation may still be missing.
3252 *
33- * Call `onerror` with parse errors while parsing.
34- *
3553 * Specific rules can be turned off by setting them to `false` (or `0`).
3654 * The default, when `emitParseErrors: true`, is `true` (or `1`), and means
3755 * that rules emit as warnings.
3856 * Rules can also be configured with `2`, to turn them into fatal errors.
39- *
57+ */
58+
59+ /**
4060 * @typedef {FromParse5Options & ParseFields & ErrorFields } Options
61+ * Configuration.
4162 */
4263
4364import { parse , parseFragment } from 'parse5'
@@ -51,27 +72,33 @@ const base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
5172const fatalities = { 2 : true , 1 : false , 0 : null }
5273
5374/**
75+ * Turn serialized HTML into a hast tree.
76+ *
5477 * @param {VFileCompatible } value
55- * @param {Options } [options={}]
78+ * Serialized HTML to parse.
79+ * @param {Options | null | undefined } [options={}]
80+ * Configuration (optional).
5681 * @returns {Root }
82+ * Tree.
5783 */
58- export function fromHtml ( value , options = { } ) {
59- const warn = options . onerror || null
84+ export function fromHtml ( value , options ) {
85+ const settings = options || { }
86+ const warn = settings . onerror || null
6087 const file = value instanceof VFile ? value : new VFile ( value )
61- const fn = options . fragment ? parseFragment : parse
88+ const fn = settings . fragment ? parseFragment : parse
6289 const doc = String ( file )
6390 const p5doc = fn ( doc , {
6491 sourceCodeLocationInfo : true ,
65- onParseError : options . onerror ? onerror : null ,
92+ onParseError : settings . onerror ? onerror : null ,
6693 scriptingEnabled : false
6794 } )
6895
6996 // @ts -expect-error: `parse5` returns document or fragment, which are always
7097 // mapped to roots.
7198 return fromParse5 ( p5doc , {
7299 file,
73- space : options . space ,
74- verbose : options . verbose
100+ space : settings . space ,
101+ verbose : settings . verbose
75102 } )
76103
77104 /**
@@ -80,7 +107,7 @@ export function fromHtml(value, options = {}) {
80107 function onerror ( error ) {
81108 const code = error . code
82109 const name = camelcase ( code )
83- const setting = options [ name ]
110+ const setting = settings [ name ]
84111 const config = setting === undefined || setting === null ? true : setting
85112 const level = typeof config === 'number' ? config : config ? 1 : 0
86113 const start = {
0 commit comments