element.after(options.node.dom);\r
};\r
\r
-Canvas.prototype.nodeWrap2 = function(options) {\r
+Canvas.prototype.nodeWrap = function(options) {\r
+ options = _.extend({textNodeIdx: 0}, options);\r
+ if(typeof options.textNodeIdx === 'number')\r
+ options.textNodeIdx = [options.textNodeIdx];\r
var container = $(this.content.find('#' + options.inside.getId()).get(0));\r
\r
var containerContent = container.contents();\r
var idx2 = Math.max.apply(Math, options.textNodeIdx);\r
var textNode1 = $(containerContent.get(idx1));\r
var textNode2 = $(containerContent.get(idx2));\r
- \r
+ var sameNode = textNode1.get(0) === textNode2.get(0);\r
textNode1.after(options._with.dom);\r
textNode1.detach();\r
- textNode2.detach();\r
- \r
- \r
- options._with.dom.before(textNode1.text().substr(0, options.offsetStart));\r
- options._with.dom.append(textNode1.text().substr(options.offsetStart));\r
- for(var i = idx1 + 1; i < idx2; i++) {\r
- options._with.dom.append(containerContent[i]);\r
+ if(!sameNode)\r
+ textNode2.detach();\r
+ \r
+ var prefixOutside = textNode1.text().substr(0, options.offsetStart);\r
+ var prefixInside = textNode1.text().substr(options.offsetStart)\r
+ var suffixInside = textNode2.text().substr(0, options.offsetEnd)\r
+ var suffixOutside = textNode2.text().substr(options.offsetEnd);\r
+ var core;\r
+ if(sameNode)\r
+ core = textNode1.text().substr(options.offsetStart, options.offsetEnd - options.offsetStart);\r
+ \r
+ options._with.dom.before(prefixOutside);\r
+ if(sameNode) {\r
+ options._with.setContent(core);\r
+ } else {\r
+ options._with.dom.append(prefixInside);\r
+ for(var i = idx1 + 1; i < idx2; i++) {\r
+ options._with.dom.append(containerContent[i]);\r
+ }\r
+ options._with.dom.append(suffixInside);\r
}\r
- options._with.dom.append(textNode2.text().substr(0, options.offsetEnd));\r
- options._with.dom.after(textNode2.text().substr(options.offsetEnd));\r
-};\r
-\r
-Canvas.prototype.nodeWrap = function(options) {\r
- options = _.extend({textNodeIdx: 0}, options);\r
- \r
- if(options.textNodeIdx instanceof Array)\r
- return this.nodeWrap2(options);\r
-\r
- var element = $(this.content.find('#' + options.inside.getId()).get(0));\r
- \r
- var elementContents = element.contents();\r
- if(elementContents.length === 0 || \r
- elementContents.length - 1 < options.textNodeIdx || \r
- elementContents.get(options.textNodeIdx).nodeType != 3)\r
- return false;\r
- var textElement = elementContents.get(options.textNodeIdx);\r
-\r
- var prefix = textElement.data.substr(0, options.offsetStart);\r
- var suffix = textElement.data.substr(options.offsetEnd);\r
- var core = textElement.data.substr(options.offsetStart, options.offsetEnd - options.offsetStart);\r
- options._with.setContent(core);\r
-\r
- $(textElement).replaceWith(options._with.dom);\r
- options._with.dom.before(prefix);\r
- options._with.dom.after(suffix);\r
+ options._with.dom.after(suffixOutside);\r
};\r
\r
Canvas.prototype.nodeSplit = function(options) {\r