File tree 5 files changed +53
-3
lines changed
5 files changed +53
-3
lines changed Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+
3
+ const { XMLParser} = require ( "../src/fxp" ) ;
4
+
5
+ describe ( "XMLParser" , function ( ) {
6
+ it ( "should parse lowercase tagnames" , function ( ) {
7
+ const xmlData = `<?xml version='1.0'?>
8
+ <root>
9
+ <person>Person 1</person>
10
+ <Person>Person 2</Person>
11
+ <PERSON>Person 3</PERSON>
12
+ <person>Person 4</person>
13
+ </root>
14
+ ` ;
15
+
16
+ const parser = new XMLParser ( {
17
+ // transformTagName: (tagName) => tagName
18
+ ignoreDeclaration : true ,
19
+ transformTagName : ( tagName ) => tagName . toLowerCase ( )
20
+ } ) ;
21
+ let result = parser . parse ( xmlData ) ;
22
+
23
+ const expected = {
24
+ "root" : {
25
+ "person" : [
26
+ "Person 1" ,
27
+ "Person 2" ,
28
+ "Person 3" ,
29
+ "Person 4"
30
+ ]
31
+ }
32
+ } ;
33
+
34
+ expect ( result ) . toEqual ( expected ) ;
35
+ } ) ;
36
+ } ) ;
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ type X2jOptions = {
22
22
htmlEntities : boolean ;
23
23
ignoreDeclaration : boolean ;
24
24
ignorePiTags : boolean ;
25
+ transformTagName : ( ( tagName : string ) => string ) | false ;
25
26
} ;
26
27
type strnumOptions = {
27
28
hex : boolean ;
Original file line number Diff line number Diff line change @@ -30,7 +30,8 @@ const defaultOptions = {
30
30
{ regex : new RegExp ( "\"" , "g" ) , val : """ }
31
31
] ,
32
32
processEntities : true ,
33
- stopNodes : [ ]
33
+ stopNodes : [ ] ,
34
+ transformTagName : false ,
34
35
} ;
35
36
36
37
function Builder ( options ) {
Original file line number Diff line number Diff line change @@ -30,7 +30,8 @@ const defaultOptions = {
30
30
processEntities : true ,
31
31
htmlEntities : false ,
32
32
ignoreDeclaration : false ,
33
- ignorePiTags : false
33
+ ignorePiTags : false ,
34
+ transformTagName : false ,
34
35
} ;
35
36
36
37
const buildOptions = function ( options ) {
Original file line number Diff line number Diff line change @@ -193,6 +193,10 @@ const parseXml = function(xmlData) {
193
193
}
194
194
}
195
195
196
+ if ( this . options . transformTagName ) {
197
+ tagName = this . options . transformTagName ( tagName ) ;
198
+ }
199
+
196
200
if ( currentNode ) {
197
201
textData = this . saveTextToParentTag ( textData , currentNode , jPath ) ;
198
202
}
@@ -257,12 +261,15 @@ const parseXml = function(xmlData) {
257
261
258
262
i = closeIndex + 2 ;
259
263
} else { //Opening tag
260
-
261
264
let result = readTagExp ( xmlData , i , this . options . removeNSPrefix ) ;
262
265
let tagName = result . tagName ;
263
266
let tagExp = result . tagExp ;
264
267
let attrExpPresent = result . attrExpPresent ;
265
268
let closeIndex = result . closeIndex ;
269
+
270
+ if ( this . options . transformTagName ) {
271
+ tagName = this . options . transformTagName ( tagName ) ;
272
+ }
266
273
267
274
//save text as child node
268
275
if ( currentNode && textData ) {
@@ -322,6 +329,10 @@ const parseXml = function(xmlData) {
322
329
} else {
323
330
tagExp = tagExp . substr ( 0 , tagExp . length - 1 ) ;
324
331
}
332
+
333
+ if ( this . options . transformTagName ) {
334
+ tagName = this . options . transformTagName ( tagName ) ;
335
+ }
325
336
326
337
const childNode = new xmlNode ( tagName ) ;
327
338
if ( tagName !== tagExp && attrExpPresent ) {
You can’t perform that action at this time.
0 commit comments