@@ -20,7 +20,7 @@ function transform(node, options) {
2020
2121// Create a document.
2222function root ( node , options ) {
23- const { fragment, namespace : optionsNamespace } = options ;
23+ const { doc , fragment, namespace : optionsNamespace } = options ;
2424 const { children = [ ] } = node ;
2525 const { length : childrenLength } = children ;
2626
@@ -45,43 +45,43 @@ function root(node, options) {
4545 let el ;
4646
4747 if ( rootIsDocument ) {
48- el = document . implementation . createDocument ( namespace , '' , null ) ;
48+ el = doc . implementation . createDocument ( namespace , '' , null ) ;
4949 } else if ( fragment ) {
50- el = document . createDocumentFragment ( ) ;
50+ el = doc . createDocumentFragment ( ) ;
5151 } else {
52- el = document . createElement ( 'html' ) ;
52+ el = doc . createElement ( 'html' ) ;
5353 }
5454
5555 return appendAll ( el , children , Object . assign ( { fragment, namespace } , options ) ) ;
5656}
5757
5858// Create a `doctype`.
59- function doctype ( node ) {
60- return document . implementation . createDocumentType (
59+ function doctype ( node , { doc } ) {
60+ return doc . implementation . createDocumentType (
6161 node . name || 'html' ,
6262 node . public || '' ,
6363 node . system || '' ,
6464 ) ;
6565}
6666
6767// Create a `text`.
68- function text ( node ) {
69- return document . createTextNode ( node . value ) ;
68+ function text ( node , { doc } ) {
69+ return doc . createTextNode ( node . value ) ;
7070}
7171
7272// Create a `comment`.
73- function comment ( node ) {
74- return document . createComment ( node . value ) ;
73+ function comment ( node , { doc } ) {
74+ return doc . createComment ( node . value ) ;
7575}
7676
7777// Create an `element`.
7878function element ( node , options ) {
79- const { namespace } = options ;
79+ const { namespace, doc } = options ;
8080 // TODO: use `g` in SVG space.
8181 const { tagName = 'div' , properties = { } , children = [ ] } = node ;
8282 const el = typeof namespace !== 'undefined'
83- ? document . createElementNS ( namespace , tagName )
84- : document . createElement ( tagName ) ;
83+ ? doc . createElementNS ( namespace , tagName )
84+ : doc . createElement ( tagName ) ;
8585
8686 // Add HTML attributes.
8787 const props = Object . keys ( properties ) ;
@@ -146,5 +146,5 @@ function appendAll(node, children, options) {
146146
147147
148148export default function toDOM ( hast , options = { } ) {
149- return transform ( hast , options ) ;
149+ return transform ( hast , { ... options , doc : options . document || document } ) ;
150150}
0 commit comments