Skip to content

Commit

Permalink
fix: just one error for text before the root, and text after
Browse files Browse the repository at this point in the history
Previously the parser would raise an error for
every... single... non-whitespace... character that appeared outside
the root. That's too much. We now raise the error at most once before
the root, and at most once after.
  • Loading branch information
lddubeau committed Jun 30, 2018
1 parent c03c7d0 commit 101ea50
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
19 changes: 15 additions & 4 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
parser.closed = parser.closedRoot = parser.sawRoot = false
parser.tag = parser.error = null
parser.state = S.BEGIN
parser.ENTITIES = Object.create(sax.XML_ENTITIES);
parser.ENTITIES = Object.create(sax.XML_ENTITIES)
parser.attribList = []

// namespaces form a prototype chain.
Expand Down Expand Up @@ -323,7 +323,7 @@
ATTRIB_VALUE_ENTITY_Q: S++, // <foo bar="&quot;"
ATTRIB_VALUE_ENTITY_U: S++, // <foo bar=&quot
CLOSE_TAG: S++, // </a
CLOSE_TAG_SAW_WHITE: S++, // </a >
CLOSE_TAG_SAW_WHITE: S++ // </a >
}

sax.XML_ENTITIES = {
Expand Down Expand Up @@ -713,8 +713,19 @@
parser.state = S.OPEN_WAKA
parser.startTagPosition = parser.position
} else {
if (!isWhitespace(c) && (!parser.sawRoot || parser.closedRoot)) {
fail(parser, 'Text data outside of root node.')
if (!isWhitespace(c)) {
// We use the reportedTextBeforeRoot and
// reportedTextAfterRoot flags to avoid reporting errors
// for every single character that is out of place.
if (!parser.sawRoot && !parser.reportedTextBeforeRoot) {
fail(parser, 'Text data outside of root node.')
parser.reportedTextBeforeRoot = true
}

if (parser.closedRoot && !parser.reportedTextAfterRoot) {
fail(parser, 'Text data outside of root node.')
parser.reportedTextAfterRoot = true
}
}
if (c === '&') {
parser.state = S.TEXT_ENTITY
Expand Down
12 changes: 2 additions & 10 deletions test/issue-86.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@ require(__dirname).test({
],
[
'text',
'd'
],
[
'error',
'Text data outside of root node.\nLine: 0\nColumn: 18\nChar: e'
],
[
'text',
'e'
'de'
],
[
'error',
'Unexpected end\nLine: 0\nColumn: 20\nChar: '
]
],
],
opt: {}
})
12 changes: 1 addition & 11 deletions test/trailing-non-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@ require(__dirname).test({
Line: 0\n\
Column: 23\n\
Char: t'],
['text', 't'],
['error', 'Text data outside of root node.\n\
Line: 0\n\
Column: 24\n\
Char: o'],
['text', 'o '],
['error', 'Text data outside of root node.\n\
Line: 0\n\
Column: 26\n\
Char: m'],
['text', 'm'],
['text', 'to monkey land'],
['end'],
['ready']
],
Expand Down

0 comments on commit 101ea50

Please sign in to comment.