diff --git a/README.md b/README.md index afcd3f3d..472739c1 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,18 @@ -# sax js +# sax js - with fragments A sax-style parser for XML and HTML. Designed with [node](http://nodejs.org/) in mind, but should work fine in the browser or other CommonJS implementations. +## Note + +This is forked from [https://github.com/isaacs/sax-js](https://github.com/isaacs/sax-js). + +The `fragmentMode` option in the constructor enables functionality similar to [`XmlReaderSettings.ConformanceLevel`](https://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.conformancelevel(v=vs.110).aspx) being set to `System.Xml.ConformanceLevel.Fragment` in C#. + +This allows multiple root elements to exist. + ## What This Is * A very simple tool to parse through an XML string. diff --git a/lib/sax.js b/lib/sax.js index f125c5fe..270bb428 100644 --- a/lib/sax.js +++ b/lib/sax.js @@ -63,6 +63,7 @@ parser.strictEntities = parser.opt.strictEntities parser.ENTITIES = parser.strictEntities ? Object.create(sax.XML_ENTITIES) : Object.create(sax.ENTITIES) parser.attribList = [] + parser.fragmentMode = parser.opt.fragmentMode // namespaces form a prototype chain. // it always points at the current tag, @@ -671,7 +672,7 @@ } function end (parser) { - if (parser.sawRoot && !parser.closedRoot) strictFail(parser, 'Unclosed root tag') + if (parser.sawRoot && !parser.closedRoot && !parser.fragmentMode) strictFail(parser, 'Unclosed root tag') if ((parser.state !== S.BEGIN) && (parser.state !== S.BEGIN_WHITESPACE) && (parser.state !== S.TEXT)) { @@ -1024,7 +1025,7 @@ continue case S.TEXT: - if (parser.sawRoot && !parser.closedRoot) { + if (parser.sawRoot && !parser.closedRoot || parser.fragmentMode) { var starti = i - 1 while (c && c !== '<' && c !== '&') { c = charAt(chunk, i++) @@ -1044,7 +1045,7 @@ parser.state = S.OPEN_WAKA parser.startTagPosition = parser.position } else { - if (not(whitespace, c) && (!parser.sawRoot || parser.closedRoot)) { + if (not(whitespace, c) && (!parser.sawRoot || parser.closedRoot) && !parser.fragmentMode) { strictFail(parser, 'Text data outside of root node.') } if (c === '&') { @@ -1113,7 +1114,7 @@ parser.sgmlDecl = '' } else if ((parser.sgmlDecl + c).toUpperCase() === DOCTYPE) { parser.state = S.DOCTYPE - if (parser.doctype || parser.sawRoot) { + if (parser.doctype || parser.sawRoot && !parser.fragmentMode) { strictFail(parser, 'Inappropriately located doctype declaration') } diff --git a/package.json b/package.json index 6b6e4c86..d5326155 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "sax", - "description": "An evented streaming XML parser in JavaScript", - "author": "Isaac Z. Schlueter (http://blog.izs.me/)", + "name": "sax-with-fragments", + "description": "An evented streaming XML parser in JavaScript, with support for XML fragments.", + "author": "Faithlife Corporation", "version": "1.2.1", "main": "lib/sax.js", "license": "ISC", @@ -9,7 +9,7 @@ "test": "tap test/*.js --cov", "posttest": "standard -F test/*.js lib/*.js" }, - "repository": "git://github.com/isaacs/sax-js.git", + "repository": "git://github.com/Faithlife/sax-js.git", "files": [ "lib/sax.js", "LICENSE",