From ee83d407018193482c72fdc57a8cfea17d38127a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 28 Jun 2013 12:54:05 +0200 Subject: [PATCH] Fixing wrapping text when text not in first node of its parent --- modules/documentCanvas/canvas.js | 8 ++++++-- modules/documentCanvas/canvasManager.js | 6 ++++-- modules/documentCanvas/tests/canvas.test.js | 8 ++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 7d75e9e..cafed1a 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -62,12 +62,16 @@ Canvas.prototype.nodeInsertAfter = function(options) { }; Canvas.prototype.nodeWrap = function(options) { + options = _.extend({textNodeIdx: 0}, options); + var element = $(this.content.find('#' + options.inside.getId()).get(0)); var elementContents = element.contents(); - if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) + if(elementContents.length === 0 || + elementContents.length - 1 < options.textNodeIdx || + elementContents.get(options.textNodeIdx).nodeType != 3) return false; - var textElement = elementContents.get(0); + var textElement = elementContents.get(options.textNodeIdx); var prefix = textElement.data.substr(0, options.offsetStart); var suffix = textElement.data.substr(options.offsetEnd); diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js index 5edd4be..aa37ff2 100644 --- a/modules/documentCanvas/canvasManager.js +++ b/modules/documentCanvas/canvasManager.js @@ -110,10 +110,12 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) { offsetEnd = tmp; } var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass}); - this.canvas.nodeWrap({inside: canvasNode.create($(selection.anchorNode).parent()), + var parent = $(selection.anchorNode).parent(); + this.canvas.nodeWrap({inside: canvasNode.create(parent), _with: wrapper, offsetStart: offsetStart, - offsetEnd: offsetEnd + offsetEnd: offsetEnd, + textNodeIdx: parent.contents().index($(selection.anchorNode)) }); this.selectNode(wrapper, {movecaret: 'end'}); } diff --git a/modules/documentCanvas/tests/canvas.test.js b/modules/documentCanvas/tests/canvas.test.js index d336192..5e44dd9 100644 --- a/modules/documentCanvas/tests/canvas.test.js +++ b/modules/documentCanvas/tests/canvas.test.js @@ -61,6 +61,14 @@ define([ assertDomEqual(c.getContent(), '
Header 1
'); }); + test('wrap text in node - text not a first node', function() { + var c = canvas.create('
Alice has a cat
'); + var header = c.findNodes({tag: 'header'})[0]; + var wrapper = canvasNode.create({tag: 'aside'}); + c.nodeWrap({inside: header, _with: wrapper, offsetStart: 1, offsetEnd: 4, textNodeIdx: 2}); + assertDomEqual(c.getContent(), '
Alice has a cat
'); + }); + test('split node', function() { var c = canvas.create('
Header 1
'); var header = c.findNodes({tag: 'header'})[0]; -- 2.20.1