Skip to content

Commit

Permalink
Pedantic code style updates as suggested by IntelliJ [InspectionJS]
Browse files Browse the repository at this point in the history
nfarina committed Mar 16, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3d13441 commit 3bf4f6b
Showing 3 changed files with 40 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Ignore dependencies
node_modules

# IntelliJ
.idea
*.iml
7 changes: 4 additions & 3 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
var XmlDocument = require('../lib/xmldoc').XmlDocument;

// Demonstrate parsing an in-memory XML string
var xmlString = '<suggestions><book title="Twilight"/><book title="Twister"/></suggestions>'
var xmlString = '<suggestions><book title="Twilight"/><book title="Twister"/></suggestions>';

var suggestions = new XmlDocument(xmlString);

@@ -18,7 +18,7 @@ suggestions.eachChild(function(book) {

// Now load an XML file from disk and parse it
var fs = require('fs'),
path = require('path')
path = require('path');

fs.readFile(path.join(__dirname, "test.xml"), 'utf8', function (err,data) {

@@ -47,9 +47,10 @@ fs.readFile(path.join(__dirname, "test.xml"), 'utf8', function (err,data) {
console.log("Found %s books.", allBooks.length);

// Search for a particular book
twilight = books.childWithAttribute("isbn","478-2-23-765712-2");
var twilight = books.childWithAttribute("isbn","478-2-23-765712-2");

// Result is a single XmlElement instance for <book>
console.log("Title of book with given ISBN: '%s'", twilight.valueWithPath("title"));

return null;
});
54 changes: 32 additions & 22 deletions lib/xmldoc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(function () {

// global on the server, window in the browser
var sax, root = this;
var sax;

if (typeof module !== 'undefined' && module.exports) {
// We're being used in a Node-like environment
sax = require('sax');
root = module.exports;
}
else {
sax = root.sax;
// assume it's attached to the Window object in a browser
sax = this.sax;

if (!sax) // no sax for you!
throw new Error("Expected sax to be defined. Make sure you're including sax.js before this file.");
}
@@ -39,33 +40,34 @@ XmlElement.prototype._opentag = function(tag) {
this.lastChild = child;

delegates.unshift(child);
}
};

XmlElement.prototype._closetag = function() {
delegates.shift();
}
};

XmlElement.prototype._text = function(text) {
if (text) this.val += text;
}
};

XmlElement.prototype._cdata = function(cdata) {
if (cdata) this.val += cdata;
}
};

// Useful functions

XmlElement.prototype.eachChild = function(iterator, context) {
for (var i=0, l=this.children.length; i<l; i++)
if (iterator.call(context, this.children[i], i, this.children) === false) return;
}
};

XmlElement.prototype.childNamed = function(name) {
for (var i=0, l=this.children.length; i<l; i++) {
var child = this.children[i];
if (child.name === name) return child;
}
}
return null;
};

XmlElement.prototype.childrenNamed = function(name) {
var matches = [];
@@ -75,15 +77,16 @@ XmlElement.prototype.childrenNamed = function(name) {
matches.push(this.children[i]);

return matches;
}
};

XmlElement.prototype.childWithAttribute = function(name,value) {
for (var i=0, l=this.children.length; i<l; i++) {
var child = this.children[i];
if ( (value && child.attr[name] === value) || (!value && child.attr[name]) )
return child;
}
}
return null;
};

XmlElement.prototype.descendantWithPath = function(path) {
var descendant = this;
@@ -96,27 +99,30 @@ XmlElement.prototype.descendantWithPath = function(path) {
return undefined;

return descendant;
}
};

XmlElement.prototype.valueWithPath = function(path) {
var components = path.split('@');
var descendant = this.descendantWithPath(components[0]);
if (descendant)
return components.length > 1 ? descendant.attr[components[1]] : descendant.val;
}
else
return null;
};

// String formatting (for debugging)

XmlElement.prototype.toString = function(options) {
return this.toStringWithIndent("", options);
}
};

XmlElement.prototype.toStringWithIndent = function(indent, options) {
var s = indent + "<" + this.name;
var linebreak = options && options.compressed ? "" : "\n";

for (var name in this.attr)
s += " " + name + '="' + this.attr[name] + '"';
if (Object.prototype.hasOwnProperty.call(this.attr, name))
s += " " + name + '="' + this.attr[name] + '"';

var finalVal = this.val.trim();

@@ -142,7 +148,7 @@ XmlElement.prototype.toStringWithIndent = function(indent, options) {
else s += "/>";

return s;
}
};

/*
XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy
@@ -153,9 +159,9 @@ function XmlDocument(xml) {
xml && (xml = xml.toString().trim());

if (!xml)
throw new Error("No XML to parse!")
throw new Error("No XML to parse!");

var parser = sax.parser(true) // strict
var parser = sax.parser(true); // strict
addParserEvents(parser);

// We'll use the file-scoped "delegates" var to remember what elements we're currently
@@ -176,7 +182,7 @@ XmlDocument.prototype._opentag = function(tag) {
else
// all other tags will be the root element's children
XmlElement.prototype._opentag.apply(this,arguments);
}
};

// file-scoped global stack of delegates
var delegates = null;
@@ -205,6 +211,10 @@ function extend(destination, source) {
destination[prop] = source[prop];
}

root.XmlDocument = XmlDocument;
// Are we being used in a Node-like environment?
if (typeof module !== 'undefined' && module.exports)
module.exports.XmlDocument = XmlDocument;
else
this.XmlDocument = XmlDocument;

})()
})();

0 comments on commit 3bf4f6b

Please sign in to comment.