From c12e4f26f60ec54788ebf8491e133f6639897218 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 3 Nov 2016 19:46:22 +0100 Subject: [PATCH 1/5] add test case for bug #101 --- lib/parsers/ltx.js | 2 +- test/parse-test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index 4ba23d29..129af8d0 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -60,7 +60,7 @@ var SaxLtx = module.exports = function SaxLtx () { for (; pos < data.length; pos++) { var c = data.charCodeAt(pos) - // console.log("state", state, "c", c, data[pos]) + console.log("state", state, "c", c, data[pos]) switch (state) { case STATE_TEXT: if (c === 60 /* < */) { diff --git a/test/parse-test.js b/test/parse-test.js index 51fa16ae..ba708259 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) From b3fcb196192a8ac7c63b231aa0d95c527458b697 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Yadav Date: Thu, 3 Nov 2016 19:29:21 +0530 Subject: [PATCH 2/5] fix bug if comment contains some other tag --- lib/parsers/ltx.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index 129af8d0..ac57f0e5 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -88,7 +88,11 @@ var SaxLtx = module.exports = function SaxLtx () { break case STATE_IGNORE_TAG: if (c === 62 /* > */) { - state = STATE_TEXT + var prev_first = data.charCodeAt(pos-1); + var prev_second = data.charCodeAt(pos-2); + if( prev_first === 45 /* - */ && prev_second === 45 /* - */){ + state = STATE_TEXT + } } break case STATE_TAG: From 0c19706a6d688adf8ffb4271701d4273caa5d8e8 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Yadav Date: Thu, 3 Nov 2016 19:32:15 +0530 Subject: [PATCH 3/5] followed coding standards --- lib/parsers/ltx.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index ac57f0e5..fda91041 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -88,9 +88,9 @@ var SaxLtx = module.exports = function SaxLtx () { break case STATE_IGNORE_TAG: if (c === 62 /* > */) { - var prev_first = data.charCodeAt(pos-1); - var prev_second = data.charCodeAt(pos-2); - if( prev_first === 45 /* - */ && prev_second === 45 /* - */){ + var prev_first = data.charCodeAt(pos-1) + var prev_second = data.charCodeAt(pos-2) + if( prev_first === 45 /* - */ && prev_second === 45 /* - */) { state = STATE_TEXT } } From a501732daabd2bd8fb4f789bcd17ab3aa4615aa1 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 3 Nov 2016 20:00:06 +0100 Subject: [PATCH 4/5] split STATE_IGNORE_TAG into proper STATE_IGNORE_COMMENT + STATE_IGNORE_INSTRUCTION Fixes #101 --- lib/parsers/ltx.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index fda91041..d264b663 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,20 +77,32 @@ 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 /* ? */) { + console.log('?') + 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 /* > */) { var prev_first = data.charCodeAt(pos-1) var prev_second = data.charCodeAt(pos-2) - if( prev_first === 45 /* - */ && prev_second === 45 /* - */) { + if (prev_first === 45 /* - */ && prev_second === 45 /* - */) { + state = STATE_TEXT + } + } + break + case STATE_IGNORE_INSTRUCTION: + if (c === 62 /* > */) { + var prev = data.charCodeAt(pos-1) + if (prev === 63 /* ? */) { state = STATE_TEXT } } From f293e982b69bb73d1a8d38bbd824e326039bae9b Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Fri, 4 Nov 2016 23:23:58 +0100 Subject: [PATCH 5/5] fix coding style --- lib/parsers/ltx.js | 15 +++++++-------- test/parse-test.js | 10 +++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index d264b663..3ee888be 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -81,7 +81,6 @@ var SaxLtx = module.exports = function SaxLtx () { recordStart = undefined state = STATE_IGNORE_COMMENT } else if (c === 63 /* ? */) { - console.log('?') recordStart = undefined state = STATE_IGNORE_INSTRUCTION } else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) { @@ -92,18 +91,18 @@ var SaxLtx = module.exports = function SaxLtx () { break case STATE_IGNORE_COMMENT: if (c === 62 /* > */) { - var prev_first = data.charCodeAt(pos-1) - var prev_second = data.charCodeAt(pos-2) - if (prev_first === 45 /* - */ && prev_second === 45 /* - */) { - 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 + var prev = data.charCodeAt(pos - 1) + if (prev === 63 /* ? */) { + state = STATE_TEXT } } break diff --git a/test/parse-test.js b/test/parse-test.js index ba708259..2d65bed7 100644 --- a/test/parse-test.js +++ b/test/parse-test.js @@ -169,11 +169,11 @@ parsers.forEach(function (Parser) { parser.on('comment', function (s) { events.push({ comment: s }) }) - parser.write(""); - assert.deepEqual(events, [ - { start: 'root' , attrs: {} }, - { end: 'root' } - ]); + parser.write("") + assert.deepEqual(events, [ + { start: 'root', attrs: {} }, + { end: 'root' } + ]) } } }).export(module)