Skip to content

Commit

Permalink
Merge pull request #102 from node-xmpp/masterback
Browse files Browse the repository at this point in the history
fix #100
  • Loading branch information
sonnyp authored Nov 4, 2016
2 parents 4a515c7 + f293e98 commit 56a1d7e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
39 changes: 27 additions & 12 deletions lib/parsers/ltx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 /* < */) {
Expand All @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions test/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<?xml version='1.0'?><!-- <foo></foo><bar></bar> --><root></root>")
assert.deepEqual(events, [
{ start: 'root', attrs: {} },
{ end: 'root' }
])
}
}
}).export(module)
Expand Down

0 comments on commit 56a1d7e

Please sign in to comment.