};\r
\r
Canvas.prototype.nodeWrap = function(options) {\r
+ options = _.extend({textNodeIdx: 0}, options);\r
+\r
var element = $(this.content.find('#' + options.inside.getId()).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
+ 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
offsetEnd = tmp;\r
}\r
var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
- this.canvas.nodeWrap({inside: canvasNode.create($(selection.anchorNode).parent()),\r
+ var parent = $(selection.anchorNode).parent();\r
+ this.canvas.nodeWrap({inside: canvasNode.create(parent),\r
_with: wrapper,\r
offsetStart: offsetStart,\r
- offsetEnd: offsetEnd\r
+ offsetEnd: offsetEnd,\r
+ textNodeIdx: parent.contents().index($(selection.anchorNode))\r
});\r
this.selectNode(wrapper, {movecaret: 'end'});\r
}\r
assertDomEqual(c.getContent(), '<div wlxml-tag="section"><div wlxml-tag="header">H<span wlxml-tag="aside">eader</span> 1</div></div>');\r
});\r
\r
+ test('wrap text in node - text not a first node', function() {\r
+ var c = canvas.create('<div wlxml-tag="header">Alice <span wlxml-tag="span">has a</span> cat</div>');\r
+ var header = c.findNodes({tag: 'header'})[0];\r
+ var wrapper = canvasNode.create({tag: 'aside'});\r
+ c.nodeWrap({inside: header, _with: wrapper, offsetStart: 1, offsetEnd: 4, textNodeIdx: 2});\r
+ assertDomEqual(c.getContent(), '<div wlxml-tag="header">Alice <span wlxml-tag="span">has a</span> <span wlxml-tag="aside">cat</span></div>');\r
+ });\r
+ \r
test('split node', function() {\r
var c = canvas.create('<div wlxml-tag="section"><div wlxml-tag="header">Header 1</div></div>');\r
var header = c.findNodes({tag: 'header'})[0];\r