1
+ /* jshint laxcomma: true */
1
2
var utils = require ( './utils' )
2
- , Dom = require ( 'xmldom' ) . DOMParser
3
+ , Dom = require ( 'xmldom' ) . DOMParser ;
3
4
4
- exports . ExclusiveCanonicalization = ExclusiveCanonicalization
5
+ exports . ExclusiveCanonicalization = ExclusiveCanonicalization ;
5
6
6
- function ExclusiveCanonicalization ( ) {
7
+ function ExclusiveCanonicalization ( ) { }
7
8
8
- }
9
+ ExclusiveCanonicalization . prototype . attrCompare = function ( a , b ) {
10
+ if ( ! a . prefix && b . prefix ) return - 1 ;
11
+ if ( ! b . prefix && a . prefix ) return 1 ;
12
+ return a . name . localeCompare ( b . name ) ;
13
+ } ;
9
14
10
- ExclusiveCanonicalization . prototype . attrCompare = function ( a , b ) {
11
- if ( ! a . prefix && b . prefix ) return - 1
12
- if ( ! b . prefix && a . prefix ) return 1
13
- return a . name . localeCompare ( b . name )
14
- }
15
-
16
- ExclusiveCanonicalization . prototype . nsCompare = function ( a , b ) {
17
- var attr1 = a . prefix + a . namespaceURI
18
- var attr2 = b . prefix + b . namespaceURI
19
- if ( attr1 == attr2 ) return 0
20
- return attr1 . localeCompare ( attr2 )
21
- }
15
+ ExclusiveCanonicalization . prototype . nsCompare = function ( a , b ) {
16
+ var attr1 = a . prefix + a . namespaceURI ;
17
+ var attr2 = b . prefix + b . namespaceURI ;
18
+ if ( attr1 == attr2 ) return 0 ;
19
+ return attr1 . localeCompare ( attr2 ) ;
20
+ } ;
22
21
23
22
ExclusiveCanonicalization . prototype . renderAttrs = function ( node ) {
24
- var res = ""
25
- var attrListToRender = [ ]
23
+ var a , i , attr
24
+ , res = [ ]
25
+ , attrListToRender = [ ] ;
26
26
27
27
if ( node . attributes ) {
28
- for ( var i = 0 ; i < node . attributes . length ; i ++ ) {
29
- var attr = node . attributes [ i ]
28
+ for ( i = 0 ; i < node . attributes . length ; ++ i ) {
29
+ attr = node . attributes [ i ] ;
30
30
//ignore namespace definition attributes
31
- if ( attr . name . indexOf ( "xmlns" ) == 0 ) continue ;
32
- attrListToRender . push ( attr )
31
+ if ( attr . name . indexOf ( "xmlns" ) === 0 ) continue ;
32
+ attrListToRender . push ( attr ) ;
33
33
}
34
34
}
35
35
36
- attrListToRender . sort ( this . attrCompare )
36
+ attrListToRender . sort ( this . attrCompare ) ;
37
37
38
- for ( var a in attrListToRender ) {
39
- var attr = attrListToRender [ a ]
40
- res += " " + attr . name + "=\"" + utils . normalizeXmlIncludingCR ( attr . value ) + "\"" ;
38
+ for ( a in attrListToRender ) {
39
+ attr = attrListToRender [ a ] ;
40
+ res . push ( " " , attr . name , '="' , utils . normalizeXmlIncludingCR ( attr . value ) , '"' ) ;
41
41
}
42
42
43
- return res ;
44
- }
43
+ return res . join ( "" ) ;
44
+ } ;
45
45
46
46
47
47
/**
@@ -55,76 +55,72 @@ ExclusiveCanonicalization.prototype.renderAttrs = function(node) {
55
55
* @api private
56
56
*/
57
57
ExclusiveCanonicalization . prototype . renderNs = function ( node , prefixesInScope , defaultNs , inclusiveNamespacesPrefixList ) {
58
- var res = ""
59
- var newDefaultNs = defaultNs
60
- var nsListToRender = [ ]
61
-
62
- var currNs = node . namespaceURI || ""
58
+ var a , i , p , attr
59
+ , res = [ ]
60
+ , newDefaultNs = defaultNs
61
+ , nsListToRender = [ ]
62
+ , currNs = node . namespaceURI || "" ;
63
63
64
64
//handle the namespaceof the node itself
65
65
if ( node . prefix ) {
66
- if ( prefixesInScope . indexOf ( node . prefix ) == - 1 ) {
67
- nsListToRender . push ( { "prefix" : node . prefix , "namespaceURI" : node . namespaceURI } )
68
- prefixesInScope . push ( node . prefix ) ;
66
+ if ( prefixesInScope . indexOf ( node . prefix ) == - 1 ) {
67
+ nsListToRender . push ( { "prefix" : node . prefix , "namespaceURI" : node . namespaceURI } ) ;
68
+ prefixesInScope . push ( node . prefix ) ;
69
69
}
70
70
}
71
- else if ( defaultNs != currNs ) {
71
+ else if ( defaultNs != currNs ) {
72
72
//new default ns
73
- newDefaultNs = node . namespaceURI
74
- res += " xmlns=\"" + newDefaultNs + "\""
73
+ newDefaultNs = node . namespaceURI ;
74
+ res . push ( ' xmlns="' , newDefaultNs , '"' ) ;
75
75
}
76
76
77
77
//handle the attributes namespace
78
78
if ( node . attributes ) {
79
- for ( var i = 0 ; i < node . attributes . length ; i ++ ) {
80
- var attr = node . attributes [ i ]
81
-
79
+ for ( i = 0 ; i < node . attributes . length ; ++ i ) {
80
+ attr = node . attributes [ i ] ;
81
+
82
82
//handle all prefixed attributes that are included in the prefix list and where
83
- //the prefix is not defined already
83
+ //the prefix is not defined already
84
84
if ( attr . prefix && prefixesInScope . indexOf ( attr . localName ) === - 1 && inclusiveNamespacesPrefixList . indexOf ( attr . localName ) >= 0 ) {
85
85
nsListToRender . push ( { "prefix" : attr . localName , "namespaceURI" : attr . value } ) ;
86
86
prefixesInScope . push ( attr . localName ) ;
87
87
}
88
-
89
- //handle all prefixed attributes that are not xmlns definitions and where
90
- //the prefix is not defined already
91
- if ( attr . prefix && prefixesInScope . indexOf ( attr . prefix ) == - 1 && attr . prefix != "xmlns" ) {
92
- nsListToRender . push ( { "prefix" : attr . prefix , "namespaceURI" : attr . namespaceURI } )
88
+
89
+ //handle all prefixed attributes that are not xmlns definitions and where
90
+ //the prefix is not defined already
91
+ if ( attr . prefix && prefixesInScope . indexOf ( attr . prefix ) == - 1 && attr . prefix != "xmlns" ) {
92
+ nsListToRender . push ( { "prefix" : attr . prefix , "namespaceURI" : attr . namespaceURI } ) ;
93
93
prefixesInScope . push ( attr . prefix ) ;
94
- }
94
+ }
95
95
}
96
96
}
97
-
98
- nsListToRender . sort ( this . nsCompare )
97
+
98
+ nsListToRender . sort ( this . nsCompare ) ;
99
99
100
100
//render namespaces
101
- for ( var a in nsListToRender ) {
102
- var p = nsListToRender [ a ]
103
- res += " xmlns:" + p . prefix + "=\"" + p . namespaceURI + "\"" ;
101
+ for ( a in nsListToRender ) {
102
+ p = nsListToRender [ a ] ;
103
+ res . push ( " xmlns:" , p . prefix , '="' , p . namespaceURI , '"' ) ;
104
104
}
105
105
106
- return { "rendered" : res , "newDefaultNs" : newDefaultNs } ;
107
- }
106
+ return { "rendered" : res . join ( "" ) , "newDefaultNs" : newDefaultNs } ;
107
+ } ;
108
108
109
109
ExclusiveCanonicalization . prototype . processInner = function ( node , prefixesInScope , defaultNs , inclusiveNamespacesPrefixList ) {
110
+ if ( node . data ) return utils . normalizeXmlIncludingCR ( node . data ) ;
110
111
111
- if ( node . data ) return utils . normalizeXmlIncludingCR ( node . data )
112
+ var i , pfxCopy
113
+ , ns = this . renderNs ( node , prefixesInScope , defaultNs , inclusiveNamespacesPrefixList )
114
+ , res = [ "<" , node . tagName , ns . rendered , this . renderAttrs ( node ) , ">" ] ;
112
115
113
- var res = "<"
114
- res += node . tagName
115
- var ns = this . renderNs ( node , prefixesInScope , defaultNs , inclusiveNamespacesPrefixList )
116
- res += ns . rendered
117
- res += this . renderAttrs ( node )
118
- res += ">" ;
119
-
120
- for ( var i = 0 ; i < node . childNodes . length ; i ++ ) {
121
- var pfxCopy = prefixesInScope . slice ( 0 )
122
- res += this . processInner ( node . childNodes [ i ] , pfxCopy , ns . newDefaultNs , inclusiveNamespacesPrefixList )
116
+ for ( i = 0 ; i < node . childNodes . length ; ++ i ) {
117
+ pfxCopy = prefixesInScope . slice ( 0 ) ;
118
+ res . push ( this . processInner ( node . childNodes [ i ] , pfxCopy , ns . newDefaultNs , inclusiveNamespacesPrefixList ) ) ;
123
119
}
124
-
125
- res += "</" + node . tagName + ">"
126
- return res
127
- }
120
+
121
+ res . push ( "</" , node . tagName , ">" ) ;
122
+ return res . join ( "" ) ;
123
+ } ;
128
124
129
125
/**
130
126
* Perform canonicalization of the given node
@@ -133,20 +129,17 @@ ExclusiveCanonicalization.prototype.processInner = function(node, prefixesInScop
133
129
* @return {String }
134
130
* @api public
135
131
*/
136
- ExclusiveCanonicalization . prototype . process = function ( node , options ) {
137
- var options = options || { } ;
132
+ ExclusiveCanonicalization . prototype . process = function ( node , options ) {
133
+ options = options || { } ;
138
134
var inclusiveNamespacesPrefixList = options . inclusiveNamespacesPrefixList || [ ] ;
139
135
if ( ! ( inclusiveNamespacesPrefixList instanceof Array ) ) inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList . split ( ' ' ) ;
140
-
141
- var res = this . processInner ( node , [ ] , "" , inclusiveNamespacesPrefixList )
142
- return res
143
- //var doc = new Dom().parseFromString(res)
144
- //return doc.documentElement
145
- }
146
-
147
- ExclusiveCanonicalization . prototype . getAlgorithmName = function ( ) {
148
- return "http://www.w3.org/2001/10/xml-exc-c14n#"
149
- }
150
-
151
136
137
+ var res = this . processInner ( node , [ ] , "" , inclusiveNamespacesPrefixList ) ;
138
+ return res ;
139
+ //var doc = new Dom().parseFromString(res)
140
+ //return doc.documentElement
141
+ } ;
152
142
143
+ ExclusiveCanonicalization . prototype . getAlgorithmName = function ( ) {
144
+ return "http://www.w3.org/2001/10/xml-exc-c14n#" ;
145
+ } ;
0 commit comments