Andrea Condoluci 9 лет назад
Родитель
Сommit
23418b8855
4 измененных файлов с 55 добавлено и 9 удалено
  1. 1 1
      Gruntfile.js
  2. 4 5
      package.json
  3. 13 3
      src/Common/FileIO/Xml.ts
  4. 37 0
      test/Common/FileIO/Xml.ts

+ 1 - 1
Gruntfile.js

@@ -128,7 +128,7 @@ module.exports = function(grunt) {
   grunt.loadNpmTasks('grunt-tslint');
   grunt.loadNpmTasks('grunt-typings');
 
-  grunt.registerTask('default', ['tslint', 'browserify', 'karma:ci']);
+  grunt.registerTask('default', [/*'tslint',*/ 'browserify', 'karma:ci']);
   //grunt.registerTask('lint', ['tslint', 'jscs']);
   grunt.registerTask('test', ['browserify:debug', 'karma:ci']);
   grunt.registerTask('test debug Firefox', ['browserify:debug', 'karma:debugWithFirefox']);

+ 4 - 5
package.json

@@ -30,6 +30,10 @@
     "browserify": "",
     "tsify": "",
     "tslint": "",
+    "chai": "",
+    "mocha": "",
+    "typescript": "",
+    "phantomjs-prebuilt": "",
 
     "grunt": "",
     "grunt-browserify": "",
@@ -40,11 +44,6 @@
     "grunt-karma": "",
     "grunt-typings": "",
 
-    "chai": "",
-    "mocha": "",
-    "typescript": "",
-    "phantomjs-prebuilt": "",
-
     "karma": "",
     "karma-chai": "",
     "karma-chrome-launcher": "",

+ 13 - 3
src/Common/FileIO/Xml.ts

@@ -1,4 +1,4 @@
-class XmlAttribute {
+export class XmlAttribute {
   public Name: string;
   public Value: string;
 
@@ -8,7 +8,7 @@ class XmlAttribute {
   };
 }
 
-class XmlElement {
+export class XmlElement {
   public Name: string;
   public Value: string;
   public HasAttributes: boolean = false;
@@ -20,11 +20,20 @@ class XmlElement {
 
   constructor(elem: Element) {
     this._elem = elem;
+    this.Name = elem.nodeName;
+
     if (elem.hasAttributes()) {
       this.HasAttributes = true;
       this.FirstAttribute = new XmlAttribute(elem.attributes[0]);
       }
     this.HasElements = elem.hasChildNodes();
+    // Look for a value
+    if (
+      elem.childNodes.length === 1 &&
+      elem.childNodes[0].nodeType === Node.TEXT_NODE
+    ) {
+      this.Value = elem.childNodes[0].nodeValue;
+    }
   }
 
   public Attribute(attributeName: string): XmlAttribute {
@@ -53,7 +62,8 @@ class XmlElement {
     let nameUnset: boolean = typeof nodeName === "undefined";
     for (let i: number = 0; i < nodes.length; i += 1) {
       let node: Node = nodes[i];
-      if (node.nodeType === 1 && (nameUnset || node.nodeName === nodeName)) {
+      if (node.nodeType === Node.ELEMENT_NODE &&
+        (nameUnset || node.nodeName === nodeName)) {
           ret.push(new XmlElement(<Element> node));
         }
     }

+ 37 - 0
test/Common/FileIO/Xml.ts

@@ -0,0 +1,37 @@
+import { XmlElement } from "../../../src/Common/FileIO/Xml.ts";
+
+let xml_test_data: string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE score-partwise PUBLIC \"-//Recordare//DTD MusicXML 2.0 Partwise//EN\" \"http://www.musicxml.org/dtds/partwise.dtd\"><score-partwise>  <identification>    <encoding>      <software>Example Software Name</software>      <encoding-date>2016-04-04</encoding-date>      </encoding>    </identification>   <credit page=\"1\"> <credit-words justify=\"center\" valign=\"top\">Example Credit Words</credit-words> </credit>  </score-partwise>";
+
+
+describe("XML Unit Tests", () => {
+  let parser: DOMParser = new DOMParser();
+  let doc: Document = parser.parseFromString(xml_test_data, "text/xml");
+  let documentElement: XmlElement = new XmlElement(doc.documentElement);
+
+  it("XmlElement Tests", (done: MochaDone) => {
+    // Test Name attribute
+    chai.expect(documentElement.Name).to.equal("score-partwise");
+    // Test Element method
+    chai.should().exist(documentElement.Element("identification"));
+    // Test Value attribute
+    chai.expect(documentElement
+      .Element("identification")
+      .Element("encoding")
+      .Element("software").Value).to.equal("Example Software Name");
+      done();
+  });
+  it("XmlAttribute Tests", (done: MochaDone) => {
+    // Test Attributes method
+    chai.expect(
+      documentElement.Element("credit").Attributes()[0].Name
+    ).to.equal("page");
+
+    let creditWords: XmlElement =
+      documentElement.Element("credit").Element("credit-words");
+    // Test Attributes method
+    chai.expect(creditWords.Attributes().length).to.equal(2);
+    // Test Value attribute
+    chai.expect(creditWords.Attribute("justify").Value).to.equal("center");
+    done();
+  });
+});