5 'smartxml/transformations',
6 'wlxml/extensions/metadata/metadata'
7 ], function($, _, smartxml, transformations, metadataExtension) {
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 _getXMLDOMToDump: function() {
83 var DOM = this._$.clone(true, true),
86 DOM.find('*').addBack().each(function() {
89 contents = parent.contents(),
90 idx = contents.index(el),
94 var txt, documentNode, metaNode;
96 if(data[formatter_prefix+ 'orig_before']) {
97 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
98 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
99 txt.data = data[formatter_prefix+ 'orig_before_original'];
101 el.before(data[formatter_prefix+ 'orig_before']);
104 if(data[formatter_prefix+ 'orig_after']) {
105 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
106 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
107 txt.data = data[formatter_prefix+ 'orig_after_original'];
109 el.after(data[formatter_prefix+ 'orig_after']);
112 if(data[formatter_prefix+ 'orig_begin']) {
113 el.prepend(data[formatter_prefix+ 'orig_begin']);
115 if(data[formatter_prefix+ 'orig_end']) {
116 contents = el.contents();
117 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
118 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
119 txt.data = data[formatter_prefix+ 'orig_end_original'];
121 el.append(data[formatter_prefix+ 'orig_end']);
126 if(this.nodeType === Node.ELEMENT_NODE) {
127 documentNode = doc.createDocumentNode(this);
128 metaNode = $('<metadata>');
129 documentNode.getMetadata().forEach(function(row) {
130 metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
132 if(metaNode.children().length) {
133 $(this).prepend(metaNode);
145 // WLXMLElementNode.prototype.transformations.register(transformations.createContextTransformation({
146 // name: 'wlxml.setMetaAttribute',
147 // impl: function(args) {
148 // this.setMetaAttribute(args.name, args.value);
150 // getChangeRoot: function() {
151 // return this.context;
157 var WLXMLDocumentNode = function() {
158 smartxml.DocumentNode.apply(this, arguments);
160 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
162 var WLXMLDocument = function(xml, options) {
163 this.classMethods = {};
164 this.classTransformations = {};
165 smartxml.Document.call(this, xml, [metadataExtension]);
166 this.options = options;
169 var formatter_prefix = '_wlxml_formatter_';
172 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
173 $.extend(WLXMLDocument.prototype, {
174 ElementNodeFactory: WLXMLElementNode,
175 loadXML: function(xml) {
176 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
177 this.trigger('contentSet');
180 normalizeXML: function(nativeNode) {
182 prefixLength = 'dc:'.length;
184 $(nativeNode).find(':not(iframe)').addBack().contents()
185 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
188 text = {original: el.text(), trimmed: $.trim(el.text())},
189 elParent = el.parent(),
190 hasSpanParent = elParent.prop('tagName') === 'SPAN',
191 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
192 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
195 var addInfo = function(toAdd, where, transformed, original) {
196 var parentContents = elParent.contents(),
197 idx = parentContents.index(el[0]),
198 prev = idx > 0 ? parentContents[idx-1] : null,
199 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
202 if(where === 'above') {
203 target = prev ? $(prev) : elParent;
204 key = prev ? 'orig_after' : 'orig_begin';
205 } else if(where === 'below') {
206 target = next ? $(next) : elParent;
207 key = next ? 'orig_before' : 'orig_end';
208 } else { throw new Error();}
210 target.data(formatter_prefix + key, toAdd);
211 if(transformed !== undefined) {
212 target.data(formatter_prefix + key + '_transformed', transformed);
214 if(original !== undefined) {
215 target.data(formatter_prefix + key + '_original', original);
219 text.transformed = text.trimmed;
221 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
222 var startSpace = /\s/g.test(text.original.substr(0,1)),
223 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
224 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
225 text.transformed += text.trimmed;
226 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
228 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
229 text.transformed = ' ';
233 if(!text.transformed) {
234 addInfo(text.original, 'below');
236 return true; // continue
239 if(text.transformed !== text.original) {
240 // if(!text.trimmed) {
241 // addInfo(text.original, 'below');
243 var startingMatch = text.original.match(/^\s+/g),
244 endingMatch = text.original.match(/\s+$/g),
245 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
246 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
248 if(endingWhiteSpace) {
249 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
250 endingWhiteSpace = endingWhiteSpace.substr(1);
252 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
255 if(startingWhiteSpace && text.trimmed) {
256 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
257 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
259 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
263 /* globals document */
264 el.replaceWith(document.createTextNode(text.transformed));
267 $(nativeNode).find('metadata').each(function() {
268 var metadataNode = $(this),
269 owner = doc.createDocumentNode(metadataNode.parent()[0]);
271 metadataNode.children().each(function() {
272 owner.addMetadataRow({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()});
274 metadataNode.remove();
278 registerClassTransformation: function(Transformation, className) {
279 var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
280 thisClassTransformations[Transformation.prototype.name] = function(args) {
281 var nodeInstance = this;
282 return nodeInstance.transform(Transformation, args);
286 registerClassMethod: function(methodName, method, className) {
287 var thisClassMethods = (this.classMethods[className] = this.classMethods[className] || {});
288 thisClassMethods[methodName] = method;
291 registerExtension: function(extension) {
293 smartxml.Document.prototype.registerExtension.call(this, extension);
296 _.pairs(extension.wlxmlClass).forEach(function(pair) {
297 var className = pair[0],
298 classExtension = pair[1];
300 _.pairs(classExtension.methods || {}).forEach(function(pair) {
303 doc.registerClassMethod(name, method, className);
306 _.pairs(classExtension.transformations || {}).forEach(function(pair) {
309 doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
319 attrs: {href: {type: 'string'}}
325 WLXMLDocumentFromXML: function(xml, options) {
326 options = _.extend({wlxmlClasses: wlxmlClasses}, options);
327 return new WLXMLDocument(xml, options);
330 WLXMLElementNodeFromXML: function(xml) {
331 return this.WLXMLDocumentFromXML(xml).root;