1
1
/**
2
2
* @typedef {import('hast').Root } Root
3
3
* @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
5
8
*
6
9
* @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.
9
22
*
10
23
* @typedef Error
24
+ * Error from `parse5`.
11
25
* @property {string } code
12
26
* @property {number } startLine
13
27
* @property {number } startCol
17
31
* @property {number } endOffset
18
32
*
19
33
* @callback OnError
34
+ * Handle errors.
20
35
* @param {VFileMessage } error
36
+ * Message.
21
37
* @returns {void }
38
+ * Nothing.
22
39
*
23
40
* @typedef ParseFields
24
- * @property {boolean| undefined } [fragment=false]
41
+ * @property {boolean | null | undefined } [fragment=false]
25
42
* Specify whether to parse a fragment, instead of a complete document.
43
+ *
26
44
* In document mode, unopened `html`, `head`, and `body` elements are opened
27
45
* 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
+ *
29
49
* > 👉 **Note**: parse errors are currently being added to HTML.
30
50
* > Not all errors emitted by parse5 (or us) are specced yet.
31
51
* > Some documentation may still be missing.
32
52
*
33
- * Call `onerror` with parse errors while parsing.
34
- *
35
53
* Specific rules can be turned off by setting them to `false` (or `0`).
36
54
* The default, when `emitParseErrors: true`, is `true` (or `1`), and means
37
55
* that rules emit as warnings.
38
56
* Rules can also be configured with `2`, to turn them into fatal errors.
39
- *
57
+ */
58
+
59
+ /**
40
60
* @typedef {FromParse5Options & ParseFields & ErrorFields } Options
61
+ * Configuration.
41
62
*/
42
63
43
64
import { parse , parseFragment } from 'parse5'
@@ -51,27 +72,33 @@ const base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
51
72
const fatalities = { 2 : true , 1 : false , 0 : null }
52
73
53
74
/**
75
+ * Turn serialized HTML into a hast tree.
76
+ *
54
77
* @param {VFileCompatible } value
55
- * @param {Options } [options={}]
78
+ * Serialized HTML to parse.
79
+ * @param {Options | null | undefined } [options={}]
80
+ * Configuration (optional).
56
81
* @returns {Root }
82
+ * Tree.
57
83
*/
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
60
87
const file = value instanceof VFile ? value : new VFile ( value )
61
- const fn = options . fragment ? parseFragment : parse
88
+ const fn = settings . fragment ? parseFragment : parse
62
89
const doc = String ( file )
63
90
const p5doc = fn ( doc , {
64
91
sourceCodeLocationInfo : true ,
65
- onParseError : options . onerror ? onerror : null ,
92
+ onParseError : settings . onerror ? onerror : null ,
66
93
scriptingEnabled : false
67
94
} )
68
95
69
96
// @ts -expect-error: `parse5` returns document or fragment, which are always
70
97
// mapped to roots.
71
98
return fromParse5 ( p5doc , {
72
99
file,
73
- space : options . space ,
74
- verbose : options . verbose
100
+ space : settings . space ,
101
+ verbose : settings . verbose
75
102
} )
76
103
77
104
/**
@@ -80,7 +107,7 @@ export function fromHtml(value, options = {}) {
80
107
function onerror ( error ) {
81
108
const code = error . code
82
109
const name = camelcase ( code )
83
- const setting = options [ name ]
110
+ const setting = settings [ name ]
84
111
const config = setting === undefined || setting === null ? true : setting
85
112
const level = typeof config === 'number' ? config : config ? 1 : 0
86
113
const start = {
0 commit comments