@@ -40,7 +40,7 @@ export class XmlPatcher implements patch.IPatcher {
4040 if ( key [ 0 ] === '@' ) {
4141 target . setAttribute ( key . substr ( 1 ) , element ) ;
4242 } else {
43- let node = this . createNode ( xml , this . detectNamespace ( key ) ) ;
43+ let node = this . createNode ( xml , this . detectNamespace ( xml , key ) ) ;
4444 target . appendChild ( node ) ;
4545 this . transformObject ( xml , element , node ) ;
4646 }
@@ -79,15 +79,36 @@ export class XmlPatcher implements patch.IPatcher {
7979 } ;
8080 }
8181
82- private detectNamespace ( nodeName : string ) : { name : string , namespace : string | null , localName : string } {
82+ private detectNamespace ( xml : Document , nodeName : string ) : { name : string , namespace : string | null , localName : string } {
8383 var colon = nodeName . lastIndexOf ( ':' ) ;
8484
8585 if ( colon > 0 ) {
86- return {
87- name : nodeName ,
88- namespace : this . namespaces [ nodeName . substr ( 0 , colon ) ] ,
89- localName : nodeName . substr ( colon + 1 )
90- } ;
86+ const namespace = this . namespaces [ nodeName . substr ( 0 , colon ) ] ;
87+ const localName = nodeName . substr ( colon + 1 ) ;
88+
89+ const defaultNamespace = xml . documentElement . lookupNamespaceURI ( '' ) ;
90+ const existingPrefix = xml . documentElement . lookupPrefix ( namespace ) ;
91+
92+
93+ if ( defaultNamespace === namespace ) {
94+ return {
95+ name : localName ,
96+ namespace : namespace ,
97+ localName : localName
98+ } ;
99+ } else if ( existingPrefix !== null ) {
100+ return {
101+ name : existingPrefix + ':' + localName ,
102+ namespace : namespace ,
103+ localName : localName
104+ } ;
105+ } else {
106+ return {
107+ name : nodeName ,
108+ namespace : namespace ,
109+ localName : nodeName . substr ( colon + 1 )
110+ } ;
111+ }
91112 }
92113
93114 return {
@@ -179,7 +200,7 @@ export class XmlPatcher implements patch.IPatcher {
179200 private add ( xml : Document , select : any , patch : IPatch ) : boolean {
180201 let arrayOperation = this . detectArrayOperation ( patch . path ) ;
181202 if ( arrayOperation . isArrayOperation ) {
182- let parsedName = this . detectNamespace ( patch . value ) ;
203+ let parsedName = this . detectNamespace ( xml , patch . value ) ;
183204 if ( arrayOperation . append ) {
184205 let node = < SVGSVGElement > select ( arrayOperation . path , xml , true ) ;
185206 let newNode = this . createNode ( xml , parsedName ) ;
@@ -202,11 +223,11 @@ export class XmlPatcher implements patch.IPatcher {
202223 var lastSlash = patch . path . lastIndexOf ( '/' ) ;
203224 var parentPath = patch . path . substr ( 0 , lastSlash ) ;
204225 var newNodeName = patch . path . substr ( lastSlash + 1 ) ;
205- var parsedName = this . detectNamespace ( newNodeName ) ;
226+ var parsedName = this . detectNamespace ( xml , newNodeName ) ;
206227 node = < SVGSVGElement > select ( parentPath , xml , true ) ;
207228 if ( node ) {
208229 if ( newNodeName [ 0 ] == '@' ) {
209- let attributeName = this . detectNamespace ( newNodeName ) ;
230+ let attributeName = this . detectNamespace ( xml , newNodeName ) ;
210231 if ( attributeName . namespace ) {
211232 node . setAttributeNS ( attributeName . namespace , attributeName . name , patch . value ) ;
212233 } else {
@@ -231,7 +252,7 @@ export class XmlPatcher implements patch.IPatcher {
231252 if ( arrayOperation . isArrayOperation ) {
232253 var node = < SVGSVGElement > select ( arrayOperation . path , xml , true ) ;
233254 var childNode = node . childNodes [ arrayOperation . index ] ;
234- var newNode = this . createNode ( xml , this . detectNamespace ( patch . value ) ) ;
255+ var newNode = this . createNode ( xml , this . detectNamespace ( xml , patch . value ) ) ;
235256 node . insertBefore ( newNode , childNode )
236257 node . removeChild ( childNode ) ;
237258 return true ;
0 commit comments