},
toXML: function() {
- var d = function(element, parent, level) {
- console.log(element.getText ? 'text: ' + element.getText() : 'node: ' + element.getWlxmlTag());
- var isElementNode = element instanceof documentElement.DocumentNodeElement;
- var parentChildren = parent.contents();
- var dom;
-
- var elXML = element.toXML(level);
- if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) {
- $(parentChildren[0]).after(elXML);
- } else {
- parent.prepend(elXML);
- }
-
-
- if(isElementNode) {
- elXML.each(function() {
- if(this.nodeType === Node.ELEMENT_NODE) {
- dom = $(this);
- return;
- }
- });
- var elementChildren = element.children();
- for(var i = elementChildren.length - 1; i >= 0; i--) {
- d(elementChildren[i], dom, level + 1);
- }
- }
- }
var parent = $('<div>');
- d(this.doc(), parent, 0);
+ this.doc().toXML(parent, 0)
return parent.html();
}
});
DocumentNodeElement.prototype = new DocumentElement();
+var addParts = function(parts, parent) {
+ var parentChildren = parent.contents();
+ if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) {
+ $(parentChildren[0]).after(parts);
+ } else {
+ parent.prepend(parts);
+ }
+}
+
$.extend(DocumentNodeElement.prototype, {
data: function() {
var dom = this.dom(),
return dom.removeData(args[1]);
return dom.data.apply(dom, arguments);
},
- toXML: function(level) {
- var node = $('<' + this.getWlxmlTag() + '>'),
- toret = $('<div>');
+ toXML: function(parent, level) {
+ var node = $('<' + this.getWlxmlTag() + '>');
+
if(this.getWlxmlClass())
node.attr('class', this.getWlxmlClass());
var meta = this.getWlxmlMetaAttrs();
node.attr(key, this.data('other-attrs')[key]);
}, this);
- if(this.data('orig-before') && this.data('orig-before').length) {
- this.data('orig-before').forEach(function(toAdd) {
- if(toAdd)
- toret.prepend(document.createTextNode(toAdd));
- });
- } else if(level && this.getWlxmlTag() !== 'span') {
- toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
- }
- if(this.data('orig-append') && this.data('orig-append').length) {
- this.data('orig-append').forEach(function(toAdd) {
- if(toAdd)
- node.prepend(toAdd);
- });
- //toret = toret.prepend(document.createTextNode(this.data('orig-prepend')));
- } else if(this.getWlxmlTag() !== 'span'){
- node.append('\n' + (new Array(level * 2 + 1)).join(' '));
+ var addFormatting = function() {
+ var toret = $('<div>');
+
+ if(this.data('orig-before') && this.data('orig-before').length) {
+ this.data('orig-before').forEach(function(toAdd) {
+ if(toAdd)
+ toret.prepend(document.createTextNode(toAdd));
+ });
+ } else if(level && this.getWlxmlTag() !== 'span') {
+ toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
+ }
+ if(this.data('orig-append') && this.data('orig-append').length) {
+ this.data('orig-append').forEach(function(toAdd) {
+ if(toAdd)
+ node.prepend(toAdd);
+ });
+ } else if(this.getWlxmlTag() !== 'span'){
+ node.append('\n' + (new Array(level * 2 + 1)).join(' '));
+ }
+ toret.append(node);
+ return toret.contents();
+ }.bind(this);
+
+ var parts = addFormatting(node);
+
+ var children = this.children();
+ for(var i = children.length - 1; i >= 0; i--) {
+ children[i].toXML(node, level + 1);
}
- toret.append(node);
- return toret.contents();
+ addParts(parts, parent);
},
append: function(params) {
if(params.tag !== 'span')
DocumentTextElement.prototype = new DocumentElement();
$.extend(DocumentTextElement.prototype, {
- toXML: function() {
- return this.getText();
+ toXML: function(parent) {
+ addParts(this.getText(), parent);
},
_setupDOMHandler: function(htmlElement) {
var $element = $(htmlElement);