From 0edc8da749fb97a5ebaf9f818510c9977e7bbbe3 Mon Sep 17 00:00:00 2001 From: Manuel Alabor Date: Thu, 29 Jan 2015 15:39:33 +0100 Subject: [PATCH] teach sax_ltx parser how to parse CDATA and fix issue #19 --- lib/sax/sax_ltx.js | 19 ++++++++++++++++--- test/cdata-ltx-test.js | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/cdata-ltx-test.js diff --git a/lib/sax/sax_ltx.js b/lib/sax/sax_ltx.js index f6fc55db..1e4ed9e0 100644 --- a/lib/sax/sax_ltx.js +++ b/lib/sax/sax_ltx.js @@ -10,7 +10,8 @@ var STATE_TEXT = 0, STATE_ATTR_NAME = 4, STATE_ATTR_EQ = 5, STATE_ATTR_QUOT = 6, - STATE_ATTR_VALUE = 7 + STATE_ATTR_VALUE = 7, + STATE_CDATA = 8 var SaxLtx = module.exports = function SaxLtx() { events.EventEmitter.call(this) @@ -69,13 +70,25 @@ var SaxLtx = module.exports = function SaxLtx() { attrs = {} } break + case STATE_CDATA: + if(c === 93 /* ] */ && data.substr(pos + 1, 2) === ']>') { + var cData = endRecording(); + this.emit('text', unescapeXml(cData)); + state = STATE_IGNORE_TAG; + } + break; case STATE_TAG_NAME: if (c === 47 /* / */ && recordStart === pos) { recordStart = pos + 1 endTag = true } else if (c === 33 /* ! */ || c === 63 /* ? */) { - recordStart = undefined - state = STATE_IGNORE_TAG + if(data.substr(pos + 1, 7) === '[CDATA[') { + state = STATE_CDATA; + recordStart = pos + 8; + } else { + recordStart = undefined + state = STATE_IGNORE_TAG + } } else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) { tagName = endRecording() pos-- diff --git a/test/cdata-ltx-test.js b/test/cdata-ltx-test.js new file mode 100644 index 00000000..88f3661b --- /dev/null +++ b/test/cdata-ltx-test.js @@ -0,0 +1,23 @@ +'use strict'; + +var vows = require('vows') + , assert = require('assert') + , ltx = require('./../lib/index') + , SaxLtxParser = ltx.availableSaxParsers.filter(function(saxParser) { + return (saxParser.name === 'SaxLtx'); + } + )[0]; + +function parse(xml) { + return ltx.parse(xml, SaxLtxParser); +} + +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)