first approach to wlxml library
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 3 Oct 2013 14:16:02 +0000 (16:16 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 9 Oct 2013 14:56:55 +0000 (16:56 +0200)
src/smartxml/smartxml.js
src/wlxml/wlxml.js [new file with mode: 0644]
src/wlxml/wlxml.test.js [new file with mode: 0644]
tests/main.js

index 9862fab..96fcdfe 100644 (file)
@@ -71,6 +71,14 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, {
         this._$.attr(name, value);
     },
 
         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);
     },
     append: function(documentNode) {
         this._$.append(documentNode.nativeNode);
     },
diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js
new file mode 100644 (file)
index 0000000..4af5e48
--- /dev/null
@@ -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 (file)
index 0000000..95c230f
--- /dev/null
@@ -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('<section class="class.subclass"></section>');
+        expect(doc.root.getClass()).to.equal('class.subclass');
+    });
+});
+
+});
\ No newline at end of file
index ba4aa38..a05c379 100644 (file)
@@ -13,7 +13,8 @@
 
         paths: {
             'fnpjs': '../fnpjs',
 
         paths: {
             'fnpjs': '../fnpjs',
-            'libs': '../../libs'
+            'libs': '../../libs',
+            'smartxml': '../smartxml'
         },
 
         map: {
         },
 
         map: {