Skip to content

Commit

Permalink
fix cdata issue #132 (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp authored Oct 2, 2019
1 parent d6fb837 commit 6ec0417
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/parsers/ltx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {

This comment has been minimized.

Copy link
@elazutkin-dynata

elazutkin-dynata Oct 2, 2019

For my own education: constant STATE_IGNORE_CDATA is declared above as 10, checked if state is equal to it in one place, yet it is never assigned to state. How is it supposed to work?

This comment has been minimized.

Copy link
@sonnyp

sonnyp Oct 2, 2019

Author Member

Yep. I was tired and went too fast.

Thanks for the heads up.

STATE_IGNORE_CDATA is a performance optimization that is not being used.

Won't make a big different to enable it but I'll do that alongside adding much more unit tests for CDATA.

This comment has been minimized.

Copy link
@elazutkin-dynata

elazutkin-dynata Oct 2, 2019

Thank for satisfying my curiosity. And let me thank you again for the great project.

// 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)
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions test/cdata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -24,6 +25,10 @@ vows.describe('sax_ltx').addBatch({
var el = parse('<root><![CDATA[Content &amp; "more content&quot;]]></root>')
assert.strictEqual(el.name, 'root')
assert.strictEqual(el.getText(), 'Content &amp; "more content&quot;')
},
'issue-132': () => {
var el = parse('<a><b><![CDATA[]]></b><b><![CDATA[--><c>&d;]]></b></a>')
assert.deepStrictEqual(el, h('a', null, h('b', null, ''), h('b', null, '--><c>&d;')))
}
}
}).export(module)

0 comments on commit 6ec0417

Please sign in to comment.