sax-with-fragments
Advanced tools
Comparing version 1.2.1 to 1.3.0
126
lib/sax.js
@@ -168,2 +168,3 @@ ;(function (sax) { // wrapper for non-node envs | ||
} | ||
if (!Stream) Stream = function () {} | ||
@@ -267,13 +268,4 @@ var streamWraps = sax.EVENTS.filter(function (ev) { | ||
// character classes and tokens | ||
var whitespace = '\r\n\t ' | ||
// this really needs to be replaced with character classes. | ||
// XML allows all manner of ridiculous numbers and digits. | ||
var number = '0124356789' | ||
var letter = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
// (Letter | "_" | ":") | ||
var quote = '\'"' | ||
var attribEnd = whitespace + '>' | ||
var CDATA = '[CDATA[' | ||
@@ -285,7 +277,2 @@ var DOCTYPE = 'DOCTYPE' | ||
// turn all the string character sets into character class objects. | ||
whitespace = charClass(whitespace) | ||
number = charClass(number) | ||
letter = charClass(letter) | ||
// http://www.w3.org/TR/REC-xml/#NT-NameStartChar | ||
@@ -299,27 +286,25 @@ // This implementation works on strings, a single character at a time | ||
var nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040\.\d-]/ | ||
var nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/ | ||
var entityStart = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/ | ||
var entityBody = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040\.\d-]/ | ||
var entityBody = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/ | ||
quote = charClass(quote) | ||
attribEnd = charClass(attribEnd) | ||
function isWhitespace (c) { | ||
return c === ' ' || c === '\n' || c === '\r' || c === '\t' | ||
} | ||
function charClass (str) { | ||
return str.split('').reduce(function (s, c) { | ||
s[c] = true | ||
return s | ||
}, {}) | ||
function isQuote (c) { | ||
return c === '"' || c === '\'' | ||
} | ||
function isRegExp (c) { | ||
return Object.prototype.toString.call(c) === '[object RegExp]' | ||
function isAttribEnd (c) { | ||
return c === '>' || isWhitespace(c) | ||
} | ||
function is (charclass, c) { | ||
return isRegExp(charclass) ? !!c.match(charclass) : charclass[c] | ||
function isMatch (regex, c) { | ||
return regex.test(c) | ||
} | ||
function not (charclass, c) { | ||
return !is(charclass, c) | ||
function notMatch (regex, c) { | ||
return !isMatch(regex, c) | ||
} | ||
@@ -669,4 +654,4 @@ | ||
er += '\nLine: ' + parser.line + | ||
'\nColumn: ' + parser.column + | ||
'\nChar: ' + parser.c | ||
'\nColumn: ' + parser.column + | ||
'\nChar: ' + parser.c | ||
} | ||
@@ -958,3 +943,3 @@ er = new Error(er) | ||
entity = entity.replace(/^0+/, '') | ||
if (numStr.toLowerCase() !== entity) { | ||
if (isNaN(num) || numStr.toLowerCase() !== entity) { | ||
strictFail(parser, 'Invalid character entity') | ||
@@ -971,3 +956,3 @@ return '&' + parser.entity + ';' | ||
parser.startTagPosition = parser.position | ||
} else if (not(whitespace, c)) { | ||
} else if (!isWhitespace(c)) { | ||
// have to process this as a text node. | ||
@@ -1009,5 +994,7 @@ // weird, but happens. | ||
parser.c = c | ||
if (!c) { | ||
break | ||
} | ||
if (parser.trackPosition) { | ||
@@ -1022,2 +1009,3 @@ parser.position++ | ||
} | ||
switch (parser.state) { | ||
@@ -1057,3 +1045,3 @@ case S.BEGIN: | ||
} else { | ||
if (not(whitespace, c) && (!parser.sawRoot || parser.closedRoot) && !parser.fragmentMode) { | ||
if (!isWhitespace(c) && (!parser.sawRoot || parser.closedRoot) && !parser.fragmentMode) { | ||
strictFail(parser, 'Text data outside of root node.') | ||
@@ -1092,5 +1080,5 @@ } | ||
parser.sgmlDecl = '' | ||
} else if (is(whitespace, c)) { | ||
} else if (isWhitespace(c)) { | ||
// wait for it... | ||
} else if (is(nameStart, c)) { | ||
} else if (isMatch(nameStart, c)) { | ||
parser.state = S.OPEN_TAG | ||
@@ -1138,3 +1126,3 @@ parser.tagName = c | ||
parser.state = S.TEXT | ||
} else if (is(quote, c)) { | ||
} else if (isQuote(c)) { | ||
parser.state = S.SGML_DECL_QUOTED | ||
@@ -1164,3 +1152,3 @@ parser.sgmlDecl += c | ||
parser.state = S.DOCTYPE_DTD | ||
} else if (is(quote, c)) { | ||
} else if (isQuote(c)) { | ||
parser.state = S.DOCTYPE_QUOTED | ||
@@ -1184,3 +1172,3 @@ parser.q = c | ||
parser.state = S.DOCTYPE | ||
} else if (is(quote, c)) { | ||
} else if (isQuote(c)) { | ||
parser.state = S.DOCTYPE_DTD_QUOTED | ||
@@ -1269,3 +1257,3 @@ parser.q = c | ||
parser.state = S.PROC_INST_ENDING | ||
} else if (is(whitespace, c)) { | ||
} else if (isWhitespace(c)) { | ||
parser.state = S.PROC_INST_BODY | ||
@@ -1278,3 +1266,3 @@ } else { | ||
case S.PROC_INST_BODY: | ||
if (!parser.procInstBody && is(whitespace, c)) { | ||
if (!parser.procInstBody && isWhitespace(c)) { | ||
continue | ||
@@ -1303,3 +1291,3 @@ } else if (c === '?') { | ||
case S.OPEN_TAG: | ||
if (is(nameBody, c)) { | ||
if (isMatch(nameBody, c)) { | ||
parser.tagName += c | ||
@@ -1313,3 +1301,3 @@ } else { | ||
} else { | ||
if (not(whitespace, c)) { | ||
if (!isWhitespace(c)) { | ||
strictFail(parser, 'Invalid character in tag name') | ||
@@ -1334,3 +1322,3 @@ } | ||
// haven't read the attribute name yet. | ||
if (is(whitespace, c)) { | ||
if (isWhitespace(c)) { | ||
continue | ||
@@ -1341,3 +1329,3 @@ } else if (c === '>') { | ||
parser.state = S.OPEN_TAG_SLASH | ||
} else if (is(nameStart, c)) { | ||
} else if (isMatch(nameStart, c)) { | ||
parser.attribName = c | ||
@@ -1359,5 +1347,5 @@ parser.attribValue = '' | ||
openTag(parser) | ||
} else if (is(whitespace, c)) { | ||
} else if (isWhitespace(c)) { | ||
parser.state = S.ATTRIB_NAME_SAW_WHITE | ||
} else if (is(nameBody, c)) { | ||
} else if (isMatch(nameBody, c)) { | ||
parser.attribName += c | ||
@@ -1372,3 +1360,3 @@ } else { | ||
parser.state = S.ATTRIB_VALUE | ||
} else if (is(whitespace, c)) { | ||
} else if (isWhitespace(c)) { | ||
continue | ||
@@ -1386,3 +1374,3 @@ } else { | ||
openTag(parser) | ||
} else if (is(nameStart, c)) { | ||
} else if (isMatch(nameStart, c)) { | ||
parser.attribName = c | ||
@@ -1398,5 +1386,5 @@ parser.state = S.ATTRIB_NAME | ||
case S.ATTRIB_VALUE: | ||
if (is(whitespace, c)) { | ||
if (isWhitespace(c)) { | ||
continue | ||
} else if (is(quote, c)) { | ||
} else if (isQuote(c)) { | ||
parser.q = c | ||
@@ -1426,3 +1414,3 @@ parser.state = S.ATTRIB_VALUE_QUOTED | ||
case S.ATTRIB_VALUE_CLOSED: | ||
if (is(whitespace, c)) { | ||
if (isWhitespace(c)) { | ||
parser.state = S.ATTRIB | ||
@@ -1433,3 +1421,3 @@ } else if (c === '>') { | ||
parser.state = S.OPEN_TAG_SLASH | ||
} else if (is(nameStart, c)) { | ||
} else if (isMatch(nameStart, c)) { | ||
strictFail(parser, 'No whitespace between attributes') | ||
@@ -1445,3 +1433,3 @@ parser.attribName = c | ||
case S.ATTRIB_VALUE_UNQUOTED: | ||
if (not(attribEnd, c)) { | ||
if (!isAttribEnd(c)) { | ||
if (c === '&') { | ||
@@ -1464,5 +1452,5 @@ parser.state = S.ATTRIB_VALUE_ENTITY_U | ||
if (!parser.tagName) { | ||
if (is(whitespace, c)) { | ||
if (isWhitespace(c)) { | ||
continue | ||
} else if (not(nameStart, c)) { | ||
} else if (notMatch(nameStart, c)) { | ||
if (parser.script) { | ||
@@ -1479,3 +1467,3 @@ parser.script += '</' + c | ||
closeTag(parser) | ||
} else if (is(nameBody, c)) { | ||
} else if (isMatch(nameBody, c)) { | ||
parser.tagName += c | ||
@@ -1487,3 +1475,3 @@ } else if (parser.script) { | ||
} else { | ||
if (not(whitespace, c)) { | ||
if (!isWhitespace(c)) { | ||
strictFail(parser, 'Invalid tagname in closing tag') | ||
@@ -1496,3 +1484,3 @@ } | ||
case S.CLOSE_TAG_SAW_WHITE: | ||
if (is(whitespace, c)) { | ||
if (isWhitespace(c)) { | ||
continue | ||
@@ -1530,6 +1518,13 @@ } | ||
if (c === ';') { | ||
parser[buffer] += parseEntity(parser) | ||
parser.entity = '' | ||
parser.state = returnState | ||
} else if (is(parser.entity.length ? entityBody : entityStart, c)) { | ||
if (parser.opt.unparsedEntities) { | ||
var parsedEntity = parseEntity(parser) | ||
parser.entity = '' | ||
parser.state = returnState | ||
parser.write(parsedEntity) | ||
} else { | ||
parser[buffer] += parseEntity(parser) | ||
parser.entity = '' | ||
parser.state = returnState | ||
} | ||
} else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) { | ||
parser.entity += c | ||
@@ -1545,4 +1540,5 @@ } else { | ||
default: | ||
default: /* istanbul ignore next */ { | ||
throw new Error(parser, 'Unknown state: ' + parser.state) | ||
} | ||
} | ||
@@ -1558,2 +1554,3 @@ } // while | ||
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */ | ||
/* istanbul ignore next */ | ||
if (!String.fromCodePoint) { | ||
@@ -1600,2 +1597,3 @@ (function () { | ||
} | ||
/* istanbul ignore next */ | ||
if (Object.defineProperty) { | ||
@@ -1602,0 +1600,0 @@ Object.defineProperty(String, 'fromCodePoint', { |
@@ -5,8 +5,10 @@ { | ||
"author": "Faithlife Corporation", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"main": "lib/sax.js", | ||
"license": "ISC", | ||
"scripts": { | ||
"test": "tap test/*.js --cov", | ||
"posttest": "standard -F test/*.js lib/*.js" | ||
"test": "tap test/*.js --cov -j4", | ||
"preversion": "npm test", | ||
"postversion": "npm publish", | ||
"postpublish": "git push origin --all; git push origin --tags" | ||
}, | ||
@@ -20,5 +22,10 @@ "repository": "git://github.com/Faithlife/sax-js.git", | ||
"devDependencies": { | ||
"standard": "^5.3.1", | ||
"tap": "^5.2.0" | ||
"tap": "^15.1.6" | ||
}, | ||
"tap": { | ||
"statements": 79, | ||
"branches": 75, | ||
"functions": 80, | ||
"lines": 79 | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55570
1
1446
1