diff --git a/lib/parsers/ltx.js b/lib/parsers/ltx.js index 3ee888be..5548b172 100644 --- a/lib/parsers/ltx.js +++ b/lib/parsers/ltx.js @@ -13,6 +13,7 @@ var STATE_ATTR_NAME = 5 var STATE_ATTR_EQ = 6 var STATE_ATTR_QUOT = 7 var STATE_ATTR_VALUE = 8 +var STATE_CDATA = 9 var SaxLtx = module.exports = function SaxLtx () { EventEmitter.call(this) @@ -73,13 +74,27 @@ var SaxLtx = module.exports = function SaxLtx () { attrs = {} } break + case STATE_CDATA: + if (c === 93 /* ] */ && data.substr(pos + 1, 2) === ']>') { + var cData = endRecording() + if (cData) { + this.emit('text', unescapeXML(cData)) + } + state = STATE_IGNORE_COMMENT + } + break case STATE_TAG_NAME: if (c === 47 /* / */ && recordStart === pos) { recordStart = pos + 1 endTag = true } else if (c === 33 /* ! */) { - recordStart = undefined - state = STATE_IGNORE_COMMENT + if (data.substr(pos + 1, 7) === '[CDATA[') { + recordStart = pos + 8 + state = STATE_CDATA + } else { + recordStart = undefined + state = STATE_IGNORE_COMMENT + } } else if (c === 63 /* ? */) { recordStart = undefined state = STATE_IGNORE_INSTRUCTION @@ -93,7 +108,8 @@ var SaxLtx = module.exports = function SaxLtx () { if (c === 62 /* > */) { var prevFirst = data.charCodeAt(pos - 1) var prevSecond = data.charCodeAt(pos - 2) - if (prevFirst === 45 /* - */ && prevSecond === 45 /* - */) { + if ((prevFirst === 45 /* - */ && prevSecond === 45 /* - */) || + (prevFirst === 93 /* ] */ && prevSecond === 93 /* ] */)) { state = STATE_TEXT } } diff --git a/test/cdata-test.js b/test/cdata-test.js new file mode 100644 index 00000000..4ae00e23 --- /dev/null +++ b/test/cdata-test.js @@ -0,0 +1,24 @@ +'use strict' + +var vows = require('vows') +var assert = require('assert') +var ltx = require('..') +var parsers = require('../lib/parsers') + +var Parser = parsers.find(function (parser) { + return (parser.name === 'SaxLtx') +}) + +var parse = function (s) { + return ltx.parse(s, {Parser: Parser}) +} + +vows.describe('sax_ltx').addBatch({ + 'CDATA parsing': { + 'issue-19: parse CDATA content as text': function () { + var el = parse('') + assert.equal(el.name, 'root') + assert.equal(el.getText(), 'Content') + } + } +}).export(module)