Fix
[fnpeditor.git] / modules / documentCanvas / transformations.js
index cd20d2b..2b5a5eb 100644 (file)
@@ -1,5 +1,7 @@
 define(['libs/jquery-1.9.1.min'], function($) {\r
 \r
+    'use strict';\r
+\r
     var transformations = {};\r
 \r
     transformations.fromXML = {\r
@@ -35,6 +37,20 @@ define(['libs/jquery-1.9.1.min'], function($) {
             transform(toBlock, 'div');\r
             transform(toInline, 'span');\r
 \r
+            toret.find(":not(iframe)").addBack().contents().filter(function() {\r
+                return this.nodeType == 3} ).each(function() {\r
+                    var n = $(this); \r
+                    var hasText = /\S/g.test(n.text());\r
+                    if(!hasText) {\r
+                        n.remove();\r
+                        return;\r
+                    }\r
+                    var startSpace = /\s/g.test(n.text().substr(0,1));\r
+                    var endSpace = /\s/g.test(n.text().substr(-1)) && n.text().length > 1;\r
+                    var trimmed = $.trim(n.text());\r
+                    n.get(0).data = (startSpace ? ' ' : '') + trimmed + (endSpace ? ' ' : '');\r
+            });\r
+            \r
             return toret.children();\r
         },\r
         getMetaData: function(xml) {\r
@@ -54,9 +70,9 @@ define(['libs/jquery-1.9.1.min'], function($) {
     }\r
 \r
     transformations.toXML = {\r
-        getXML: function(documentDescription) {\r
+        getXML: function(body) {\r
             \r
-            var inner = $(documentDescription.HTMLTree);\r
+            var inner = body.clone();\r
             var toret = $('<div></div>');\r
             toret.append(inner);\r
             \r
@@ -74,31 +90,14 @@ define(['libs/jquery-1.9.1.min'], function($) {
                     var wlxmlName = split.splice(1).join('-');\r
                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;\r
                     console.log(name + ': ' + value);\r
-                    toret.attr(wlxmlName, value);\r
+                    if(value.length && value.length > 0)\r
+                        toret.attr(wlxmlName, value);\r
                 }\r
                     \r
                 toret.append(div.contents());\r
                 return toret;\r
             });\r
-            \r
-            var meta = $('<metadata></metadata>\n');\r
-            _.each(_.keys(documentDescription.metadata), function(key) {\r
-                meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');\r
-            });\r
-            meta.append('\n');\r
-            \r
-            var metadata = toret.find('metadata');\r
-            if(metadata.length === 0) {\r
-                var section = toret.find('section');\r
-                section = section.length ? $(section[0]) : null;\r
-                if(section) {\r
-                    section.prepend(meta)\r
-                }\r
-            } else {\r
-                metadata.replaceWith(meta);\r
-            }\r
-            \r
-            \r
+\r
             return vkbeautify.xml(toret.html());\r
         }\r
     }\r