Fixing wrapping text when text not in first node of its parent
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 10:54:05 +0000 (12:54 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 10:54:05 +0000 (12:54 +0200)
modules/documentCanvas/canvas.js
modules/documentCanvas/canvasManager.js
modules/documentCanvas/tests/canvas.test.js

index 7d75e9e..cafed1a 100644 (file)
@@ -62,12 +62,16 @@ Canvas.prototype.nodeInsertAfter = function(options) {
 };\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
index 5edd4be..aa37ff2 100644 (file)
@@ -110,10 +110,12 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {
             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
index d336192..5e44dd9 100644 (file)
@@ -61,6 +61,14 @@ define([
             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