@@ -4,7 +4,7 @@ import { XmlText } from "./XmlText";
4
4
5
5
/**
6
6
* @internal
7
- *
7
+ *
8
8
* Represents an XML node.
9
9
*/
10
10
export class XmlNode {
@@ -21,36 +21,106 @@ export class XmlNode {
21
21
return node ;
22
22
}
23
23
24
- constructor ( private name : string , public readonly children : Stringable [ ] = [ ] ) { }
24
+ public constructor ( private name : string , public readonly children : Stringable [ ] = [ ] ) { }
25
25
26
- withName ( name : string ) : XmlNode {
26
+ public withName ( name : string ) : XmlNode {
27
27
this . name = name ;
28
28
return this ;
29
29
}
30
30
31
- addAttribute ( name : string , value : any ) : XmlNode {
31
+ public addAttribute ( name : string , value : any ) : XmlNode {
32
32
this . attributes [ name ] = value ;
33
33
return this ;
34
34
}
35
35
36
- addChildNode ( child : Stringable ) : XmlNode {
36
+ public addChildNode ( child : Stringable ) : XmlNode {
37
37
this . children . push ( child ) ;
38
38
return this ;
39
39
}
40
40
41
- removeAttribute ( name : string ) : XmlNode {
41
+ public removeAttribute ( name : string ) : XmlNode {
42
42
delete this . attributes [ name ] ;
43
43
return this ;
44
44
}
45
45
46
- toString ( ) : string {
46
+ /**
47
+ * @internal
48
+ * Alias of {@link XmlNode#withName(string)} for codegen brevity.
49
+ */
50
+ public n ( name : string ) : XmlNode {
51
+ this . name = name ;
52
+ return this ;
53
+ }
54
+
55
+ /**
56
+ * @internal
57
+ * Alias of {@link XmlNode#addChildNode(string)} for codegen brevity.
58
+ */
59
+ public c ( child : Stringable ) : XmlNode {
60
+ this . children . push ( child ) ;
61
+ return this ;
62
+ }
63
+
64
+ /**
65
+ * @internal
66
+ * Checked version of {@link XmlNode#addAttribute(string)} for codegen brevity.
67
+ */
68
+ public a ( name : string , value : any ) : XmlNode {
69
+ if ( value != null ) {
70
+ this . attributes [ name ] = value ;
71
+ }
72
+ return this ;
73
+ }
74
+
75
+ /**
76
+ * Create a child node.
77
+ * Used in serialization of string fields.
78
+ * @internal
79
+ */
80
+ public cc ( input : any , field : string , withName : string = field ) : void {
81
+ if ( input [ field ] != null ) {
82
+ const node = XmlNode . of ( field , input [ field ] ) . withName ( withName ) ;
83
+ this . c ( node ) ;
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Creates list child nodes.
89
+ * @internal
90
+ */
91
+ public l ( input : any , listName : string , memberName : string , valueProvider : Function ) : void {
92
+ if ( input [ listName ] != null ) {
93
+ const nodes = valueProvider ( ) ;
94
+ nodes . map ( ( node : any ) => {
95
+ node . withName ( memberName ) ;
96
+ this . c ( node ) ;
97
+ } ) ;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Creates list child nodes with container.
103
+ * @internal
104
+ */
105
+ public lc ( input : any , listName : string , memberName : string , valueProvider : Function ) : void {
106
+ if ( input [ listName ] != null ) {
107
+ const nodes = valueProvider ( ) ;
108
+ const containerNode = new XmlNode ( memberName ) ;
109
+ nodes . map ( ( node : any ) => {
110
+ containerNode . c ( node ) ;
111
+ } ) ;
112
+ this . c ( containerNode ) ;
113
+ }
114
+ }
115
+
116
+ public toString ( ) : string {
47
117
const hasChildren = Boolean ( this . children . length ) ;
48
118
let xmlText = `<${ this . name } ` ;
49
119
// add attributes
50
120
const attributes = this . attributes ;
51
121
for ( const attributeName of Object . keys ( attributes ) ) {
52
122
const attribute = attributes [ attributeName ] ;
53
- if ( typeof attribute !== "undefined" && attribute != = null ) {
123
+ if ( attribute != null ) {
54
124
xmlText += ` ${ attributeName } ="${ escapeAttribute ( "" + attribute ) } "` ;
55
125
}
56
126
}
0 commit comments