wlxml node: setClass
[fnpeditor.git] / src / wlxml / wlxml.js
1 define([
2     'smartxml/smartxml'
3 ], function(smartxml) {
4     
5 'use strict';
6
7 // utils
8
9 var isMetaAttribute = function(attrName) {
10     return attrName.substr(0, 5) === 'meta-';
11 };
12
13 //
14
15 var WLXMLElementNode = function(nativeNode, document) {
16     smartxml.ElementNode.call(this, nativeNode, document);
17 };
18 WLXMLElementNode.prototype = Object.create(smartxml.ElementNode.prototype);
19
20 $.extend(WLXMLElementNode.prototype, smartxml.ElementNode.prototype, {
21     getClass: function() {
22         return this.getAttr('class');
23     },
24     setClass: function(klass) {
25         return this.setAttr('class', klass);
26     },
27     getMetaAttributes: function() {
28         var toret = [];
29         this.getAttrs().forEach(function(attr) {
30             if(isMetaAttribute(attr.name)) {
31                 toret.push({name: attr.name.substr(5), value: attr.value});
32             }
33         });
34         return toret;
35     },
36     getOtherAttributes: function() {
37         var toret = {};
38         this.getAttrs().forEach(function(attr) {
39             if(attr.name !== 'class' && !isMetaAttribute(attr.name)) {
40                 toret[attr.name] = attr.value;
41             }
42         });
43         return toret;
44     }
45 });
46
47
48 var WLXMLDocument = function(xml) {
49     smartxml.Document.call(this, xml);
50 };
51 WLXMLDocument.prototype = Object.create(smartxml.Document.prototype);
52 $.extend(WLXMLDocument.prototype, {
53     ElementNodeFactory: WLXMLElementNode
54 });
55
56
57 return {
58     WLXMLDocumentFromXML: function(xml) {
59         return new WLXMLDocument(xml);
60     },
61
62     WLXMLElementNodeFromXML: function(xml) {
63         return this.WLXMLDocumentFromXML(xml).root;
64     }
65 };
66
67 });