diff --git a/src/parser.js b/src/parser.js index b5b0034..d993eff 100644 --- a/src/parser.js +++ b/src/parser.js @@ -58,6 +58,17 @@ function parseOpenElement(context) { var name = context.readRegex(nameRegex); context.callbacks.openElement(name); readAttributes(context, false); + + if (name !== 'script' && name !== 'xmp') { + return; + } + + //just read until the closing tags for elements that allow cdata + var regex = new RegExp('^([\\s\\S]*?)(?:$|)', 'i'); + var match = regex.exec(context.substring); + context.read(match[0].length); + context.callbacks.cdata(match[1]); + context.callbacks.closeElement(name); } function parseEndElement(context) { diff --git a/tests/cdata-tests.js b/tests/cdata-tests.js index 92b3d63..52b8638 100644 --- a/tests/cdata-tests.js +++ b/tests/cdata-tests.js @@ -85,4 +85,53 @@ describe('CDATA', function() { cdataCount.should.equal(1); textCount.should.equal(1); }); + + it('script tags do not require CDATA', function() { + var closeCount = 0, cdataCount = 0, openCount = 0; + helpers.parseString('', { + openElement: function(name) { + name.should.equal('script'); + openCount++; + }, + + closeElement: function(name) { + name.should.equal('script'); + closeCount++; + }, + + cdata: function(value) { + value.should.equal(''); + cdataCount++; + } + }); + + cdataCount.should.equal(1); + closeCount.should.equal(1); + openCount.should.equal(1); + }); + + it('xmp tags do not require CDATA', function() { + var closeCount = 0, cdataCount = 0, openCount = 0; + helpers.parseString('<foo>', { + openElement: function(name) { + name.should.equal('xmp'); + openCount++; + }, + + closeElement: function(name) { + name.should.equal('xmp'); + closeCount++; + }, + + cdata: function(value) { + value.should.equal(''); + cdataCount++; + } + }); + + cdataCount.should.equal(1); + closeCount.should.equal(1); + openCount.should.equal(1); + }); + }); \ No newline at end of file diff --git a/tests/files/good-expected.html b/tests/files/good-expected.html index 89a4e55..8aafe89 100644 --- a/tests/files/good-expected.html +++ b/tests/files/good-expected.html @@ -3,7 +3,7 @@ tommy montgomery - + @@ -19,6 +19,14 @@

tommy montgomery

- + +
+ ]]> +
+ + \ No newline at end of file diff --git a/tests/files/good.html b/tests/files/good.html index b378bbf..fa1f8ad 100644 --- a/tests/files/good.html +++ b/tests/files/good.html @@ -2,9 +2,12 @@ tommy montgomery - + - + @@ -18,6 +21,14 @@

tommy montgomery

- + +
+ ]]> +
+ + \ No newline at end of file diff --git a/tests/integration-tests.js b/tests/integration-tests.js index e051e29..f8ecfcb 100644 --- a/tests/integration-tests.js +++ b/tests/integration-tests.js @@ -4,6 +4,7 @@ var helpers = require('./helpers'); describe('Integration', function() { it('real life HTML document', function(done) { var content = ''; + var inScriptTag = false; helpers.parser.parseFile(__dirname + '/files/good.html', 'utf8', { docType: function(value) { content += '\n'; @@ -11,6 +12,9 @@ describe('Integration', function() { openElement: function(name) { content += '<' + name; + if (name === 'script') { + inScriptTag = true; + } }, closeOpenedElement: function(token) { @@ -19,6 +23,9 @@ describe('Integration', function() { closeElement: function(name) { content += ''; + if (name === 'script') { + inScriptTag = false; + } }, attribute: function(name, value) { @@ -27,6 +34,18 @@ describe('Integration', function() { text: function(value) { content += value; + }, + + comment: function(value) { + content += ''; + }, + + cdata: function(value) { + if (inScriptTag) { + content += value; + } else { + content += ''; + } } }, function(err) { should.not.exist(err);