Skip to content

Commit

Permalink
Merge pull request #1 from ChristianAlexander/master
Browse files Browse the repository at this point in the history
Update fork for fragment support.
  • Loading branch information
amanda-mitchell authored Aug 23, 2016
2 parents 7a3ad9d + cf30ff8 commit 9a851e6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 5 additions & 4 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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++)
Expand All @@ -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 === '&') {
Expand Down Expand Up @@ -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')
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "sax",
"description": "An evented streaming XML parser in JavaScript",
"author": "Isaac Z. Schlueter <[email protected]> (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",
"scripts": {
"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",
Expand Down

0 comments on commit 9a851e6

Please sign in to comment.