}\r
\r
Canvas.prototype.splitNode = function(options) {\r
+ options = _.extend({textNodeIdx: 0}, options);\r
+ \r
var element = $(this.content.find('#' + options.node.id).get(0));\r
\r
var elementContents = element.contents();\r
- if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3)\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(0);\r
+ \r
+ var textElement = elementContents.get(options.textNodeIdx);\r
+\r
+ var succeedingNodes = [];\r
+ var passed = false;\r
+ elementContents.each(function() {\r
+ var node = this;\r
+ if(passed)\r
+ succeedingNodes.push(node);\r
+ if(node.isSameNode(textElement))\r
+ passed = true;\r
+ });\r
\r
var prefix = textElement.data.substr(0, options.offset);\r
var suffix = textElement.data.substr(options.offset);\r
- var prefixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class'));\r
- var suffixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class'));\r
- prefixNode.text(prefix);\r
- suffixNode.text(suffix);\r
- element.before(prefixNode);\r
- element.after(suffixNode);\r
- element.remove();\r
+ \r
+ var $textElement = $(textElement);\r
+ $textElement.before(prefix);\r
+ $textElement.remove();\r
+ var newNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class'));\r
+ newNode.append(suffix);\r
+ succeedingNodes.forEach(function(node) {\r
+ newNode.append(node)\r
+ });\r
+ element.after(newNode);\r
}\r
\r
Canvas.prototype.createList = function(options) {\r
);\r
});\r
\r
+ test('split node with subnodes', function() {\r
+ var c = new canvas.Canvas('<section><header class="some.class">Fancy and nice <span>header</span> 1</header></section>');\r
+ var header = c.getNode({tag: 'header'})[0];\r
+ c.splitNode({node: header, textNodeIdx: 0, offset: 5});\r
+ assert.xmlEqual(c.toXML(), '\\r
+ <section> \\r
+ <header class="some.class">Fancy</header>\\r
+ <header class="some.class"> and nice <span>header</span> 1</header>\\r
+ </section>'\r
+ );\r
+ });\r
+ \r
test('list', function() {\r
var c = new canvas.Canvas('<section><div>Alice</div>has<div>a cat</div></section>');\r
var div1 = c.getNode({tag:'div'})[0];\r