5 'smartxml/transformations',
6 'wlxml/extensions/metadata/metadata'
7 ], function($, _, smartxml, transformations, metadataExtension) {
14 var WLXMLDocumentNodeMethods = {
15 isInside: function(query) {
16 var parent = this.getParent(query);
19 getParent: function(query) {
21 var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
23 me.concat(this.parents()).some(function(node) {
33 var AttributesList = function() {};
34 AttributesList.prototype = Object.create({});
35 AttributesList.prototype.keys = function() {
39 var getClassLists = function(klassName) {
41 classParts = [''].concat(klassName.split('.')),
44 classParts.forEach(function(part) {
45 classCurrent = classCurrent ? classCurrent + '.' + part : part;
46 toret.push(classCurrent);
51 var installObject = function(instance, klass) {
55 getClassLists(klass).forEach(function(klassName) {
56 _.extend(methods, instance.document.classMethods[klassName] || {});
57 _.extend(methods, instance.document.classTransformations[klassName] || {});
59 instance.object = Object.create(_.extend({}, methods, transformations));
60 _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
61 instance.object[key] = _.bind(instance.object[key], instance);
65 var WLXMLElementNode = function(nativeNode, document) {
66 smartxml.ElementNode.call(this, nativeNode, document);
67 installObject(this, this.getClass());
69 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
71 $.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementNode.prototype, {
72 getClass: function() {
73 return this.getAttr('class') || '';
75 setClass: function(klass) {
76 if(klass !== this.klass) {
77 installObject(this, klass);
78 return this.setAttr('class', klass);
82 if(typeof query === 'string') {
83 query = {klass: query};
85 return (_.isUndefined(query.klass) || this.getClass().substr(0, query.klass.length) === query.klass) &&
86 (_.isUndefined(query.tagName) || this.getTagName() === query.tagName);
88 getMetaAttributes: function() {
89 var toret = new AttributesList(),
90 classParts = [''].concat(this.getClass().split('.')),
91 classCurrent, classDesc;
93 classParts.forEach(function(part) {
94 classCurrent = classCurrent ? classCurrent + '.' + part : part;
95 classDesc = this.document.options.wlxmlClasses[classCurrent];
97 _.keys(classDesc.attrs).forEach(function(attrName) {
98 toret[attrName] = _.extend({value: this.getAttr(attrName)}, classDesc.attrs[attrName]);
104 setMetaAttribute: function(key, value) {
105 this.setAttr(key, value);
107 getOtherAttributes: function() {
110 this.getAttrs().forEach(function(attr) {
111 if(attr.name !== 'class' && !node.isMetaAttribute(attr.name)) {
112 toret[attr.name] = {value: attr.value};
117 isMetaAttribute: function(attrName) {
118 return attrName !== 'class' &&_.contains(_.keys(this.getMetaAttributes()), attrName);
121 _getXMLDOMToDump: function() {
122 var DOM = this._$.clone(true, true),
125 DOM.find('*').addBack().each(function() {
127 parent = el.parent(),
128 contents = parent.contents(),
129 idx = contents.index(el),
133 var txt, documentNode, metaNode;
135 if(data[formatter_prefix+ 'orig_before']) {
136 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
137 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
138 txt.data = data[formatter_prefix+ 'orig_before_original'];
140 el.before(data[formatter_prefix+ 'orig_before']);
143 if(data[formatter_prefix+ 'orig_after']) {
144 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
145 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
146 txt.data = data[formatter_prefix+ 'orig_after_original'];
148 el.after(data[formatter_prefix+ 'orig_after']);
151 if(data[formatter_prefix+ 'orig_begin']) {
152 el.prepend(data[formatter_prefix+ 'orig_begin']);
154 if(data[formatter_prefix+ 'orig_end']) {
155 contents = el.contents();
156 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
157 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
158 txt.data = data[formatter_prefix+ 'orig_end_original'];
160 el.append(data[formatter_prefix+ 'orig_end']);
165 if(this.nodeType === Node.ELEMENT_NODE) {
166 documentNode = doc.createDocumentNode(this);
167 metaNode = $('<metadata>');
168 documentNode.getMetadata().forEach(function(row) {
169 metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
171 if(metaNode.children().length) {
172 $(this).prepend(metaNode);
184 // WLXMLElementNode.prototype.transformations.register(transformations.createContextTransformation({
185 // name: 'wlxml.setMetaAttribute',
186 // impl: function(args) {
187 // this.setMetaAttribute(args.name, args.value);
189 // getChangeRoot: function() {
190 // return this.context;
196 var WLXMLDocumentNode = function() {
197 smartxml.DocumentNode.apply(this, arguments);
199 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
202 var WLXMLTextNode = function() {
203 smartxml.TextNode.apply(this, arguments);
205 WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype);
206 $.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods);
208 var WLXMLDocument = function(xml, options) {
209 this.classMethods = {};
210 this.classTransformations = {};
211 smartxml.Document.call(this, xml, [metadataExtension]);
212 this.options = options;
215 var formatter_prefix = '_wlxml_formatter_';
218 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
219 $.extend(WLXMLDocument.prototype, {
220 ElementNodeFactory: WLXMLElementNode,
221 TextNodeFactory: WLXMLTextNode,
222 loadXML: function(xml) {
223 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
224 this.trigger('contentSet');
227 normalizeXML: function(nativeNode) {
229 prefixLength = 'dc:'.length;
231 $(nativeNode).find('metadata').each(function() {
232 var metadataNode = $(this),
233 owner = doc.createDocumentNode(metadataNode.parent()[0]),
234 metadata = owner.getMetadata();
236 metadataNode.children().each(function() {
237 metadata.add({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}, {undoable: false});
239 metadataNode.remove();
241 nativeNode.normalize();
243 $(nativeNode).find(':not(iframe)').addBack().contents()
244 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
247 text = {original: el.text(), trimmed: $.trim(el.text())},
248 elParent = el.parent(),
249 hasSpanParent = elParent.prop('tagName') === 'SPAN',
250 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
251 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
254 var addInfo = function(toAdd, where, transformed, original) {
255 var parentContents = elParent.contents(),
256 idx = parentContents.index(el[0]),
257 prev = idx > 0 ? parentContents[idx-1] : null,
258 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
261 if(where === 'above') {
262 target = prev ? $(prev) : elParent;
263 key = prev ? 'orig_after' : 'orig_begin';
264 } else if(where === 'below') {
265 target = next ? $(next) : elParent;
266 key = next ? 'orig_before' : 'orig_end';
267 } else { throw new Error();}
269 target.data(formatter_prefix + key, toAdd);
270 if(transformed !== undefined) {
271 target.data(formatter_prefix + key + '_transformed', transformed);
273 if(original !== undefined) {
274 target.data(formatter_prefix + key + '_original', original);
278 text.transformed = text.trimmed;
280 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
281 var startSpace = /\s/g.test(text.original.substr(0,1)),
282 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
283 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
284 text.transformed += text.trimmed;
285 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
287 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
288 text.transformed = ' ';
292 if(!text.transformed) {
293 addInfo(text.original, 'below');
295 return true; // continue
298 if(text.transformed !== text.original) {
299 // if(!text.trimmed) {
300 // addInfo(text.original, 'below');
302 var startingMatch = text.original.match(/^\s+/g),
303 endingMatch = text.original.match(/\s+$/g),
304 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
305 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
307 if(endingWhiteSpace) {
308 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
309 endingWhiteSpace = endingWhiteSpace.substr(1);
311 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
314 if(startingWhiteSpace && text.trimmed) {
315 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
316 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
318 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
322 /* globals document */
323 el.replaceWith(document.createTextNode(text.transformed));
329 registerClassTransformation: function(Transformation, className) {
330 var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
331 thisClassTransformations[Transformation.prototype.name] = function() {
332 var nodeInstance = this;
333 var args = Array.prototype.slice.call(arguments, 0);
334 return nodeInstance.transform(Transformation, args);
338 registerClassMethod: function(methodName, method, className) {
339 var thisClassMethods = (this.classMethods[className] = this.classMethods[className] || {});
340 thisClassMethods[methodName] = method;
343 registerExtension: function(extension) {
345 smartxml.Document.prototype.registerExtension.call(this, extension);
348 _.pairs(extension.wlxmlClass).forEach(function(pair) {
349 var className = pair[0],
350 classExtension = pair[1];
352 _.pairs(classExtension.methods || {}).forEach(function(pair) {
355 doc.registerClassMethod(name, method, className);
358 _.pairs(classExtension.transformations || {}).forEach(function(pair) {
361 doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
371 attrs: {href: {type: 'string'}}
377 WLXMLDocumentFromXML: function(xml, options, Factory) {
378 options = _.extend({wlxmlClasses: wlxmlClasses}, options);
379 Factory = Factory || WLXMLDocument;
380 return new Factory(xml, options);
383 WLXMLElementNodeFromXML: function(xml) {
384 return this.WLXMLDocumentFromXML(xml).root;
387 WLXMLDocument: WLXMLDocument