From: Aleksander Ɓukasz Date: Thu, 3 Oct 2013 14:16:02 +0000 (+0200) Subject: first approach to wlxml library X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/0487914c40881829a526a7a09b6d545f08aa28e7?ds=sidebyside first approach to wlxml library --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 9862fab..96fcdfe 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -71,6 +71,14 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { this._$.attr(name, value); }, + getAttrs: function() { + var toret = []; + for(var i = 0; i < this.nativeNode.attributes.length; i++) { + toret.push(this.nativeNode.attributes[i]); + } + return toret; + }, + append: function(documentNode) { this._$.append(documentNode.nativeNode); }, diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js new file mode 100644 index 0000000..4af5e48 --- /dev/null +++ b/src/wlxml/wlxml.js @@ -0,0 +1,58 @@ +define([ + 'smartxml/smartxml' +], function(smartxml) { + +'use strict'; + +// utils + +var isMetaAttribute = function(attrName) { + return attrName.substr(0, 5) === 'meta-'; +}; + +// + +var WLXMLElementNode = function(nativeNode) { + smartxml.ElementNode.call(this, nativeNode); +}; +WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype); + +$.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, { + getClass: function() { + return this.getAttr('class'); + }, + getMetaAttributes: function() { + var toret = {}; + this.getAttrs().forEach(function(attr) { + if(isMetaAttribute(attr.name)) + meta[attr.name.substr(5)] = attr.value; + }); + return toret; + }, + getOtherAttributes: function() { + var toret = {}; + this.getAttrs().forEach(function(attr) { + if(attr.name != 'class' && !isMetaAttribute(attr.name)) + toret[attr.name] = attr.value; + }); + return toret; + } +}); + + +var WLXMLDocument = function(xml) { + smartxml.Document.call(this, xml); +}; +WLXMLDocument.prototype = Object.create(smartxml.Document.prototype); +$.extend(WLXMLDocument.prototype, { + ElementNodeFactory: WLXMLElementNode +}); + + +return { + WLXMLDocumentFromXML: function(xml) { + return new WLXMLDocument(xml); + } +}; + +}); \ No newline at end of file diff --git a/src/wlxml/wlxml.test.js b/src/wlxml/wlxml.test.js new file mode 100644 index 0000000..95c230f --- /dev/null +++ b/src/wlxml/wlxml.test.js @@ -0,0 +1,18 @@ +define([ + 'libs/chai', + './wlxml.js' +], function(chai, wlxml) { + +'use strict'; + +var expect = chai.expect; + + +describe('how it works', function() { + it('does something', function() { + var doc = wlxml.WLXMLDocumentFromXML('
'); + expect(doc.root.getClass()).to.equal('class.subclass'); + }); +}); + +}); \ No newline at end of file diff --git a/tests/main.js b/tests/main.js index ba4aa38..a05c379 100644 --- a/tests/main.js +++ b/tests/main.js @@ -13,7 +13,8 @@ paths: { 'fnpjs': '../fnpjs', - 'libs': '../../libs' + 'libs': '../../libs', + 'smartxml': '../smartxml' }, map: {