Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sax_ltx recognize CDATA #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/sax/sax_ltx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissmanu

what do you think about the implementation? i dont like the way how i had to implement the look ahead for detecting CDATA at all. any other ideas?

I get the feeling but I like how it doesn't interfere too much with the rest of the code. 👍 and maybe we can come up with a more 'elegant' solution to this in the future.

} else if (c <= 32 || c === 47 /* / */ || c === 62 /* > */) {
tagName = endRecording()
pos--
Expand Down
23 changes: 23 additions & 0 deletions test/cdata-ltx-test.js
Original file line number Diff line number Diff line change
@@ -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('<root><![CDATA[Content]]></root>');
assert.equal(el.name, 'root');
assert.equal(el.getText(), 'Content');
}
}
}).export(module)