1
1
import ns from 'web-namespaces' ;
2
2
import find from 'property-information/find' ;
3
- import schema from 'property-information/html' ;
3
+ import html from 'property-information/html' ;
4
+ import svg from 'property-information/svg' ;
4
5
5
6
function transform ( node , options ) {
6
7
switch ( node . type ) {
@@ -53,7 +54,11 @@ function root(node, options) {
53
54
el = doc . createElement ( 'html' ) ;
54
55
}
55
56
56
- return appendAll ( el , children , Object . assign ( { fragment, namespace } , options ) ) ;
57
+ return appendAll (
58
+ el ,
59
+ children ,
60
+ Object . assign ( { fragment, namespace, impliedNamespace : namespace } , options ) ,
61
+ ) ;
57
62
}
58
63
59
64
// Create a `doctype`.
@@ -78,11 +83,21 @@ function comment(node, { doc }) {
78
83
// Create an `element`.
79
84
function element ( node , options ) {
80
85
const { namespace, doc } = options ;
81
- // TODO: use `g` in SVG space.
82
- const { tagName = 'div' , properties = { } , children = [ ] } = node ;
83
- const el = typeof namespace !== 'undefined'
84
- ? doc . createElementNS ( namespace , tagName )
85
- : doc . createElement ( tagName ) ;
86
+ let impliedNamespace = options . impliedNamespace || namespace ;
87
+ const { tagName = impliedNamespace === ns . svg ? 'g' : 'div' , properties = { } , children = [ ] } = node ;
88
+
89
+ if (
90
+ ( impliedNamespace === null || impliedNamespace === undefined || impliedNamespace === ns . html )
91
+ && tagName === 'svg'
92
+ ) {
93
+ impliedNamespace = ns . svg ;
94
+ }
95
+
96
+ const schema = impliedNamespace === ns . svg ? svg : html ;
97
+
98
+ const el = impliedNamespace === null || impliedNamespace === undefined
99
+ ? doc . createElement ( tagName )
100
+ : doc . createElementNS ( impliedNamespace , tagName ) ;
86
101
87
102
// Add HTML attributes.
88
103
const props = Object . keys ( properties ) ;
@@ -131,7 +146,7 @@ function element(node, options) {
131
146
}
132
147
}
133
148
134
- return appendAll ( el , children , options ) ;
149
+ return appendAll ( el , children , { ... options , impliedNamespace } ) ;
135
150
}
136
151
137
152
// Add all children.
0 commit comments