1
1
/**
2
2
* @import {Root} from 'hast'
3
3
* @import {Options} from 'rehype-dom-parse'
4
- * @import {Parser, Processor} from 'unified'
4
+ * @import {Processor} from 'unified'
5
5
*/
6
6
7
7
import { fromDom } from 'hast-util-from-dom'
@@ -22,42 +22,41 @@ export default function parse(options) {
22
22
23
23
self . parser = parser
24
24
25
- /** @type {Parser<Root> } */
25
+ /**
26
+ * @param {string } value
27
+ * Value to parse.
28
+ * @returns {Root }
29
+ * Tree.
30
+ */
26
31
function parser ( value ) {
27
- const create = settings . fragment === false ? createDocument : createFragment
32
+ const create = settings . fragment === false ? parseDocument : parseFragment
28
33
// Assume document/fragment in -> root out.
29
34
return /** @type {Root } */ ( fromDom ( create ( value ) ) )
30
35
}
31
36
}
32
37
33
38
/**
34
- * Create a fragment .
39
+ * Create a document .
35
40
*
36
41
* @param {string } value
37
- * HTML .
38
- * @returns {DocumentFragment }
39
- * Document fragment .
42
+ * Value to parse .
43
+ * @returns {Document }
44
+ * Document.
40
45
*/
41
- function createFragment ( value ) {
42
- const node = createDocument ( '<!doctype html><body>' + value )
43
-
44
- /**
45
- * Pretend as a DocumentFragment node, which is fine for `fromDom`.
46
- */
47
- return /** @type {DocumentFragment } */ ( {
48
- nodeType : 11 ,
49
- childNodes : node . body . childNodes
50
- } )
46
+ function parseDocument ( value ) {
47
+ return new DOMParser ( ) . parseFromString ( value , 'text/html' )
51
48
}
52
49
53
50
/**
54
- * Create a document .
51
+ * Parse as a fragment .
55
52
*
56
53
* @param {string } value
57
- * HTML .
58
- * @returns {Document }
59
- * Document.
54
+ * Value to parse .
55
+ * @returns {DocumentFragment }
56
+ * Document fragment .
60
57
*/
61
- function createDocument ( value ) {
62
- return new DOMParser ( ) . parseFromString ( value , 'text/html' )
58
+ function parseFragment ( value ) {
59
+ const template = document . createElement ( 'template' )
60
+ template . innerHTML = value
61
+ return template . content
63
62
}
0 commit comments