5 ], function($, _, smartxml) {
11 var isMetaAttribute = function(attrName) {
12 return attrName.substr(0, 5) === 'meta-';
17 var AttributesList = function() {};
18 AttributesList.prototype = Object.create({});
19 AttributesList.prototype.keys = function() {
24 var WLXMLElementNode = function(nativeNode, document) {
25 smartxml.ElementNode.call(this, nativeNode, document);
27 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
29 $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
30 getClass: function() {
31 return this.getAttr('class') || '';
33 setClass: function(klass) {
34 return this.setAttr('class', klass);
36 getMetaAttributes: function() {
37 var toret = new AttributesList(),
38 classParts = [''].concat(this.getClass().split('.')),
39 classCurrent, classDesc;
41 classParts.forEach(function(part) {
42 classCurrent = classCurrent ? classCurrent + '.' + part : part;
43 classDesc = this.document.options.wlxmlClasses[classCurrent];
45 _.keys(classDesc.attrs).forEach(function(attrName) {
46 toret[attrName] = _.extend({value: this.getAttr('meta-' + attrName)}, classDesc.attrs[attrName]);
52 setMetaAttribute: function(key, value) {
53 this.setAttr('meta-'+key, value);
55 getOtherAttributes: function() {
57 this.getAttrs().forEach(function(attr) {
58 if(attr.name !== 'class' && !isMetaAttribute(attr.name)) {
59 toret[attr.name] = attr.value;
65 _getXMLDOMToDump: function() {
66 var DOM = this._$.clone(true, true);
68 DOM.find('*').addBack().each(function() {
71 contents = parent.contents(),
72 idx = contents.index(el),
78 if(data[formatter_prefix+ 'orig_before']) {
79 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
80 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
81 txt.data = data[formatter_prefix+ 'orig_before_original'];
83 el.before(data[formatter_prefix+ 'orig_before']);
86 if(data[formatter_prefix+ 'orig_after']) {
87 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
88 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
89 txt.data = data[formatter_prefix+ 'orig_after_original'];
91 el.after(data[formatter_prefix+ 'orig_after']);
94 if(data[formatter_prefix+ 'orig_begin']) {
95 el.prepend(data[formatter_prefix+ 'orig_begin']);
97 if(data[formatter_prefix+ 'orig_end']) {
98 contents = el.contents();
99 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
100 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
101 txt.data = data[formatter_prefix+ 'orig_end_original'];
103 el.append(data[formatter_prefix+ 'orig_end']);
116 var WLXMLDocument = function(xml, options) {
117 smartxml.Document.call(this, xml);
118 this.options = options;
121 var formatter_prefix = '_wlxml_formatter_';
123 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
124 $.extend(WLXMLDocument.prototype, {
125 ElementNodeFactory: WLXMLElementNode,
127 loadXML: function(xml) {
128 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
129 $(this.dom).find(':not(iframe)').addBack().contents()
130 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
133 text = {original: el.text(), trimmed: $.trim(el.text())},
134 elParent = el.parent(),
135 hasSpanParent = elParent.prop('tagName') === 'SPAN',
136 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
137 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
140 var addInfo = function(toAdd, where, transformed, original) {
141 var parentContents = elParent.contents(),
142 idx = parentContents.index(el[0]),
143 prev = idx > 0 ? parentContents[idx-1] : null,
144 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
147 if(where === 'above') {
148 target = prev ? $(prev) : elParent;
149 key = prev ? 'orig_after' : 'orig_begin';
150 } else if(where === 'below') {
151 target = next ? $(next) : elParent;
152 key = next ? 'orig_before' : 'orig_end';
153 } else { throw new Error();}
155 target.data(formatter_prefix + key, toAdd);
156 if(transformed !== undefined) {
157 target.data(formatter_prefix + key + '_transformed', transformed);
159 if(original !== undefined) {
160 target.data(formatter_prefix + key + '_original', original);
164 text.transformed = text.trimmed;
166 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
167 var startSpace = /\s/g.test(text.original.substr(0,1)),
168 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
169 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
170 text.transformed += text.trimmed;
171 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
173 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
174 text.transformed = ' ';
178 if(!text.transformed) {
179 addInfo(text.original, 'below');
181 return true; // continue
184 if(text.transformed !== text.original) {
185 // if(!text.trimmed) {
186 // addInfo(text.original, 'below');
188 var startingMatch = text.original.match(/^\s+/g),
189 endingMatch = text.original.match(/\s+$/g),
190 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
191 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
193 if(endingWhiteSpace) {
194 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
195 endingWhiteSpace = endingWhiteSpace.substr(1);
197 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
200 if(startingWhiteSpace && text.trimmed) {
201 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
202 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
204 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
209 el.replaceWith(document.createTextNode(text.transformed));
211 this.trigger('contentSet');
218 attrs: {uri: {type: 'string'}}
223 WLXMLDocumentFromXML: function(xml, options) {
224 options = _.extend({wlxmlClasses: wlxmlClasses}, options);
225 return new WLXMLDocument(xml, options);
228 WLXMLElementNodeFromXML: function(xml) {
229 return this.WLXMLDocumentFromXML(xml).root;