5 'smartxml/transformations'
6 ], function($, _, smartxml, transformations) {
11 var metadataKey = 'wlxml.metadata';
14 var AttributesList = function() {};
15 AttributesList.prototype = Object.create({});
16 AttributesList.prototype.keys = function() {
20 var installObject = function(instance, klass) {
21 var methods = instance.document.classMethods[klass] || {},
22 transformations = instance.document.classTransformations[klass] || {};
24 instance.object = Object.create(_.extend({}, methods, transformations));
25 _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
26 instance.object[key] = _.bind(instance.object[key], instance);
30 var WLXMLElementNode = function(nativeNode, document) {
31 smartxml.ElementNode.call(this, nativeNode, document);
32 installObject(this, this.getClass());
34 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
36 $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
37 getClass: function() {
38 return this.getAttr('class') || '';
40 setClass: function(klass) {
41 if(klass !== this.klass) {
42 installObject(this, klass);
43 return this.setAttr('class', klass);
47 return this.getClass().substr(0, klass.length) === klass;
49 getMetaAttributes: function() {
50 var toret = new AttributesList(),
51 classParts = [''].concat(this.getClass().split('.')),
52 classCurrent, classDesc;
54 classParts.forEach(function(part) {
55 classCurrent = classCurrent ? classCurrent + '.' + part : part;
56 classDesc = this.document.options.wlxmlClasses[classCurrent];
58 _.keys(classDesc.attrs).forEach(function(attrName) {
59 toret[attrName] = _.extend({value: this.getAttr(attrName)}, classDesc.attrs[attrName]);
65 setMetaAttribute: function(key, value) {
66 this.setAttr(key, value);
68 getOtherAttributes: function() {
71 this.getAttrs().forEach(function(attr) {
72 if(attr.name !== 'class' && !node.isMetaAttribute(attr.name)) {
73 toret[attr.name] = {value: attr.value};
78 isMetaAttribute: function(attrName) {
79 return attrName !== 'class' &&_.contains(_.keys(this.getMetaAttributes()), attrName);
82 getMetadata: function() {
83 return this.getData(metadataKey) || [];
86 addMetadataRow: function(row) {
87 this.setMetadataRow(null, row);
90 setMetadataRow: function(index, row) {
91 var metadata = this.getData(metadataKey) || [];
92 if(typeof index !== 'number' || index > metadata.length - 1) {
94 index = metadata.length - 1;
96 metadata[index] = _.extend(metadata[index], row);
98 this.setData(metadataKey, metadata);
99 this.triggerChangeEvent('metadataChange', {index: index});
102 _getXMLDOMToDump: function() {
103 var DOM = this._$.clone(true, true),
106 DOM.find('*').addBack().each(function() {
108 parent = el.parent(),
109 contents = parent.contents(),
110 idx = contents.index(el),
114 var txt, documentNode, metaNode;
116 if(data[formatter_prefix+ 'orig_before']) {
117 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
118 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
119 txt.data = data[formatter_prefix+ 'orig_before_original'];
121 el.before(data[formatter_prefix+ 'orig_before']);
124 if(data[formatter_prefix+ 'orig_after']) {
125 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
126 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
127 txt.data = data[formatter_prefix+ 'orig_after_original'];
129 el.after(data[formatter_prefix+ 'orig_after']);
132 if(data[formatter_prefix+ 'orig_begin']) {
133 el.prepend(data[formatter_prefix+ 'orig_begin']);
135 if(data[formatter_prefix+ 'orig_end']) {
136 contents = el.contents();
137 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
138 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
139 txt.data = data[formatter_prefix+ 'orig_end_original'];
141 el.append(data[formatter_prefix+ 'orig_end']);
146 if(this.nodeType === Node.ELEMENT_NODE) {
147 documentNode = doc.createDocumentNode(this);
148 metaNode = $('<metadata>');
149 documentNode.getMetadata().forEach(function(row) {
150 metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
152 if(metaNode.children().length) {
153 $(this).prepend(metaNode);
165 // WLXMLElementNode.prototype.transformations.register(transformations.createContextTransformation({
166 // name: 'wlxml.setMetaAttribute',
167 // impl: function(args) {
168 // this.setMetaAttribute(args.name, args.value);
170 // getChangeRoot: function() {
171 // return this.context;
177 var WLXMLDocumentNode = function() {
178 smartxml.DocumentNode.apply(this, arguments);
180 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
182 var WLXMLDocument = function(xml, options) {
183 this.classMethods = {};
184 this.classTransformations = {};
185 smartxml.Document.call(this, xml);
186 this.options = options;
189 var formatter_prefix = '_wlxml_formatter_';
192 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
193 $.extend(WLXMLDocument.prototype, {
194 ElementNodeFactory: WLXMLElementNode,
195 loadXML: function(xml) {
196 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
197 this.trigger('contentSet');
200 normalizeXML: function(nativeNode) {
202 prefixLength = 'dc:'.length;
204 $(nativeNode).find(':not(iframe)').addBack().contents()
205 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
208 text = {original: el.text(), trimmed: $.trim(el.text())},
209 elParent = el.parent(),
210 hasSpanParent = elParent.prop('tagName') === 'SPAN',
211 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
212 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
215 var addInfo = function(toAdd, where, transformed, original) {
216 var parentContents = elParent.contents(),
217 idx = parentContents.index(el[0]),
218 prev = idx > 0 ? parentContents[idx-1] : null,
219 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
222 if(where === 'above') {
223 target = prev ? $(prev) : elParent;
224 key = prev ? 'orig_after' : 'orig_begin';
225 } else if(where === 'below') {
226 target = next ? $(next) : elParent;
227 key = next ? 'orig_before' : 'orig_end';
228 } else { throw new Error();}
230 target.data(formatter_prefix + key, toAdd);
231 if(transformed !== undefined) {
232 target.data(formatter_prefix + key + '_transformed', transformed);
234 if(original !== undefined) {
235 target.data(formatter_prefix + key + '_original', original);
239 text.transformed = text.trimmed;
241 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
242 var startSpace = /\s/g.test(text.original.substr(0,1)),
243 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
244 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
245 text.transformed += text.trimmed;
246 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
248 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
249 text.transformed = ' ';
253 if(!text.transformed) {
254 addInfo(text.original, 'below');
256 return true; // continue
259 if(text.transformed !== text.original) {
260 // if(!text.trimmed) {
261 // addInfo(text.original, 'below');
263 var startingMatch = text.original.match(/^\s+/g),
264 endingMatch = text.original.match(/\s+$/g),
265 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
266 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
268 if(endingWhiteSpace) {
269 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
270 endingWhiteSpace = endingWhiteSpace.substr(1);
272 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
275 if(startingWhiteSpace && text.trimmed) {
276 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
277 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
279 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
283 /* globals document */
284 el.replaceWith(document.createTextNode(text.transformed));
287 $(nativeNode).find('metadata').each(function() {
288 var metadataNode = $(this),
289 owner = doc.createDocumentNode(metadataNode.parent()[0]);
291 metadataNode.children().each(function() {
292 owner.addMetadataRow({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()});
294 metadataNode.remove();
298 registerClassTransformation: function(Transformation, className) {
299 var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
300 thisClassTransformations[Transformation.prototype.name] = function(args) {
301 var nodeInstance = this;
302 return nodeInstance.transform(Transformation, args);
306 registerClassMethod: function(methodName, method, className) {
307 var thisClassMethods = (this.classMethods[className] = this.classMethods[className] || {});
308 thisClassMethods[methodName] = method;
311 registerExtension: function(extension) {
313 smartxml.Document.prototype.registerExtension.call(this, extension);
316 _.pairs(extension.wlxmlClass).forEach(function(pair) {
317 var className = pair[0],
318 classExtension = pair[1];
320 _.pairs(classExtension.methods || {}).forEach(function(pair) {
323 doc.registerClassMethod(name, method, className);
326 _.pairs(classExtension.transformations || {}).forEach(function(pair) {
329 doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
339 attrs: {href: {type: 'string'}}
345 WLXMLDocumentFromXML: function(xml, options) {
346 options = _.extend({wlxmlClasses: wlxmlClasses}, options);
347 return new WLXMLDocument(xml, options);
350 WLXMLElementNodeFromXML: function(xml) {
351 return this.WLXMLDocumentFromXML(xml).root;