5 'smartxml/transformations',
6 'wlxml/extensions/metadata/metadata',
7 'wlxml/extensions/comments/comments',
8 'wlxml/extensions/edumed/edumed'
9 ], function($, _, smartxml, transformations, metadataExtension, commentExtension, edumedExtension) {
16 var WLXMLDocumentNodeMethods = {
17 isInside: function(query) {
18 var parent = this.getParent(query);
21 getParent: function(query) {
23 var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
25 me.concat(this.parents()).some(function(node) {
29 return !!toret || (!node.sameNode(this) && node.isContextRoot());
34 isContextRoot: function() {
35 var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
40 me.concat(this.parents()).some(function(node) {
41 if(_.isFunction(node.object.isContextRoot) && node.object.isContextRoot(this)) {
50 var getClassLists = function(klassName) {
52 classParts = [''].concat(klassName.split('.')),
55 classParts.forEach(function(part) {
56 classCurrent = classCurrent ? classCurrent + '.' + part : part;
57 toret.push(classCurrent);
62 var installObject = function(instance, klass) {
66 getClassLists(klass).forEach(function(klassName) {
67 _.extend(methods, instance.document.classMethods[klassName] || {});
68 _.extend(methods, instance.document.classTransformations[klassName] || {});
70 instance.object = Object.create(_.extend({}, methods, transformations));
71 _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
72 if(_.isFunction(instance.object[key])) {
73 instance.object[key] = _.bind(instance.object[key], instance);
78 var WLXMLElementNode = function(nativeNode, document) {
79 smartxml.ElementNode.call(this, nativeNode, document);
80 installObject(this, this.getClass());
82 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
84 $.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementNode.prototype, {
85 getClass: function() {
86 return this.getAttr('class') || '';
88 getClassHierarchy: function() {
89 return getClassLists(this.getClass());
91 setClass: function(klass) {
92 if(klass !== this.klass) {
93 installObject(this, klass);
94 return this.setAttr('class', klass);
98 if(typeof query === 'string') {
99 query = {klass: query};
101 return (_.isUndefined(query.klass) || this.getClass().substr(0, query.klass.length) === query.klass) &&
102 (_.isUndefined(query.tagName) || this.getTagName() === query.tagName);
104 hasChild: function(query) {
105 return this.contents().some(function(child) {
106 return child.is(query);
109 find: function(query) {
110 var doc = this.document;
111 return this._$.find('*')
113 return doc.createDocumentNode(this);
116 return this.is(query);
120 _getXMLDOMToDump: function() {
121 var DOM = this._$.clone(true, true),
124 DOM.find('*').addBack().each(function() {
126 parent = el.parent(),
127 contents = parent.contents(),
128 idx = contents.index(el),
132 var txt, documentNode, metaNode;
134 if(data[formatter_prefix+ 'orig_before']) {
135 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
136 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
137 txt.data = data[formatter_prefix+ 'orig_before_original'];
139 el.before(data[formatter_prefix+ 'orig_before']);
142 if(data[formatter_prefix+ 'orig_after']) {
143 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
144 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
145 txt.data = data[formatter_prefix+ 'orig_after_original'];
147 el.after(data[formatter_prefix+ 'orig_after']);
150 if(data[formatter_prefix+ 'orig_begin']) {
151 el.prepend(data[formatter_prefix+ 'orig_begin']);
153 if(data[formatter_prefix+ 'orig_end']) {
154 contents = el.contents();
155 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
156 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
157 txt.data = data[formatter_prefix+ 'orig_end_original'];
159 el.append(data[formatter_prefix+ 'orig_end']);
164 if(this.nodeType === Node.ELEMENT_NODE) {
165 documentNode = doc.createDocumentNode(this);
166 metaNode = $('<metadata>');
167 documentNode.getMetadata().forEach(function(row) {
168 metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
170 if(metaNode.children().length) {
171 $(this).prepend(metaNode);
181 var WLXMLDocumentNode = function() {
182 smartxml.DocumentNode.apply(this, arguments);
184 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
187 var WLXMLTextNode = function() {
188 smartxml.TextNode.apply(this, arguments);
190 WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype);
191 $.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods, {
192 is: function() { return false; }
195 var WLXMLDocument = function(xml, options) {
196 this.classMethods = {};
197 this.classTransformations = {};
198 smartxml.Document.call(this, xml, [metadataExtension, commentExtension, edumedExtension]);
199 this.options = options;
202 var formatter_prefix = '_wlxml_formatter_';
205 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
206 $.extend(WLXMLDocument.prototype, {
207 ElementNodeFactory: WLXMLElementNode,
208 TextNodeFactory: WLXMLTextNode,
209 loadXML: function(xml) {
210 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
211 this.trigger('contentSet');
214 normalizeXML: function(nativeNode) {
216 prefixLength = 'dc:'.length;
218 $(nativeNode).find('metadata').each(function() {
219 var metadataNode = $(this),
220 owner = doc.createDocumentNode(metadataNode.parent()[0]),
221 metadata = owner.getMetadata();
223 metadataNode.children().each(function() {
224 metadata.add({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}, {undoable: false});
226 metadataNode.remove();
228 nativeNode.normalize();
230 $(nativeNode).find(':not(iframe)').addBack().contents()
231 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
234 text = {original: el.text(), trimmed: $.trim(el.text())},
235 elParent = el.parent(),
236 hasSpanParent = elParent.prop('tagName') === 'SPAN',
237 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
238 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
241 var addInfo = function(toAdd, where, transformed, original) {
242 var parentContents = elParent.contents(),
243 idx = parentContents.index(el[0]),
244 prev = idx > 0 ? parentContents[idx-1] : null,
245 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
248 if(where === 'above') {
249 target = prev ? $(prev) : elParent;
250 key = prev ? 'orig_after' : 'orig_begin';
251 } else if(where === 'below') {
252 target = next ? $(next) : elParent;
253 key = next ? 'orig_before' : 'orig_end';
254 } else { throw new Error();}
256 target.data(formatter_prefix + key, toAdd);
257 if(transformed !== undefined) {
258 target.data(formatter_prefix + key + '_transformed', transformed);
260 if(original !== undefined) {
261 target.data(formatter_prefix + key + '_original', original);
265 text.transformed = text.trimmed;
267 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
268 var startSpace = /\s/g.test(text.original.substr(0,1)),
269 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
270 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
271 text.transformed += text.trimmed;
272 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
274 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
275 text.transformed = ' ';
279 if(!text.transformed) {
280 addInfo(text.original, 'below');
282 return true; // continue
285 if(text.transformed !== text.original) {
286 // if(!text.trimmed) {
287 // addInfo(text.original, 'below');
289 var startingMatch = text.original.match(/^\s+/g),
290 endingMatch = text.original.match(/\s+$/g),
291 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
292 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
294 if(endingWhiteSpace) {
295 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
296 endingWhiteSpace = endingWhiteSpace.substr(1);
298 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
301 if(startingWhiteSpace && text.trimmed) {
302 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
303 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
305 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
309 /* globals document */
310 el.replaceWith(document.createTextNode(text.transformed));
316 registerClassTransformation: function(Transformation, className) {
317 var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
318 thisClassTransformations[Transformation.prototype.name] = function() {
319 var nodeInstance = this;
320 var args = Array.prototype.slice.call(arguments, 0);
321 return nodeInstance.transform(Transformation, args);
325 registerClassMethod: function(methodName, method, className) {
326 var thisClassMethods = (this.classMethods[className] = this.classMethods[className] || {});
327 thisClassMethods[methodName] = method;
330 registerExtension: function(extension) {
331 smartxml.Document.prototype.registerExtension.call(this, extension);
334 _.pairs(extension.wlxmlClass).forEach(function(pair) {
335 var className = pair[0],
336 classExtension = pair[1];
338 _.pairs(classExtension.methods || {}).forEach(function(pair) {
341 doc.registerClassMethod(name, method, className);
344 _.pairs(classExtension.transformations || {}).forEach(function(pair) {
347 doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
356 WLXMLDocumentFromXML: function(xml, options, Factory) {
357 Factory = Factory || WLXMLDocument;
358 return new Factory(xml, options);
360 WLXMLElementNodeFromXML: function(xml) {
361 return this.WLXMLDocumentFromXML(xml).root;
364 WLXMLDocument: WLXMLDocument,
365 getClassHierarchy: getClassLists