+function html2text(rootElement) {
+ var stack = [];
+ var result = "";
+
+ stack.push([ELEM_START, rootElement]);
+ console.log("SERIALIZING")
+
+ while( stack.length > 0) {
+ var pair = stack.pop();
+
+ var event = pair[0];
+ var node = pair[1];
+
+ // console.log("NODE", event, node);
+
+ if(event == ELEM_END) {
+ result += "</" + node + ">\n";
+ continue;
+ };
+
+ switch(node.nodeType) {
+ case ELEMENT_NODE:
+ if(!node.hasAttribute('x-node'))
+ break;
+
+ var tag_name = NAMESPACES[node.getAttribute('x-ns')] + node.getAttribute('x-node');
+ // console.log("ELEMENT: ", tag_name);
+
+ /* retrieve attributes */
+ var attr_ids = [];
+ for(var i=0; i < node.attributes.length; i++) {
+ var attr = node.attributes.item(i);
+
+ // check if name starts with "x-attr-name"
+ var m = attr.name.match(XATTR_RE);
+ if(m !== null)
+ attr_ids.push(m[1]);
+
+ };
+
+ result += '<' + tag_name;
+
+ $.each(attr_ids, function() {
+ result += ' ' + NAMESPACES[node.getAttribute('x-attr-ns-'+this)];
+ result += node.getAttribute('x-attr-name-'+this);
+ result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
+ });
+ result += '>'
+
+ stack.push([ELEM_END, tag_name]);
+ for(var i = node.childNodes.length-1; i >= 0; i--)
+ stack.push([ELEM_START, node.childNodes.item(i)]);
+
+ break;
+ case TEXT_NODE:
+ result += node.nodeValue;
+ break;
+ }
+ }
+
+ return result;
+}
+
+function html2xml(options) {
+ try {
+ return options.success(html2text(options.htmlElement));
+ } catch(e) {
+ options.error("Nie udało się zserializować tekstu:" + e)
+ }
+};
\ No newline at end of file