Skip to content

Commit

Permalink
Merge pull request #34 from nfarina/handle-doctype
Browse files Browse the repository at this point in the history
Handle "doctype" elements.
  • Loading branch information
nfarina committed Apr 27, 2016
2 parents a52de63 + c05c8e4 commit a21c2ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/xmldoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ function XmlDocument(xml) {
if (!xml)
throw new Error("No XML to parse!");

// Stores doctype (if defined)
this.doctype = "";

// Expose the parser to the other delegates while the parser is running
this.parser = sax.parser(true); // strict
addParserEvents(this.parser);
Expand All @@ -199,7 +202,7 @@ function XmlDocument(xml) {
// parsing; they will push and pop off the stack as we get deeper into the XML hierarchy.
// It's safe to use a global because JS is single-threaded.
delegates = [this];

this.parser.write(xml);

// Remove the parser as it is no longer needed and should not be exposed to clients
Expand All @@ -218,6 +221,10 @@ XmlDocument.prototype._opentag = function(tag) {
XmlElement.prototype._opentag.apply(this,arguments);
};

XmlDocument.prototype._doctype = function(doctype) {
this.doctype += doctype;
}

// file-scoped global stack of delegates
var delegates = null;

Expand All @@ -231,6 +238,7 @@ function addParserEvents(parser) {
parser.ontext = parser_text;
parser.oncdata = parser_cdata;
parser.oncomment = parser_comment;
parser.ondoctype = parser_doctype;
parser.onerror = parser_error;
}

Expand All @@ -240,6 +248,7 @@ function parser_closetag() { delegates[0] && delegates[0]._closetag.apply(delega
function parser_text() { delegates[0] && delegates[0]._text.apply(delegates[0],arguments) }
function parser_cdata() { delegates[0] && delegates[0]._cdata.apply(delegates[0],arguments) }
function parser_comment() { delegates[0] && delegates[0]._comment.apply(delegates[0],arguments) }
function parser_doctype() { delegates[0] && delegates[0]._doctype.apply(delegates[0],arguments) }
function parser_error() { delegates[0] && delegates[0]._error.apply(delegates[0],arguments) }

// a relatively standard extend method
Expand Down
15 changes: 15 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ t.test('cdata handling', function (t) {
t.end();
})

t.test('doctype handling', function (t) {

var docWithType = new XmlDocument('<!DOCTYPE HelloWorld><hello>world</hello>');
t.equal(docWithType.doctype, " HelloWorld");

var docWithoutType = new XmlDocument('<hello>world</hello>');
t.equal(docWithoutType.doctype, "");

t.throws(function() {
new XmlDocument('<hello><!DOCTYPE HelloWorld>world</hello>');
});

t.end();
})

t.test('comment handling', function (t) {

var xmlString = '<hello><!-- World --></hello>';
Expand Down

0 comments on commit a21c2ec

Please sign in to comment.