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) {
34 var getClassLists = function(klassName) {
36 classParts = [''].concat(klassName.split('.')),
39 classParts.forEach(function(part) {
40 classCurrent = classCurrent ? classCurrent + '.' + part : part;
41 toret.push(classCurrent);
46 var installObject = function(instance, klass) {
50 getClassLists(klass).forEach(function(klassName) {
51 _.extend(methods, instance.document.classMethods[klassName] || {});
52 _.extend(methods, instance.document.classTransformations[klassName] || {});
54 instance.object = Object.create(_.extend({}, methods, transformations));
55 _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
56 if(_.isFunction(instance.object[key])) {
57 instance.object[key] = _.bind(instance.object[key], instance);
62 var WLXMLElementNode = function(nativeNode, document) {
63 smartxml.ElementNode.call(this, nativeNode, document);
64 installObject(this, this.getClass());
66 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
68 $.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementNode.prototype, {
69 getClass: function() {
70 return this.getAttr('class') || '';
72 getClassHierarchy: function() {
73 return getClassLists(this.getClass());
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 hasChild: function(query) {
89 return this.contents().some(function(child) {
90 return child.is(query);
94 _getXMLDOMToDump: function() {
95 var DOM = this._$.clone(true, true),
98 DOM.find('*').addBack().each(function() {
100 parent = el.parent(),
101 contents = parent.contents(),
102 idx = contents.index(el),
106 var txt, documentNode, metaNode;
108 if(data[formatter_prefix+ 'orig_before']) {
109 txt = idx > 0 && contents[idx-1].nodeType === Node.TEXT_NODE ? contents[idx-1] : null;
110 if(txt && txt.data === data[formatter_prefix + 'orig_before_transformed']) {
111 txt.data = data[formatter_prefix+ 'orig_before_original'];
113 el.before(data[formatter_prefix+ 'orig_before']);
116 if(data[formatter_prefix+ 'orig_after']) {
117 txt = idx < contents.length-1 && contents[idx+1].nodeType === Node.TEXT_NODE ? contents[idx+1] : null;
118 if(txt && txt.data === data[formatter_prefix + 'orig_after_transformed']) {
119 txt.data = data[formatter_prefix+ 'orig_after_original'];
121 el.after(data[formatter_prefix+ 'orig_after']);
124 if(data[formatter_prefix+ 'orig_begin']) {
125 el.prepend(data[formatter_prefix+ 'orig_begin']);
127 if(data[formatter_prefix+ 'orig_end']) {
128 contents = el.contents();
129 txt = (contents.length && contents[contents.length-1].nodeType === Node.TEXT_NODE) ? contents[contents.length-1] : null;
130 if(txt && txt.data === data[formatter_prefix + 'orig_end_transformed']) {
131 txt.data = data[formatter_prefix+ 'orig_end_original'];
133 el.append(data[formatter_prefix+ 'orig_end']);
138 if(this.nodeType === Node.ELEMENT_NODE) {
139 documentNode = doc.createDocumentNode(this);
140 metaNode = $('<metadata>');
141 documentNode.getMetadata().forEach(function(row) {
142 metaNode.append('<dc:'+ row.key + '>' + row.value + '</dc:' + row.key + '>');
144 if(metaNode.children().length) {
145 $(this).prepend(metaNode);
157 // WLXMLElementNode.prototype.transformations.register(transformations.createContextTransformation({
158 // name: 'wlxml.setMetaAttribute',
159 // impl: function(args) {
160 // this.setMetaAttribute(args.name, args.value);
162 // getChangeRoot: function() {
163 // return this.context;
169 var WLXMLDocumentNode = function() {
170 smartxml.DocumentNode.apply(this, arguments);
172 WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
175 var WLXMLTextNode = function() {
176 smartxml.TextNode.apply(this, arguments);
178 WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype);
179 $.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods, {
180 is: function() { return false; }
183 var WLXMLDocument = function(xml, options) {
184 this.classMethods = {};
185 this.classTransformations = {};
186 smartxml.Document.call(this, xml, [metadataExtension, commentExtension]);
187 this.options = options;
190 var formatter_prefix = '_wlxml_formatter_';
193 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
194 $.extend(WLXMLDocument.prototype, {
195 ElementNodeFactory: WLXMLElementNode,
196 TextNodeFactory: WLXMLTextNode,
197 loadXML: function(xml) {
198 smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
199 this.trigger('contentSet');
202 normalizeXML: function(nativeNode) {
204 prefixLength = 'dc:'.length;
206 $(nativeNode).find('metadata').each(function() {
207 var metadataNode = $(this),
208 owner = doc.createDocumentNode(metadataNode.parent()[0]),
209 metadata = owner.getMetadata();
211 metadataNode.children().each(function() {
212 metadata.add({key: (this.tagName).toLowerCase().substr(prefixLength), value: $(this).text()}, {undoable: false});
214 metadataNode.remove();
216 nativeNode.normalize();
218 $(nativeNode).find(':not(iframe)').addBack().contents()
219 .filter(function() {return this.nodeType === Node.TEXT_NODE;})
222 text = {original: el.text(), trimmed: $.trim(el.text())},
223 elParent = el.parent(),
224 hasSpanParent = elParent.prop('tagName') === 'SPAN',
225 hasSpanBefore = el.prev().length && $(el.prev()).prop('tagName') === 'SPAN',
226 hasSpanAfter = el.next().length && $(el.next()).prop('tagName') === 'SPAN';
229 var addInfo = function(toAdd, where, transformed, original) {
230 var parentContents = elParent.contents(),
231 idx = parentContents.index(el[0]),
232 prev = idx > 0 ? parentContents[idx-1] : null,
233 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
236 if(where === 'above') {
237 target = prev ? $(prev) : elParent;
238 key = prev ? 'orig_after' : 'orig_begin';
239 } else if(where === 'below') {
240 target = next ? $(next) : elParent;
241 key = next ? 'orig_before' : 'orig_end';
242 } else { throw new Error();}
244 target.data(formatter_prefix + key, toAdd);
245 if(transformed !== undefined) {
246 target.data(formatter_prefix + key + '_transformed', transformed);
248 if(original !== undefined) {
249 target.data(formatter_prefix + key + '_original', original);
253 text.transformed = text.trimmed;
255 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
256 var startSpace = /\s/g.test(text.original.substr(0,1)),
257 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
258 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '');
259 text.transformed += text.trimmed;
260 text.transformed += (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
262 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1) {
263 text.transformed = ' ';
267 if(!text.transformed) {
268 addInfo(text.original, 'below');
270 return true; // continue
273 if(text.transformed !== text.original) {
274 // if(!text.trimmed) {
275 // addInfo(text.original, 'below');
277 var startingMatch = text.original.match(/^\s+/g),
278 endingMatch = text.original.match(/\s+$/g),
279 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
280 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
282 if(endingWhiteSpace) {
283 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ') {
284 endingWhiteSpace = endingWhiteSpace.substr(1);
286 addInfo(endingWhiteSpace, 'below', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
289 if(startingWhiteSpace && text.trimmed) {
290 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ') {
291 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
293 addInfo(startingWhiteSpace, 'above', !text.trimmed ? text.transformed : undefined, !text.trimmed ? text.original : undefined);
297 /* globals document */
298 el.replaceWith(document.createTextNode(text.transformed));
304 registerClassTransformation: function(Transformation, className) {
305 var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
306 thisClassTransformations[Transformation.prototype.name] = function() {
307 var nodeInstance = this;
308 var args = Array.prototype.slice.call(arguments, 0);
309 return nodeInstance.transform(Transformation, args);
313 registerClassMethod: function(methodName, method, className) {
314 var thisClassMethods = (this.classMethods[className] = this.classMethods[className] || {});
315 thisClassMethods[methodName] = method;
318 registerExtension: function(extension) {
320 smartxml.Document.prototype.registerExtension.call(this, extension);
323 _.pairs(extension.wlxmlClass).forEach(function(pair) {
324 var className = pair[0],
325 classExtension = pair[1];
327 _.pairs(classExtension.methods || {}).forEach(function(pair) {
330 doc.registerClassMethod(name, method, className);
333 _.pairs(classExtension.transformations || {}).forEach(function(pair) {
336 doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
345 WLXMLDocumentFromXML: function(xml, options, Factory) {
346 Factory = Factory || WLXMLDocument;
347 return new Factory(xml, options);
349 WLXMLElementNodeFromXML: function(xml) {
350 return this.WLXMLDocumentFromXML(xml).root;
353 WLXMLDocument: WLXMLDocument,
354 getClassHierarchy: getClassLists