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 getOtherAttributes: function() {
54 this.getAttrs().forEach(function(attr) {
55 if(attr.name !== 'class' && !isMetaAttribute(attr.name)) {
56 toret[attr.name] = attr.value;
62 _getXMLDOMToDump: function() {
63 var DOM = this._$.clone(true, true);
65 DOM.find('*').addBack().each(function() {
68 contents = parent.contents(),
69 idx = contents.index(el),
75 if(data[formatter_prefix+ 'orig_before']) {
76 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
77 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
78 txt.data = data[formatter_prefix+ 'orig_before_original'];
80 el.before(data[formatter_prefix+ 'orig_before']);
83 if(data[formatter_prefix+ 'orig_after']) {
84 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
85 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
86 txt.data = data[formatter_prefix+ 'orig_after_original'];
88 el.after(data[formatter_prefix+ 'orig_after']);
91 if(data[formatter_prefix+ 'orig_begin']) {
92 el.prepend(data[formatter_prefix+ 'orig_begin']);
94 if(data[formatter_prefix+ 'orig_end']) {
95 contents = el.contents();
96 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
97 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
98 txt.data = data[formatter_prefix+ 'orig_end_original'];
100 el.append(data[formatter_prefix+ 'orig_end']);
113 var WLXMLDocument = function(xml, options) {
114 smartxml.Document.call(this, xml);
115 this.options = options;
118 var formatter_prefix = '_wlxml_formatter_';
120 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
121 $.extend(WLXMLDocument.prototype, {
122 ElementNodeFactory: WLXMLElementNode,
124 loadXML: function(xml) {
125 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
126 $(this.dom).find(':not(iframe)').addBack().contents()
127 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
130 text = {original: el.text(), trimmed: $.trim(el.text())},
131 elParent = el.parent(),
132 hasSpanParent = elParent.prop('tagName') === 'SPAN',
133 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
134 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
137 var addInfo = function(toAdd, where, transformed, original) {
138 var parentContents = elParent.contents(),
139 idx = parentContents.index(el[0]),
140 prev = idx > 0 ? parentContents[idx-1] : null,
141 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
144 if(where === 'above') {
145 target = prev ? $(prev) : elParent;
146 key = prev ? 'orig_after' : 'orig_begin';
147 } else if(where === 'below') {
148 target = next ? $(next) : elParent;
149 key = next ? 'orig_before' : 'orig_end';
150 } else { throw new Error();}
152 target.data(formatter_prefix + key, toAdd);
153 if(transformed !== undefined) {
154 target.data(formatter_prefix + key + '_transformed', transformed);
156 if(original !== undefined) {
157 target.data(formatter_prefix + key + '_original', original);
161 text.transformed = text.trimmed;
163 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
164 var startSpace = /\s/g.test(text.original.substr(0,1)),
165 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
166 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
167 text.transformed += text.trimmed;
168 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
170 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
171 text.transformed = ' ';
175 if(!text.transformed) {
176 addInfo(text.original, 'below');
178 return true; // continue
181 if(text.transformed !== text.original) {
182 // if(!text.trimmed) {
183 // addInfo(text.original, 'below');
185 var startingMatch = text.original.match(/^\s+/g),
186 endingMatch = text.original.match(/\s+$/g),
187 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
188 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
190 if(endingWhiteSpace) {
191 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
192 endingWhiteSpace = endingWhiteSpace.substr(1);
194 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
197 if(startingWhiteSpace && text.trimmed) {
198 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
199 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
201 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
206 el.replaceWith(document.createTextNode(text.transformed));
208 this.trigger('contentSet');
215 attrs: {uri: {type: 'string'}}
220 WLXMLDocumentFromXML: function(xml, options) {
221 options = _.extend({wlxmlClasses: wlxmlClasses}, options);
222 return new WLXMLDocument(xml, options);
225 WLXMLElementNodeFromXML: function(xml) {
226 return this.WLXMLDocumentFromXML(xml).root;