Skip to content
This repository has been archived by the owner on Dec 21, 2019. It is now read-only.

Commit

Permalink
- added hash->html_string transformation
Browse files Browse the repository at this point in the history
 - renamed rootToHash to rootToHashes
 - code is still pretty raw :)
  • Loading branch information
kamicane committed Feb 23, 2009
1 parent 90f5f57 commit df5f517
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions mootools-xml.js
@@ -1,4 +1,5 @@
/* XML Methods for MooTools */ /* XML Methods for MooTools */
/* Requires MooTools v1.2 */


var XML = { var XML = {


Expand Down Expand Up @@ -33,12 +34,12 @@ var XML = {
return root; return root;
}, },


rootToHash: function(root){ rootToHashes: function(root){
var hashes = [], children = root.childNodes; var hashes = [], children = root.childNodes;


for (var i = 0, l = children.length; i < l; i++){ for (var i = 0, l = children.length; i < l; i++){
var h = XML.nodeToHash(children[i]); var hash = XML.nodeToHash(children[i]);
if (h) hashes.push(h); if (hash) hashes.push(hash);
} }


return hashes; return hashes;
Expand All @@ -58,7 +59,7 @@ var XML = {
if (attribute.nodeValue && attribute.nodeValue != 'inherit') attributesHash[attribute.nodeName] = attribute.nodeValue; if (attribute.nodeValue && attribute.nodeValue != 'inherit') attributesHash[attribute.nodeName] = attribute.nodeValue;
} }


return {tag: node.nodeName.toLowerCase(), attributes: attributesHash, children: XML.rootToHash(node)}; return {tag: node.nodeName.toLowerCase(), attributes: attributesHash, children: XML.rootToHashes(node)};


case 'textnode': return {text: node.nodeValue}; case 'textnode': return {text: node.nodeValue};


Expand All @@ -79,6 +80,24 @@ var XML = {
return element; return element;
}, },


hashToHTML: function(hash, level){
var tabs = new Array(level || 0).join('\t');
if (hash.text) return tabs + hash.text;
var attributes = [''];
for (var p in hash.attributes) attributes.push(p + '="' + hash.attributes[p] + '"');
attributes = attributes.join(' ');
var open = tabs + '<' + hash.tag + attributes + '>\n';
var close = '\n' + tabs + '</' + hash.tag + '>';
var children = XML.hashesToHTML(hash.children, level + 1);
return open + children + close;
},

hashesToHTML: function(hashes, level){
var html = [];
for (var i = 0, l = hashes.length; i < l; i++) html.push(XML.hashToHTML(hashes[i], level));
return html.join('\n');
},

transform: function(xml, xsl){ transform: function(xml, xsl){
xml = (typeof xml == 'string') ? XML.rootFromFile(xml) : xml; xml = (typeof xml == 'string') ? XML.rootFromFile(xml) : xml;
xsl = (typeof xsl == 'string') ? XML.rootFromFile(xsl) : xsl; xsl = (typeof xsl == 'string') ? XML.rootFromFile(xsl) : xsl;
Expand Down

0 comments on commit df5f517

Please sign in to comment.