Keeping xmlns and other attributes intact
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 18 Jul 2013 13:05:48 +0000 (15:05 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 18 Jul 2013 13:05:48 +0000 (15:05 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 8905d3e..079c229 100644 (file)
@@ -24,17 +24,20 @@ $.extend(Canvas.prototype, {
                 if(currentTag.attr('wlxml-tag'))
                     return;
 
-                var meta = {};
+                var meta = {}, others = {};
                 for(var i = 0; i < this.attributes.length; i++) {
                     var attr = this.attributes[i];
                     if(attr.name.substr(0, 5) === 'meta-')
                         meta[attr.name.substr(5)] = attr.value;
+                    else if(attr.name !== 'class')
+                        others[attr.name] = attr.value;
                 }
 
                 var element = documentElement.DocumentNodeElement.create({
                     tag: currentTag.prop('tagName').toLowerCase(),
                     klass: currentTag.attr('class'),
-                    meta: meta
+                    meta: meta,
+                    others: others
                 });
 
                 element.dom().append(currentTag.contents());
index a68e66b..76ece55 100644 (file)
@@ -1102,6 +1102,15 @@ describe('Canvas', function() {
             expect(input.isEqualNode(output)).to.be.true;
         });
 
+        it('keeps arbitrary node attributes intact', function() {
+            var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
+                $xmlOut = $(canvas.fromXML(xmlIn).toXML());
+
+            expect($xmlOut.attr('a')).to.equal('1');
+            expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
+
+        });
+
         describe('formatting output xml', function() {
             /*it('keeps white spaces at the edges of input xml', function() {
                 var xmlIn = '  <section></section>  ',
index f5a022b..9c2749e 100644 (file)
@@ -122,6 +122,7 @@ $.extend(DocumentNodeElement, {
                 dom.attr('wlxml-meta-'+key, params.meta[key]);
             });
         }
+        dom.data('other-attrs', params.others);
         return dom;
     },
 
@@ -164,6 +165,9 @@ $.extend(DocumentNodeElement.prototype, {
         meta.forEach(function(attr) {
             node.attr('meta-' + attr.name, attr.value);
         });
+        _.keys(this.data('other-attrs') || {}).forEach(function(key) {
+            node.attr(key, this.data('other-attrs')[key]);
+        }, this);
 
         if(this.data('orig-before') !== undefined) {
             toret.append(document.createTextNode(this.data('orig-before')));