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

Add an emitEmpty option. #231

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Settings supported:
* `strictEntities` - Boolean. If true, only parse [predefined XML
entities](http://www.w3.org/TR/REC-xml/#sec-predefined-ent)
(`&`, `'`, `>`, `<`, and `"`)
* `emitEmpty` - Boolean. If true, then SAX will emit `cdata` and `comment`
events even if the CDATA section or comment were empty. By default,
when these XML structures are empty, SAX does not emit an event.

## Methods

Expand Down
8 changes: 6 additions & 2 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
parser.noscript = !!(strict || parser.opt.noscript)
parser.state = S.BEGIN
parser.strictEntities = parser.opt.strictEntities
parser.emitEmpty = parser.opt.emitEmpty
parser.ENTITIES = parser.strictEntities ? Object.create(sax.XML_ENTITIES) : Object.create(sax.ENTITIES)
parser.attribList = []

Expand Down Expand Up @@ -140,6 +141,9 @@

function flushBuffers (parser) {
closeText(parser)
// We do not need to check emitEmpty here. We are guaranteed
// to produce a cdata event with an empty string when the cdata
// section ends.
if (parser.cdata !== '') {
emitNode(parser, 'oncdata', parser.cdata)
parser.cdata = ''
Expand Down Expand Up @@ -1180,7 +1184,7 @@
if (c === '-') {
parser.state = S.COMMENT_ENDED
parser.comment = textopts(parser.opt, parser.comment)
if (parser.comment) {
if (parser.emitEmpty || parser.comment) {
emitNode(parser, 'oncomment', parser.comment)
}
parser.comment = ''
Expand Down Expand Up @@ -1221,7 +1225,7 @@

case S.CDATA_ENDING_2:
if (c === '>') {
if (parser.cdata) {
if (parser.emitEmpty || parser.cdata) {
emitNode(parser, 'oncdata', parser.cdata)
}
emitNode(parser, 'onclosecdata')
Expand Down
47 changes: 47 additions & 0 deletions test/emit_empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var xml = '<r><![CDATA[]]><!----></r>'
require(__dirname).test({
xml: xml,
expect: [
['opentagstart', {'name': 'R', 'attributes': {}}],
['opentag', {'name': 'R', 'attributes': {}, 'isSelfClosing': false}],
['opencdata', undefined],
['closecdata', undefined],
['closetag', 'R']
]
})

require(__dirname).test({
xml: xml,
opt: { emitEmpty: true },
expect: [
['opentagstart', {'name': 'R', 'attributes': {}}],
['opentag', {'name': 'R', 'attributes': {}, 'isSelfClosing': false}],
['opencdata', undefined],
['cdata', ''],
['closecdata', undefined],
['comment', ''],
['closetag', 'R']
]
})

// The following test illustrates an effect of emitEmpty together with
// hitting the buffer limit. Namely, a trailing cdata event with an
// empty string will be emitted after the buffer is emptied.
var sax = require('../lib/sax')
var bl = sax.MAX_BUFFER_LENGTH
sax.MAX_BUFFER_LENGTH = 10
require(__dirname).test({
opt: { emitEmpty: true },
expect: [
['opentagstart', {'name': 'R', 'attributes': {}}],
['opentag', {'name': 'R', 'attributes': {}, 'isSelfClosing': false}],
['opencdata', undefined],
['cdata', '12345678901'],
['cdata', ''],
['closecdata', undefined],
['closetag', 'R']
]
}).write('<r><![CDATA[12345678901')
.write(']]></r>')

sax.MAX_BUFFER_LENGTH = bl