X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/921b131224a4b6210b8631c5ab7926304bcf3069..db3139e6d432413b4bc06c68c5a75c3bc8e4c8dd:/src/smartxml/smartxml.js?ds=inline diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 248333e..e3c831d 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -1,52 +1,213 @@ define([ - 'libs/jquery' -], function($) { + 'libs/jquery', + 'libs/underscore', + 'libs/backbone', + 'smartxml/events' +], 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'); } this.document = document; - this.nativeNode = nativeNode; - this._$ = $(nativeNode); + this._setNativeNode(nativeNode); + }; $.extend(DocumentNode.prototype, { - detach: function() { this._$.detach(); }, + _setNativeNode: function(nativeNode) { + this.nativeNode = nativeNode; + this._$ = $(nativeNode); + }, + + 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 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; + }, + + parents: function() { + var parent = this.parent(), + parents = parent ? parent.parents() : []; + if(parent) { + parents.unshift(parent); + } + return parents; }, - before: function(node) { - this._$.before(node.nativeNode); + prev: function() { + var myIdx = this.getIndex(); + return myIdx > 0 ? this.parent().contents()[myIdx-1] : null; }, + 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); } - node.append(this); + 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; + } + }, + + triggerChangeEvent: function(type, metaData) { + var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {})); + 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) { DocumentNode.call(this, nativeNode, document); }; +ElementNode.prototype = Object.create(DocumentNode.prototype); -$.extend(ElementNode.prototype, 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); + } else { + this._$.removeData(_.keys(this._$.data())); + this._$.data(key); + } + }, + + getData: function(key) { + if(key) { + return this._$.data(key); + } + return this._$.data(); + }, + getTagName: function() { return this.nativeNode.tagName.toLowerCase(); }, @@ -55,12 +216,7 @@ $.extend(ElementNode.prototype, DocumentNode.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; }, @@ -69,12 +225,35 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { return this._$.contents().index(node._$); }, + setTag: function(tagName) { + var node = this.document.createDocumentNode({tagName: tagName}), + oldTagName = this.getTagName(), + myContents = this._$.contents(); + + this.getAttrs().forEach(function(attribute) { + node.setAttr(attribute.name, attribute.value, true); + }); + node.setData(this.getData()); + + if(this.sameNode(this.document.root)) { + defineDocumentProperties(this.document, node._$); + } + this._$.replaceWith(node._$); + this._setNativeNode(node._$[0]); + this._$.append(myContents); + this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()}); + }, + getAttr: function(name) { return this._$.attr(name); }, - setAttr: function(name, value) { + setAttr: function(name, value, silent) { + var oldVal = this.getAttr(name); this._$.attr(name, value); + if(!silent) { + this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value}); + } }, getAttrs: function() { @@ -85,8 +264,21 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { return toret; }, - append: function(documentNode) { - this._$.append(documentNode.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() { @@ -95,18 +287,20 @@ $.extend(ElementNode.prototype, DocumentNode.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; @@ -115,9 +309,10 @@ $.extend(ElementNode.prototype, DocumentNode.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; } } @@ -135,67 +330,302 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { }; }, + wrapText: function(params) { + return this.document._wrapText(_.extend({inside: this}, params)); + }, + toXML: function() { var wrapper = $('