canvas api wip: splitting nodes with subnodes
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 20 Jun 2013 09:02:52 +0000 (11:02 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 20 Jun 2013 09:02:52 +0000 (11:02 +0200)
modules/documentCanvas/canvas.js
modules/documentCanvas/tests/canvas.test.js

index 2d364ac..44f88a9 100644 (file)
@@ -68,22 +68,40 @@ Canvas.prototype.insertNode = function(options) {
 }\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
index 386e375..a948787 100644 (file)
@@ -81,6 +81,18 @@ define([
             );\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