Fixing some xml output formatting problems
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.test3.js
index df95a78..50f03e9 100644 (file)
@@ -1102,6 +1102,26 @@ 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/');
+        });
+
+        it('doesn\' serialize meta attribute if its empty', function() {
+            var c;
+
+            c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
+            c.doc().setWlxmlMetaAttr('uri', '');
+            expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
+
+            c = canvas.fromXML('<section class="uri"></section>');
+            c.doc().setWlxmlMetaAttr('uri', '');
+            expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
+        });
+
         describe('formatting output xml', function() {
             /*it('keeps white spaces at the edges of input xml', function() {
                 var xmlIn = '  <section></section>  ',
@@ -1122,7 +1142,18 @@ describe('Canvas', function() {
                 expect(partsIn).to.deep.equal(partsOut);
             });
 
-            it('nests new children elements', function() {
+            it('keeps white space between XML nodes - inline case', function() {
+                var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
+                c = canvas.fromXML(xmlIn),
+                xmlOut = c.toXML();
+
+                var partsIn = xmlIn.split('\n\n\n'),
+                    partsOut = xmlOut.split('\n\n\n');
+                
+                expect(partsIn).to.deep.equal(partsOut);
+            });
+
+            it('nests new children block elements', function() {
                 var c = canvas.fromXML('<section></section>');
     
                 c.doc().append({tag: 'header'});
@@ -1132,6 +1163,55 @@ describe('Canvas', function() {
                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
 
             });
+
+            it('doesn\'t nest new children inline elements', function() {
+                var c = canvas.fromXML('<section></section>');
+    
+                c.doc().append({tag: 'span'});
+
+                var xmlOut = c.toXML();
+                expect(xmlOut).to.equal('<section><span></span></section>');
+            });
+
+            it('keeps original white space at the end of text', function() {
+                
+                var xmlIn = '<header>    Some text ended with white space \
+                \
+                <span class="uri">Some text</span> some text\
+            \
+            </header>',
+                    c = canvas.fromXML(xmlIn);
+
+            var xmlOut = c.toXML();
+            console.log(xmlOut);
+            expect(xmlOut).to.equal(xmlIn);
+            });
+
+            it('keeps white space around text node', function() {
+                var xmlIn = '<section>\
+                <header>header1</header>\
+                Some text surrounded by white space\
+                <header>header2</header>\
+            </section>',
+                    c = canvas.fromXML(xmlIn);
+
+                var xmlOut = c.toXML();
+                expect(xmlOut).to.equal(xmlIn);
+            });
+
+            it('keeps white space around text node - last node case', function() {
+                var xmlIn = '<section>\
+                <header>header</header>\
+                    \
+                Some text surrounded by white space\
+                    \
+            </section>',
+                    c = canvas.fromXML(xmlIn);
+
+                var xmlOut = c.toXML();
+                expect(xmlOut).to.equal(xmlIn);
+            });
+
         })
     })
 });