2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function($, _, documentElement) {
9 var Canvas = function(xml) {
10 xml = $.parseXML(xml);
11 this.d = xml !== null ? $(xml.childNodes[0]) : null;
14 $.extend(Canvas.prototype, {
19 return documentElement.wrap(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
22 wrapText: function(params) {
23 params = _.extend({textNodeIdx: 0}, params);
24 if(typeof params.textNodeIdx === 'number')
25 params.textNodeIdx = [params.textNodeIdx];
27 var childrenInside = params.inside.children(),
28 idx1 = Math.min.apply(Math, params.textNodeIdx),
29 idx2 = Math.max.apply(Math, params.textNodeIdx),
30 textNode1 = childrenInside[idx1],
31 textNode2 = childrenInside[idx2],
32 sameNode = textNode1.sameNode(textNode2),
33 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
34 prefixInside = textNode1.getText().substr(params.offsetStart),
35 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
36 suffixOutside = textNode2.getText().substr(params.offsetEnd)
39 var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
40 textNode1.after(wrapperElement);
43 if(prefixOutside.length > 0)
44 wrapperElement.before({text:prefixOutside});
46 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
47 wrapperElement.append({text: core});
50 if(prefixInside.length > 0)
51 wrapperElement.append({text: prefixInside});
52 for(var i = idx1 + 1; i < idx2; i++) {
53 wrapperElement.append(childrenInside[i]);
55 if(suffixInside.length > 0)
56 wrapperElement.append({text: suffixInside});
58 if(suffixOutside.length > 0)
59 wrapperElement.after({text: suffixOutside});
60 return wrapperElement;
66 fromXML: function(xml) {
67 return new Canvas(xml);