X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/73e8795a3d19b7221f34353799d39808d57a4d05..5cd41e637e9c0d4e1b5d2e03a68ad8ad3ed6778f:/src/smartxml/core.js?ds=sidebyside diff --git a/src/smartxml/core.js b/src/smartxml/core.js index dd472ce..867353d 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -1,10 +1,11 @@ -define([ - -], function() { +define(function(require) { 'use strict'; /* globals Node */ -var TEXT_NODE = Node.TEXT_NODE; + +var _ = require('libs/underscore'), + TEXT_NODE = Node.TEXT_NODE; + var INSERTION = function(implementation) { var toret = function(node) { @@ -98,6 +99,8 @@ var elementNodeTransformations = { if(this.sameNode(this.document.root)) { this.document._defineDocumentProperties(node._$); } + + /* TODO: This invalidates old references to this node. Caching instances on nodes would fix this. */ this._$.replaceWith(node._$); this._setNativeNode(node._$[0]); this._$.append(myContents); @@ -186,7 +189,6 @@ var elementNodeTransformations = { var textNodeTransformations = { setText: function(text) { - //console.log('smartxml: ' + text); this.nativeNode.data = text; this.triggerTextChangeEvent(); }, @@ -251,6 +253,27 @@ var textNodeTransformations = { }); return {first: parentElement, second: newElement}; + }, + + divideWithElementNode: function(node, params) { + var insertion = this.getNodeInsertion(node), + myText = this.getText(); + + if(params.offset === myText.length) { + return this.after(node); + } + if(params.offset === 0) { + return this.before(node); + } + + var lhsText = myText.substr(0, params.offset), + rhsText = myText.substr(params.offset), + rhsTextNode = this.document.createDocumentNode({text: rhsText}); + + this.setText(lhsText); + this.after(insertion.ofNode); + insertion.ofNode.after(rhsTextNode); + return insertion.ofNode; } }; @@ -347,7 +370,7 @@ var documentTransformations = { insertion.ofNode.triggerChangeEvent('nodeAdded'); return insertion.ofNode; } -} +}; return { document: {