@@ -4,6 +4,8 @@ const validAttribute = /^[a-zA-Z_][a-zA-Z0-9\-_:.]*$/;
44
55export type Visitor = < T > ( el : T , node : ParserNode ) => T | null | undefined | void ;
66
7+ let namespace = '' ;
8+
79export function materialize ( node : ParserNode , visitor : Visitor | null = null ) {
810 let el : any ;
911
@@ -23,8 +25,16 @@ export function materialize(node: ParserNode, visitor: Visitor|null = null) {
2325 break ;
2426
2527 case 'element' : {
28+ if ( node . tag === 'svg' ) {
29+ namespace = 'http://www.w3.org/2000/svg'
30+ }
31+
2632 el = createElement ( node ) ;
2733 el . append ( ...node . children . map ( ( n ) => materialize ( n , visitor ) ) ) ;
34+
35+ if ( node . tag === 'svg' ) {
36+ namespace = 'http://www.w3.org/2000/svg'
37+ }
2838 break ;
2939 }
3040
@@ -44,16 +54,14 @@ export function createTextNode(node: TextNode) {
4454}
4555
4656export 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 ) ;
5058
5159 node . attributes . forEach ( ( a : ParserAttribute ) => setAttribute ( el , a . name , a . value ) ) ;
5260
5361 return el ;
5462}
5563
56- export function setAttribute ( el : HTMLElement , attribute : string , value : string | number | boolean ) {
64+ export function setAttribute ( el : Element , attribute : string , value : string | number | boolean ) {
5765 if ( ! validAttribute . test ( attribute ) ) {
5866 return ;
5967 }
0 commit comments