@@ -4,6 +4,8 @@ const validAttribute = /^[a-zA-Z_][a-zA-Z0-9\-_:.]*$/;
4
4
5
5
export type Visitor = < T > ( el : T , node : ParserNode ) => T | null | undefined | void ;
6
6
7
+ let namespace = '' ;
8
+
7
9
export function materialize ( node : ParserNode , visitor : Visitor | null = null ) {
8
10
let el : any ;
9
11
@@ -23,8 +25,16 @@ export function materialize(node: ParserNode, visitor: Visitor|null = null) {
23
25
break ;
24
26
25
27
case 'element' : {
28
+ if ( node . tag === 'svg' ) {
29
+ namespace = 'http://www.w3.org/2000/svg'
30
+ }
31
+
26
32
el = createElement ( node ) ;
27
33
el . append ( ...node . children . map ( ( n ) => materialize ( n , visitor ) ) ) ;
34
+
35
+ if ( node . tag === 'svg' ) {
36
+ namespace = 'http://www.w3.org/2000/svg'
37
+ }
28
38
break ;
29
39
}
30
40
@@ -44,16 +54,14 @@ export function createTextNode(node: TextNode) {
44
54
}
45
55
46
56
export function createElement ( node : ElementNode ) {
47
- const el = document . createElement ( node . tag ) ;
48
- el [ '@attributes' ] = node . attributes ;
49
- el [ '@node' ] = node ;
57
+ const el = namespace ? document . createElementNS ( namespace , node . tag ) : document . createElement ( node . tag ) ;
50
58
51
59
node . attributes . forEach ( ( a : ParserAttribute ) => setAttribute ( el , a . name , a . value ) ) ;
52
60
53
61
return el ;
54
62
}
55
63
56
- export function setAttribute ( el : HTMLElement , attribute : string , value : string | number | boolean ) {
64
+ export function setAttribute ( el : Element , attribute : string , value : string | number | boolean ) {
57
65
if ( ! validAttribute . test ( attribute ) ) {
58
66
return ;
59
67
}
0 commit comments