X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/989364fcf105797896a7172a4c9367f6b07848b2..ab30b4d5d52bca4de5a3b29468e09bffd7f76466:/src/wlxml/wlxml.js?ds=inline diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index 9fb6a98..f370223 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -3,14 +3,34 @@ define([ 'libs/underscore', 'smartxml/smartxml', 'smartxml/transformations', - 'wlxml/extensions/metadata/metadata' -], function($, _, smartxml, transformations, metadataExtension) { + 'wlxml/extensions/metadata/metadata', + 'wlxml/extensions/comments/comments' +], function($, _, smartxml, transformations, metadataExtension, commentExtension) { 'use strict'; /* globals Node */ +var WLXMLDocumentNodeMethods = { + isInside: function(query) { + var parent = this.getParent(query); + return !!parent; + }, + getParent: function(query) { + /* globals Node */ + var me = this.nodeType === Node.ELEMENT_NODE ? [this] : [], + toret; + me.concat(this.parents()).some(function(node) { + if(node.is(query)) { + toret = node; + return true; + } + }); + return toret; + }, +}; + var AttributesList = function() {}; AttributesList.prototype = Object.create({}); AttributesList.prototype.keys = function() { @@ -39,7 +59,9 @@ var installObject = function(instance, klass) { }); instance.object = Object.create(_.extend({}, methods, transformations)); _.keys(methods).concat(_.keys(transformations)).forEach(function(key) { - instance.object[key] = _.bind(instance.object[key], instance); + if(_.isFunction(instance.object[key])) { + instance.object[key] = _.bind(instance.object[key], instance); + } }); }; @@ -49,18 +71,30 @@ var WLXMLElementNode = function(nativeNode, document) { }; WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype); -$.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, { +$.extend(WLXMLElementNode.prototype, WLXMLDocumentNodeMethods, smartxml.ElementNode.prototype, { getClass: function() { return this.getAttr('class') || ''; }, + getClassHierarchy: function() { + return getClassLists(this.getClass()); + }, setClass: function(klass) { if(klass !== this.klass) { installObject(this, klass); return this.setAttr('class', klass); } }, - is: function(klass) { - return this.getClass().substr(0, klass.length) === klass; + is: function(query) { + if(typeof query === 'string') { + query = {klass: query}; + } + return (_.isUndefined(query.klass) || this.getClass().substr(0, query.klass.length) === query.klass) && + (_.isUndefined(query.tagName) || this.getTagName() === query.tagName); + }, + hasChild: function(query) { + return this.contents().some(function(child) { + return child.is(query); + }.bind(this)); }, getMetaAttributes: function() { var toret = new AttributesList(), @@ -175,10 +209,19 @@ var WLXMLDocumentNode = function() { }; WLXMLDocumentNode.prototype = Object.create(smartxml.DocumentNode.prototype); + +var WLXMLTextNode = function() { + smartxml.TextNode.apply(this, arguments); +}; +WLXMLTextNode.prototype = Object.create(smartxml.TextNode.prototype); +$.extend(WLXMLTextNode.prototype, WLXMLDocumentNodeMethods, { + is: function() { return false; } +}); + var WLXMLDocument = function(xml, options) { this.classMethods = {}; this.classTransformations = {}; - smartxml.Document.call(this, xml, [metadataExtension]); + smartxml.Document.call(this, xml, [metadataExtension, commentExtension]); this.options = options; }; @@ -188,6 +231,7 @@ var formatter_prefix = '_wlxml_formatter_'; WLXMLDocument.prototype = Object.create(smartxml.Document.prototype); $.extend(WLXMLDocument.prototype, { ElementNodeFactory: WLXMLElementNode, + TextNodeFactory: WLXMLTextNode, loadXML: function(xml) { smartxml.Document.prototype.loadXML.call(this, xml, {silent: true}); this.trigger('contentSet'); @@ -297,8 +341,9 @@ $.extend(WLXMLDocument.prototype, { registerClassTransformation: function(Transformation, className) { var thisClassTransformations = (this.classTransformations[className] = this.classTransformations[className] || {}); - thisClassTransformations[Transformation.prototype.name] = function(args) { + thisClassTransformations[Transformation.prototype.name] = function() { var nodeInstance = this; + var args = Array.prototype.slice.call(arguments, 0); return nodeInstance.transform(Transformation, args); }; }, @@ -342,14 +387,18 @@ var wlxmlClasses = { return { - WLXMLDocumentFromXML: function(xml, options) { + WLXMLDocumentFromXML: function(xml, options, Factory) { options = _.extend({wlxmlClasses: wlxmlClasses}, options); - return new WLXMLDocument(xml, options); + Factory = Factory || WLXMLDocument; + return new Factory(xml, options); }, WLXMLElementNodeFromXML: function(xml) { return this.WLXMLDocumentFromXML(xml).root; - } + }, + + WLXMLDocument: WLXMLDocument, + getClassHierarchy: getClassLists }; }); \ No newline at end of file