'use strict';
+/* globals Node */
+
// utils
var isMetaAttribute = function(attrName) {
};
var installObject = function(instance, klass) {
- var methods = instance.document.classMethods[klass] || instance.document.classTransformations;
- if(methods) {
- instance.object = Object.create(_.extend({
- transform: function(name, args) {
- // TODO: refactor with DocumentElement.transform
- var Transformation = instance.document.classTransformations[klass].get(name),
- transformation;
- if(Transformation) {
- transformation = new Transformation(instance.document, instance, args);
- }
- return instance.document.transform(transformation);
- }
- }, methods));
- _.keys(methods).forEach(function(key) {
- instance.object[key] = _.bind(instance.object[key], instance);
- });
- }
-}
+ var methods = instance.document.classMethods[klass] || {},
+ transformations = instance.document.classTransformations[klass] || {};
+
+ instance.object = Object.create(_.extend({}, methods, transformations));
+ _.keys(methods).concat(_.keys(transformations)).forEach(function(key) {
+ instance.object[key] = _.bind(instance.object[key], instance);
+ });
+};
var WLXMLElementNode = function(nativeNode, document) {
smartxml.ElementNode.call(this, nativeNode, document);
return this.getAttr('class') || '';
},
setClass: function(klass) {
- var methods, object;
if(klass !== this.klass) {
installObject(this, klass);
return this.setAttr('class', klass);
var WLXMLDocumentNode = function() {
smartxml.DocumentNode.apply(this, arguments);
-}
+};
WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype);
var WLXMLDocument = function(xml, options) {
ElementNodeFactory: WLXMLElementNode,
loadXML: function(xml) {
smartxml.Document.prototype.loadXML.call(this, xml, {silent: true});
- $(this.dom).find(':not(iframe)').addBack().contents()
+ this.trigger('contentSet');
+ },
+
+ normalizeXML: function(nativeNode) {
+ $(nativeNode).find(':not(iframe)').addBack().contents()
.filter(function() {return this.nodeType === Node.TEXT_NODE;})
.each(function() {
var el = $(this),
}
//}
}
-
+ /* globals document */
el.replaceWith(document.createTextNode(text.transformed));
});
- this.trigger('contentSet');
},
registerClassTransformation: function(Transformation, className) {
- var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || new transformations.TransformationStorage());
- return thisClassTransformations.register(Transformation);
+ var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {});
+ thisClassTransformations[Transformation.prototype.name] = function(args) {
+ var nodeInstance = this;
+ return nodeInstance.transform(Transformation, args);
+ };
},
registerClassMethod: function(methodName, method, className) {
registerExtension: function(extension) {
//debugger;
smartxml.Document.prototype.registerExtension.call(this, extension);
- var doc = this,
- existingPropertyNames = _.values(this);
-
- var getTrans = function(desc, methodName) {
- if(typeof desc === 'function') {
- desc = {impl: desc};
- }
- if(!desc.impl) {
- throw new Error('Got transformation description without implementation.')
- }
- desc.name = desc.name || methodName;
- return desc;
- };
+ var doc = this;
_.pairs(extension.wlxmlClass).forEach(function(pair) {
var className = pair[0],
});
_.pairs(classExtension.transformations || {}).forEach(function(pair) {
- var transformation = getTrans(pair[1], pair[0]);
- doc.registerClassTransformation(transformations.createContextTransformation(transformation), className);
- });
+ var name = pair[0],
+ desc = pair[1];
+ doc.registerClassTransformation(transformations.createContextTransformation(desc, name), className);
+ });
});
}