From 4c81ad4075b0e8e67f984cce3494f9853179a9d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 20 Jun 2013 11:02:52 +0200 Subject: [PATCH 1/1] canvas api wip: splitting nodes with subnodes --- modules/documentCanvas/canvas.js | 36 +++++++++++++++------ modules/documentCanvas/tests/canvas.test.js | 12 +++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 2d364ac..44f88a9 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -68,22 +68,40 @@ Canvas.prototype.insertNode = function(options) { } Canvas.prototype.splitNode = function(options) { + options = _.extend({textNodeIdx: 0}, options); + var element = $(this.content.find('#' + options.node.id).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 succeedingNodes = []; + var passed = false; + elementContents.each(function() { + var node = this; + if(passed) + succeedingNodes.push(node); + if(node.isSameNode(textElement)) + passed = true; + }); var prefix = textElement.data.substr(0, options.offset); var suffix = textElement.data.substr(options.offset); - var prefixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class')); - var suffixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class')); - prefixNode.text(prefix); - suffixNode.text(suffix); - element.before(prefixNode); - element.after(suffixNode); - element.remove(); + + var $textElement = $(textElement); + $textElement.before(prefix); + $textElement.remove(); + var newNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class')); + newNode.append(suffix); + succeedingNodes.forEach(function(node) { + newNode.append(node) + }); + element.after(newNode); } Canvas.prototype.createList = function(options) { diff --git a/modules/documentCanvas/tests/canvas.test.js b/modules/documentCanvas/tests/canvas.test.js index 386e375..a948787 100644 --- a/modules/documentCanvas/tests/canvas.test.js +++ b/modules/documentCanvas/tests/canvas.test.js @@ -81,6 +81,18 @@ define([ ); }); + test('split node with subnodes', function() { + var c = new canvas.Canvas('
Fancy and nice header 1
'); + var header = c.getNode({tag: 'header'})[0]; + c.splitNode({node: header, textNodeIdx: 0, offset: 5}); + assert.xmlEqual(c.toXML(), '\ +
\ +
Fancy
\ +
and nice header 1
\ +
' + ); + }); + test('list', function() { var c = new canvas.Canvas('
Alice
has
a cat
'); var div1 = c.getNode({tag:'div'})[0]; -- 2.20.1