Building js & css for editor with r.js/lessc via grunt tasks
[fnpeditor.git] / modules / documentCanvas / transformations.js
1 define(['libs/jquery-1.9.1.min'], function($) {\r
2 \r
3     'use strict';\r
4 \r
5     var transformations = {};\r
6 \r
7     transformations.fromXML = {\r
8         getHTMLTree: function(xml) {\r
9             var inner = $(xml).clone();\r
10             var toret = $('<div></div>');\r
11             toret.append(inner);\r
12             \r
13             var toBlock = ['div', 'section', 'header'];\r
14             var toInline = ['aside', 'span'];\r
15             \r
16             var transform = function(tags, replacingTagName) {\r
17                 tags.forEach(function(tagName) {\r
18                     tagName = tagName.toLowerCase();\r
19                     console.log('running ' + tagName);\r
20                     toret.find(tagName).replaceWith(function() {\r
21                         var currentTag = $(this);\r
22                         if(currentTag.attr('wlxml-tag'))\r
23                             return;\r
24                         var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);\r
25                         toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));\r
26                         for(var i = 0; i < this.attributes.length; i++) {\r
27                             var attr = this.attributes.item(i);\r
28                             var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;\r
29                             toret.attr('wlxml-' + attr.name, value)\r
30                         }\r
31                         toret.append(currentTag.contents());\r
32                         return toret;\r
33                     });\r
34                 });\r
35             }\r
36             \r
37             transform(toBlock, 'div');\r
38             transform(toInline, 'span');\r
39 \r
40             toret.find(":not(iframe)").addBack().contents().filter(function() {\r
41                 return this.nodeType == 3} ).each(function() {\r
42                     var n = $(this); \r
43                     var hasText = /\S/g.test(n.text());\r
44                     if(!hasText) {\r
45                         n.remove();\r
46                         return;\r
47                     }\r
48                     var startSpace = /\s/g.test(n.text().substr(0,1));\r
49                     var endSpace = /\s/g.test(n.text().substr(-1)) && n.text().length > 1;\r
50                     var trimmed = $.trim(n.text());\r
51                     n.get(0).data = (startSpace ? ' ' : '') + trimmed + (endSpace ? ' ' : '');\r
52             });\r
53             \r
54             return toret.children();\r
55         },\r
56         getMetaData: function(xml) {\r
57             var toret = {};\r
58             $(xml).find('metadata').children().each(function() {\r
59                 var node = $(this);\r
60                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();\r
61             })\r
62             return toret;\r
63         },\r
64         getDocumentDescription: function(xml) {\r
65             return {\r
66                 HTMLTree: this.getHTMLTree(xml),\r
67                 metadata: this.getMetaData(xml)\r
68             }\r
69         }\r
70     }\r
71 \r
72     transformations.toXML = {\r
73         getXML: function(body) {\r
74             \r
75             var inner = body.clone();\r
76             var toret = $('<div></div>');\r
77             toret.append(inner);\r
78             \r
79             toret.find('div, span').replaceWith(function() {\r
80                 var div = $(this);\r
81                 var tagName = div.attr('wlxml-tag');\r
82                 var toret = $('<'+tagName+'>');\r
83                                    \r
84                 for(var i = 0; i < this.attributes.length; i++) {\r
85                     var attr = this.attributes.item(i);\r
86                     var split = attr.name.split('-')\r
87                     console.log(split);\r
88                     if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) \r
89                         continue;\r
90                     var wlxmlName = split.splice(1).join('-');\r
91                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;\r
92                     console.log(name + ': ' + value);\r
93                     if(value.length && value.length > 0)\r
94                         toret.attr(wlxmlName, value);\r
95                 }\r
96                     \r
97                 toret.append(div.contents());\r
98                 return toret;\r
99             });\r
100 \r
101             return vkbeautify.xml(toret.html());\r
102         }\r
103     }\r
104 \r
105     return transformations;\r
106 \r
107 });