diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index 4ba23d29..3ee888be 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -5,13 +5,14 @@ var EventEmitter = require('events').EventEmitter var unescapeXML = require('../escape').unescapeXML var STATE_TEXT = 0 -var STATE_IGNORE_TAG = 1 -var STATE_TAG_NAME = 2 -var STATE_TAG = 3 -var STATE_ATTR_NAME = 4 -var STATE_ATTR_EQ = 5 -var STATE_ATTR_QUOT = 6 -var STATE_ATTR_VALUE = 7 +var STATE_IGNORE_COMMENT = 1 +var STATE_IGNORE_INSTRUCTION = 2 +var STATE_TAG_NAME = 3 +var STATE_TAG = 4 +var STATE_ATTR_NAME = 5 +var STATE_ATTR_EQ = 6 +var STATE_ATTR_QUOT = 7 +var STATE_ATTR_VALUE = 8 var SaxLtx = module.exports = function SaxLtx () { EventEmitter.call(this) @@ -60,7 +61,6 @@ var SaxLtx = module.exports = function SaxLtx () { for (; pos < data.length; pos++) { var c = data.charCodeAt(pos) - // console.log("state", state, "c", c, data[pos]) switch (state) { case STATE_TEXT: if (c === 60 /* < */) { @@ -77,18 +77,33 @@ var SaxLtx = module.exports = function SaxLtx () { if (c === 47 /* / */ && recordStart === pos) { recordStart = pos + 1 endTag = true - } else if (c === 33 /* ! */ || c === 63 /* ? */) { + } else if (c === 33 /* ! */) { recordStart = undefined - state = STATE_IGNORE_TAG + state = STATE_IGNORE_COMMENT + } else if (c === 63 /* ? */) { + recordStart = undefined + state = STATE_IGNORE_INSTRUCTION } else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) { tagName = endRecording() pos-- state = STATE_TAG } break - case STATE_IGNORE_TAG: + case STATE_IGNORE_COMMENT: if (c === 62 /* > */) { - state = STATE_TEXT + var prevFirst = data.charCodeAt(pos - 1) + var prevSecond = data.charCodeAt(pos - 2) + if (prevFirst === 45 /* - */ && prevSecond === 45 /* - */) { + state = STATE_TEXT + } + } + break + case STATE_IGNORE_INSTRUCTION: + if (c === 62 /* > */) { + var prev = data.charCodeAt(pos - 1) + if (prev === 63 /* ? */) { + state = STATE_TEXT + } } break case STATE_TAG: diff --git a/test/parse-test.js b/test/parse-test.js index 51fa16ae..2d65bed7 100644 --- a/test/parse-test.js +++ b/test/parse-test.js @@ -156,6 +156,24 @@ parsers.forEach(function (Parser) { xmlns: 'urn:ietf:params:xml:ns:xmpp-sasl' } }) + }, + 'bug: elements in comments': function () { + var parser = new Parser() + var events = [] + parser.on('startElement', function (name, attrs) { + events.push({ start: name, attrs: attrs }) + }) + parser.on('endElement', function (name) { + events.push({ end: name }) + }) + parser.on('comment', function (s) { + events.push({ comment: s }) + }) + parser.write("") + assert.deepEqual(events, [ + { start: 'root', attrs: {} }, + { end: 'root' } + ]) } } }).export(module)