Skip to content

Commit

Permalink
add toString sorter for attributes output
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjinyun committed Dec 18, 2015
1 parent f053be7 commit 447e45e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
23 changes: 13 additions & 10 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,25 +902,28 @@ function ProcessingInstruction() {
ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
_extends(ProcessingInstruction,Node);
function XMLSerializer(){}
XMLSerializer.prototype.serializeToString = function(node){
XMLSerializer.prototype.serializeToString = function(node,attributeSorter){
node.serializeToString(attributeSorter);
}
Node.prototype.toString =function(attributeSorter){
var buf = [];
serializeToString(node,buf);
serializeToString(this,buf,attributeSorter);
return buf.join('');
}
Node.prototype.toString =function(){
return XMLSerializer.prototype.serializeToString(this);
}
function serializeToString(node,buf){
function serializeToString(node,buf,attributeSorter,isHTML){
switch(node.nodeType){
case ELEMENT_NODE:
var attrs = node.attributes;
var len = attrs.length;
var child = node.firstChild;
var nodeName = node.tagName;
var isHTML = htmlns === node.namespaceURI
isHTML = (htmlns === node.namespaceURI) ||isHTML
buf.push('<',nodeName);
if(attributeSorter){
buf.sort.apply(attrs, attributeSorter);
}
for(var i=0;i<len;i++){
serializeToString(attrs.item(i),buf,isHTML);
serializeToString(attrs.item(i),buf,attributeSorter,isHTML);
}
if(child || isHTML && !/^(?:meta|link|img|br|hr|input|button)$/i.test(nodeName)){
buf.push('>');
Expand All @@ -931,7 +934,7 @@ function serializeToString(node,buf){
}
}else{
while(child){
serializeToString(child,buf);
serializeToString(child,buf,attributeSorter,isHTML);
child = child.nextSibling;
}
}
Expand All @@ -944,7 +947,7 @@ function serializeToString(node,buf){
case DOCUMENT_FRAGMENT_NODE:
var child = node.firstChild;
while(child){
serializeToString(child,buf);
serializeToString(child,buf,attributeSorter,isHTML);
child = child.nextSibling;
}
return;
Expand Down
3 changes: 3 additions & 0 deletions test/test-define.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(__dirname)
exports.XMLSerializer = require('../dom').XMLSerializer ;
exports.DOMParser = require('../dom-parser').DOMParser;

0 comments on commit 447e45e

Please sign in to comment.