@@ -20,7 +20,7 @@ function transform(node, options) {
20
20
21
21
// Create a document.
22
22
function root ( node , options ) {
23
- const { fragment, namespace : optionsNamespace } = options ;
23
+ const { doc , fragment, namespace : optionsNamespace } = options ;
24
24
const { children = [ ] } = node ;
25
25
const { length : childrenLength } = children ;
26
26
@@ -45,43 +45,43 @@ function root(node, options) {
45
45
let el ;
46
46
47
47
if ( rootIsDocument ) {
48
- el = document . implementation . createDocument ( namespace , '' , null ) ;
48
+ el = doc . implementation . createDocument ( namespace , '' , null ) ;
49
49
} else if ( fragment ) {
50
- el = document . createDocumentFragment ( ) ;
50
+ el = doc . createDocumentFragment ( ) ;
51
51
} else {
52
- el = document . createElement ( 'html' ) ;
52
+ el = doc . createElement ( 'html' ) ;
53
53
}
54
54
55
55
return appendAll ( el , children , Object . assign ( { fragment, namespace } , options ) ) ;
56
56
}
57
57
58
58
// Create a `doctype`.
59
- function doctype ( node ) {
60
- return document . implementation . createDocumentType (
59
+ function doctype ( node , { doc } ) {
60
+ return doc . implementation . createDocumentType (
61
61
node . name || 'html' ,
62
62
node . public || '' ,
63
63
node . system || '' ,
64
64
) ;
65
65
}
66
66
67
67
// Create a `text`.
68
- function text ( node ) {
69
- return document . createTextNode ( node . value ) ;
68
+ function text ( node , { doc } ) {
69
+ return doc . createTextNode ( node . value ) ;
70
70
}
71
71
72
72
// Create a `comment`.
73
- function comment ( node ) {
74
- return document . createComment ( node . value ) ;
73
+ function comment ( node , { doc } ) {
74
+ return doc . createComment ( node . value ) ;
75
75
}
76
76
77
77
// Create an `element`.
78
78
function element ( node , options ) {
79
- const { namespace } = options ;
79
+ const { namespace, doc } = options ;
80
80
// TODO: use `g` in SVG space.
81
81
const { tagName = 'div' , properties = { } , children = [ ] } = node ;
82
82
const el = typeof namespace !== 'undefined'
83
- ? document . createElementNS ( namespace , tagName )
84
- : document . createElement ( tagName ) ;
83
+ ? doc . createElementNS ( namespace , tagName )
84
+ : doc . createElement ( tagName ) ;
85
85
86
86
// Add HTML attributes.
87
87
const props = Object . keys ( properties ) ;
@@ -146,5 +146,5 @@ function appendAll(node, children, options) {
146
146
147
147
148
148
export default function toDOM ( hast , options = { } ) {
149
- return transform ( hast , options ) ;
149
+ return transform ( hast , { ... options , doc : options . document || document } ) ;
150
150
}
0 commit comments