5 'smartxml/transformations'
6 ], function($, _, smartxml, transformations) {
12 var isMetaAttribute = function(attrName) {
13 return attrName.substr(0, 5) === 'meta-';
18 var AttributesList = function() {};
19 AttributesList.prototype = Object.create({});
20 AttributesList.prototype.keys = function() {
25 var WLXMLElementNode = function(nativeNode, document) {
26 smartxml.ElementNode.call(this, nativeNode, document);
28 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
30 $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
31 getClass: function() {
32 return this.getAttr('class') || '';
34 setClass: function(klass) {
35 return this.setAttr('class', klass);
37 getMetaAttributes: function() {
38 var toret = new AttributesList(),
39 classParts = [''].concat(this.getClass().split('.')),
40 classCurrent, classDesc;
42 classParts.forEach(function(part) {
43 classCurrent = classCurrent ? classCurrent + '.' + part : part;
44 classDesc = this.document.options.wlxmlClasses[classCurrent];
46 _.keys(classDesc.attrs).forEach(function(attrName) {
47 toret[attrName] = _.extend({value: this.getAttr('meta-' + attrName)}, classDesc.attrs[attrName]);
53 setMetaAttribute: function(key, value) {
54 this.setAttr('meta-'+key, value);
56 getOtherAttributes: function() {
58 this.getAttrs().forEach(function(attr) {
59 if(attr.name !== 'class' && !isMetaAttribute(attr.name)) {
60 toret[attr.name] = attr.value;
66 _getXMLDOMToDump: function() {
67 var DOM = this._$.clone(true, true);
69 DOM.find('*').addBack().each(function() {
72 contents = parent.contents(),
73 idx = contents.index(el),
79 if(data[formatter_prefix+ 'orig_before']) {
80 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
81 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
82 txt.data = data[formatter_prefix+ 'orig_before_original'];
84 el.before(data[formatter_prefix+ 'orig_before']);
87 if(data[formatter_prefix+ 'orig_after']) {
88 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
89 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
90 txt.data = data[formatter_prefix+ 'orig_after_original'];
92 el.after(data[formatter_prefix+ 'orig_after']);
95 if(data[formatter_prefix+ 'orig_begin']) {
96 el.prepend(data[formatter_prefix+ 'orig_begin']);
98 if(data[formatter_prefix+ 'orig_end']) {
99 contents = el.contents();
100 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
101 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
102 txt.data = data[formatter_prefix+ 'orig_end_original'];
104 el.append(data[formatter_prefix+ 'orig_end']);
113 WLXMLElementNode.prototype.transformations.register(transformations.createContextTransformation({
114 name: 'wlxml.setMetaAttribute',
115 impl: function(args) {
116 this.setMetaAttribute(args.name, args.value);
118 getChangeRoot: function() {
125 var WLXMLDocument = function(xml, options) {
126 smartxml.Document.call(this, xml);
127 this.options = options;
130 var formatter_prefix = '_wlxml_formatter_';
132 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
133 $.extend(WLXMLDocument.prototype, {
134 ElementNodeFactory: WLXMLElementNode,
136 loadXML: function(xml) {
137 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
138 $(this.dom).find(':not(iframe)').addBack().contents()
139 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
142 text = {original: el.text(), trimmed: $.trim(el.text())},
143 elParent = el.parent(),
144 hasSpanParent = elParent.prop('tagName') === 'SPAN',
145 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
146 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
149 var addInfo = function(toAdd, where, transformed, original) {
150 var parentContents = elParent.contents(),
151 idx = parentContents.index(el[0]),
152 prev = idx > 0 ? parentContents[idx-1] : null,
153 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
156 if(where === 'above') {
157 target = prev ? $(prev) : elParent;
158 key = prev ? 'orig_after' : 'orig_begin';
159 } else if(where === 'below') {
160 target = next ? $(next) : elParent;
161 key = next ? 'orig_before' : 'orig_end';
162 } else { throw new Error();}
164 target.data(formatter_prefix + key, toAdd);
165 if(transformed !== undefined) {
166 target.data(formatter_prefix + key + '_transformed', transformed);
168 if(original !== undefined) {
169 target.data(formatter_prefix + key + '_original', original);
173 text.transformed = text.trimmed;
175 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
176 var startSpace = /\s/g.test(text.original.substr(0,1)),
177 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
178 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
179 text.transformed += text.trimmed;
180 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
182 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
183 text.transformed = ' ';
187 if(!text.transformed) {
188 addInfo(text.original, 'below');
190 return true; // continue
193 if(text.transformed !== text.original) {
194 // if(!text.trimmed) {
195 // addInfo(text.original, 'below');
197 var startingMatch = text.original.match(/^\s+/g),
198 endingMatch = text.original.match(/\s+$/g),
199 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
200 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
202 if(endingWhiteSpace) {
203 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
204 endingWhiteSpace = endingWhiteSpace.substr(1);
206 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
209 if(startingWhiteSpace && text.trimmed) {
210 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
211 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
213 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
218 el.replaceWith(document.createTextNode(text.transformed));
220 this.trigger('contentSet');
227 attrs: {uri: {type: 'string'}}
232 WLXMLDocumentFromXML: function(xml, options) {
233 options = _.extend({wlxmlClasses: wlxmlClasses}, options);
234 return new WLXMLDocument(xml, options);
237 WLXMLElementNodeFromXML: function(xml) {
238 return this.WLXMLDocumentFromXML(xml).root;