From 6ec0417ada4ce5a86b7dc24c31906e786dfbbfd0 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Wed, 2 Oct 2019 02:34:37 +0200 Subject: [PATCH] fix cdata issue #132 (#133) --- lib/parsers/ltx.js | 10 +++++++++- test/cdata-test.js | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index 9cf1554c..ea5f86a6 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -14,6 +14,7 @@ var STATE_ATTR_EQ = 6 var STATE_ATTR_QUOT = 7 var STATE_ATTR_VALUE = 8 var STATE_CDATA = 9 +var STATE_IGNORE_CDATA = 10 var SaxLtx = module.exports = function SaxLtx () { EventEmitter.call(this) @@ -83,6 +84,13 @@ var SaxLtx = module.exports = function SaxLtx () { if (endcomment !== -1) { pos = endcomment + 2 // target the '>' character } + } else if (state === STATE_IGNORE_CDATA) { + // if we're looping through a CDATA, fast-forward using + // indexOf to the first end-CDATA character ]]> + var endCDATA = data.indexOf(']]>', pos) + if (endCDATA !== -1) { + pos = endCDATA + 2 // target the '>' character + } } var c = data.charCodeAt(pos) @@ -104,7 +112,7 @@ var SaxLtx = module.exports = function SaxLtx () { if (cData) { this.emit('text', cData) } - state = STATE_IGNORE_COMMENT + state = STATE_TEXT } break case STATE_TAG_NAME: diff --git a/test/cdata-test.js b/test/cdata-test.js index 9c5b74e6..598358f5 100644 --- a/test/cdata-test.js +++ b/test/cdata-test.js @@ -4,6 +4,7 @@ var vows = require('vows') var assert = require('assert') var ltx = require('..') var parsers = require('../lib/parsers') +var h = ltx.createElement var Parser = parsers.find(function (parser) { return (parser.name === 'SaxLtx') @@ -24,6 +25,10 @@ vows.describe('sax_ltx').addBatch({ var el = parse('') assert.strictEqual(el.name, 'root') assert.strictEqual(el.getText(), 'Content & "more content"') + }, + 'issue-132': () => { + var el = parse('&d;]]>') + assert.deepStrictEqual(el, h('a', null, h('b', null, ''), h('b', null, '-->&d;'))) } } }).export(module)