+/*
+ * TODO: this doesn't support prefix redefinitions
+ */
+HTMLSerializer.prototype._unassignNamespace = function(nsData) {
+ this.nsMap[nsData.uri] = undefined;
+};
+
+HTMLSerializer.prototype._assignNamespace = function(uri) {
+ if(uri === null) {
+ // default namespace
+ return ({"prefix": "", "uri": "", "fresh": false});
+ }
+
+ if(this.nsMap[uri] === undefined) {
+ // this prefix hasn't been defined yet in current context
+ var prefix = NAMESPACES[uri];
+
+ if (prefix === undefined) { // not predefined
+ prefix = "ns" + this.nsCounter;
+ this.nsCounter += 1;
+ }
+
+ this.nsMap[uri] = prefix;
+ return ({
+ "prefix": prefix,
+ "uri": uri,
+ "fresh": true
+ });
+ }
+
+ return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
+};
+
+HTMLSerializer.prototype._join = function(prefix, name) {
+ if(!!prefix)
+ return prefix + ":" + name;
+ return name;
+};
+
+HTMLSerializer.prototype._rjoin = function(prefix, name) {
+ if(!!name)
+ return prefix + ":" + name;
+ return prefix;
+};
+
+HTMLSerializer.prototype._serializeElement = function(node) {
+ var self = this;
+
+ var ns = node.getAttribute('x-ns');
+ var nsPrefix = null;
+ var newNamespaces = [];
+
+ var nsData = self._assignNamespace(node.getAttribute('x-ns'));
+
+ if(nsData.fresh) {
+ newNamespaces.push(nsData);
+ self.stack.push({
+ "type": NS_END,
+ "namespace": nsData
+ });
+ }
+
+ var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
+
+ /* retrieve attributes */
+ var attributeIDs = [];
+ 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)
+ attributeIDs.push(m[1]);
+ };
+
+ /* print out */
+ if (getPadding(tagName))
+ self.result += '\n';
+
+ self.result += '<' + tagName;
+
+ $.each(attributeIDs, function() {
+ var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
+
+ if(nsData.fresh) {
+ newNamespaces.push(nsData);
+ self.stack.push({
+ "type": NS_END,
+ "namespace": nsData
+ });
+ };
+
+ self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
+ self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
+ });
+
+ /* print new namespace declarations */
+ $.each(newNamespaces, function() {
+ self.result += " " + self._rjoin("xmlns", this.prefix);
+ self.result += '="' + this.uri + '"';
+ });
+
+ if (node.childNodes.length > 0) {
+ self.result += ">";
+ self._pushTagEnd(tagName);
+ self._pushChildren(node);
+ }
+ else {
+ self.result += "/>";
+ };
+};
+
+function html2text(params) {