5 'smartxml/transformations',
6 'wlxml/extensions/metadata/metadata',
7 'wlxml/extensions/comments/comments'
8 ], function($, _, smartxml, transformations, metadataExtension, commentExtension) {
15 var WLXMLDocumentNodeMethods = {
16 isInside: function(query) {
17 var parent = this.getParent(query);
20 getParent: function(query) {
22 var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
24 me.concat(this.parents()).some(function(node) {
32 isContextRoot: function() {
33 var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [],
35 me.concat(this.parents()).some(function(node) {
36 if(_.isFunction(node.object.isContextRoot) && node.object.isContextRoot(this)) {
45 var getClassLists = function(klassName) {
47 classParts = [''].concat(klassName.split('.')),
50 classParts.forEach(function(part) {
51 classCurrent = classCurrent ? classCurrent + '.' + part : part;
52 toret.push(classCurrent);
57 var installObject = function(instance, klass) {
61 getClassLists(klass).forEach(function(klassName) {
62 _.extend(methods, instance.document.classMethods[klassName] || {});
63 _.extend(methods, instance.document.classTransformations[klassName] || {});
65 instance.object = Object.create(_.extend({}, methods, transformations));
66 _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
67 if(_.isFunction(instance.object[key])) {
68 instance.object[key] = _.bind(instance.object[key], instance);
73 var WLXMLElementNode = function(nativeNode, document) {
74 smartxml.ElementNode.call(this, nativeNode, document);
75 installObject(this, this.getClass());
77 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
79 $.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementNode.prototype, {
80 getClass: function() {
81 return this.getAttr('class') || '';
83 getClassHierarchy: function() {
84 return getClassLists(this.getClass());
86 setClass: function(klass) {
87 if(klass !== this.klass) {
88 installObject(this, klass);
89 return this.setAttr('class', klass);
93 if(typeof query === 'string') {
94 query = {klass: query};
96 return (_.isUndefined(query.klass) || this.getClass().substr(0, query.klass.length) === query.klass) &&
97 (_.isUndefined(query.tagName) || this.getTagName() === query.tagName);
99 hasChild: function(query) {
100 return this.contents().some(function(child) {
101 return child.is(query);
105 _getXMLDOMToDump: function() {
106 var DOM = this._$.clone(true, true),
109 DOM.find('*').addBack().each(function() {
111 parent = el.parent(),
112 contents = parent.contents(),
113 idx = contents.index(el),
117 var txt, documentNode, metaNode;
119 if(data[formatter_prefix+ 'orig_before']) {
120 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
121 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
122 txt.data = data[formatter_prefix+ 'orig_before_original'];
124 el.before(data[formatter_prefix+ 'orig_before']);
127 if(data[formatter_prefix+ 'orig_after']) {
128 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
129 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
130 txt.data = data[formatter_prefix+ 'orig_after_original'];
132 el.after(data[formatter_prefix+ 'orig_after']);
135 if(data[formatter_prefix+ 'orig_begin']) {
136 el.prepend(data[formatter_prefix+ 'orig_begin']);
138 if(data[formatter_prefix+ 'orig_end']) {
139 contents = el.contents();
140 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
141 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
142 txt.data = data[formatter_prefix+ 'orig_end_original'];
144 el.append(data[formatter_prefix+ 'orig_end']);
149 if(this.nodeType === Node.ELEMENT_NODE) {
150 documentNode = doc.createDocumentNode(this);
151 metaNode = $('<metadata>');
152 documentNode.getMetadata().forEach(function(row) {
153 metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
155 if(metaNode.children().length) {
156 $(this).prepend(metaNode);
166 var WLXMLDocumentNode = function() {
167 smartxml.DocumentNode.apply(this, arguments);
169 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
172 var WLXMLTextNode = function() {
173 smartxml.TextNode.apply(this, arguments);
175 WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype);
176 $.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods, {
177 is: function() { return false; }
180 var WLXMLDocument = function(xml, options) {
181 this.classMethods = {};
182 this.classTransformations = {};
183 smartxml.Document.call(this, xml, [metadataExtension, commentExtension]);
184 this.options = options;
187 var formatter_prefix = '_wlxml_formatter_';
190 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
191 $.extend(WLXMLDocument.prototype, {
192 ElementNodeFactory: WLXMLElementNode,
193 TextNodeFactory: WLXMLTextNode,
194 loadXML: function(xml) {
195 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
196 this.trigger('contentSet');
199 normalizeXML: function(nativeNode) {
201 prefixLength = 'dc:'.length;
203 $(nativeNode).find('metadata').each(function() {
204 var metadataNode = $(this),
205 owner = doc.createDocumentNode(metadataNode.parent()[0]),
206 metadata = owner.getMetadata();
208 metadataNode.children().each(function() {
209 metadata.add({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}, {undoable: false});
211 metadataNode.remove();
213 nativeNode.normalize();
215 $(nativeNode).find(':not(iframe)').addBack().contents()
216 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
219 text = {original: el.text(), trimmed: $.trim(el.text())},
220 elParent = el.parent(),
221 hasSpanParent = elParent.prop('tagName') === 'SPAN',
222 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
223 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
226 var addInfo = function(toAdd, where, transformed, original) {
227 var parentContents = elParent.contents(),
228 idx = parentContents.index(el[0]),
229 prev = idx > 0 ? parentContents[idx-1] : null,
230 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
233 if(where === 'above') {
234 target = prev ? $(prev) : elParent;
235 key = prev ? 'orig_after' : 'orig_begin';
236 } else if(where === 'below') {
237 target = next ? $(next) : elParent;
238 key = next ? 'orig_before' : 'orig_end';
239 } else { throw new Error();}
241 target.data(formatter_prefix + key, toAdd);
242 if(transformed !== undefined) {
243 target.data(formatter_prefix + key + '_transformed', transformed);
245 if(original !== undefined) {
246 target.data(formatter_prefix + key + '_original', original);
250 text.transformed = text.trimmed;
252 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
253 var startSpace = /\s/g.test(text.original.substr(0,1)),
254 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
255 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
256 text.transformed += text.trimmed;
257 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
259 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
260 text.transformed = ' ';
264 if(!text.transformed) {
265 addInfo(text.original, 'below');
267 return true; // continue
270 if(text.transformed !== text.original) {
271 // if(!text.trimmed) {
272 // addInfo(text.original, 'below');
274 var startingMatch = text.original.match(/^\s+/g),
275 endingMatch = text.original.match(/\s+$/g),
276 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
277 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
279 if(endingWhiteSpace) {
280 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
281 endingWhiteSpace = endingWhiteSpace.substr(1);
283 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
286 if(startingWhiteSpace && text.trimmed) {
287 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
288 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
290 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
294 /* globals document */
295 el.replaceWith(document.createTextNode(text.transformed));
301 registerClassTransformation: function(Transformation, className) {
302 var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
303 thisClassTransformations[Transformation.prototype.name] = function() {
304 var nodeInstance = this;
305 var args = Array.prototype.slice.call(arguments, 0);
306 return nodeInstance.transform(Transformation, args);
310 registerClassMethod: function(methodName, method, className) {
311 var thisClassMethods = (this.classMethods[className] = this.classMethods[className] || {});
312 thisClassMethods[methodName] = method;
315 registerExtension: function(extension) {
316 smartxml.Document.prototype.registerExtension.call(this, extension);
319 _.pairs(extension.wlxmlClass).forEach(function(pair) {
320 var className = pair[0],
321 classExtension = pair[1];
323 _.pairs(classExtension.methods || {}).forEach(function(pair) {
326 doc.registerClassMethod(name, method, className);
329 _.pairs(classExtension.transformations || {}).forEach(function(pair) {
332 doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
341 WLXMLDocumentFromXML: function(xml, options, Factory) {
342 Factory = Factory || WLXMLDocument;
343 return new Factory(xml, options);
345 WLXMLElementNodeFromXML: function(xml) {
346 return this.WLXMLDocumentFromXML(xml).root;
349 WLXMLDocument: WLXMLDocument,
350 getClassHierarchy: getClassLists