Skip to content

Commit 61594e1

Browse files
committed
Improved string concat and general clean up
This reduces the string concatenation Ordo from O(n^2) to O(n). I also cleaned up some to make JSHint happy.
1 parent 7c4c88e commit 61594e1

File tree

1 file changed

+77
-84
lines changed

1 file changed

+77
-84
lines changed

lib/exclusive-canonicalization.js

Lines changed: 77 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1+
/* jshint laxcomma: true */
12
var utils = require('./utils')
2-
, Dom = require('xmldom').DOMParser
3+
, Dom = require('xmldom').DOMParser;
34

4-
exports.ExclusiveCanonicalization = ExclusiveCanonicalization
5+
exports.ExclusiveCanonicalization = ExclusiveCanonicalization;
56

6-
function ExclusiveCanonicalization() {
7+
function ExclusiveCanonicalization() {}
78

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+
};
914

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+
};
2221

2322
ExclusiveCanonicalization.prototype.renderAttrs = function(node) {
24-
var res = ""
25-
var attrListToRender = []
23+
var a, i, attr
24+
, res = []
25+
, attrListToRender = [];
2626

2727
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];
3030
//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);
3333
}
3434
}
3535

36-
attrListToRender.sort(this.attrCompare)
36+
attrListToRender.sort(this.attrCompare);
3737

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), '"');
4141
}
4242

43-
return res;
44-
}
43+
return res.join("");
44+
};
4545

4646

4747
/**
@@ -55,76 +55,72 @@ ExclusiveCanonicalization.prototype.renderAttrs = function(node) {
5555
* @api private
5656
*/
5757
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 || "";
6363

6464
//handle the namespaceof the node itself
6565
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);
6969
}
7070
}
71-
else if (defaultNs!=currNs) {
71+
else if (defaultNs!=currNs) {
7272
//new default ns
73-
newDefaultNs = node.namespaceURI
74-
res += " xmlns=\"" + newDefaultNs + "\""
73+
newDefaultNs = node.namespaceURI;
74+
res.push(' xmlns="', newDefaultNs, '"');
7575
}
7676

7777
//handle the attributes namespace
7878
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+
8282
//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
8484
if (attr.prefix && prefixesInScope.indexOf(attr.localName) === -1 && inclusiveNamespacesPrefixList.indexOf(attr.localName) >= 0) {
8585
nsListToRender.push({"prefix": attr.localName, "namespaceURI": attr.value});
8686
prefixesInScope.push(attr.localName);
8787
}
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});
9393
prefixesInScope.push(attr.prefix);
94-
}
94+
}
9595
}
9696
}
97-
98-
nsListToRender.sort(this.nsCompare)
97+
98+
nsListToRender.sort(this.nsCompare);
9999

100100
//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, '"');
104104
}
105105

106-
return {"rendered": res, "newDefaultNs": newDefaultNs};
107-
}
106+
return {"rendered": res.join(""), "newDefaultNs": newDefaultNs};
107+
};
108108

109109
ExclusiveCanonicalization.prototype.processInner = function(node, prefixesInScope, defaultNs, inclusiveNamespacesPrefixList) {
110+
if (node.data) return utils.normalizeXmlIncludingCR(node.data);
110111

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), ">"];
112115

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));
123119
}
124-
125-
res+= "</" + node.tagName + ">"
126-
return res
127-
}
120+
121+
res.push("</", node.tagName, ">");
122+
return res.join("");
123+
};
128124

129125
/**
130126
* Perform canonicalization of the given node
@@ -133,20 +129,17 @@ ExclusiveCanonicalization.prototype.processInner = function(node, prefixesInScop
133129
* @return {String}
134130
* @api public
135131
*/
136-
ExclusiveCanonicalization.prototype.process = function(node, options) {
137-
var options = options || {};
132+
ExclusiveCanonicalization.prototype.process = function(node, options) {
133+
options = options || {};
138134
var inclusiveNamespacesPrefixList = options.inclusiveNamespacesPrefixList || [];
139135
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-
151136

137+
var res = this.processInner(node, [], "", inclusiveNamespacesPrefixList);
138+
return res;
139+
//var doc = new Dom().parseFromString(res)
140+
//return doc.documentElement
141+
};
152142

143+
ExclusiveCanonicalization.prototype.getAlgorithmName = function() {
144+
return "http://www.w3.org/2001/10/xml-exc-c14n#";
145+
};

0 commit comments

Comments
 (0)