X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/6f88a272f930c30868c43b724368b4bcf6f0a3a7..db3139e6d432413b4bc06c68c5a75c3bc8e4c8dd:/src/smartxml/smartxml.js?ds=sidebyside diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d8b6bf0..e3c831d 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -6,11 +6,21 @@ define([ ], function($, _, Backbone, events) { 'use strict'; - +/* globals Node */ var TEXT_NODE = Node.TEXT_NODE; +var INSERTION = function(implementation) { + var toret = function(node) { + var insertion = this.getNodeInsertion(node); + implementation.call(this, insertion.ofNode.nativeNode); + this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}); + return insertion.ofNode; + }; + return toret; +}; + var DocumentNode = function(nativeNode, document) { if(!document) { throw new Error('undefined document for a node'); @@ -26,42 +36,142 @@ $.extend(DocumentNode.prototype, { this._$ = $(nativeNode); }, - detach: function() { this._$.detach(); }, + clone: function() { + return this.document.createDocumentNode(this._$.clone(true, true)[0]); + }, + + getPath: function(ancestor) { + var nodePath = [this].concat(this.parents()), + toret, idx; + ancestor = ancestor || this.document.root; + + nodePath.some(function(node, i) { + if(node.sameNode(ancestor)) { + idx = i; + return true; + } + }); + + if(idx !== 'undefined') { + nodePath = nodePath.slice(0, idx); + } + toret = nodePath.map(function(node) {return node.getIndex(); }); + toret.reverse(); + return toret; + }, + + isRoot: function() { + return this.document.root.sameNode(this); + }, + + detach: function() { + var parent = this.parent(); + this._$.detach(); + this.triggerChangeEvent('nodeDetached', {parent: parent}); + return this; + }, + + replaceWith: function(node) { + var toret; + if(this.isRoot()) { + return this.document.replaceRoot(node); + } + toret = this.after(node); + this.detach(); + return toret; + }, sameNode: function(otherNode) { - return otherNode && this.nativeNode === otherNode.nativeNode; + return !!(otherNode) && this.nativeNode === otherNode.nativeNode; }, parent: function() { - return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null; + var parentNode = this.nativeNode.parentNode; + if(parentNode && parentNode.nodeType === Node.ELEMENT_NODE) { + return this.document.createDocumentNode(parentNode); + } + return null; }, - after: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - this._$.after(node.nativeNode); - return node; + parents: function() { + var parent = this.parent(), + parents = parent ? parent.parents() : []; + if(parent) { + parents.unshift(parent); + } + return parents; }, - before: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); - this._$.before(node.nativeNode); - return node; + prev: function() { + var myIdx = this.getIndex(); + return myIdx > 0 ? this.parent().contents()[myIdx-1] : null; }, - wrapWith: function(node) { - node = node instanceof ElementNode ? node : this.document.createElementNode(node); + next: function() { + if(this.isRoot()) { + return null; + } + var myIdx = this.getIndex(), + parentContents = this.parent().contents(); + return myIdx < parentContents.length - 1 ? parentContents[myIdx+1] : null; + }, + + isSurroundedByTextElements: function() { + var prev = this.prev(), + next = this.next(); + return prev && (prev.nodeType === Node.TEXT_NODE) && next && (next.nodeType === Node.TEXT_NODE); + }, + + after: INSERTION(function(nativeNode) { + return this._$.after(nativeNode); + }), + before: INSERTION(function(nativeNode) { + return this._$.before(nativeNode); + }), + + wrapWith: function(node) { + var insertion = this.getNodeInsertion(node); if(this.parent()) { - this.before(node); + this.before(insertion.ofNode); + } + insertion.ofNode.append(this); + return insertion.ofNode; + }, + + /** + * Removes parent of a node if node has no siblings. + */ + unwrap: function() { + if(this.isRoot()) { + return; + } + var parent = this.parent(), + grandParent; + if(parent.contents().length === 1) { + grandParent = parent.parent(); + parent.unwrapContent(); + return grandParent; } - node.append(this); - return node; }, triggerChangeEvent: function(type, metaData) { var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {})); - this.document.trigger('change', event); + if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) { + this.document.trigger('change', event); + } }, + + getNodeInsertion: function(node) { + return this.document.getNodeInsertion(node); + }, + + getIndex: function() { + if(this.isRoot()) { + return 0; + } + return this.parent().indexOf(this); + } }); var ElementNode = function(nativeNode, document) { @@ -72,6 +182,16 @@ ElementNode.prototype = Object.create(DocumentNode.prototype); $.extend(ElementNode.prototype, { nodeType: Node.ELEMENT_NODE, + detach: function() { + var next; + if(this.parent() && this.isSurroundedByTextElements()) { + next = this.next(); + this.prev().appendText(next.getText()); + next.detach(); + } + return DocumentNode.prototype.detach.call(this); + }, + setData: function(key, value) { if(value !== undefined) { this._$.data(key, value); @@ -96,12 +216,7 @@ $.extend(ElementNode.prototype, { var toret = [], document = this.document; this._$.contents().each(function() { - if(this.nodeType === Node.ELEMENT_NODE) { - toret.push(document.createElementNode(this)); - } - else if(this.nodeType === Node.TEXT_NODE) { - toret.push(document.createTextNode(this)); - } + toret.push(document.createDocumentNode(this)); }); return toret; }, @@ -111,7 +226,7 @@ $.extend(ElementNode.prototype, { }, setTag: function(tagName) { - var node = this.document.createElementNode({tagName: tagName}), + var node = this.document.createDocumentNode({tagName: tagName}), oldTagName = this.getTagName(), myContents = this._$.contents(); @@ -149,9 +264,21 @@ $.extend(ElementNode.prototype, { return toret; }, - append: function(node) { - node = node instanceof DocumentNode ? node : this.document.createElementNode(node); - this._$.append(node.nativeNode); + append: INSERTION(function(nativeNode) { + this._$.append(nativeNode); + }), + + prepend: INSERTION(function(nativeNode) { + this._$.prepend(nativeNode); + }), + + insertAtIndex: function(nativeNode, index) { + var contents = this.contents(); + if(index < contents.length) { + return contents[index].before(nativeNode); + } else if(index === contents.length) { + return this.append(nativeNode); + } }, unwrapContent: function() { @@ -160,18 +287,20 @@ $.extend(ElementNode.prototype, { return; } - var parentContents = parent.contents(), - myContents = this.contents(), + var myContents = this.contents(), myIdx = parent.indexOf(this); + if(myContents.length === 0) { return this.detach(); } - var moveLeftRange, moveRightRange, leftMerged; + var prev = this.prev(), + next = this.next(), + moveLeftRange, moveRightRange, leftMerged; - if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) { - parentContents[myIdx-1].appendText(myContents[0].getText()); + if(prev && (prev.nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) { + prev.appendText(myContents[0].getText()); myContents[0].detach(); moveLeftRange = true; leftMerged = true; @@ -180,9 +309,10 @@ $.extend(ElementNode.prototype, { } if(!(leftMerged && myContents.length === 1)) { - if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) { - parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText()); - myContents[myContents.length-1].detach(); + var lastContents = _.last(myContents); + if(next && (next.nodeType === TEXT_NODE) && (lastContents.nodeType === TEXT_NODE)) { + next.prependText(lastContents.getText()); + lastContents.detach(); moveRightRange = true; } } @@ -200,6 +330,10 @@ $.extend(ElementNode.prototype, { }; }, + wrapText: function(params) { + return this.document._wrapText(_.extend({inside: this}, params)); + }, + toXML: function() { var wrapper = $('